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

More Related Content

What's hot

Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Uninformed search /Blind search in AI
Uninformed search /Blind search in AIUninformed search /Blind search in AI
Uninformed search /Blind search in AIKirti Verma
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and SearchHitesh Mohapatra
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assemblerBansari Shah
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit IIManoj Patil
 
Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AIAmey Kerkar
 
Water jug problem ai part 6
Water jug problem ai part 6Water jug problem ai part 6
Water jug problem ai part 6Kirti Verma
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languagesSOMNATHMORE2
 
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
 

What's hot (20)

Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Code generation
Code generationCode generation
Code generation
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
Uninformed search /Blind search in AI
Uninformed search /Blind search in AIUninformed search /Blind search in AI
Uninformed search /Blind search in AI
 
Ontology engineering
Ontology engineering Ontology engineering
Ontology engineering
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
And or graph
And or graphAnd or graph
And or graph
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
 
Assemblers
AssemblersAssemblers
Assemblers
 
Water jug problem ai part 6
Water jug problem ai part 6Water jug problem ai part 6
Water jug problem ai part 6
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
 
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.pdfSHUJEHASSAN
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Open course(programming languages) 20150225
Open course(programming languages) 20150225Open course(programming languages) 20150225
Open course(programming languages) 20150225JangChulho
 
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
 
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 NotationKevin 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

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
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 torqueBhangaleSonal
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 

Recently uploaded (20)

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
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
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 

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