Compiler Design Unit 3
Compiler Design Unit 3
Parse tree
Semantic analysis
Dependency graph
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.
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.
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:
EE 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 : 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
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
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
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)