Syntax-Directed Translation
Syntax-Directed Translation
COMPILER DESIGN
Course code:
SCS1303
August 11, 2024 SCS1303 Compiler Design 1
UNIT 3
INTERMEDIATE CODE GENERATION
– Semantic actions
A→XYZ {Y.VAL=2*A.VAL}
August 11, 2024 SCS1303 Compiler Design 10
August 11, 2024 SCS1303 Compiler Design 11
Annotated Parse Tree
• A parse tree showing the values of attributes at each node is called
an annotated parse tree.
• The process of computing the attributes values at the nodes is called
annotating(or decorating) of the parse tree.
• Of course, the order of these computations depends on the
dependency graph induced by the semantic rules.
The flow of information happens bottom-up and all the children attributes
are computed before parents.
August 11, 2024 SCS1303 Compiler Design 13
Example
TOP
Z Z.VAL
Y Y.VAL
X X.VAL
S→E$
E → E1 + E2
E → E1 * E2
E → (E1)
E→I
I → I1 digit
I → digit
Input: 23*5+4$
TOP
E E.VAL
+
E E.VAL
.
August 11, 2024 SCS1303 Compiler Design 19
e Moves Input STATE VAL Production used
q (1) 23*5+4$ _ _
u
(2) 3*5+4$ 2 _
(3) 3*5+4$ I 2 I → digit
e (4)
(5)
*5+4$
*5+4$
I3
I
2_
(23) I → I digit
n (6) *5+4$ E (23) E→I
(7) 5+4$ E* (23) _
c (8) +4$ E*5 (23) _ _
e (9)
(10)
+4$
+4$
E*I
E*E
(23) _ 5
(23) _ 5
I → digit
E→I
o (11)
(12)
+4$
4$
E
E+
(115)
(115) _
E→E*E
o (16)
(17)
$
_
E
E$
(119)
(119) _
E→E+E
e
August 11, 2024 SCS1303 Compiler Design 20
Intermediate Code
• The operator and keyword nodes of the parse tree are moved
to their parents and a chain of single productions is replaced
by single link in syntax tree the internal nodes are operators
and child nodes are operands.
• In the parse tree, most of the leaf nodes are single child to
their parent nodes.
• In the syntax tree, we can eliminate this extra information.
• Syntax tree is a variant of parse tree. In the syntax tree,
interior nodes are operators and leaves are operands.
• Syntax tree is usually used when represent a program in a tree
structure.
• A sentence id + id * id would have the following syntax tree:
* d
a +
b c
• The syntax tree for the statement if a=b then a=c+d else b=c-d
if-then-else
= = =
a b a b -
+
c d c d
– Triples and
– Indirect triples.
Disadvantage :
• Temporaries are implicit and difficult to rearrange code.
• It is difficult to optimize because optimization involves moving
intermediate code. When a triple is moved, any other triple
referring to it must be updated also.
• With help of pointer one can directly access symbol table
entry.
Conditional jump:
if x < y goto L
op arg1 arg2
(0) < x y
(1) if (0) L
op arg1 arg2
(0) =[] y i
(1) = x (0)
x[i] := y
op arg1 arg2
(0) []= x i
(1) = (0) y
• The three address code for id=E consist of code to evaluate E into
some temporary t ,followed by the assignment id.place=t.
• GEN would enter the operator + , and the values of A,B and
C into the quadruple array.
of A= − B*(C+D)
= − B*(C+D)
Sy − B*(C+D) id
id=
A
A−
nt B*(C+D) id = − A− −
ax *(C+D) id = − id A− − B
- *(C+D) id = − E A− − B T1 = - B
*(C+D) id = E A− T1
di (C+D) id= E * A− T1 −
re C+D) id= E * ( A− T1 − −
ct +D) id= E * ( id A− T1 − − C
+D) id= E * ( E A− −T1 − − C
e D) id= E * ( E + A− T1 − − C −
d ) id= E * ( E + id A− T1 − − C − D
tr ) id= E * ( E + E A− T1 − − C − D T2 = C + D
a ) id= E * ( E
id= E * ( E )
A− T1 − − T2
A− T1 − − T2 −
ns id= E * E A− T1 − T2 T3 = T1 * T2
la id= E A− T3 A = T3
ti11, 2024
August
S S
SCS1303 Compiler Design 68
Assignment Statement with mixed types
• The constants and variables would be of different types,hence
the compiler must either reject certain mixed-mode
operations or generate appropriate coercion (mode
conversion) instructions.
Three-addresss code:
T1=I int * J
T2= inttoreal T1
T3=Y real +T2
X=T3
goto L
If A goto L
If A relop B goto L
e.g-2.
A relational expression A< B is equivalent to the conditional
statement
if A < B then 1 else 0
if E then S1 else S2
if E then S1 else S2
August 11, 2024 SCS1303 Compiler Design 83
Attributes used
if E then S1 else S2
Case V1 L1
Case V2 L2
.
.
.
Case Vn-1 Ln-1
Case T, Ln Equivalent code:
label NEXT if T=V goto L
Single argument
x y z
Elist , E
E.place=z
Elist , E x y
E.place=y
E x
E.place=x
For example:
int x;
Should be processed by the compiler as:
insert (x, int)
August 11, 2024 SCS1303 Compiler Design 109
Lookup
In the symbol table, lookup() operation is used to search a name.
It is used to determine:
• The existence of symbol in the table.
• The declaration of the symbol before it is used.
• Check whether the name is used in the scope.
• Initialization of the symbol.
• Checking whether the name is declared multiple times.
Drawbacks:
1. As the array fills, collisions become
more frequent – reduced
performance.
2. Table size is an issue – dynamically
increasing the table size is a difficulty.
1. fixed length
Name Type
e.g.
c a l c u l a t e int
int calculate
s u m int
int sum
a int
int a,b b int
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
c a l c u l a t e $ s u m $ a $ b $