Compiler Design
Spring 2018
3.0 Frontend
1
Compiler Design Spring 2018 3.0 Frontend Thomas R. Gross Computer - - PowerPoint PPT Presentation
Compiler Design Spring 2018 3.0 Frontend Thomas R. Gross Computer Science Department ETH Zurich, Switzerland 1 Admin issues Recitation sessions take place only when announced In the lecture / on course website / on the mailing list
1
2
3
Source program ASM file “Front-end” IR “Back-end” Optimizer Question: How to build IR (tree)?
4
§ Also known as scanning § Scanner, Lexer
§ Also known as parsing § Parser
5
6
7
§ (Speed an issue in real life)
8
§ Other terms: Word, sentence
9
10
§ (a, b, g may be empty)
12
S à aBa S à aXa Xb à Xbc | c Ba à aBa | b
14
16
17
18
22
23
24
S ⇒ E ⇒ Id ⇒ L M ⇒ L L M ⇒ a L M ⇒ ap M ⇒ ap
26
29
30
§ We’ve always picked the right production § Found w = a3 + b
31
32
33
34
36
… Id à L M L à a | b | ... | z M à L M | N M | e N à 0 | 1 | ... | 9
37
38
regexp regexp
40
41
regexp
43
§ Start symbol: S § Terminals: { a, b, …, z, +, -, *, /, ( , ), Id } § Non-Terminals: { S, E, Op}
44
45
48
50
51
52
53
54
§ Later more (parse trees)
55
56
58
59 S
60 S E
S à E
61 S E Op E E
S à E E àE Op E
62 S E Op E E Id
S à E E àE Op E E à Id
63 S E Op E E Id +
S à E E àE Op E E à Id Op à +
64 S E Op E E Id + Id
S à E E àE Op E E à Id Op à + E à Id
67 S E Op E E Id + Id
68
70 S E Op E E Id + Op E E Id * Id S E Op E E Id * Op E E Id + Id
Note: Each tree can be obtained using both a left-most and a right-most derivation.
71
72
74
76
77 S E Op E E Id * Op E E Id + Id S E Op E E Id + Op E E Id * Id
78
79
80
81
82
83
84
if (Cond) then (Body) if (Cond) then (Body) else (Body) if (Cond) then (Body) if (Cond) then (Body) else (Body)
85
88
89
§ Proof (for one grammar) § Some kinds of grammars are certified unambiguous
§ We will look at those in compiler design
91
92
§ Building IR: Via derivations or separate transformation step
94
95 S E Op E E Id a7 + Id b + VAR b VAR a7
96
§ Find derivation S ⇒* w
98