Short Notes: Intermediate Code Generation | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


  
Intermediate codes are machine independent codes, but they are close to machine 
instructions. The given program in a source language is converted to an equivalent 
program in an intermediate language by the intermediate code generator.
• The designer of the compiler decides the intermediate language.
• Syntax trees can be used as an intermediate language.
• Postfix notations, three address code (quadruples) can be used as an 
intermediate language.
Syntax Tree
Syntax tree is a variant of the parse tree, where each leaf represents an operand 
and each interior node represent an operator.
P ro d u ctio n Sem an tic R u le
E — s-E l op E2 
E - < E 1 )
E — -E l 
E - ^ i d
E .val = NODE (op: E l.v a l, E 2.val)
E .val = E l.v a l
E .val = U n ary E 1 .val
E .val = (L E A F (id))
A sentence a * (b +d) would have the following syntax tree
/ \
3 / + \
b a
Example of a syntax tree
P ro d u ctio n S em an tic R u le P ro g ra m F ra g m en t
E — » E l o p E 2 E .code = E l. code Print op
||E2.code||op
E — ? ( E l) E .code = E l. code
E -+ id E .code = id Print id
Page 2


  
Intermediate codes are machine independent codes, but they are close to machine 
instructions. The given program in a source language is converted to an equivalent 
program in an intermediate language by the intermediate code generator.
• The designer of the compiler decides the intermediate language.
• Syntax trees can be used as an intermediate language.
• Postfix notations, three address code (quadruples) can be used as an 
intermediate language.
Syntax Tree
Syntax tree is a variant of the parse tree, where each leaf represents an operand 
and each interior node represent an operator.
P ro d u ctio n Sem an tic R u le
E — s-E l op E2 
E - < E 1 )
E — -E l 
E - ^ i d
E .val = NODE (op: E l.v a l, E 2.val)
E .val = E l.v a l
E .val = U n ary E 1 .val
E .val = (L E A F (id))
A sentence a * (b +d) would have the following syntax tree
/ \
3 / + \
b a
Example of a syntax tree
P ro d u ctio n S em an tic R u le P ro g ra m F ra g m en t
E — » E l o p E 2 E .code = E l. code Print op
||E2.code||op
E — ? ( E l) E .code = E l. code
E -+ id E .code = id Print id
Three-Address Code
When each statement contains three addresses (two for operands and one for 
result), Most general kind of three-address code is
x = yop z
Where x, y and z are names, constants or compiler generated temporaries and op 
any operator.
But we can also use the following notation for quadruples (much better notation 
because it looks like a machine code instruction)
Op y, z, x
Apply operator op to y and z and store the result in x.
Representation of Three-Address Codes
Three-address code can be represented in various forms i.e., quadruples, triples 
and indirect triples. These forms are demonstrated by way of examples below.
e.g., A = - B*(C +0)
Three address code is as follow 
T1 = - B 
T2 = C + D 
T3 = T1*T2 
A = T3
Quadruples
O p e ra to r O p e ra n d 1 O p era n d 2 R esu lt
(1)
- B T1
<2)
+ C D T2
(3)
T1 T2 T3
(4) = A T3
Triples
O p e ra to r O p era n d 1 O p era n d 2
(1)
- B
(2)
+ C D
(3) (1) <2)
(4) = A
(3)
Indirect triple
S tatem en t
(0)
(56)
(1)
(57)
(2) (58)
(3) (59)
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

90 docs
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Short Notes: Intermediate Code Generation | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Sample Paper

,

mock tests for examination

,

study material

,

Semester Notes

,

pdf

,

past year papers

,

practice quizzes

,

Extra Questions

,

ppt

,

Viva Questions

,

Exam

,

Short Notes: Intermediate Code Generation | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Objective type Questions

,

Short Notes: Intermediate Code Generation | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Free

,

shortcuts and tricks

,

Summary

,

MCQs

,

Previous Year Questions with Solutions

,

Important questions

;