Unit 4
Unit 4
a) Syntax Tree
b) Postfix Notation
c) Three-Address Code
a) Syntax Tree
Syntax tree is nothing more than condensed form of a parse tree.
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.
To form syntax tree put parentheses in the expression, this way it's easy to
recognize which operand should come first.
Maintains structure of the construct
Suitable for high-level representations
Example
x = (a + b * c) / (a – b * c)
b) Postfix Notation
Example: 1+2/3-4*5
T1=b*c
T2=a+T1
T3=T2+d
-(a x b) + (c + d) – (a + b + c + d)
Solution-
(1) T1 = a x b
(2) T2 = uminus T1
(3) T3 = c + d
(4) T4 = T2 + T3
(5) T5 = a + b
(6) T6 = T3 + T5
(7) T7 = T4 – T6
Solution-
(2) T1 = 0
(4) T1 = 1
(5)
(4) t = 0
(6) t = 1
(7)
a := (-c * b) + (-c * d)
t1 := -c
t2 := b*t1
t3 := -c
t4 := d * t3
t5 := t2 + t4
a := t5
Solution
The commonly used representations for implementing Three Address Code are-
1. Quadruples
2. Triples
3. Indirect Triples
1. Quadruples-
2. Triples-
In triples representation,
References to the instructions are made.
Temporary variables are not used.
3. Indirect Triples-
Solution-
T1 = uminus c
T2 = b x T1
T3 = uminus c
T4 = b x T3
T5 = T2 + T4
a = T5
Quadruple Representation-
(1) uminus c T1
(2) x b T1 T2
(3) uminus c T3
(4) x b T3 T4
(5) + T2 T4 T5
(6) = T5 a
Triple Representation-
(1) uminus c
(2) x b (1)
(3) uminus c
(4) x b (3)
(6) = a (5)
Statement
35 (1)
36 (2)
37 (3)
38 (4)
39 (5)
40 (6)
(1) uminus c
(2) x b (1)
(3) uminus c
(4) x b (3)
(6) = a (5)
Backpatching
Basic Blocks-
Basic block is a set of statements that always executes in a sequence one after the
other
Instructions from intermediate code which are leaders are determined.
Following are the rules used for finding a leader:
Here,
All the statements execute in a sequence one after the other.
Thus, they form a basic block.
Three Address Code for the expression If A<B then 1 else 0 is-
Here,
The statements do not execute in a sequence one after the other.
Thus, they do not form a basic block.
Compute the basic blocks for the given three address statements-
(1) PROD = 0
(2) I = 1
(3) T2 = addr(A) – 4
(4) T4 = addr(B) – 4
(5) T1 = 4 x I
(6) T3 = T2[T1]
(7) T5 = T4[T1]
(8) T6 = T3 x T5
(9) PROD = PROD + T6
(10) I = I + 1
(11) IF I <=20 GOTO (5)
Solution-
We have-
PROD = 0 is a leader since first statement of the code is a leader.
T1 = 4 x I is a leader since target of the conditional goto statement is a leader.
Now, the given code can be partitioned into two basic blocks as-
Flow Graph
Solution-
Applications-
Construction of DAGs-
In a DAG,
Interior nodes always represent the operators.
Exterior nodes (leaf nodes) always represent the names, identifiers or constants.
Rule-02:
While constructing a DAG,
A check is made to find if there exists any node with the same value.
A new node is created only when there does not exist any node with the same
value.
This action helps in detecting the common sub-expressions and avoiding the re-
computation of the same.
Rule-03:
The assignment instructions of the form x:=y are not performed unless they are
necessary.
T1 = a + b
T2 = T1 + c
T3 = T1 x T2
Ex.2
T1:= 4*I0
T2:= a[T1]
T3:= 4*I0
T4:= b[T3]
T5:= T2 * T4
T6:= prod + T5
prod:= T6
T7:= I0 + 1
I0:= T7
if I0 <= 20 goto 1
is shown in Fig- 8.14. The node N for x is created first, but when the node labeled [
] = is created, N is killed. Thus, when the node for z is created, it cannot be
identified with N, and a new node with the same operands a0 and i0 must be
created instead.