Intermediate Code
Generation
Construct the DAG for the expression
((x+y)-((x+y)*(x-y)))+((x+y)*(x-y))
Construct the DAG for the value number for the subexpression
of the following expression, assuming + associates from the
left.
(a+b)x(a+b+c) (((a+a)+(a+a))+((a+a)+(a+a)))
((a+b)-((a+b)*(a-b)))+((a+b)*(a-b))
Translate the following arithmetic expression into:
1.A Syntax tree
[Link]
[Link]
[Link] triples
Write Three Address Code for the following expression-
a=b+c+d
• Three Address Code for the given expression is-
(1) T1 = b + c
(2) T2 = T1 + d
(3) a = T2
• Write Three Address Code for the following expression-
If A < B then 1 else 0
• Three Address Code for the given expression is-
(1) If (A < B) goto (4)
(2) T1 = 0
(3) goto (5)
(4) T1 = 1
(5)
• Write Three Address Code for the following expression-
If A < B and C < D then t = 1 else t = 0
• Three Address Code for the given expression is-
(1) If (A < B) goto (3)
(2) goto (4)
(3) If (C < D) goto (6)
(4) t = 0
(5) goto (7)
(6) t = 1
(7)
Example
• Consider the Statement
• a = b + (c * d)
• First, convert this statement into Three Address code
∴ Three Address code will be
t1 = c ∗ d
t2 = b + t1
a = t2
• After construction of the Three Address code, it will be changed to Quadruple
representation as follows
• Quadruple
Location Operator Arg1 Arg2 Result
(0) * c d t1
(1) + b t1 t2
(2) = t2 a
t1 = c ∗ d
• Triple t2 = b + t1
Location Operator Arg1 Arg2 a = t2
(0) * c d
(1) + b (0)
(2) = a (1)
Example
• Translate the following expression to quadruple, triple and indirect triple-
a + ((b x c) / (e ↑ f)) + b x a
• Three Address Code for the given expression is-
T1 = e ↑ f
T2 = b x c
T3 = T2 / T1
T4 = b x a
T5 = a + T3
T6 = T5 + T4
Quadruple Representation-
Triple Representation
Indirect Triple Representation
Three Address code for Pseudocode
switch (ch) 1. if ch = 1 goto 3
{ 2. if ch = 2 goto 6
case 1 : c = a + b; 3. T1 = a + b
break; 4. c = T1
case 2 : c = a – b; 5. goto 9
6. T1 = a – b
break;
7. c = T2
}
8. goto 9
9.
1. prod = 0
prod = 0 ;
2. i = 1
i=1;
do 3. T1 = 4 x i
{ 4. T2 = a[T1]
prod = prod + a[ i ] x b[ i ] ; 5. T3 = 4 x i
i=i+1;
6. T4 = b[T3]
} while (i <= 10) ;
7. T5 = T2 x T4
8. T6 = T5 + prod
9. prod = T6
10. T7 = i + 1
11. i = T7
12. if (i <= 10) goto (3)
Types, Declarations and
Procedures
, Procedure
Assignment Statement
• In SDT, the assignment statement is
mainly deal with expressions.
• The expression can be of type real,
integer, real, etc.,
Backpatching
• Backpatching is the process of filling up gaps in incomplete
transformations and information
• For nonterminal B we use two attributes [Link] and [Link]
together with following functions:
• makelist(i): create a new list containing only I, an index into the array of
instructions
• Merge(p1,p2): concatenates the lists pointed by p1 and p2 and returns a
pointer to the concatenated list
• Backpatch(p,i): inserts i as the target label for each of the instruction on the
list pointed to by p
Backpatching for Boolean
Expressions
•
•
Backpatching for Boolean Expressions
• Annotated parse tree for x < 100 || x > 200 && x ! = y
Flow-of-Control
Statements