programming languages fundamentals
DESCRIPTION
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 [email protected] http://www.cse.hcmut.edu.vn/~tgson. References. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/1.jpg)
1
Programming Languages Fundamentals
Cao Hoaøng Truï
Khoa Coâng Ngheä Thoâng TinÑaïi Hoïc Baùch Khoa TP. HCM
![Page 2: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/2.jpg)
2
Contact
• Trần Giang Sơn
• http://www.cse.hcmut.edu.vn/~tgson
![Page 3: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/3.jpg)
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
![Page 4: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/4.jpg)
4
Contents
• Evolution and classification
• Formal syntax and semantics
• Compilation and interpretation
![Page 5: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/5.jpg)
5
Machine Language
CPU
I/O
Memory
0101001001101011
1101111001001001
0001101010101010
![Page 6: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/6.jpg)
6
Machine Language
Operation Code Operands
Instruction:
10110011010010010011010110110001
![Page 7: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/7.jpg)
7
Assembly Language
A := B + Cif A = 0 then body
MOV r0, B ; move B into register r0ADD r0, C ; addMOV A, r0 ; storeBNE L1 ; branch if result not equal 0body
L1:
![Page 8: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/8.jpg)
8
Language Levels
Natural Language
Machine Language
Low-Level
High-Level
![Page 9: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/9.jpg)
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
![Page 10: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/10.jpg)
10
What Makes a Good Language?
• Programming environment
• Portability of programs
• Cost of use program execution program translation program creation, testing, use program maintenance
![Page 11: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/11.jpg)
11
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
![Page 12: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/12.jpg)
12
Von Neumann Languages
• Most familiar and successful
• Imperative statements
• Modification of variables
Fortran, Pascal, Basic, C, …
![Page 13: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/13.jpg)
13
Object-Oriented Languages
• Imperative statements
• Message passing among objects
Smalltalk, Eiffel, C++, Java
![Page 14: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/14.jpg)
14
Functional Languages
• Recursive definition of functions (lambda calculus)
• Expressions of function composition
Lisp, ML, Haskell
![Page 15: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/15.jpg)
15
Logic Languages
• Logical facts and rules (predicate logic)
• Computation as theorem proving
Prolog, VisiCalc
![Page 16: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/16.jpg)
16
Dataflow Languages
• Computation as token flow among nodes
• Inherently parallel model
Id, Val
![Page 17: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/17.jpg)
17
Contents
• Evolution and classification
• Formal syntax and semantics
• Compilation and interpretation
![Page 18: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/18.jpg)
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
![Page 19: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/19.jpg)
19
Formal Syntax
• Abstract syntax
• Context-free grammars
• Backus-Naur formalism (BNF)
• Syntax diagrams
• Derivations and parse trees
![Page 20: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/20.jpg)
20
Abstract Syntax
• Syntactic class
• Syntactic form
![Page 21: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/21.jpg)
21
Example: Expressions
• Syntactic class:E expressionI identifierC constantO operator
• Syntactic form:E = I | C | E O E | (E)
![Page 22: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/22.jpg)
22
Example: Expressions
a * (2 + b)
E O E
![Page 23: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/23.jpg)
23
Example: Expressions
a - b - c
E O E
![Page 24: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/24.jpg)
24
Abstract Syntax
• Advantage: simple
• Disadvantages:No terminal symbols definedAmbiguous
![Page 25: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/25.jpg)
25
Context-Free Grammars
• Start symbol
• Non-terminals
• Terminals
• Productions A 1 | 2 | … | n
(Noam Chomsky, 1959)
![Page 26: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/26.jpg)
26
Example: Unsigned Integers
6 2 5 7 3
<digit> <unsigned_integer>
![Page 27: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/27.jpg)
27
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>
![Page 28: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/28.jpg)
28
Backus-Naur Formalism
<unsigned_integer> ::= <digit> | <digit> <unsigned_integer>
(John Backus, 1960)
![Page 29: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/29.jpg)
29
Example: Expressions
<factor>
12 * 3 + 4
<term>
![Page 30: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/30.jpg)
30
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, +, , *, /
![Page 31: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/31.jpg)
31
Example: Expressions
• Productions:
<expression> <term> | <expression> <term_op> <term>
<term> <factor> | <term> <factor_op> <factor>
<factor> <unsigned_integer> | (<expression>)
<term_op> + |
<factor_op> |
![Page 32: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/32.jpg)
32
Syntax Diagrams
term
expression term_op term
expression
![Page 33: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/33.jpg)
33
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
![Page 34: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/34.jpg)
34
Parse Trees
<expression>
<expression>
<term_op>
<term>
<term>
<unsigned_integer>
<factor>
<factor_op>
<factor>
<term>
<unsigned_integer>
<unsigned_integer>
<factor>
+
12
3 4
*
![Page 35: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/35.jpg)
35
Parse Trees
<expression>
<expression>
<term_op>
<term>
<term>
<unsigned_integer>
<factor>
<factor_op>
<factor>
<term>
<unsigned_integer>
<unsigned_integer>
<factor>
+
12
3 4
*
![Page 36: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/36.jpg)
36
Formal Semantics
• Operational semantics
• Denotational semantics
• Axiomatic semantics
![Page 37: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/37.jpg)
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.
![Page 38: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/38.jpg)
38
Denotational Semantics
• Each program construct is a function that maps an input to an output.
• A program is a composition of functions.
![Page 39: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/39.jpg)
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.
![Page 40: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/40.jpg)
40
Axiomatic Semantics
{P} S {Q}
precondition
statement
postcondition
![Page 41: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/41.jpg)
41
Axiomatic Semantics
{PxE} x := E {P}
• Axiom:
![Page 42: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/42.jpg)
42
Axiomatic Semantics
{x 2} x := x + 1 {x 3}
E = x + 1P = x > 3PxE = x + 1 > 3 = x > 2
![Page 43: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/43.jpg)
43
Axiomatic Semantics
if ({P} S1 {Q}) ({Q} S2 {R})
then {P} S1 ; S2 {R}
• Rule:
![Page 44: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/44.jpg)
44
Contents
• Evolution and classification
• Formal syntax and semantics
• Compilation and interpretation
![Page 45: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/45.jpg)
45
Compilation and Interpretation
CompilerSource program
Target program
Target programInput Output
InterpreterSource program
Output
Input
![Page 46: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/46.jpg)
46
Compilation and Interpretation
• Interpreter: better flexibility and diagnostics
• Compiler: better performance
![Page 47: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/47.jpg)
47
Phases of CompilationScanner (lexical analysis)
Parser (syntactic analysis)
Semantic analysis
Machine-independentcode optimisation
Target code generation
Machine-specificcode optimization
Character stream
Token stream
Parse tree
Intermediate code
Optimised intermediate code
Target code
Optimised target code
![Page 48: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/48.jpg)
48
Phases of Compilation
Scanner (lexical analysis)
Parser (syntactic analysis)
c := a + b 7
id1 := id2 + id3 7
![Page 49: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/49.jpg)
49
Phases of Compilation
Parser (syntactic analysis)
id1 := id2 + id3 7
:=
id1
id2
id3
+
7
![Page 50: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/50.jpg)
50
Phases of Compilation
Semantic analysis
CNV (7, , t1)
(id3, t1, t2)
+ (id2, t2, t3)
ASS (t3, , id1)
:=
id1
id2
id3
+
7
![Page 51: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/51.jpg)
51
Phases of Compilation
CNV (7, , t1)
(id3, t1, t2)
+ (id2, t2, t3)
ASS (t3, , id1)
Machine-independentcode optimisation
(id3, 7.0, t1)
+ (id2, t1, id1)
![Page 52: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/52.jpg)
52
Phases of Compilation
MOV reg, id3
MUL reg, 7.0
ADD reg, id2
MOV id1, reg
(id3, 7.0, t1)
+ (id2, t1, id1)Target code generation
![Page 53: Programming Languages Fundamentals](https://reader035.vdocuments.net/reader035/viewer/2022062304/56814523550346895db1e756/html5/thumbnails/53.jpg)
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.