Module -5
Chapter 5 Introduction to Syntax Directed
Translation
Syllabus
2
3
4
5
6
7
SDD V/S SDT
8
9
SDD
SDD with BUP
10
SDD with TDP
11
12
Problem on SDD with BUP
13
1. Construct annotated parse tree for input 3*5+4n(VVIMP)
14
15
16
17
18
19
Home work
20
2. Construct annotated parse tree for input (3+4)*(5+6)n
21
3. Construct annotated parse tree for input 1*2*3*(4+5)n
22
23
24
25
Problem on SDD with TDP
26
Annotated tree and dependency graph
27
28
2. Write grammar and SDD for simple desk calculator and show
annotated parse tree for expression (3+4)*(5+6)n using TDP
29
Annotated parse tree for expression (3+4)*(5+6)n using TDP
3. Construct annotated parse tree for input 1*2*3*(4+5)n using TDP
30
4. Construct annotated parse tree for input (9+8*(7+6)+5)*4n using TDP
31
SDD for Declaration statement(VVIMP) [Semantic rules with controlled side effects]
32
1.Give SDD to process a variable declaration in C and construct dependency graph for the input real x,y,z
33
Dependency Graph
34
2.Int a,c
35
The structure of a Type (IMP-6M)
for about input.
Ans: int [2][3] array of 2 array 3 integers. The corresponding type expression
array(2,array(3,integer) is represented by following tree.
Note: Refer class notes for annotated tree and SDD
36
5.3 Application of Syntax Directed Translation(VVIMP)
37
38
39
1.Assuming suitable syntax directed definition construct a syntax tree for
expression a-4+e (10M) (VVVIMP)
Construction of syntax tree using BUP
40
Construction steps for syntax tree
41
P1=new Leaf(id, entry-a); P2=new Leaf(num,4); P3=new Node(‘-’,P1,P2);
P4=new Leaf(id, entry-c);
P5=new Node(‘+’,P3,P4);
42
2 .Assuming suitable syntax directed definition construct a syntax tree for
expression b*3-c
E🡪E+T E🡪E-T
E🡪T T🡪T*F T🡪T/F T🡪F F🡪(E)
F🡪digit F🡪id
43
3 .Assuming suitable syntax directed definition construct a syntax tree for
expression c+4/5
E🡪E+T E🡪E-T
E🡪T T🡪T*F T🡪T/F T🡪F F🡪(E)
F🡪digit F🡪id
44
4.Assuming TDP syntax directed definition to construct a syntax tree for
expression a-e+4
[ refer Class Notes]
Syntax Directed Translation scheme(out of syllabus)
45
Postfix SDT for desk calculator-continue..
46
Parser stack implementation of postfix SDT-Continue
47
48
Module -5
Chapter 6 Intermediate Code Generation
49
Chapter 6 Intermediate Code Generation
50
Intermediate Code Generation
51
52
53
6.1 Variants of Syntax tree
54
6.1.1 Directed Acyclic Graph for Expressions
55
atomic operands and interior codes corresponding to operators.
1.write DAG for below expression a+a*(b-c)+(b-c)*d
56
a*(b-c) and (b-c)*d.
SDD for Directed Acyclic Graph for Expressions
57
58
similarly for other identifier.
6.1.2 The value number method for constructing DAG
59
shown in below figure.
60
node within the array.
have number 1 and 2 respectively.
61
Algorithm for value number method for constructing DAG
Input: Label op,node l,and node r
Output: The value number of a node in the array with
signature <op,l,r >
Problems on DAG and value number method
1. ((x+y)-((x+y)*(x-y)))+((x+y)*(x-y))
2. Construct the DAG and identify the value number for the sub expression
of the following expressions, assuming + associates from left.
c) a+a+(a+a+a+(a+a+a+a))
62
6.2 Three Address Code
63
6.2.1 Address and Instruction
64
Instruction of 3-address code
65
Continue
66
Example of three address instruction
67
68
Consider the statement do i=i+1;while(a[i]<v);write 2possible translation of above statement using three address code instruction.
Note: the multiplication i*8 is appropriate for an array of elements that each take
8 units of space.
1.Difference between syntax tree and DAG for x=(a+b*c)/(a-b*c)
69
2.Difference b/w syntax tree &DAG for a+b*c-d/b*c
70
6.2.2 Quadruples
71
with fields for the operator and operands.
Triples”.
72
Example of different form of records
73
Example for 3-address code
74
Quadruple
75
1.Write the 3-addres code for the assignment a=b*-c+b*-c and also quadruple.(VVVVIMP)
76
77
2 Write the 3-addres code for the assignment (a+b*c)-(d/b*c) and also
quadruple.
3.Write the 3-addres code for the assignment a=b+c*d and
also quadruple.
78
6.2.3 Triples
79
Write syntax tree and triples for expression a+a*(b-c)+(b-c)*d
80
2 Write the 3-addres code for the assignment (a+b*c)-(d/b*c) and also
Triples
81
3.Write the 3-addres code for the assignment a=b+c*d and also Triples.
82
Indirect Triples
83
Examples of Indirect Triples for (a+b*c)-(d/b*c)
84
Translate the arithmetic expression a+-(b+c) into
85
a) Syntax tree b) Quaduples c)Triples d)Indirect Triples
6.2.4 Static single Assignment Form(SSA)
86
intermediate
representation
that
facilities
certain
code
static single assignment .
87
The same variable may be defined in 2 different control flow path in a program.
88
89
90
If(flag) x1=-1;else x2=1; X3= ᶲ(x1,x2);
Here, ᶲ(x1,x2) has value ‘x1’ if the control flow passes through the true part of the conditional and the value x2 if the control flow passes through the false part.
i.e ᶲ-function returns the value of its argument that corresponds to the control flow path that was taken to get to the assignment statement containing the ᶲ-function.
THANK YOU
91
THANK YOU
92