programming languages: types of languages …djmoon/python/python-notes/language... · programming...
TRANSCRIPT
Programming Languages: Types of Languages
• A computer programs is a set of instructions for the computer to carry out (execute)
• Programs are written using a programming language
– Types of languages:
∗ Machine language
· The only language a computer understands is machine language
· ML is binary - it consists of zeroes and ones only
· Every processor type has its own machine language
· Machine code is usually called object code
∗ Assembly language
· Uses mnemonic instructions
· 1:1 correspondence between assembly language and machine language
· For example: LDA 96
· CPU cannot directly execute program because not binary
· Assembler: Program that converts assembly language into machine lan-guage
∗ High-level languages
· Use English-like instructions
· Uses symbolic references (variables) to memory
· For example: x = y + 6
· Example languages: BASIC, FORTRAN, COBOL, C, C++, Lisp, Java,Python
· These are independent of any CPU
· Programs written in high-level languages must be converted to machinelanguage
· Such programs are called source code
1
Programming Languages: Converting High-level Programs to Object Code
• High-level source code must be converted into object code in order to execute aprogram on a computer
1. Interpreter
– Reads one HL instruction at a time
– Translates that instruction to machine code
– Executes the machine code
– Repeats until every HL instruction executed
2. Compiler
– Entire HL program converted into a machine language program
– Machine program executed
• Comparison of interpreters v compilers
– Interpreters:
∗ Programs execute slowly (must wait for each instruction to be translated)
∗ Catch errors exactly where they occur
– Compilers:
∗ Programs execute fast (cause is machine code)
∗ Generated errors may occur far from the actual problem
∗ Programs must be recompiled after every change
• There must be a compiler/interpreter for every family of chips
2
Programming Languages: Converting High-level Programs to Object Code (2)
• Comparison of high-level v low-level languages
– Low-level:
∗ Most efficient code
∗ Can take advantage of chip’s special features
– High-level
∗ Faster to write (∼ 10X faster)
∗ Self-documenting
∗ Allows programmer to ignore low-level details
∗ Portable
3
Programming Languages: Program Structure/Format
• General differences among languages include
1. Fixed v free format
– Fixed format languages (e.g., FORTRAN, Python) expect instructions to beplaced just so on a line, usually one instruction per line
– Free format languages can place instructions anywhere without restriction
∗ Lines separated by special characters called delimiters
2. Case sensitive v non-case sensitive
– In languages that are case-sensitive, upper v lower case makes a difference
4
Programming Languages: Language Components
• In general, all programming languages have same set of components
1. An alphabet
– A set of symbols from which the language is constructed
2. Reserved (key) words
– A set of words with special meaning in the language
– They cannot be redefined by the programmer
3. Syntax
– The rules governing valid sentences in the language
– Rules called a grammar
4. Semantics
– The meaning behind the constructs of the language
• Learning a language is learning these components
• Major differences are in syntax and language model (imperative, object-oriented,functional, declarative)
5
Programming Languages: Syntax and Semantics
• Every language characterized by
1. Syntax
– Rules that specify the form of valid sentences in the language
– Rules called a grammar
– BNF (Backus-Naur Form) is one technique for describing syntax
– The following will be used to describe syntax in these notes
∗ Terminal symbols (symbols that represent themselves/ stand for them-selves): Boldface, or normal font
∗ Nonterminal symbols (symbols that represent a gropu of constructs): italicsor delimited by <>
∗ Choices/options: separated by |∗ Optional components: Delimited by [ and ]
∗ Zero or more occurences of a construct: construct∗ or {construct}∗
∗ One or more occurences of a construct: construct+ or {construct}+ or con-struct...
2. Semantics
– The meanings associated with language constructs
6
Programming Languages: Program Constructs
• In general, all programming languages have same set of constructs
1. Comments
– Documentation used to
(a) Indicate history (e.g., programmer names, creation date, date last modified)
(b) Explain algorithms used
(c) Explain uses of variables
(d) Etc.
– Comments ignored by compiler
2. Identifiers
– Symbols that represent program components, e.g.,
(a) Variables
(b) Constants
(c) Reserved words
3. Variables
– Represent values stored in memory
– Have specific data type allocated specific amount of storage
4. Declarations
– Specify data type (integer, float, string, ...) of construct
– Needed so
(a) Computer allocates proper amount of storage
(b) Computer knows how to interpret the binary value stored there
5. IO statements
– Accept input from mouse, keyboard, etc.
– Write output to file, display, printer, etc.
6. Assignment statement
– Assigns values to variables
7
Programming Languages: Program Constructs (2)
7. Control statements
– Control order in which statements are executed
∗ Sequence (default)
· Statements simply executed in order that they appear in the program
∗ Loop (iteration, repetition)
· Causes set of statements to be repeated
∗ Conditional (if)
· Determines which of a set of instructions are executed based on a condition
∗ Subprogram call
· Causes control to jump to a subprogram (function, method, subroutine)
· Subprogram executes like a miniprogram
8
Programming Languages: Program Development
• Every program has 3 aspects:
1. Input - Data to be processed is made accessible to program
2. Process - Data is processed (producing meaningful info)
3. Output - Processing results are made available to user
• Software Life Cycle (Program Development Cycle)
– General process describing steps used to develop programs:
1. Analyze
∗ Basic problem-solving - identifying what the problem is
∗ Generate program requirements/specifications: Id input and output
2. Design
∗ Identify program constructs and processing required to solve problem
∗ Involves designing an algorithm:
· A step-by-step sequence of unambiguous instructions for solving a problemin a finite amount of time
3. Implement
∗ Write code
∗ Compile
∗ Debug
· Syntax errors: Errors in statement structure
4. Test
∗ Generate test cases
∗ Run/execute the program
∗ Debug
· Logic errors: Errors in problem solving strategies
· Semantic errors: Errors in use of language
5. Maintain
∗ Includes documentation and tweaking as needed
9
Programming Languages: Programming Environments
• A programming environment includes all the software that is used to create, debug,execute, and maintain programs
• The simplest environment consists of a set of unrelated programs
– An editor to create the program
– The compiler/interpreter to create the object code and/or execute the program
– Possibly debugging software to help id problems
• Environments generally consist of one of two types:
– Command line
∗ In this approach, the user types commands directly to the operating system
∗ This is a strictly text-based approach
– GUI (Graphical User Interface)
∗ In this approach, a windowed environment is provided
∗ Standard mouse click, drag-and-drop, etc. methods are used
• Many languages provide an IDE (Integrated Development Environment)
– An IDE is a GUI-based system that incorporates all of the above aspects into asingle integrated interface for programming
10