0% found this document useful (0 votes)
75 views

Compiler Design Unit 3

The document discusses Syntax Directed Translation (SDT) which allows attaching subroutines or semantic actions to productions of a Context-Free Grammar (CFG). SDT is done by attaching rules to productions in a grammar. Attributes are associated with grammar symbols and rules are associated with productions. Attributes may represent values like numbers, types, etc. Semantic rules set up dependencies between attributes represented as a dependency graph to determine evaluation order. SDT can use synthesized attributes computed from child nodes or inherited attributes computed from parent/sibling nodes. Quadruple and triple representations are used to implement three-address intermediate code.

Uploaded by

Shubham Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Compiler Design Unit 3

The document discusses Syntax Directed Translation (SDT) which allows attaching subroutines or semantic actions to productions of a Context-Free Grammar (CFG). SDT is done by attaching rules to productions in a grammar. Attributes are associated with grammar symbols and rules are associated with productions. Attributes may represent values like numbers, types, etc. Semantic rules set up dependencies between attributes represented as a dependency graph to determine evaluation order. SDT can use synthesized attributes computed from child nodes or inherited attributes computed from parent/sibling nodes. Quadruple and triple representations are used to implement three-address intermediate code.

Uploaded by

Shubham Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

SYNTAX DIRECTED TRANSLATION

There is a notational framework for intermediated


code generation that is extension of CFG. This
framework is called as SDT.
It allows subroutines or semantic actions to be
attached to the productions of a CFG. These
subroutines generates intermediate code when
called at appropriate times by parser.
SDT is done by attaching Rules to Production in a
Grammar.
Every node of syntax tree is attached with attribute
set.
SYNTAX DIRECTED TRANSLATION
The basic structure for SDT is given below:

Parse tree

Semantic analysis

Dependency graph

SDT Evaluation order for semantic rules

Translation of construct
SYNTAX DIRECTED DEFINITION
(SDD)
A Syntax-Directed Definition is a context free
grammar together with Attributes and Rules.
Attributes are associated with grammar symbols
and Rules are associated with productions.
If X is a symbol and “a” is one of its attributes then
we write X.a to denote the value of “a” at a
particular parse tree node labeled X.

Attributes may be of any kind :


numbers, types, tables, references, or strings, for
instance.
Semantic Action
Semantic rules or semantic action set up
dependencies between attributes that will be
represented by the graph, from the dependency
graph, we can drive an evaluation order for
semantic rule.
Eg: Production Semantic Rule
EE’ + T E.val = E’.val + T.val
E T E.val = T.val
T T’ * F T.val = T’.val * F.val
TF T.val =F.val
F digit F.val = digit.lex val
SYNTAX DIRECTED TRANSLATION

SDT is partitioned into two subset:


•Synthesized attributes
•Inherited attributes

Attributes are associated with the grammar that are


symbol labels of the parse tree node.
Synthesized attribute
An attribute at a node is said to be synthesized if its value is
computed from the attributed values of the children of that
node in the parse tree.
Synthesized attributes are termed as S-attributes.
A parse tree along with values of the attributes at nodes is
called annotated parse tree.
Production Semantic Rule
EE + T E.val = E.val + T.val
E T E.val = T.val
T T* F T.val = T.val * F.val
TF T.val =F.val
F digit F.val = digit.lex val
Synthesized attribute

Example of Annotated Parse Tree


Inherited attribute
An attribute at a node is said to be inherited attribute if its
value is computed from the parent or sibling

Eg: real id1, id2, id3


SYNTAX DIRECTED TRANSLATION
1.S-attributed SDT :
1. If an SDT uses only synthesized attributes, it is
called as S-attributed SDT.
2. S-attributed SDTs are evaluated in bottom-up
parsing, as the values of the parent nodes depend
upon the values of the child nodes.
3. Semantic actions are placed in rightmost place of
RHS.
2.L-attributed SDT:
1. If an SDT uses both synthesized attributes and
inherited attributes with a restriction that inherited
attribute can inherit values from left siblings only, it
is called as L-attributed SDT.
2. Attributes in L-attributed SDTs are evaluated by
depth-first and left-to-right parsing manner.
3. Semantic actions are placed anywhere in RHS.
Dependency Graph
If an attribute ‘b’ at the node in the parse tree depends on
attribute ‘c’ then semantic rule for ‘b’ at the node must be
evaluated after the semantic rule that defines ‘c’.
The interdependency between the inherited and
synthesized attributes at the nodes in a parse tree can be
depicted by a directed graph called a dependency graph.
SDT
Simple Desk Calculator:
Implement Simple Desk Calculator for 3*5+4n
SDT
Simple Desk Calculator:
THREE ADDRESS CODE
Three address code is a sequence of statements of the
general form x=y op z.
Where x , y & z are names, constants or compiler generated
temporaries and op stands for operator.

Ex: Expression a + b * c + d
Three address code will be….

t1 = b * c
t2 = a + t1
t3 = t2 + d
TYPES OF THREE ADDRESS CODE
•Assignment statement:
1) x = y op z 2) x = op y 3) x = y
•Unconditional Jump:
goto L
•Conditional Jump:
If x relop y goto L
•Indexed Assignment statement:
x = y[i]
and x[i] = y
•Address and pointer statement:
x = &y
x = *y
TYPES OF THREE ADDRESS CODE
•Param x call p, n for procedure call:
param x1
param x2
-----------
-----------
param xn
call p,n
INTERMEDIATE CODE
GENERATION
In most of compilers while translating source code in
middle is converted into a language that is neither
high level language nor a machine language. Such a
language is known as intermediate code or
intermediate text.
The most of the compiler uses:
Postfix notation,
Syntax tree,
Quadruples and Triples .
IMPLEMETATION OF
THREE ADDRESS STATEMENT
A 3-address statement is an abstract form of
intermediate code. In a compiler these statements
can be implemented as records with fields for the
operators and the operands.
To implement 3-address code following methods are
used
•Quadruples
•Triples
QUADRUPLES
A Quadruple is a record structure with four fields,
which we call op, op1, op2 and result. It is
representation of 3-address code.
Eg: a = b * -c + b * -d
3-addres code: Quadruple Table
S No. OP OP1 OP2 RESULT
T1 = -c
0 Uminus C T1
T2 = b * T1
1 * b T1 T2
T3 = -d
2 Uminus d T3
T4 = b * T3
3 * b T3 T4
T5 = T2 + T4
4 + T2 T4 T5
a = T5 5 = T5 a
QUADRUPLES
A Quadruple is a record structure with four fields,
which we call op, op1, op2 and result. It is
representation of 3-address code.
Eg: a = b * -c + b * -d
3-addres code: Quadruple Table
S No. OP OP1 OP2 RESULT
T1 = -c
0 Uminus C T1
T2 = b * T1
1 * b T1 T2
T3 = -d
2 Uminus d T3
T4 = b * T3
3 * b T3 T4
T5 = T2 + T4
4 + T2 T4 T5
a = T5 5 = T5 a
TRIPLES
To avoid entering temporary name into the symbol table, we
might refer to a temporary value by the position of the
statement that computes it. If we do so, 3-address statement can
be represented by records with only three fields: op, op1, op2
Eg: a = b * -c + b * -d Triples Table
3-addres code: S No. OP OP1 OP2
T1 = -c 0 Uminus C
T2 = b * T1 1 * b (0)
T3 = -d 2 Uminus d
T4 = b * T3 3 * b (2)
T5 = T2 + T4 4 + (1) (3)
a = T5 5 = (4)
TRIPLES
To avoid entering temporary name into the symbol table, we
might refer to a temporary value by the position of the
statement that computes it. If we do so, 3-address statement can
be represented by records with only three fields: op, op1, op2
Eg: a = b * -c + b * -d Triples Table
3-addres code: S No. OP OP1 OP2
T1 = -c 0 Uminus C
T2 = b * T1 1 * b (0)
T3 = -d 2 Uminus d
T4 = b * T3 3 * b (2)
T5 = T2 + T4 4 + (1) (3)
a = T5 5 = (4)
INDIRECT TRIPLES
• The indirect triple representation uses an additional
array to list the pointers to the triples in the desired
order. This is called indirect triples.

Index Index Index OP OP1 OP2


25 0 0 + a b
26 1 1 Uminus (0)
27 2 2 + c d
28 3 3 * (1) (2)
29 4 4 + (0) c
30 5 5 + (3) (4)
31 6 6 = x (5)
Q: Translate the expression
a = -b * (c + d)/e
into Quadruples and Triples representation.
Q: Translate the expression
a = -b * (c + d)/e
into Quadruples and Triples representation.
Sol: 3-address code : T1 = -b
T2 = c + d
T3 = T1 * T2
T4 = T3 / e
a = T4

Quadruple Table Triple Table


S No. OP OP1 OP2 RESULT S No. OP OP1 OP2

0 Uminus b T1 0 Uminus b

1 + c d T2 1 + c d

2 * T1 T2 T3 2 * (0) (1)

3 / T3 e T4 3 / (2) e

4 = T4 a 4 = (3)
Q: Translate the expression
x = -(a+b) * (c + d)+(a+b+c)
into Quadruples and Triples representation.
Q: Translate the expression
x = -(a+b) * (c + d)+(a+b+c)
into Quadruples and Triples representation.
Sol: 3-address code : T1 = a + b T6= T4 + T5
T2 = -T1 x = T6
T3 = c + d
T4 = T2 * T3
T5 = T1 + c
Quadruple Table Triple Table
S No. OP OP1 OP2 RESULT S No. OP OP1 OP2
0 + a b T1 0 + a b
1 Uminus T1 T2 1 Uminus (0)
2 + c d T3 2 + c d
3 * T2 T3 T4 3 * (1) (2)
4 + T1 c T5 4 + (0) c
5 + T4 T5 T6 5 + (3) (4)
6 = T6 x 6 = (5)
THREE ADDRESS STATEMENT FOR
BOOLEAN EXPRESSION
It has two primary purposes:
•used for computing logical values.
•Used as conditional expressions using “if-then-else”
and “while-do”.

Grammar like:
EE OR E E id relop id
E E AND E E True
E NOT E E False
E (E)
relop is for relation operator.
(If-else) case
Q: Translate the expression into Three address code
if a<b then x=y+z else p=q+r

Sol: 3-address code :


1 if a < b goto ( 3 )
2 goto ( 6 )
3 t1 = y + z
4 x = t1
5 goto ( 8 )
6 t2 = q + r
7 p = t2
8 NEXT STATEMENT
(while) case
Q: Translate the expression into Three address code
while a < b do x = y + z

Sol: 3-address code :


1 if a < b goto ( 3 )
2 goto( 6 )
3 t1 = y + z
4 x = t1
5 goto ( 1 )
6 NEXT STATEMENT
(Do-while) case
Q: Translate the expression into Three address code
do x = y + z while a < b

Sol: 3-address code :


1 t1 = y + z
2 x = t1
3 if a < b goto ( 1 )
4 goto ( 5 )
5 NEXT STATEMENT
(for) case
Q: Translate the expression into Three address code
for (i=1 ; i<=20; i++)
x=y+z

Sol: 3-address code :


1 i=1
2 If (i<=20) goto ( 7 )
3 goto( 10 )
4 t1=i+1
5 i=t1
6 goto( 2 )
7 t2=y+z
8 x=t2
9 goto ( 4 )
10 NEXT STATEMENT
(switch) case
Q: Translate the expression into Three address code
switch (i+j)
{case 1: x=y+z
case2: u=v+w
default: p=q+r

}
Sol: 3-address code : 12 t4=q+r
1 t1=i+j 13 p=t4
2 If (t1=1) goto ( 6 ) 14 NEXT STATEMENT
3 goto( 4 )
4 If (t1=2) goto ( 9 )
5 goto( 12 )
6 t2=y+z
7 x=t2
8 goto( 14 )
9 t3=v+w
10 u=t3
11 goto ( 14 )
Q: Translate the expression into Three address code

While ( A < C && B > D )


do
if (A==1)
C=C+1
else
While (A < = D)
do
A=A+3
(switch) case
Q: Translate the expression into Three address code
Solution:
1 If ( A < C ) goto ( 3 )
9 goto ( 1 )

2 goto ( 16 )
10 If ( A < = D ) goto ( 12 )

3 If ( B > D ) goto ( 5 )
11 goto ( 1 )

4 goto ( 16 )
12 T2 = A + 3

5 If ( A == 1 ) goto ( 7 )
13 A = T2

6 goto ( 10 )
14 goto ( 10 )

7 T1= C + 1
15 goto ( 1 )

8 C = T1
16 NEXT STATEMENT
TAC for ARRAY CASE

One Dimensional Array:


Representation of One Dimensional
X[i] Array in TAC
Three parameters required:-
A[10]  T1= i*datatype value
a) Base Address: Address of X[0]
b) Low Point : Lp T2= add(a)
c) Width of each entry: w
T3= T1+T2
So, X[i] can be represented as:
X[i] = Base + (i - Lp) * w
= Base + i*w – Lp*w

Const Var Const

So, the Base can (Base – Lp*w ) and we calculate only (i*w)
TAC for 1D-ARRAY CASE

Example:
8 T4 = i * 4
Int a[10], b[10], i, x;
x=0; 9 T5 = addr(b)
for(i=0; i<10; i++)
10 T6 = T4+T5
x+=a[i]*b[i]
Solution: 11 T7 = T3 * T6
1 x=0
12 T8 = x + T7
2 i=0
13 x = T8
3 If(i<10) goto ( 5 )
14 T9 = i + 1
4 goto ( 17 )
15 i = T9
5 T1 =i * 4
16 goto ( 3 )
6 T2 = addr(a)
17 NEXT STATEMENT
7 T3 = T1+T2
TAC for ARRAY CASE

Two Dimensional Array: Representation of One Dimensional


X[i][j] Array in TAC
T1= n * i
Five parameters required:-
a) Base Address: Address of X[0][0] X[i][j] 
T2= T1 + j
b) Low Point of Row : Lr
c) Low Point of Column : Lc T3= T2 * w
d) High Point of Column : Hc
N=No. of
e) Width of each entry : w Element in T4 = addr(X)
a row
So, X[i][j] can be represented as: T5 = T3+T4
X[i][j] = Base + (i - Lr) * (Hc – Lc + 1) * w + ( j – Lc ) * w
X[i][j] = Base + (i - Lr) * N * w + ( j – Lc ) * w
After Calculation
X[i][j] = ( Base - (Lr*N + Lc ) * w ) + (N*i + j ) * w

Base Address Need Calculate

So, X[i][j] = Base Address + (N*i + j ) * w


TAC for 2D-ARRAY CASE

Example:
8 T8 = i * N
Int X[i,j]= Y[i+j,k]+Z;

Solution: 9 T9 = T8 + j
1 T1=i+j
10 T10 = addr(X)
2 T2=T1 * N
11 T10[T9] = T7
3 T3 = T2 + k
12 Goto (Next statement)
4 T4 = T3 + w

5 T5 = addr(Y)

6 T6 = T4+T5

7 T7 = T6 + Z
Q: Translate the expression into Three address code

C=0
Do
{
if(a<b) then
x++;
else
x--;
C++;
} while(C<5)

You might also like