0% found this document useful (0 votes)
11 views31 pages

Session 16

Uploaded by

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

Session 16

Uploaded by

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

COMPILER DESIGN

COURSE CODE:21CS3204R

CO3: TOPIC: INTERMEDIATE CODE GENERATION


Intermediate forms of source programs/Intermediate code can be represented as:
1. Abstract syntax tree
2. Polish notation
3. Three address code

2
1.Abstract syntax tree(AST) OR Syntax Tree:
 In AST each internal node represents an operator, leaf node represents an operand.
Ex: a*(b+c)/d

3
Example: x =(a + b * c) / (a – b * c)

4
CONSTRUCTING ABSTRACT SYNTAX TREE FOR EXPRESSIONS:
1. Mknode(op,left,right)
2. Mkleaf(id,entry-id)
3. Mkleaf(num,value)
Ex: Construct syntax tree for x*y-5+z

5
Symbol operation
x p1=mkleaf(id, entry-x)
Y p2=mkleaf(id,entry-y)
* p3=mknode(*,p1,p2)
5 p4=mkleaf(num,5)
- p5=mknode(-,p3,p4)
z p6=mkleaf(id,entry-z)
+ p7=mknode(+,p5,p6)

6
SDD (Syntax Directed Definition) for x*y-5+z
Production Semantic Rule
E→E1+T E.node=mknode(+,E1.node,T.node)
E→E1-T E.node=mknode(-,E1.node,T.node)
E→E1*T E.node=mknode(*,E1.node,T.node)
E→T E.node=T.node
T→id T.node=mkleaf(id,entry-id)
T→num T.node=mkleaf(num,num.val)

7
Construction of AST or ST

8
CONSTRUCTING ABSTRACT SYNTAX TREE FOR EXPRESSIONS:

• operators : one field for operator, remaining fields ptrs to operands mknode( op,left,right )

• .identifier : one field with label id and another ptr to symbol table mkleaf(id, id.entry)

• .number : one field with label num and another to keep the value of the number mkleaf(num,val)

Example: construct the AST for the expression w= a- 4 + c

Symbol Operation

a P 1 = mkleaf(id, entry.a)

4 P 2 = mkleaf(num, 4)

- P 3 = mknode(-, P 1 , P 2 )

c P 4 = mkleaf(id, entry.c)

+ P 5 = mknode(+, P 3 , P 4 )
9
SDD For a-4+c
Production Semantic Rule
E→E1+T E.node=mknode(+,E1.node,T.node)
E→E1-T E.node=mknode(-,E1.node,T.node)
E→T E.node=T.node
T→id T.node=mkleaf(id, entry-id)
T→num T.node=mkleaf(num,num.val)

10
• AST For a-4+c

11
2. Polish Notation: The way of writing an operator and an operand.
 Infix ex: a+b
 Prefix ex: +ab
 Postfix ex: ab+

12
Convert infix expression into postfix expression using stacks:
Algorithms:
1. Read infix expression from left to right.
2. If the symbol is left parenthesis ‘(‘ then push it onto the stack.
3. If the symbol is operand then add it to the postfix expression.
4. If the symbol is an operator then check the stack is empty or not.
a. if the stack is empty push the operator onto the stack.
b. if the stack is not empty , then check the priority of the operators.
i). If reading operator has higher priority than priority of the stack’s top most
operator then push it onto the stack.
13
ii). If the priority of reading operator less than or equal to the priority of operator present
at top of the stack then pop the operator and add it to the postfix expression.
5. If the symbol is right parenthesis ‘)’ then pop all the symbols form the stack and place
them in postfix expression untill we get ‘(‘ which is mapped but should not placed in
postfix expression.
6.After completion of reading infix expression , if stack is not empty then pop all the
symbols form the stack and add to postfix expression.

14
Ex:1 convert the infix expression a+b*c+(d*e+f)*g into postfix expression using stack
implementation.

15
Ex 2: convert the infix expression A+B*C/(E-F) into postfix expression using stack
implementation.

16
Convert infix expression into prefix expression using stacks:
Algorithm:
1. Read infix expression from left to right.
2. Reverse the infix expression.
3. If the symbol is left parenthesis ‘(‘ then push it onto the stack.
4. If the symbol is operand then add it to the postfix expression.
5. If the symbol is an operator then check the stack is empty or not.
a. if the stack is empty push the operator onto the stack.
b. if the stack is not empty , then check the priority of the operators.
i). If reading operator has higher priority than priority of the stack’s top most
operator then push it onto the stack.

17
ii). If the priority of reading operator less than or equal to the priority of operator present
at top of the stack then pop the operator and add it to the postfix expression.
5. If the symbol is right parenthesis ‘)’ then pop all the symbols form the stack and place
them in postfix expression untill we get ‘(‘ which is mapped but should not placed in
postfix expression.
6.After completion of reading infix expression , if stack is not empty then pop all the
symbols form the stack and add to postfix expression.
7. Reverse the final output.

18
Ex1: convert the infix expression (A+B^C)*D+E^5 into prefix expression using stack
implementation.
 Reverse the expression then 5^E+D*(C^B+A)
Input stack output
5 …. 5
^ ^ 5
E …. 5E
+ + 5E^
D + 5E^D

19
Input stack output
* +,* 5E^D
( +,( 5E^D*
C ( 5E^DC*+
^ (,^ 5E^DC*+
B (,^ 5E^DC*+B^
+ (,+ 5E^DC*+B^
A (,+ 5E^DC*+B^A
) ….. 5E^DC*+B^A+ Reverse the o/p

20
3.Three –Address Code Notation:
Rules:
1. Each instruction should contain at most 3-addresses.
2. At most one operator on RHS.
Ex1: a+b*c
t1=b*c
a+t1
Ex2: a=b*-c+b*-c

21
Three-Address code can be Represented in three ways
1. Quadruple
2. Triple
3. Indirect Triple

22
1. Quadruple:
In Quadruple each instruction should contain 4-fields
1.op
2. arg1
3.arg2
4.Result

23
Ex: a=b*-c+b*-c
Sol:
t1=-c op arg1 arg2 Result
(0) - c t1
t2=b*t1 (1) * b t1 t2
t3=-c (2) - c t3
(3) * b t3 t4
t4=b*t3
(4) + t2 t4 t5
t5=t2+t4

24
Advantage –
• Easy to rearrange code for global optimization.
• One can quickly access value of temporary variables using symbol table.
Disadvantage –
• Contain lot of temporaries.
• All temporary variables are stored in symbol table so, it requires more memory.
• Temporary variable creation increases time and space complexity.

25
2. Triple
 Each instruction should contain 3-fileds
1.op
2.arg1
3.arg 2

26
a=b*-c+b*-c
t1=-c op arg1 arg2
(0) - c
t2=b*t1
(1) * b (0)
t3=-c (2) - c
t4=b*t3 (3) * b (2)
(4) + (1) (3)
t=t2+t4

27
Advantages
• In Triple there is less no.of temporary variables are used.
• It requires less amount of memory.
• It reduces time complexity and space complexity.

28
3. Indirect Triple
 In indirect triple we have pointers that is known as indirect triple.
 In indirect triple we have pointers those pointers are point to Triples and Triples
information is available in Triple Table.
100 (0)
101 (1)
102 (2)
103 (3)
104 (4)

29
Advantages:
• There is no need of temporary variables.
• Based on the address we can access the data.

EX2: Construct Quadruples, Triples, and Indirect Triples for the expression
-(a + b) * (c + d) - (a + b + c)

30
Thank you

31

You might also like