Chapter 5 Intermediate Code Generaration-1
Chapter 5 Intermediate Code Generaration-1
a + a +
* * *
c c c
SDD for creating syntax tree
(Syntax Directed Definition)
Production Semantic Rules
1) S-> id:=E S.Node = mknode(‘assign’, mkleaf(id ,
id.place, E.nptr)
2) E -> E1+E2 E.Node = mknode(‘+’, E1.nptr,E2.nptr)
3) E -> E1*E2 E.Node = mknode(‘*’, E1.nptr,E2.nptr)
4) E -> -E1 E.Node = mkunode(‘uminus’, E1.nptr)
5) E-> (E1) E.node = E1.nptr
6) E -> id E.nptr = mkleaf(id, id.place)
Parse trees
Parse trees are usually generated as the next
step after lexical analysis (which turns the
source code into a series of tokens that can
be viewed as meaningful units, as opposed to
just a sequence of characters).
a= b*-c + b*-c
a +
t1 = – c
* * t2 = b * t1
t3 = - c
uminus b uminus
t4 = b * t3
b
t5 = t2+ t4
c c a= t5
Forms of three address
instructions
x = y op z (assignment statement for binary operation)
x = op y (assignment statement for unary operation)
x = y (copy statement)
goto L (unconditional jump)
if x relop y goto L (conditional jump)
Procedure calls using:
param x1
param x2 ……
call p,n
y = call p,n
x = y[i] and x[i] = y (indexed assigment)
x = &y and x = *y and *x =y (address and pointer
assignment)
Data structures for three address
codes
Quadruples
Has four fields: op, arg1, arg2 and result
Triples
Temporaries are not used and instead
references to instructions are made
Indirect triples
In addition to triples we use a list of pointers to
triples
Quadruple
A quadruple is a record structure with four
fields: op,
arg1, arg2, and result
–The op field contains an internal code for an
operator – Statements with unary
operators do not use arg2
– The contents of fields arg1, arg2, and
result are
typically pointers to symbol table entries
– If so, temporaries must be entered into the
symbol
Example: a = b*-c + b*-c
t1 = – c
t2 = b * t1
t3 = - c
t4 = b * t3
t5 = t2+ t4
a= t5
Example: a = b*-c + b*-c
t1 = – c
t2 = b * t1
t3 = - c
t4 = b * t3
t5 = t2+ t4
a= t5
Triple
Triples:
- To avoid entering temporary names into the
symbol table, we might refer to a temporary
value by the position of the statement that
computes it.