SlideShare uma empresa Scribd logo
1 de 18
Syntax-Directed Translation into Three
Address Code
 Syntax-directed translation rules can be defined to
generate the three address code while parsing the
input. It may be required to generate temporary names
for interior nodes which are assigned to non-terminal
E on the left side of the production E→E1 op E2. we
associate two attributes place and code associated with
each non-terminal.
 E.place, the name that will hold the value of E.
 E.code, the sequence of three-address statements
evaluating E.
Syntax-Directed Translation into
Three Address Code
 To generate intermediate code for SDD, first searching
is applied to get the information of the identifier from
the symbol table. After searching, the three address
code is generated for the program statement. Function
lookup will search the symbol table for the lexeme and
store it in id.place.
 Function newtemp is defined to return a new
temporary variable when invoked and
 gen function generates the three address statement in
one of the above standard forms depending on the
arguments passed to it.
Three-address code for expressions
Production Semantic rule
S → id = E {id.place = lookup(id.name);
if id.place ≠ null then S.code = E.code | | gen( id.place ":="
E.place) else S.code = type_error}
E → – E1 {E.place = newtemp();
E.code = E1.code | | gen(E.place ":=" "–" E1.place)}
E → E1 + E2 {E.place = newtemp();
E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "+"
E2.place)}
E → E1 – E2 {E.place = newtemp();
E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "–"
E2.place)}
E → E1 * E2 {E.place = newtemp();
E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "*"
E2.place)}
E → E1 / E2 {E.place = newtemp();
E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "/"
E2.place)}
E → id {E.place = lookup(id.name), E.code = " "}
Three-address code for expressions
Example :- Syntax tree for a = –(b – c)
Translation of Boolean Expression
Boolean Expression have 2 primary purpose.
1. Used to computing logical values.
2. Used to computing conditional expression using
if then else or while-do.
E → E1 or E2
E → E1 and E2
E → not E1
E → id1 relop id2
E → (E1)
E → true
E → false
Boolean Expression: Grammar and Actions
Production Semantic Rule
E → E1 or E2
{E.place = newtemp();
gen(E.place "=" E1.place "or" E2.place)}
E → E1 and E2 {E.place = newtemp();
gen(E.place "=" E1.place "and" E2.place)}
E → not E1 {E.place = newtemp();
gen(E.place "=" "not" E1.place)}
E → (E1) {E.place = E1.place}
E → id1 relop id2 {E.place = newtemp();
gen( "if" id1.place relop.op id2.place "goto" nextstat + 3)
gen(E.place "=" "0")
gen("goto" nextstat + 2)
gen(E.place "=" "1")}
E → true {E.place = newtemp();
gen(E.place "=" "1")}
E → false {E.place = newtemp();
gen(E.place "=" "0")}
Boolean Expression: Example
Example1 :-
Given Boolean Expression
if x < y then 1 else 0
The address code for the
above expression is
1. if x < y go to 3
2. t1 = 0
3. go to 4
4. t1 = 1
Production Semantic Rule
E → E1 or E2
{E.place = newtemp();
gen(E.place "=" E1.place "or" E2.place)}
E → E1 and E2 {E.place = newtemp();
gen(E.place "=" E1.place "and" E2.place)}
E → not E1 {E.place = newtemp();
gen(E.place "=" "not" E1.place)}
E → (E1) {E.place = E1.place}
E → id1 relop id2 {E.place = newtemp();
gen( "if" id1.place relop.op id2.place "goto" nextstat + 3)
gen(E.place "=" "0")
gen("goto" nextstat + 2)
gen(E.place "=" "1")}
E → true {E.place = newtemp();
gen(E.place "=" "1")}
E → false {E.place = newtemp();
gen(E.place "=" "0")}
Boolean Expression: Example
Example 2 :-
Given Boolean Expression
x or y and not z
The address code for
the above expression is
t1 = not z
t2 = y and t1
t3 = x or t2
Production Semantic Rule
E → E1 or E2
{E.place = newtemp();
gen(E.place "=" E1.place "or" E2.place)}
E → E1 and E2 {E.place = newtemp();
gen(E.place "=" E1.place "and" E2.place)}
E → not E1 {E.place = newtemp();
gen(E.place "=" "not" E1.place)}
E → (E1) {E.place = E1.place}
E → id1 relop id2 {E.place = newtemp();
gen( "if" id1.place relop.op id2.place "goto" nextstat + 3)
gen(E.place "=" "0")
gen("goto" nextstat + 2)
gen(E.place "=" "1")}
E → true {E.place = newtemp();
gen(E.place "=" "1")}
E → false {E.place = newtemp();
gen(E.place "=" "0")}
Boolean Expression: Example
Example : Give three address code for the
following: while(P < Q)do
if(R < S) then a = b + c;
Solution 1. if P < Q goto (3)
2. goto (8)
3. if R < S goto (5)
4. goto (1)
5. t1: = b + c
6. a: = t1
7. goto (1)
8. Next
Control Statements
Flow of control statements can be shown pictorially as
Control low for if-then-else statement Control low for while statement
Translation of Control Flow Statements
Production Semantic Rule
S → if E then S1 {E.true = newlabel();
E.false = S.next;
S1.next = S.next;
S.code = E.code | | gen(E.true,":") | | S1.code}
S → if E then S1 else
S2
{E.true = newlabel();
E.false = newlabel();
S1next = S.next;
S2.next = S.next;
S.code = E.code | | gen(E.true,":") | | S1.code | | gen("
GOTO ", S.next) | | gen(E.false, " :") | | S2.code}
S → while E do S1 {S.begin = newlabel();
E.true = newlabel();
E.false = S.next;
S1.next = S.next;
S.code = gen(S.begin":") | | E.code | | gen(E.true,":") | |
S1..code | | gen("GOTO",S.begin)}
Control Flow Statements
Example : Give three address code for the following:
While (a < 5) do a: = b + 2
Solution:-
L1:
If a < 5 goto L2
goto last
L2:
t1 = b + 2
a = t1
goto L1
last:
Control Flow Statements
Example : Give three address code for
the following:
while a<b
do
if c < d then
x = y + z
else
x = y - z
done
Solution:-
L1. if a<b then GOTO L2
GOTO LNEXT
L2. if c<d then GOTO L3
GOTO L4
L3. tl = y + z
x = tl
GOTO L1
L4. tl= y - z
x = tl
GOTO L1
LNEXT.
Backpatching
Back patching is a technique to solve the problem
of replacing symbolic names into the goto
statements by the actual target addresses. Some
languages do not permit to use symbolic names in
the branches, for this we maintain a list of
branches that have the same target labels and then
replace them once they are defined. To manipulate
the lists of jumps.
Backpatching
To perform backpatching the following three functions are
used:
 makelist(i): This function creates a new list containing an
index i into the array of statements and then returns a
pointer pointing to the newly created list.
 merge(p1, p2): This function concatenates the two lists
pointed to by the pointers p1 and p2 respectively, and then
returns a pointer to the concatenated list.
 backpatch(p, i): This function inserts i as the target labels
for each of the statements on the list pointed by p.
Backpatching for Boolean Expressions


Backpatching for Boolean Expressions
 Annotated parse tree for x < 100 || x > 200 && x ! = y
100 if x < 100 then 106
101 goto 102
102 if x>200 then 104
103 goto 107
104 if x ! = y then 106
105 goto 107
106 true
107 false

Mais conteúdo relacionado

Mais procurados

Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
Sravanthi Emani
 

Mais procurados (20)

Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
 
Unification and Lifting
Unification and LiftingUnification and Lifting
Unification and Lifting
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
First order predicate logic(fopl)
First order predicate logic(fopl)First order predicate logic(fopl)
First order predicate logic(fopl)
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Signed Addition And Subtraction
Signed Addition And SubtractionSigned Addition And Subtraction
Signed Addition And Subtraction
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Three Address code
Three Address code Three Address code
Three Address code
 
Partial redundancy elimination
Partial redundancy eliminationPartial redundancy elimination
Partial redundancy elimination
 
Address in the target code in Compiler Construction
Address in the target code in Compiler ConstructionAddress in the target code in Compiler Construction
Address in the target code in Compiler Construction
 

Semelhante a Syntax-Directed Translation into Three Address Code

14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
bolovv
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
hccit
 

Semelhante a Syntax-Directed Translation into Three Address Code (20)

14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Ch8a
Ch8aCh8a
Ch8a
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Open course(programming languages) 20150225
Open course(programming languages) 20150225Open course(programming languages) 20150225
Open course(programming languages) 20150225
 
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
 
Ch8b
Ch8bCh8b
Ch8b
 
Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Lesson 11: Functions and Function Notation
Lesson 11: Functions and Function NotationLesson 11: Functions and Function Notation
Lesson 11: Functions and Function Notation
 
Ch3
Ch3Ch3
Ch3
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 

Último

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Último (20)

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 

Syntax-Directed Translation into Three Address Code

  • 1.
  • 2. Syntax-Directed Translation into Three Address Code  Syntax-directed translation rules can be defined to generate the three address code while parsing the input. It may be required to generate temporary names for interior nodes which are assigned to non-terminal E on the left side of the production E→E1 op E2. we associate two attributes place and code associated with each non-terminal.  E.place, the name that will hold the value of E.  E.code, the sequence of three-address statements evaluating E.
  • 3. Syntax-Directed Translation into Three Address Code  To generate intermediate code for SDD, first searching is applied to get the information of the identifier from the symbol table. After searching, the three address code is generated for the program statement. Function lookup will search the symbol table for the lexeme and store it in id.place.  Function newtemp is defined to return a new temporary variable when invoked and  gen function generates the three address statement in one of the above standard forms depending on the arguments passed to it.
  • 4. Three-address code for expressions Production Semantic rule S → id = E {id.place = lookup(id.name); if id.place ≠ null then S.code = E.code | | gen( id.place ":=" E.place) else S.code = type_error} E → – E1 {E.place = newtemp(); E.code = E1.code | | gen(E.place ":=" "–" E1.place)} E → E1 + E2 {E.place = newtemp(); E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "+" E2.place)} E → E1 – E2 {E.place = newtemp(); E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "–" E2.place)} E → E1 * E2 {E.place = newtemp(); E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "*" E2.place)} E → E1 / E2 {E.place = newtemp(); E.code = E1.code | | E2.code | | gen(E.place ":=" E1.place "/" E2.place)} E → id {E.place = lookup(id.name), E.code = " "}
  • 5. Three-address code for expressions Example :- Syntax tree for a = –(b – c)
  • 6. Translation of Boolean Expression Boolean Expression have 2 primary purpose. 1. Used to computing logical values. 2. Used to computing conditional expression using if then else or while-do. E → E1 or E2 E → E1 and E2 E → not E1 E → id1 relop id2 E → (E1) E → true E → false
  • 7. Boolean Expression: Grammar and Actions Production Semantic Rule E → E1 or E2 {E.place = newtemp(); gen(E.place "=" E1.place "or" E2.place)} E → E1 and E2 {E.place = newtemp(); gen(E.place "=" E1.place "and" E2.place)} E → not E1 {E.place = newtemp(); gen(E.place "=" "not" E1.place)} E → (E1) {E.place = E1.place} E → id1 relop id2 {E.place = newtemp(); gen( "if" id1.place relop.op id2.place "goto" nextstat + 3) gen(E.place "=" "0") gen("goto" nextstat + 2) gen(E.place "=" "1")} E → true {E.place = newtemp(); gen(E.place "=" "1")} E → false {E.place = newtemp(); gen(E.place "=" "0")}
  • 8. Boolean Expression: Example Example1 :- Given Boolean Expression if x < y then 1 else 0 The address code for the above expression is 1. if x < y go to 3 2. t1 = 0 3. go to 4 4. t1 = 1 Production Semantic Rule E → E1 or E2 {E.place = newtemp(); gen(E.place "=" E1.place "or" E2.place)} E → E1 and E2 {E.place = newtemp(); gen(E.place "=" E1.place "and" E2.place)} E → not E1 {E.place = newtemp(); gen(E.place "=" "not" E1.place)} E → (E1) {E.place = E1.place} E → id1 relop id2 {E.place = newtemp(); gen( "if" id1.place relop.op id2.place "goto" nextstat + 3) gen(E.place "=" "0") gen("goto" nextstat + 2) gen(E.place "=" "1")} E → true {E.place = newtemp(); gen(E.place "=" "1")} E → false {E.place = newtemp(); gen(E.place "=" "0")}
  • 9. Boolean Expression: Example Example 2 :- Given Boolean Expression x or y and not z The address code for the above expression is t1 = not z t2 = y and t1 t3 = x or t2 Production Semantic Rule E → E1 or E2 {E.place = newtemp(); gen(E.place "=" E1.place "or" E2.place)} E → E1 and E2 {E.place = newtemp(); gen(E.place "=" E1.place "and" E2.place)} E → not E1 {E.place = newtemp(); gen(E.place "=" "not" E1.place)} E → (E1) {E.place = E1.place} E → id1 relop id2 {E.place = newtemp(); gen( "if" id1.place relop.op id2.place "goto" nextstat + 3) gen(E.place "=" "0") gen("goto" nextstat + 2) gen(E.place "=" "1")} E → true {E.place = newtemp(); gen(E.place "=" "1")} E → false {E.place = newtemp(); gen(E.place "=" "0")}
  • 10. Boolean Expression: Example Example : Give three address code for the following: while(P < Q)do if(R < S) then a = b + c; Solution 1. if P < Q goto (3) 2. goto (8) 3. if R < S goto (5) 4. goto (1) 5. t1: = b + c 6. a: = t1 7. goto (1) 8. Next
  • 11. Control Statements Flow of control statements can be shown pictorially as Control low for if-then-else statement Control low for while statement
  • 12. Translation of Control Flow Statements Production Semantic Rule S → if E then S1 {E.true = newlabel(); E.false = S.next; S1.next = S.next; S.code = E.code | | gen(E.true,":") | | S1.code} S → if E then S1 else S2 {E.true = newlabel(); E.false = newlabel(); S1next = S.next; S2.next = S.next; S.code = E.code | | gen(E.true,":") | | S1.code | | gen(" GOTO ", S.next) | | gen(E.false, " :") | | S2.code} S → while E do S1 {S.begin = newlabel(); E.true = newlabel(); E.false = S.next; S1.next = S.next; S.code = gen(S.begin":") | | E.code | | gen(E.true,":") | | S1..code | | gen("GOTO",S.begin)}
  • 13. Control Flow Statements Example : Give three address code for the following: While (a < 5) do a: = b + 2 Solution:- L1: If a < 5 goto L2 goto last L2: t1 = b + 2 a = t1 goto L1 last:
  • 14. Control Flow Statements Example : Give three address code for the following: while a<b do if c < d then x = y + z else x = y - z done Solution:- L1. if a<b then GOTO L2 GOTO LNEXT L2. if c<d then GOTO L3 GOTO L4 L3. tl = y + z x = tl GOTO L1 L4. tl= y - z x = tl GOTO L1 LNEXT.
  • 15. Backpatching Back patching is a technique to solve the problem of replacing symbolic names into the goto statements by the actual target addresses. Some languages do not permit to use symbolic names in the branches, for this we maintain a list of branches that have the same target labels and then replace them once they are defined. To manipulate the lists of jumps.
  • 16. Backpatching To perform backpatching the following three functions are used:  makelist(i): This function creates a new list containing an index i into the array of statements and then returns a pointer pointing to the newly created list.  merge(p1, p2): This function concatenates the two lists pointed to by the pointers p1 and p2 respectively, and then returns a pointer to the concatenated list.  backpatch(p, i): This function inserts i as the target labels for each of the statements on the list pointed by p.
  • 17. Backpatching for Boolean Expressions  
  • 18. Backpatching for Boolean Expressions  Annotated parse tree for x < 100 || x > 200 && x ! = y 100 if x < 100 then 106 101 goto 102 102 if x>200 then 104 103 goto 107 104 if x ! = y then 106 105 goto 107 106 true 107 false