1
- 1. Overview
1. Overview 1.1 Motivation 1.2 Structure of a Compiler 1.3 - - PowerPoint PPT Presentation
1. Overview 1.1 Motivation 1.2 Structure of a Compiler 1.3 Grammars 1.4 Syntax Tree and Ambiguity 1.5 Chomsky's Classification of Grammars 1.6 The Z# Language 1 Short History of Compiler Construction Formerly "a mystery", today
1
2
3
(instruction set, registers, addressing modes, run-time data structures, ...)
(efficiency considerations)
4
5
1 (ident) "val" 3 (assign)
(number) 10 4 (times)
(ident) "val" 5 (plus)
(ident) "i"
ident = number * ident + ident Term Expression Statement
6
ident = number * ident + ident Term Expression Statement
7
8
9
10
scanner parser ... code generator loader source code machine code
... compiler ... source code intermediate code (e.g. Common Intermediate Language (CIL))
scanner parser source code interpretation
11
12
13
Statement = "if" "(" Condition ")" Statement ["else" Statement].
"if", ">=", ident, number, ...
Statement, Expr, Type, ...
Statement = Designator "=" Expr ";". Designator = ident ["." ident]. ...
CSharp
14
John Backus: developed the first Fortran compiler Peter Naur: edited the Algol60 report
15
Expr = [ "+" | "-" ] Term { ( "+" | "-" ) Term }. Term = Factor { ( "*" | "/" ) Factor }. Factor = ident | number | "(" Expr ")".
16
Expr = [ "+" | "-" ] Term { ( "+" | "-" ) Term }. Term = Factor { ( "*" | "/" ) Factor }. Factor = ident | number | "(" Expr ")".
Term + Term
Expr
Expr = Term { ( "+" | "-" ) Term }. Term = Factor { ( "*" | "/" ) Factor }. Factor = [ "+" | "-" ] ( ident | number | "(" Expr ")" ).
17
Expr = ["+" | "-"] Term {("+" | "-") Term}. Term = Factor {("*" | "/") Factor}. Factor = ident | number | "(" Expr ")".
18
Expr = [ "+" | "-" ] Term { ( "+" | "-" ) Term }. Term = Factor { ( "*" | "/" ) Factor }. Factor = ident | number | "(" Expr ")".
19
20
Term + Factor * Factor
Term + ident * Factor
21
A = B C. B = [ b ]. C = c | d | .
22
23
A = b | A a. A ⇒ A a ⇒ A a a ⇒ A a a a ⇒ b a a a a a ...
A = b | a A. A ⇒ a A ⇒ a a A ⇒ a a a A ⇒ ... a a a a a b
A = b | "(" A ")". A ⇒ (A) ⇒ ((A)) ⇒ (((A))) ⇒ (((... (b)...)))
Expr = Term { "+" Term }. Term = Factor { "*" Factor }. Factor = id | "(" Expr ")". Expr ⇒ Term ⇒ Factor ⇒ "(" Expr ")"
24
A = b | A a.
E = T | E "+" T.
T T + T T + T + T ...
E = T { "+" T }.
25
26
<Expr> ::= <Sign> <Term> <Expr> ::= <Expr> <Addop> <Term> <Sign> ::= + <Sign> ::=
::= <Addop> ::= + <Addop> ::=
::= <Factor> <Term> ::= <Term> <Mulop> <Factor> <Mulop> ::= * <Mulop> ::= / <Factor> ::= ident <Factor> ::= number <Factor> ::= ( <Expr> )
27
ε number + * ident Factor Term number Factor Term Mulop Factor Sign Term Addop Expr Expr
Expr = [ Sign ] Term { Addop Term }.
number ident * + ident
28
T = F | T "*" T. F = id.
id F T id F T * T id F T * T id F T id F T * T id F T * T
29
T = F | T "*" T. F = id.
T = F | T "*" F. F = id.
T = F { "*" F }. F = id. id F T id F T * T id F T * T
30
Statement = Assignment | "if" Condition Statement | "if" Condition Statement "else" Statement | ... . Condition Condition Statement Statement Statement Statement if (a < b) if (b < c) x = c; else x = b; Condition Condition Statement Statement Statement Statement
31
32
A = a A b | B c B. aBc = d. dB = bb. A ⇒ aAb ⇒ aBcBb ⇒ dBb ⇒ bbb
A = a b c.
A = b | b B.
33
34
class P const int size = 10; class Table { int[] pos; int[] neg; } Table val; { void Main () int x, i; { /*---------- initialize val ----------*/ val = new Table; val.pos = new int[size]; val.neg = new int[size]; i = 0; while (i < size) { val.pos[i] = 0; val.neg[i] = 0; i++; } /*---------- read values ----------*/ read(x); while (-size < x && x < size) { if (0 <= x) { val.pos[x]++; } else { val.neg[-x]++; } read(x); } } }
35
ident = letter { letter | digit | '_' }.
number = digit { digit }.
charConst = '\'' char '\''.
class if else while read write return break void const new
+
/ % ++
!= > >= < <= && || ( ) [ ] { } = ; , .
/* ... */
36
Program = "class" ident { ConstDecl | VarDecl | ClassDecl } "{" { MethodDecl } "}". class P ... declarations ... { ... methods ... }
ConstDecl = "const" Type ident "=" ( number | charConst ) ";". VarDecl = Type ident { "," ident } ";". MethodDecl = (Type | "void") ident "(" [ FormPars ] ")" Block. Type = ident [ "[" "]" ]. FormPars = Type ident { "," Type ident }.
37
Block = "{" {Statement} "}". Statement = Designator ( "=" Expr ";" | "(" [ActPars] ")" ";" | "++" ";" | "--" ";" ) | "if" "(" Condition ")" Block [ "else" Block ] | "while" "(" Condition ")" Block | "break" ";" | "return" [ Expr ] ";" | "read" "(" Designator ")" ";" | "write" "(" Expr [ "," number ] ")" ";" | ";". ActPars = Expr { "," Expr }.
38
Condition = CondTerm { "||" CondTerm }. CondTerm = CondFact { "&&" CondFact }. CondFact = Expr Relop Expr. Relop = "==" | "!=" | ">" | ">=" | "<" | "<=". Expr = [ "-" ] Term { Addop Term }. Term = Factor { Mulop Factor }. Factor = Designator [ "(" [ ActPars ] ")" ] | number | charConst | "new" ident [ "[" Expr "]" ] | "(" Expr ")". Designator = ident [ "[" Expr "]" ] { "." ident [ "[" Expr "]" ] }. Addop = "+" | "-". Mulop = "*" | "/" | "%".