Lecture Notes
Lecture Notes
Compilers
Mubaraka Sani Ibrahim
1 / ??
Overview
2 / ??
Evaluation
3 / ??
Books
4 / ??
Today’s Objectives
5 / ??
Introduction
6 / ??
Introduction To Programming Language
7 / ??
Introduction To Programming Language
7 / ??
Introduction To Programming Language
7 / ??
Introduction To Programming Language
7 / ??
Introduction To Programming Language
7 / ??
Natural Language
8 / ??
Natural Language
8 / ??
Why High Level Language
9 / ??
Why High Level Language
9 / ??
Machine language
10 / ??
Machine language
10 / ??
Machine language
10 / ??
Assembly Language
11 / ??
Assembly Language
11 / ??
Assembly Language
11 / ??
Assembly Language
11 / ??
Machine Instructions
12 / ??
Properties Of High Level Language
13 / ??
High level of abstraction to low level implementation
details
15 / ??
How to instruct a computer
16 / ??
Why Study Compiler
17 / ??
How Compiler Works
18 / ??
Examples Of Compiled Languages
19 / ??
Examples Of Compiled Languages
19 / ??
Examples Of Both Interpreted and Compiled
Languages
■ Python, Java, C,
20 / ??
Examples Of Both Interpreted and Compiled
Languages
■ Python, Java, C,
◆ Source code translated into target program
20 / ??
Examples Of Both Interpreted and Compiled
Languages
■ Python, Java, C,
◆ Source code translated into target program
■ Target program could be intermediate form (byte
code)
20 / ??
Examples Of Both Interpreted and Compiled
Languages
■ Python, Java, C,
◆ Source code translated into target program
■ Target program could be intermediate form (byte
code)
◆ The bytecode is interpreted by virtual machine
20 / ??
Example of input and output to a compiler
21 / ??
Input to a compiler
22 / ??
Output of a compiler
■ Register file
■ Machine code:
◆ Load/Store Instructions
23 / ??
Output of a compiler
■ Register file
■ Machine code:
◆ Load/Store Instructions
◆ Arithmetic and logical operations on registers
23 / ??
Output of a compiler
■ Register file
■ Machine code:
◆ Load/Store Instructions
◆ Arithmetic and logical operations on registers
◆ Branch Instructions
23 / ??
Interpreter
24 / ??
Interpreter
24 / ??
Interpreter
24 / ??
Interpreter
24 / ??
How Interpreter Works
25 / ??
Structure Of Compiler
26 / ??
Objective
27 / ??
Compiler Architecture
28 / ??
Compiler
29 / ??
Compiler
29 / ??
Compiler
29 / ??
Lexical Analysis
■ Also scanner
■ Reads characters as input
30 / ??
Lexical Analysis
■ Also scanner
■ Reads characters as input
■ Recognise words(group characters into tokens)
■ Info about token is stored in Symbol Table
30 / ??
Lexical Analysis
■ Example:
◆ Ist his ase nte nce
31 / ??
Lexical Analysis
■ Example:
◆ Ist his ase nte nce
◆ Is this a sentence
31 / ??
Scanner
32 / ??
Syntax Analysis
■ Also Parser
■ Uses tokens to create a tree-like structure
■ Determines the grammatical structure of each token
■ Output syntax tree
33 / ??
Parser
34 / ??
Semantic Analysis
35 / ??
Semantic Analyser
36 / ??
Analysis
37 / ??
Intermediate Code Generation
38 / ??
Example
39 / ??
Intermediate Code Generation
40 / ??
Code Optimization
41 / ??
Code Generation
42 / ??
Synthesis
43 / ??
Issues
■ Language design
■ Debugging
■ Feedback
■ Speed
44 / ??
Automata Theory
45 / ??
Automata Theory
46 / ??
Automata Theory
47 / ??
Example 1
48 / ??
Example 2
0 1
1
0,1
start 0111
0
0
1
49 / ??
Example 2
0 111 1
1
0,1
start 0111
0
0
1
50 / ??
Example 1
51 / ??
Definitions
52 / ??
Definitions
■ If Σ is an Alphabet
■ Σ∗ is a set of all alphabets over Σ
■ A language L over Σ is a subsubset of Σ∗ .
53 / ??
Definitions
In Example 1:
Σ = {P ush}
Σ∗ = { λ,
Push,
Push Push,
Push Push Push,
Push Push Push Push...}
L= {P ushn |n is odd }
54 / ??
55 / ??
Deterministic Finite Automata(DFA)
56 / ??
57 / ??
58 / ??
Definitions
59 / ??
Example 1
0
0
start 1
1 0
60 / ??
Example 2
0 0 0
1
start q1 q2 q3
0 1
61 / ??
Example 2
0 0 0
1
start q1 q2 q3
0 1
61 / ??
Example 3
0 0
1
start q1 q2
■ Input: 0
■ Input: 01
■ Input: 011
62 / ??
Example 3
0 1 0
start q1 q2
63 / ??
Example 3
1
0 0
start q1 q2
63 / ??
Exercise 3
0 0 0,1
1
q q0 0 q00 1 q000
start
0
64 / ??
Exercise 3
0 0 0,1
1
q q0 0 q00 1 q000
start
0
64 / ??
Regular Expression RE
65 / ??
Regular Expression
66 / ??
Regular Expression
67 / ??
Regular Expression
Example
68 / ??
Regular Expression
69 / ??
Non Deterministic Finite Automata(NFA)
70 / ??
71 / ??
Non Deterministic Finite Automata (NFA)
72 / ??
Exercise
■ a∗
a
start q0
73 / ??
Non Deterministic Finite Automata (NFA)
■ (aU b)∗
a
start q0
74 / ??
Non Deterministic Finite Automata (NFA)
q0 b q1 b q2
start
75 / ??
Non Deterministic Finite Automata (NFA)
q0 b q1 b q2
start
b b
76 / ??
Non Deterministic Finite Automata (NFA)
λ
q1 q2
77 / ??
Non Deterministic Finite Automata (NFA)
start q1
a
b
q2 λ q3
a,b
a
78 / ??
Lex
79 / ??
Scanner
80 / ??
Lexical analyser
81 / ??
How to write a scanner(lexical analyser)
82 / ??
Lex
83 / ??
Structure of Lex program
84 / ??
Structure of Lex program
85 / ??
Structure of Lex program
86 / ??
Example 1
87 / ??
Example 2
88 / ??
Lexical Analyser
89 / ??
How to write & compile a Lex program
90 / ??
Token
What is token?
Smallest meaningful sequence of characters in programming
language.
Programming language keywords, operators, character literals,
string literals, numerical literals and punctuation symbols are
examples of tokens.
91 / ??
Token
92 / ??
Token
93 / ??
Tokens
94 / ??
Tokens
95 / ??
How a scanner recognises tokens
96 / ??
Regular Expressions
97 / ??
Regular Expressions
Operator precedence: () * ˙ |
98 / ??
Regular Definitions
Notations:
1. The + symbol denotes one or more instance
2. The ? symbol denotes zero or one instance
3. The [ ] symbol denotes character classes
99 / ??
Regular Expression Notation
■ Notation example:
100 / ??
Regular Expression For Some Tokens
101 / ??
Building Finite Automata For lexical Tokens
if {return IF;}
102 / ??
Building Finite Automata For lexical Tokens
103 / ??
Building Finite Automata For lexical Tokens
104 / ??
Building Finite Automata For lexical Tokens
105 / ??
Syntax Analysis
106 / ??
Definitions
syntax: the way in which words are put together to form phrases,
clauses, or sentences. The rules governing the formation of
statements in a programming language.
syntax analysis: the task concerned with fitting a sequence of
tokens into a specified syntax.
parsing: To break a sentence down into its component parts of
speech with an explanation of the form, function, and syntactical
relationship of each part.
107 / ??
Parse tree
108 / ??
Syntax Analysis
109 / ??
Grammar
110 / ??
Definition of Context Free Grammar
111 / ??
Grammar
112 / ??
Grammar
113 / ??
Grammar
114 / ??
Grammar
115 / ??
Regular Grammar
116 / ??
Context-free Grammar
117 / ??
Grammar
118 / ??
Example
119 / ??
Example
119 / ??
Example
120 / ??
Example
120 / ??
Example
121 / ??
Example
121 / ??
Parsing
122 / ??
Parser
123 / ??
Example
124 / ??
Example
125 / ??
Example
126 / ??
Grammar Ambiguity
127 / ??
Grammar Ambiguity
128 / ??
Grammar Ambiguity
129 / ??
Grammar Ambiguity
130 / ??
Grammar Ambiguity
131 / ??
Left Recursion
132 / ??
Left Recursion
133 / ??
Left Recursion
134 / ??
Left Factoring
135 / ??
Left Factoring
Therefore grammar
stmt → if expr then stmt else stmt
| if expr then stmt
can be re-written as:
′
stmt → if expr then stmt stmt
′
stmt → else stmt | λ
■ Left factoring can be defined as re-writing grammar to
eliminate multiple production rules that begin with the same
token
136 / ??
Left Factoring
In general:
1. Let A → α β 1 | α β 2 be two production rules for the non
terminal symbol A
2. if the input begins with a non empty string derived from α
3. and we do not know whether to expand A to α β 1 or α β 2
′
4. then we may defer the decision by expanding A to α A
′
5. after seeing the input derived from α, we expand A to β 1 or
to β 2
6. this means, left-factored, the original productions become
′
A→ α A
A→ α β 1 | β 2
137 / ??
Syntax Analysis/Parsing
138 / ??
Some Compiler Functions
139 / ??
Syntax Analysis
140 / ??
Top down parser
141 / ??
Top down parsing
Example:
Given the following Grammar G for the input id+id*id. Use top
down parsing to determine a sequence of parse trees for the
input string.
′
E → TE
′ ′
E → + TE | ǫ
′
T → FT
′ ′
T → *FT | ǫ
F → (E) | id
142 / ??
Top down parsing
143 / ??
Recursive descent parser
144 / ??
Recursive descent parsing
145 / ??
Recursive descent parsing
Example:
Given the Grammar:
S→cAd
A → ab | a
To construct a parse tree for the input string w = cad
1. Tree begins with a single node labelled S
2. S has a production rule thus the first tree is constructed.
3. The input pointer moves to the first element of the input
string a.
4. The left-most leaf c matches the first symbol of the input
string, so the pointer moves to the second element a.
5. There is a production rule for A so it’s expanded to ab.
6. Second symbol a has a match so we proceed to the third
symbol d.
7. But d does not match b so the pointer is reset to position 2.
146 / ??
Recursive descent parser
147 / ??
FIRST and FOLLOW
148 / ??
Rules to Create FIRST
149 / ??
Rules to Create FOLLOW
150 / ??
Rules to Create FOLLOW
151 / ??
Rules to Create FOLLOW
152 / ??
Rules to Create FOLLOW
153 / ??
Bottom up parsing
154 / ??
Example
155 / ??
Bottom up parsing
156 / ??
Shift reduce parsing
157 / ??
Shift reduce parsing
■ If the stack contains the start symbol and the input buffer
is empty then the parser halts and announces successful
parsing
■ Otherwise the parser might encounter an error.
■ The shift-reduce parser has four operations: (1) Shift (2)
Reduce (3) Accept (4) Error
(1) Shift: Shift the next input symbol on top of the stack.
(2) Reduce: Replace the string within the stack with a
nonterminal at the head of the appropriate production rule.
(3) Accept: Announce successful completion of parsing.
(4) Error: Discovers syntax error while parsing.
158 / ??
Shift reduce parsing
159 / ??
LR-Parsing
160 / ??
LR-Parsing
161 / ??
Example 1
162 / ??
Example 1
163 / ??
Example 1
164 / ??
Example 1
165 / ??
Example 1
166 / ??
Example 1
167 / ??
Example 1
168 / ??
Example 1
169 / ??
Example 1
170 / ??
Example 1
171 / ??
Yacc
172 / ??
How to write LR Parser
173 / ??
Declaration Part
174 / ??
Translation Rules
175 / ??
Yacc
176 / ??
Yacc
177 / ??
Yacc
178 / ??
Yacc
179 / ??
Yacc
180 / ??
Yacc
181 / ??
Yacc
182 / ??
Yacc
183 / ??
Yacc
184 / ??
Yacc
185 / ??
Semantic Analysis
186 / ??
Symbol Table
187 / ??
Symbol Table
188 / ??
Symbol Table Object
189 / ??
Symbol Table Implementation
190 / ??
Symbol Table Implementation
191 / ??
Symbol Table Implementation
192 / ??
Symbol Table
193 / ??
Example
Figure 69:
194 / ??
Example
Figure 70:
195 / ??
Example
Figure 72:
197 / ??
Example
Figure 73:
198 / ??
Type Checking
199 / ??
Type rules
200 / ??
Type checking
Figure 74:
201 / ??
Example
Example:
a+b
a is of type integer and b is of type float.
The compiler converts one of the operands before the addition.
For example the integer 2 is converted to float in the calculation
of 2 * 3.142
t1 = (float)2
t2 = t1 * t2
202 / ??
Type Conversion
203 / ??
Type Conversion
204 / ??
Type checking: Implementation
205 / ??
Type System
206 / ??
Type Checking
Figure 75:
207 / ??
Processing Declaration
Figure 76:
208 / ??
Example
Figure 77:
209 / ??
Example
Figure 78:
210 / ??
Example
Figure 79:
211 / ??
Example
Figure 80:
212 / ??
Scope & Binding
213 / ??
Scope
214 / ??
Scope
215 / ??
Static Scope
216 / ??
Static Scope
217 / ??
Static Scope
Figure 81:
218 / ??
Dynamic Scope
219 / ??
Environment & State
220 / ??
Environment & State
Figure 82:
221 / ??
Example
Figure 83:
222 / ??
Example
223 / ??
Binding Names to Locations
224 / ??
Binding Locations to Values
225 / ??
Memory Management
226 / ??
Memory Management
227 / ??
Memory Management
228 / ??
Memory Management
229 / ??
Data Allocation
230 / ??
Explicit allocation
231 / ??
Basic Memory Allocation
232 / ??
Chunks
■ Pointers that reside inside a block are under user control and
will point to the start of blocks.
■ Pointers inside the allocation administration are under the
control of the allocator and will point to chunks.
■ Chunk size field and there are bits in each chunk for
administration purposes
■ One of the bits is a free bit, which indicates if a chunk is
free.
233 / ??
Memory Structure: Used by Malloc
Figure 84:
234 / ??
Memory Structure: Used by Malloc
235 / ??
Data allocation with implicit deallocation
236 / ??
Data allocation with implicit deallocation
237 / ??
Basic Garbage Collection Algorithm
238 / ??
Basic Garbage Collection Algorithm
239 / ??
Reference Counting
240 / ??
Reference Counting
241 / ??
Example
Figure 85:
242 / ??
Example
Figure 86:
243 / ??
Mark & Scan
244 / ??
Mark & Scan
245 / ??
Two Space Copying
246 / ??
Basic Garbage Collection Algorithm
247 / ??
Garbage Chunks
248 / ??
Compaction
249 / ??
Role Of Compiler In Garbage Collection
250 / ??
Intermediate Code Generation
251 / ??
Intermediate Representation(IR)
252 / ??
Intermediate Representation
253 / ??
Example
254 / ??
Figure 87:
Intermediate Code Generation
255 / ??
Types Of Intermediate Representation
256 / ??
Types Of Intermediate Representation
256 / ??
Abstract Syntax Trees Construction
257 / ??
Example
Figure 88:
258 / ??
Linear intermediate representation(Linear IR)
259 / ??
Stack-Machine Code
260 / ??
Example
Figure 89:
261 / ??
Three-Address Code
262 / ??
Three-Address Code
262 / ??
Example
Figure 90:
263 / ??
Storing Three-Address Code
264 / ??
Storing Three-Address Code
Figure 91:
265 / ??
Example
Figure 92:
266 / ??
Example
Figure 93:
267 / ??
Example
Figure 94:
268 / ??
Example
Figure 95:
269 / ??
Example
Figure 96:
270 / ??
Example
Figure 97:
271 / ??
Example
273 / ??
Example
Figure 100:
274 / ??
Code Generation
275 / ??
Code Generation
276 / ??
Code Generator
Figure 101:
277 / ??
Code Generation
278 / ??
Code Generation
278 / ??
Code Generator
279 / ??
Code Generator
280 / ??
Instruction Selection
281 / ??
Register Allocation and Assignment
282 / ??
Register Allocation and Assignment
283 / ??
Instruction Ordering
284 / ??
Target Machine
285 / ??
Target Machine
286 / ??
References
287 / ??