0% found this document useful (0 votes)
4 views15 pages

CD Unit-4 (Part-2)

o

Uploaded by

testmg875
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)
4 views15 pages

CD Unit-4 (Part-2)

o

Uploaded by

testmg875
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/ 15

Compiler Design (CD)

Unit – 4 (Part – 2)
Intermediate Code
Generation

Mr. Viral H. Panchal


Computer Engineering
Department
CGPIT, UTU
Topics to be covered
 Looping
• Different intermediate forms
• Different representation of Three Address
code
Different intermediate
forms
Different intermediate forms
 Different forms of intermediate code are:

1. Abstract syntax tree


2. Postfix notation
3. Three address code

CD  Unit 4 (Part – 2) – Intermediate Code


Prof.Viral
Mr. Jay RH.Dhamsaniya
Panchal #3130006 (PS)  Unit 1 – Basic Probability 4
Abstract syntax tree & DAG
 A syntax tree depicts the natural hierarchical structure of a source
program.
 A DAG (Directed Acyclic Graph) gives the same information but in a more
compact way because common sub-expressions are identified.
 Ex: a = b * -c+b*-c
= =

+
a +
a

* * *

b uminus b uminus b uminus

c c Syntax Tree c DAG


CD  Unit 4 (Part – 2) – Intermediate Code
Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 5
Postfix Notation
 Postfix notation is a linearization of a syntax tree.
 In postfix notation the operands occurs first and then operators are
arranged.
 Ex: (A + B) * (C + D)
Postfix notation: A B +
CD+*

 Ex: (A + B) * C
Postfix notation: A B +
C*

Postfix notation: A B *
 Ex: (A * B) + (C * D)
CD*+
CD  Unit 4 (Part – 2) – Intermediate Code
Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 6
Three address code
 Three address code is a sequence of statements of the general form,
a:= b op c
 Where a, b or c are the operands that can be names or constants and op
stands for any operator.
 Example: a = b + c + d
t1=b+c
t2=t1+d
a= t2
 Here t1 and t2 are the temporary names generated by the compiler.
 There are at most three addresses allowed (two for operands and one for
result). Hence, this representation is called three-address code.

CD  Unit 4 (Part – 2) – Intermediate Code


Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 7
Different
Representation of
Three Address Code
Different Representation of Three Address Code
 There are three types of representation used for three address code:
1. Quadruples
2. Triples
3. Indirect triples
 Ex: x= -a*b + -a*b
t1= - a
t2 = t1 * b
t3= - a Three Address
Code
t4 = t3 * b
t5 = t2 + t4
x= t5

CD  Unit 4 (Part – 2) – Intermediate Code


Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 9
Quadruple
 The quadruple is a structure with at the most four fields such as op, arg1,
arg2 and result.
 The op field is used to represent the internal code for operator.
 The arg1 and arg2 represent the two operands.
 And result field is used to store the result of an expression.
Quadruple
No Operat Arg1 Arg Result
x= -a*b + - .(0) or 2
a*b uminus a t1
t1= - a (1) * t1 b t2
t2 = t1 * b (2) uminus a t3
t3= - a (3) * t3 b t4
t4 = t3 * b (4) + t2 t4 t5
t5 = t2 + t4 (5) = t5 x
x= t5
CD  Unit 4 (Part – 2) – Intermediate Code
Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 10
Triple
 To avoid entering temporary names into the symbol table, we might refer
a temporary value by the position of the statement that computes it.
 If we do so, three address statements can be represented by records with
only three fields: op, arg1 and arg2.

Quadruple Triple
No Operat Arg1 Arg Result No Operat Arg Arg
.(0) or 2 .(0) or
uminus 1
a 2
uminus a t1
(1) * t1 b t2 (1) * (0) b
(2) uminus a t3 (2) uminus a
(3) * t3 b t4 (3) * (2) b
(4) + t2 t4 t5 (4) + (1) (3)
(5) = t5 x (5) = x (4)

CD  Unit 4 (Part – 2) – Intermediate Code


Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 11
Indirect Triple
 In the indirect triple representation the listing of triples has been done.
And listing pointers are used instead of using statement.
 This implementation is called indirect triples.

Triple Indirect Triple

No Operat Arg Arg Stateme No Operat Arg Arg


.(0) or .(0) or
uminus 1 2
uminus 1
a 2 (0 nt
(14) a
(1) * (0) b )(1 (15) (1) * (14) b
(2) uminus a )(2 (16) (2) uminus a
)(3 (17) (3) * (16) b
(3) * (2) b
)(4 (18) (4) + (15) (17)
(4) + (1) (3)
)(5 (19) (5) = x (18)
(5) = x (4)
)
CD  Unit 4 (Part – 2) – Intermediate Code
Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 12
Exercise
Write quadruple, triple and indirect triple for following:
1. -(a*b)+(c+d)
2. a*-(b+c)
3. x=(a+b*c)^(d*e)+f*g^h
4. g+a*(b-c)+(x-y)*d

CD  Unit 4 (Part – 2) – Intermediate Code


Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 13
References
Books:
1. Compilers Principles, Techniques and Tools, PEARSON
Education (Second Edition)
Authors: Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman
2. Compiler Design, PEARSON (for Gujarat Technological
University)
Authors: Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman

CD  Unit 4 (Part – 2) – Intermediate Code


Mr.
Prof.
Viral
JayH.RPanchal
Dhamsaniya #3130006 (PS)  Unit 1 – Basic Probability 14
Thank You

You might also like