Intermediate Code Generation. Professor Yihjia Tsai Tamkang University. Introduction. Intermediate representation (IR) Generally a program for an abstract machine (can be assembly language or slightly above) Easy to produce and translate into target code Why?
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
expr.t = 952+
expr.t = 95
term.t = 2
expr.t = 9
term.t = 5
term.t = 9
9

5
+
2
Annotated parse tree corresponding to “95+2”
Syntaxdirected definition for desk calculator program
Draw the annotated parse tree for “3*5+4 $”
L
$
E.Val = 19
E.val = 15
+
T.val=4
T.val = 15
F.val=4
T.val=3
T.val=5
*
F.val=3
F.val=5
digit.lexval=4
digit.lexval=3
digit.lexval=5
Annotated parse tree corresponding to “3*5+4 $”
Syntaxdirected definition with inherited attribute
L.in for declaration of variables of type int or real
Draw the annotated parse tree for “real id1, id2, id3”
D
L.in = real
T.type = real
L.in = real
,
id3
real
L.in = real
,
id2
id1
Annotated parse tree for “real id1, id2, id3” with
inherited attribute in at each node L
E → T R
R → addop T { print(addop.lexeme) } R1  Λ
R → subop T { print(subop.lexeme) } R2  Λ
T → num{ print(num.val) }
E
R
T

T
{ print (‘’) }
R
9
{ print (‘9’) }
+
T
{ print (‘+’) }
R
5
{ print (‘5’) }
Λ
2
{ print (‘2’) }
Parse tree for “95+2” showing actions; when performed in depthfirst order, prints “952+”
expr → expr + term { print (‘+’) }
expr → expr – term { print (‘’) }
expr → term
term → 0 { print (‘0’) }
term → 1 { print (‘1’) }
…
term → 9 { print (‘9’) }
expr
+
{ print (‘+’) }
expr
term

{ print (‘’) }
expr
term
2
{ print (‘2’) }
term
9
{ print (‘9’) }
5
{ print (‘5’) }
Actions translating “95+2” into “952+”
p1 = mkleaf(id, entry_a);
p2 = mkleaf(num, 4);
p3 = mknode(‘’, p1, p2);
p4 = mkleaf(id, entry_c);
p5 = mknode(‘+’, p3, p4);
What is the syntax tree?
E.nptr = mknode(‘+’, E1.nptr, T.nptr)
give a syntax tree, dag and postfix notation
x := y <op> z
t1 := y * z t2 := x + t1
param x1
param x2
…
param xn
call p, n