Intermediate Code Generator
Dr.P.Kumaran
AP/CSE
Intermediate Code Generations
An intermediate representation of the final machine language
code is produced. This phase bridges the analysis and synthesis
phases of translation.
Example
temp1:= int to real (60)
temp2:= id3 * temp1
temp3:= id2 + temp2
id1:= temp3.
id1 = id2 + id3 * id4
Phases of Compiler
Semantic Analysis
Position:= initial + rate *60
Example
• Syntax Directed Definition (SDD)
– Production + Semantic Rules
• Syntax Directed Translation (SDT)
– Production + Semantic Action
Semantic Analysis
• Grammar + Semantic Rules= SDD
• SDD for Evaluation of Expression
E  E+T {E.value=E.value+T.value}
E T {E.value=T.value}
T T*F {T.value=T.value*F.value}
T F {T.value=F.value}
F  num {F.value=num.value}
Syntax Directed Definition (SDD)
Example1
• 2+3*4
Syntax Directed Definition (SDD) Cont..
• Example2
Syntax Directed Definition (SDD) Cont..
L  E {L.val=E.val}
E  E1+ T {E.val= E1.val+ T.val}
E  T {E.val=T.val}
T T1*F {T.val= T1.val*F.val}
TF {T.val=F.val}
F(E) {F.val=E.val}
F digit {F.val=digit.lexval}
Syntax Directed Definition (SDD) Cont..
(9+10)
SDD  Attribute Types
• Synthesized Attribute value defined by its
child and itself
• D  TL {L.inh=T.type}
• T int {T.type=‘integer’}
• Treal {T.type=‘real’}
• L L1,id {L1.inh= L.inh }
addtype{id.entry, L.inh}
• L id addtype{id.entry, L.inh}
Syntax Directed Definition (SDD) Cont..
• Example3
• real id1,id2,id3
Syntax Directed Definition (SDD) Cont..
SDD  Attribute Types
• Inherited Attribute value defined by its
parent or siblings or itself
S- Attributed Definitions: synthesized
L- Attributed Definitions: Synthesized and
Inherited
• E E*T {E.val=E.val*T.val}
• E T {E.val=T.val}
• T  F-T {T.val=F.val – T.val}
• T F {T.val=F.val}
• F 2 {F.val=2}
• F 4 {F.val=4}
Syntax Directed Definition (SDD) Cont..
• Example4
• 4-2-4*2
Syntax Directed Definition (SDD) Cont..
Example1
Syntax Directed Translation (SDT)
E  E+T {printf(“+”);} 1
E T {} 2
T T*F {printf(“*”);} 3
T F {} 4
F  num {printf(num.val);} 5
2+3*4 (Infix to Postfix conversion) – TDP top
down and left to right
Syntax Directed Translation (SDT) Cont..
• 2+3*4 (Infix to Postfix conversion) – BUP
Syntax Directed Translation (SDT) Cont..
Example2
Syntax Directed Translation (SDT) Cont..
S xxW {printf(1);}
S  y {printf(2);}
W Sz {printf(3);}
• xxxxyzz
Syntax Directed Translation (SDT) Cont..
• Graphical IR
– Graph
• Control-Flow Graph
• Dependence Graph
• Call Graph
– Tree
• Parse Tree
• Abstract syntax tree
• Directed acyclic graph
• Linear IR
– 3-Address Code
– Stack Machine Code
– Linear Code
• Hybrid IR
Intermediate Code Generator
Graphical IR - Graph
Control-flow graph
Graphical IR- Graph
Dependence Graph
Graphical IR- Graph
Call Graph
Graphical IR - Tree
Parse Tree
Graphical IR - Tree
Abstract Syntax Trees
a ×2+ a× 2 × b
Graphical IR - Tree
Directed Acyclic Graphs
a ×2+ a× 2 × b
ICG- Graphical Representation
• Common Sub Expression Elimination
a= b*-c + b*-c
Linear IR
3 Address Code
Linear IR
Stack Machine Code
3- Address Code
x:=y op z
x,y, and z names, constants, or temporaries
op  Operator (arithmetic or logical )
Example:
x+y*z
t1:=y*z
t2:=x+t1
3- Address Code
a= b*-c + b*-c
• Assignment Statement : x:=y op z
– op  Arithmetic or logical operator
• Assignment Statement : x:= op y
– op Unary Operator
• Copy Statement : x:=y
• Unconditional Jump : goto L
• Conditional Jump : if x relop y goto L
• Procedure call : p(x1,x2,x3,…,xn)
Param x1
Param x2
…………..
Param xn
Call p,n
Type of 3- Address Statements
• In compiler the 3 address code is field of records
 Quadruples
 Triples
 Indirect Triples
3- Address Code
3- Address Code
Indirect Triples
Examples
Example 1
Example 2
Example 3
IMG-DECLARATIONS
• D  TL {L.inh=T.type}
• T int {T.type=‘integer’}
• Treal {T.type=‘real’}
• L L1,id {L1.inh= L.inh }
addtype{id.entry, L.inh}
• L id addtype{id.entry, L.inh}
IMG-Declarations
• real id1,id2,id3
IMG-Declarations
• mktable(previous)
• enter(table,name,type,offset)
• addwidth(table,width)
• enterproc(table,name,newtable)
• lookup(table,name)
IMG-Declarations
IMG-Declarations
IMG-Declarations
IMG-Declarations
IMG-Declarations
Example 4
Example 5
Example 6
Example 7
Example 8
Example 9

Intermediate Code Generation.pptx