Professional Documents
Culture Documents
Title Page
CS432F/CSL 728: JJ II
Compiler Design J I
July 2004
Page 1 of 100
S. Arun-Kumar Go Back
sak@cse.iitd.ernet.in
Department of Computer Science and Engineering Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Contents Home Page
Contd . . .
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
. . . Contd JJ II
– IR: Properties
Go Back
– Typical Instruction Set
– IR: Generation
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 4 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 1 Home Page
J I
Page 5 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 2 Home Page
Page 6 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 3 Home Page
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 4 Home Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 5 Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 6 Home Page
target code
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 7 Home Page
target code
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 7
Home Page
Title Page
stream of
characters
SCANNER JJ II
stream of
tokens
PARSER
parse tree J I
ERROR− SEMANTIC ANALYZER SYMBOL
abstract
HANDLER TABLE
syntax tree
MANAGER
I.R. CODE GENERATOR Page 12 of 100
intermediate
representation
OPTIMIZER
optimized Go Back
intermediate
representation
CODE GENERATOR
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 13 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
JJ II
Page 14 of 100
• Keeps track of line numbers and column numbers and Full Screen
passes them as parameters to the other phases to en-
able error-reporting to the user.
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
• Examples of lexemes.
Go Back
– preprocessor directives
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
• Examples of tokens.
J I
– Reserved words (e.g. begin, end, struct, if etc.)
– Keywords (integer, true etc.) Page 16 of 100
rameter names)
– Literal constants (numeric, string, character con- Full Screen
stants etc.)
– Punctuation marks (:, , etc.) Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Scanning: 4
Title Page
JJ II
• Identification of tokens is usually done by a Determinis-
tic Finite-state automaton (DFA). J I
• The set of tokens of a language is represented by a
large regular expression. Page 17 of 100
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 5
Home Page
Title Page
a−e,g−z
identifier if identifier
f JJ II
0−9,a−z 0−9,a−z
2 3 4 0−9
J I
i z
j−
0−9
h,
5 6
a−
space 7 8 real
( int
14
other
* 10 * 11 )
13 9 12 Close
Quit
The Big Picture
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Syntax Analysis Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CF-Grammars: Home Page
– X ∈ N is a nonterminal and
– α ∈ (N ∪ T )∗ is a string of terminals and nontermi-
Full Screen
nals
Close
• a start symbol S ∈ N .
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Example Home Page
J I
S ⇒ ab
Page 21 of 100
S ⇒ aSb ⇒ aabb
S ⇒ aSb ⇒ aaSbb ⇒ aaabbb Go Back
ture of derivations.
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Empty word Home Page
S ⇒ SS ⇒ SSS ⇒ SS ⇒ . . . Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
1 JJ II
Derivation sequences
• put an artificial order in which productions are fired. J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees
2 Home Page
Title Page
S
JJ II
S S
J I
S S εε Page 24 of 100
Go Back
a S b a S b
Full Screen
Derivation tree of
ε a S b
abaabb Close
ε
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees
3 Home Page
Title Page
S JJ II
S S
J I
S S a S b Page 25 of 100
Go Back
a S b ε a S b
Full Screen
ε ε
Another
Close
Derivation tree of
abaabb
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees
4 Home Page
Title Page
S
JJ II
S S
J I
S S a S b Page 26 of 100
ε a b
Go Back
a S b S
Full Screen
ε ε
Yet another
Close
Derivation tree of
abaabb
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 27 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 1
Home Page
E → I | C | E+E | E∗E
I → y | z Title Page
C → 4
Consider the sentence y + 4 ∗ z. JJ II
J I
E E
Page 28 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 2
Home Page
E → I | C | E+E | E∗E
I → y | z Title Page
C → 4
Consider the sentence y + 4 ∗ z. JJ II
J I
E E
Page 29 of 100
E + E E * E
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 3
Home Page
E → I | C | E+E | E∗E
I → y | z Title Page
C → 4
Consider the sentence y + 4 ∗ z. JJ II
J I
E E
Page 30 of 100
E + E E * E
Go Back
* E E + E
I E I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 4
Home Page
E → I | C | E+E | E∗E
I → y | z Title Page
C → 4
Consider the sentence y + 4 ∗ z. JJ II
J I
E E
Page 31 of 100
E + E E * E
Go Back
* E E + E
I E I
Full Screen
I I C
y C z
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 5
Home Page
E → I | C | E+E | E∗E
I → y | z Title Page
C → 4
Consider the sentence y + 4 ∗ z. JJ II
J I
E E
Page 32 of 100
E + E E * E
Go Back
* E E + E
I E I
Full Screen
I I C
y C z
Close
z y 4
Quit
4
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 33 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 0
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
a − a / b
Page 34 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 1
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
− a / b
Page 35 of 100
Principle: Go Back
Reduce
whenever possible.
Full Screen
Shift only when
reduce is
impossible Close
a
Shift Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 2
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
− a / b
Page 36 of 100
Go Back
Full Screen
Close
D Reduce by r5 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 3
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
− a / b
Page 37 of 100
Go Back
Full Screen
Close
T Reduce by r4 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 4
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
− a / b
Page 38 of 100
Go Back
Full Screen
Close
E Reduce by r2 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 5
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
a / b
Page 39 of 100
Go Back
Full Screen
Close
− Shift
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 6
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 40 of 100
Go Back
Full Screen
a
Shift Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 7
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 41 of 100
Go Back
Full Screen
D Reduce by r5 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 42 of 100
Go Back
Full Screen
T Reduce by r4 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 43 of 100
Go Back
Full Screen
T Reduce by r4 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 44 of 100
Go Back
Full Screen
Close
E Reduce by r1 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
b
Page 45 of 100
Go Back
Full Screen
Close
/
Shift
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 46 of 100
Go Back
Full Screen
b Shift Close
/
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 47 of 100
Go Back
Full Screen
D Reduce by r5 Close
/
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 48 of 100
Go Back
Full Screen
T
Reduce by r4 Close
/
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 14a
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 49 of 100
Go Back
k!
Full Screen
uc
St
E Reduce by r2 Close
/
Get back!
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 14b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 50 of 100
Go Back
Full Screen
E Reduce by r2 Close
/
Get back!
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 51 of 100
Go Back
Full Screen
T
Reduce by r4 Close
Get back! /
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 52 of 100
Go Back
Full Screen
D Reduce by r5 Close
Get back! /
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 53 of 100
Go Back
Full Screen
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
b
Page 54 of 100
Go Back
Full Screen
Close
/
Get back! Shift
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9b
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 55 of 100
Go Back
Full Screen
Get back to
where you Close
once belonged!
E Reduce by r1 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8b
fied
Home Page
o d i
m Principle:
r1. E E − T
Title Page
r2 E T Reduce whenever possible, but
r3 T T / D but depending upon
JJ II
r4 T D
lookahead
r5 D a | b | ( E )
J I
/ b
Page 56 of 100
Shift instead
of reduce here!
Go Back
ce
i f t − redu
Sh ict Full Screen
confl
T Reduce by r4 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
/ b
Page 57 of 100
Go Back
Full Screen
T Reduce by r4 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
b
Page 58 of 100
Go Back
Full Screen
/ Shift
T Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 59 of 100
Go Back
b
Shift Full Screen
/
T Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 60 of 100
Go Back
D Reduce by r5
Full Screen
/
T
Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 61 of 100
Go Back
Full Screen
T Reduce by r3 Close
−
E
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13
Home Page
r1. E E − T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I
Page 62 of 100
Go Back
Full Screen
Close
E Reduce by r1 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 0 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 63 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 1 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 64 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 2 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 65 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 66 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3a Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 67 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3b Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 68 of 100
Go Back
Full Screen
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 4 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 69 of 100
Go Back
Full Screen
D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 70 of 100
Go Back
T T
Full Screen
D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5a Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 71 of 100
Go Back
T T
Full Screen
D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5b Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 72 of 100
Go Back
T T
Full Screen
D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 6 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 73 of 100
Go Back
T T
Full Screen
D D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 7 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
Page 74 of 100
E
T
Go Back
T T
Full Screen
D D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 8 Home Page
Title Page
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II
J I
E
Page 75 of 100
E
T
Go Back
T T
Full Screen
D D D
Close
a − a / b
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Yacc.
• Shift-reduce conflicts if any, are automatically detected Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Semantic Analysis: 1 Home Page
– unbounded time
J I
• The parser of a programming language provides the
framework within which the target code is to be gener-
ated. Page 78 of 100
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Semantic Analysis: 2
Title Page
JJ II
eters
– type consistency between declaration and use. Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 80 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
0 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 81 of 100
n
F
Go Back
( )
E
E T Full Screen
−
T
F
Close
F
n
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
1 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 82 of 100
n
F
Go Back
( )
E
E T Full Screen
− Synthesized Attributes
4 3 2 1
T
F
Close
F
n
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
2 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 83 of 100
n
F
Go Back
( )
E
E T Full Screen
− Synthesized Attributes
4 3 2 1
T
F
Close
4 F
n
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
3 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 84 of 100
n
F
Go Back
( )
E
E T Full Screen
− Synthesized Attributes
4 3 2 1
4 T
F
Close
4 F
n
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
4 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 85 of 100
n
F
Go Back
( )
E
4 E T Full Screen
− Synthesized Attributes
4 3 2 1
4 T
F
Close
4 F
n
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
5 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 86 of 100
n
F
Go Back
( )
E
4 E T Full Screen
− Synthesized Attributes
4 3 2 1
4 T
F
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
6 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 87 of 100
n
F
Go Back
( )
E
4 E T Full Screen
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
7 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 88 of 100
n
F
Go Back
( )
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
8 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 89 of 100
n
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
9 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T F
/
Page 90 of 100
n
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
10 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T 3 F
/
Page 91 of 100
n
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
11 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T 3 F
/
Page 92 of 100
n 2
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
12 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T
−
J I
T 3 F 2
/
Page 93 of 100
n 2
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
13 Home Page
Title Page
E T F E
JJ II
/ ( ) n
T 1
−
J I
T 3 F 2
/
Page 94 of 100
n 2
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:
14 Home Page
Title Page
E T F E 1
JJ II
/ ( ) n
T 1
−
J I
T 3 F 2
/
Page 95 of 100
n 2
3
F
Go Back
( )
3
E
4 E T Full Screen
1
− Synthesized Attributes
4 3 2 1
4 T
F 1
Close
4 F
n 1
Quit
4 n
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An Attribute Grammar Home Page
Title Page
E 1
E0 → E1−T B E0.val := sub(E1.val, T.val)
JJ II
T 1
E → T B E.val := T.val
T 3 F 2
/ J I
F
3
n 2
T0 → T1/F B T0.val := div(T1.val, F.val)
Page 96 of 100
( )
3
E
4 E T 1
T → F B T.val := F.val
− Go Back
4 T
F 1
4 n
F → n B F.val := n.val Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 0
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
T L
J I
I
,
int L Page 97 of 100
z
I
Go Back
L ,
y Full Screen
I
D L T I
Close
x y z int x
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 1
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
T L
J I
I
,
int int L Page 98 of 100
z
I
Go Back
L ,
y Full Screen
I
D L T I
Close
x y z int x
Quit
, int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 2
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
int T L
J I
I
,
int int L Page 99 of 100
z
I
Go Back
L ,
y Full Screen
I
D L T I
Close
x y z int x
Quit
, int int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 3
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
int T L int
J I
I
,
int int L Page 100 of 100
z
I
Go Back
L ,
y Full Screen
I
D L T I
Close
x y z int x
Quit
, int int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 4
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
int T L int
J I
I int
,
int int L int Page 101 of 100
z
I
Go Back
L ,
y Full Screen
I
D L T I
Close
x y z int x
Quit
, int int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 5 Home Page
D
Title Page
int T L int
JJ II
I int
,
int int L int
J I
z int
I int
L int , Page 102 of 100
y
Go Back
I
D L T I
Full Screen
x y z int x
Close
, int int
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 6
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
int T L int
J I
I int
,
int int L int Page 103 of 100
z int
I int
Go Back
L int ,
I int
D L T I
Close
x y z int x
Quit
, int int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 7
C -style declarations generating int x, y, z. Home Page
D → TL T → int | float
L → L,I | I I → x | y | z Title Page
D
JJ II
int T L int
J I
I int
,
int int L int Page 104 of 100
z int
I int
Go Back
L int ,
I int
D L T I
Close
x y z int x int
Quit
, int int
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
D
D → TL B L.in := T.type JJ II
int T L int
,
I int T → int B T.type := int.int
int int L int J I
z int
L int ,
I int
T → float B T.type := float.f loat
Page 105 of 100
y int
I int L0 → L1,I B L1 := L0.in
Go Back
I → id B id.type := I.in
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 0
Home Page
E → E−T | T
T → T /F | F Title Page
F → n | (E)
Suppose we want to evaluate an expression (4 − 1)/2. JJ II
/
Go Back
−− 2 Full Screen
Close
4 1
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 0 Home Page
Title Page
JJ II
/
J I
−− 2
Go Back
Full Screen
4 1
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 1 Home Page
Title Page
JJ II
/
J I
−− 2
Go Back
Full Screen
4 1
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 2 Home Page
Title Page
JJ II
/
J I
3 2
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 3 Home Page
Title Page
JJ II
/
J I
3 2
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 4
Home Page
Title Page
1 JJ II
J I
Go Back
Full Screen
Close
But what we actually get during parsing is a tree that looks Quit
like . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 1
. . . THIS! E
Home Page
Title Page
T
T F JJ II
/
J I
n
F
/
Go Back
E T
−
−− n Full Screen
T
F
n n Close
F
n
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 2
Home Page
Title Page
We use attribute grammar rules to construct the abstract
syntax tree (AST)!. JJ II
But in order to do that we first require two procedures for
tree construction.
J I
makeLeaf(literal) : Creates a node with label literal and re-
turns a pointer to it. Page 114 of 100
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 3 Home Page
Title Page
T → F B T.ptr := F.ptr
Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Symbol Table:1
Title Page
JJ II
• The store house of context-sensitive and run-time infor-
mation about every identifier in the source program.
J I
• All accesses relating to an identifier require to first find
the attributes of the identifier from the symbol table Page 116 of 100
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
• scope/visibility information
Page 117 of 100
• base address
• addresses to location of auxiliary symbol tables (in case Go Back
of records, procedures, classes)
• address of the location containing the string which ac- Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
– insertion
Page 118 of 100
– search
– deletions are purely logical (depending on scope Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Semantic analysis:
• Determination of types of identifiers from declara- Go Back
tions
• type checking to ensure that operands are used in Full Screen
type-valid contexts.
• Checking scope, visibility violations. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
Symbol Table:5 JJ II
IR generation: . Memory allocation and relativea address
calculation. J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intermediate Home Page
memory size)
Full Screen
– number of registers
– memory size Close
– word length
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR Properties: 1
1. It is fairly low-level containing instructions common to Home Page
cations
• no commitment to Representational Issues Go Back
4. To ensure type-safety
Full Screen
Next
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
IR: Representation? JJ II
• No commitment to word boundaries or byte boundaries
• No commitment to representation of J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
stoop? JJ II
• so as to be interpreted easily,
Page 124 of 100
• the interpreter is fairly small,
• execution speeds are high, Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
rise? JJ II
• typed variables,
• temporary variables instead of registers. J I
• array-indexing,
Page 125 of 100
• random access to record fields,
• parameter-passing, Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instruction
Home Page
JJ II
Three address code: A suite of instructions. Each instruc-
tion has at most 3 operands.
J I
• an opcode representing an operation with at most 2
operands
Page 126 of 100
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
A typical instruction JJ II
set: 2 J I
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional) Page 127 of 100
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
set: 2 JJ II
• Assignments (LOAD-STORE)
– x := y bop z, where bop is a binary operation J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
set: 2 JJ II
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional) J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
set: 2 JJ II
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional) J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instruction Home Page
• Assignments (LOAD-STORE) JJ II
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instruction Home Page
• Assignments (LOAD-STORE)
JJ II
• Jumps (conditional and unconditional)
• Procedures and parameters J I
Picture
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Pointers Home Page
Title Page
x y x y
*x *y x := ^y @y *y
JJ II
x y x y
J I
*x @z *z @z
x := *y Page 133 of 100
*z *z
z z Go Back
y y
*y *y
Full Screen
x x
*x := y @z
@z z
z Close
*z *y
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Generation Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example Home Page
Title Page
E → id B JJ II
J I
E.place := id.place;
E.code := emit()
Page 137 of 100
E0 → E1 − E2 B
Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example
Home Page
S → id := E B Title Page
JJ II
S.code := E.code
|| emit(id.place:=E.place)
J I
S0 → while E do S1 B
Page 138 of 100
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example
Home Page
S → id := E B Title Page
JJ II
S.code := E.code
|| emit(id.place:=E.place)
J I
S0 → while E do S1 B
Page 139 of 100
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Calling Chain Home Page
Main program
Title Page
Globals
Procedure P2
Locals of P2 JJ II
Procedure P21
Locals of P21 J I
Body of P21
Call P21
Page 141 of 100
Body of P2
Call P21
Go Back
Procedure P1
Locals of P1
Full Screen
Body of P1
Call P2
Close
Main body
Call P1
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 1 Home Page
Main program
Title Page
Globals
Procedure P2 JJ II
Locals of P2
J I
Procedure P21
Locals of P21
Body of P21
Body of P2 Go Back
Procedure P1
Full Screen
Locals of P1
Body of P1
Close
Main body
Globals
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 2 Home Page
Main program
Title Page
Globals
Procedure P2 JJ II
Locals of P2
J I
Procedure P21
Locals of P21
Body of P21
Body of P2 Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 3 Home Page
Main program
Title Page
Globals
Procedure P2 JJ II
Locals of P2
Procedure P21 J I
Locals of P21
Main body
Globals
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 4 Home Page
Main program
Title Page
Globals
Procedure P2 JJ II
Return address to last of P2
Locals of P2
Dynamic link to last P2
Locals of P21
Procedure P21 J I
Static link last P2
Locals of P21 Formal par P21
Main body
Globals
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 5 Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit