SlideShare a Scribd company logo
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

More Related Content

What's hot

Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
swapnac12
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
FellowBuddy.com
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
vikas dhakane
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
vikas dhakane
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
Radhakrishnan Chinnusamy
 
ELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOLELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOL
Shashank Rustagi
 
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
Muhammad Haroon
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
sanchi29
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
SOMNATHMORE2
 
And or graph
And or graphAnd or graph
And or graph
Ali A Jalil
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
RASHIARORA8
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theory
Sampath Kumar S
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
indhu mathi
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 

What's hot (20)

Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
ELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOLELEMENTS OF TRANSPORT PROTOCOL
ELEMENTS OF TRANSPORT PROTOCOL
 
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
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
And or graph
And or graphAnd or graph
And or graph
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
1.1. the central concepts of automata theory
1.1. the central concepts of automata theory1.1. the central concepts of automata theory
1.1. the central concepts of automata theory
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

Similar to 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
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
SHUJEHASSAN
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
Divya Devan
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
Divya Devan
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
Sumathi Gnanasekaran
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
KamranAli649587
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
Iffat Anjum
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
Radhakrishnan Chinnusamy
 
Open course(programming languages) 20150225
Open course(programming languages) 20150225Open course(programming languages) 20150225
Open course(programming languages) 20150225
JangChulho
 
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)
codin9cafe
 
Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
GauravChaudhary531452
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Lesson 11: Functions and Function Notation
Lesson 11: Functions and Function NotationLesson 11: Functions and Function Notation
Lesson 11: Functions and Function Notation
Kevin Johnson
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 

Similar to 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
 
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
 

Recently uploaded

Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 

Recently uploaded (20)

Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 

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