530 likes | 668 Views
Programming Languages Fundamentals. Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM. Contact. Trần Giang Sơn tgson@cse.hcmut.edu.vn http://www.cse.hcmut.edu.vn/~tgson. References.
E N D
Programming LanguagesFundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM
Contact • Trần Giang Sơn • tgson@cse.hcmut.edu.vn • http://www.cse.hcmut.edu.vn/~tgson
References • Ngôn ngữ lập trình – các mô hình và nguyên lý, Cao Hoàng Trụ, Khoa Khoa học máy tính, Đại học Bách Khoa, 1992 • Programming languages: design and implementation, second edition, Terrence W.Partt, 1990. Printice - Hall International Editions • Fundamentals of programming languages, Ellis Horowits, 1983, Computer Science Press
Contents • Evolution and classification • Formal syntax and semantics • Compilation and interpretation
Machine Language CPU Memory 0101001001101011 1101111001001001 I/O 0001101010101010
Machine Language Instruction: Operation Code Operands 10110011010010010011010110110001
Assembly Language • A := B + C • if A = 0 then body • MOV r0, B ; move B into register r0 • ADD r0, C ; add • MOV A, r0 ; store • BNE L1 ; branch if result not equal 0 • body • L1:
Language Levels Natural Language High-Level Low-Level Machine Language
What Makes a Good Language? • Clarity, simplicity, unity of language concepts • Clarity of program syntax • Naturalness for the application • Support for abstraction • Ease of program verification
What Makes a Good Language? • Programming environment • Portability of programs • Cost of use • program execution • program translation • program creation, testing, use • program maintenance
Language Classification • Imperative • von Neumann Fortran, Pascal, Basic, C • object-oriented Smalltalk, Eiffel, C++, Java • Declarative • functional Lisp, ML, Haskell • dataflow Id, Val • logic Prolog, VisiCalc
Von Neumann Languages • Most familiar and successful • Imperative statements • Modification of variables Fortran, Pascal, Basic, C, …
Object-Oriented Languages • Imperative statements • Message passing among objects Smalltalk, Eiffel, C++, Java
Functional Languages • Recursive definition of functions (lambda calculus) • Expressions of function composition Lisp, ML, Haskell
Logic Languages • Logical facts and rules (predicate logic) • Computation as theorem proving Prolog, VisiCalc
Dataflow Languages • Computation as token flow among nodes • Inherently parallel model Id, Val
Contents • Evolution and classification • Formal syntax and semantics • Compilation and interpretation
Formal Syntax and Semantics • Computer languages must be precise • Both their form (syntax) and meaning (semantics) must be specified without ambiguity • Both programmers and computers can tell what a program is supposed to do
Formal Syntax • Abstract syntax • Context-free grammars • Backus-Naur formalism (BNF) • Syntax diagrams • Derivations and parse trees
Abstract Syntax • Syntactic class • Syntactic form
Example: Expressions • Syntactic class: E expression I identifier C constant O operator • Syntactic form: E = I | C | E O E | (E)
Example: Expressions a * (2 + b) E O E
Example: Expressions a - b - c E O E
Abstract Syntax • Advantage: simple • Disadvantages: • No terminal symbols defined • Ambiguous
Context-Free Grammars • Start symbol • Non-terminals • Terminals • Productions A a1 | a2 | … | an (Noam Chomsky, 1959)
Example: Unsigned Integers 6 2 5 7 3 <digit> <unsigned_integer>
Example: Unsigned Integers • Start symbol <unsigned_integer> • Non-terminals <unsigned_integer>,<digit> • Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Productions <unsigned_integer> <digit> | <digit> <unsigned_integer>
Backus-Naur Formalism <unsigned_integer> ::= <digit> | <digit> <unsigned_integer> (John Backus, 1960)
Example: Expressions <factor> 12 * 3 + 4 <term>
Example: Expressions • Start symbol <expression> • Non-terminals <expression>, <term>, <factor>, <unsigned_integer>, <term_op>, <factor_op> • Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, *, /
Example: Expressions • Productions: <expression> <term> | <expression> <term_op> <term> <term> <factor> | <term> <factor_op> <factor> <factor> <unsigned_integer> | (<expression>) <term_op> + | - <factor_op> * | /
Syntax Diagrams expression term expression term_op term
Derivations <expression> <expression> <term_op> <term> <term> + <factor> <term> <factor_op> <factor> + <unsigned_integer> <factor>* <unsigned_integer> + 4 <unsigned_integer>* 3+ 4 12 * 3+ 4
Parse Trees <expression> <expression> <term_op> <term> + <term> <factor> <term> <factor_op> <factor> <factor> * <unsigned_integer> <unsigned_integer> <unsigned_integer> 3 4 12
Parse Trees <expression> <expression> <term_op> <term> + <term> <factor> <term> <factor_op> <factor> <factor> * <unsigned_integer> <unsigned_integer> <unsigned_integer> 3 4 12
Formal Semantics • Operational semantics • Denotational semantics • Axiomatic semantics
Operational Semantics • A virtual computer to execute a program. • A set of formally defined operations to specify how the internal state of the virtual computer may change.
Denotational Semantics • Each program construct is a function that maps an input to an output. • A program is a composition of functions.
Axiomatic Semantics • The effect of a statement is defined via its precondition and postcondition. • A set of axioms and rules to define the effect of program constructs.
Axiomatic Semantics {P} S {Q} precondition statement postcondition
Axiomatic Semantics • Axiom: {PxE} x := E {P}
Axiomatic Semantics {x 2}x := x + 1{x 3} E = x + 1 P = x > 3 PxE= x + 1 > 3 = x > 2
Axiomatic Semantics • Rule: if ({P} S1 {Q}) ({Q} S2 {R}) then {P} S1 ; S2 {R}
Contents • Evolution and classification • Formal syntax and semantics • Compilation and interpretation
Compilation and Interpretation Compiler Source program Target program Target program Input Output Interpreter Source program Output Input
Compilation and Interpretation • Interpreter: better flexibility and diagnostics • Compiler: better performance
Phases of Compilation Character stream Scanner (lexical analysis) Token stream Parser (syntactic analysis) Parse tree Semantic analysis Intermediate code Machine-independent code optimisation Optimised intermediate code Target code generation Target code Machine-specific code optimization Optimised target code
Phases of Compilation c := a + b * 7 Scanner (lexical analysis) id1 := id2 + id3* 7 Parser (syntactic analysis)
:= + id1 * id2 id3 7 Phases of Compilation id1 := id2 + id3* 7 Parser (syntactic analysis)
:= id1 + id2 * id3 7 Phases of Compilation Semantic analysis CNV (7, , t1) * (id3, t1, t2) + (id2, t2, t3) ASS (t3, , id1)