Download presentation
Presentation is loading. Please wait.
Published byEleanor Lynch Modified over 8 years ago
1
1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM
2
2 Contact Trần Giang Sơn tgson@cse.hcmut.edu.vn http://www.cse.hcmut.edu.vn/~tgson
3
3 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
4
4 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation
5
5 Machine Language CPU I/O Memory 0101001001101011 1101111001001001 0001101010101010
6
6 Machine Language Operation CodeOperands Instruction: 10110011010010010011010110110001
7
7 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:
8
8 Language Levels Natural Language Machine Language Low-Level High-Level
9
9 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
10
10 What Makes a Good Language? Programming environment Portability of programs Cost of use program execution program translation program creation, testing, use program maintenance
11
11 Language Classification Imperative von NeumannFortran, Pascal, Basic, C object-orientedSmalltalk, Eiffel, C++, Java Declarative functionalLisp, ML, Haskell dataflowId, Val logicProlog, VisiCalc
12
12 Von Neumann Languages Most familiar and successful Imperative statements Modification of variables Fortran, Pascal, Basic, C, …
13
13 Object-Oriented Languages Imperative statements Message passing among objects Smalltalk, Eiffel, C++, Java
14
14 Functional Languages Recursive definition of functions (lambda calculus) Expressions of function composition Lisp, ML, Haskell
15
15 Logic Languages Logical facts and rules (predicate logic) Computation as theorem proving Prolog, VisiCalc
16
16 Dataflow Languages Computation as token flow among nodes Inherently parallel model Id, Val
17
17 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation
18
18 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
19
19 Formal Syntax Abstract syntax Context-free grammars Backus-Naur formalism (BNF) Syntax diagrams Derivations and parse trees
20
20 Abstract Syntax Syntactic class Syntactic form
21
21 Example: Expressions Syntactic class: Eexpression Iidentifier Cconstant Ooperator Syntactic form: E = I | C | E O E | (E)
22
22 Example: Expressions a * (2 + b) E O E
23
23 Example: Expressions a - b - c E O E
24
24 Abstract Syntax Advantage: simple Disadvantages: No terminal symbols defined Ambiguous
25
25 Context-Free Grammars Start symbol Non-terminals Terminals Productions A 1 | 2 | … | n (Noam Chomsky, 1959)
26
26 Example: Unsigned Integers 6 2 5 7 3
27
27 Example: Unsigned Integers Start symbol Non-terminals, Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Productions |
28
28 Backus-Naur Formalism ::= | (John Backus, 1960)
29
29 Example: Expressions 12 * 3 + 4
30
30 Example: Expressions Start symbol Non-terminals,,,,, Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, , *, /
31
31 Example: Expressions Productions: | | ( ) + | |
32
32 Syntax Diagrams term expressionterm_opterm expression
33
33 Derivations + + 4 3 + 4 12 3 + 4
34
34 Parse Trees + 12 34 *
35
35 Parse Trees + 12 34 *
36
36 Formal Semantics Operational semantics Denotational semantics Axiomatic semantics
37
37 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.
38
38 Denotational Semantics Each program construct is a function that maps an input to an output. A program is a composition of functions.
39
39 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.
40
40 Axiomatic Semantics {P} S {Q} preconditionstatementpostcondition
41
41 Axiomatic Semantics {P x E } x := E {P} Axiom:
42
42 Axiomatic Semantics {x 2} x := x + 1 {x 3} E = x + 1 P = x > 3 P x E = x + 1 > 3 = x > 2
43
43 Axiomatic Semantics if ({P} S 1 {Q}) ({Q} S 2 {R}) then {P} S 1 ; S 2 {R} Rule:
44
44 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation
45
45 Compilation and Interpretation Compiler Source programTarget program InputOutput Interpreter Source programOutput Input
46
46 Compilation and Interpretation Interpreter: better flexibility and diagnostics Compiler: better performance
47
47 Phases of Compilation Scanner (lexical analysis) Parser (syntactic analysis) Semantic analysis Machine-independent code optimisation Target code generation Machine-specific code optimization Character stream Token stream Parse tree Intermediate code Optimised intermediate code Target code Optimised target code
48
48 Phases of Compilation Scanner (lexical analysis) Parser (syntactic analysis) c := a + b 7 id 1 := id 2 + id 3 7
49
49 Phases of Compilation Parser (syntactic analysis) id 1 := id 2 + id 3 7 := id 1 id 2 id 3 + 7
50
50 Phases of Compilation Semantic analysis CNV (7,, t 1 ) (id 3, t 1, t 2 ) + (id 2, t 2, t 3 ) ASS (t 3,, id 1 ) := id 1 id 2 id 3 + 7
51
51 Phases of Compilation CNV (7,, t 1 ) (id 3, t 1, t 2 ) + (id 2, t 2, t 3 ) ASS (t 3,, id 1 ) Machine-independent code optimisation (id 3, 7.0, t 1 ) + (id 2, t 1, id 1 )
52
52 Phases of Compilation MOV reg, id 3 MUL reg, 7.0 ADD reg, id 2 MOV id 1, reg (id 3, 7.0, t 1 ) + (id 2, t 1, id 1 ) Target code generation
53
53 Exercises Define a formal syntax for a simple language supporting only the assignment statement and arithmetic expressions. Write the derivation and draw the parse tree of ‘ c := (a + b) 7 ’ using the defined syntax.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.