SlideShare a Scribd company logo
1 of 26
Download to read offline
4. Syntax-Directed Translation Schemes
• Any SDT can be implemented by first building a
parse tree and then performing the actions in a
left-to-right depth-first order.
• Some SDT's can be implemented during parsing,
1
• Some SDT's can be implemented during parsing,
without building a parse tree.
– But, not all SDT's can be implemented during parsing.
• Two important classes of SDD's will be
considered:
– LR-grammar and S-attributed SDD
– LL-grammar and L-attributed SDD
Postfix Translation Schemes
2
SDT's with all actions at the right ends of the
production bodies are called postfix SDT's
Example: Postfix SDT implementing the desk calculator
Since the underlying grammar is LR, and the SDD is
S-attributed, these actions can be correctly performed
along with the reduction steps of the parser
Parser-Stack Implementation of Postfix SDT's
3
Parser stack
Implementing the desk calculator
on a bottom-up parsing stack
SDT's With Actions Inside Productions
• Consider a production: B → X {a} Y
– The action a is done after we have recognized X (if X is
a terminal) or all the terminals derived from X (if X is a
nonterminal)
• Insert marker nonterminals to remove the embedded
4
• Insert marker nonterminals to remove the embedded
action and to change the SDT to a postfix SDT
– Rewrite the product with marker nonterminal M into
B → X M Y
M → ε {a}
• Problems with inserting marker nonterminals
– May introduce conflicts in the parse table
– How to propagate inherited attributes?
Any SDT Can Be Implemented
1. Ignoring the actions, parse the input and produce
a parse tree as a result.
2. Then, examine each interior node N, say one for
production A → α. Add additional children to N
5
production A → α. Add additional children to N
for the actions in α, so the children of N from left
to right have exactly the symbols and actions of
α.
3. Perform a preorder traversal of the tree, and as
soon as a node labeled by an action is visited,
perform that action.
Parse Tree With Actions Embedded
6
3 * 5 + 4
+ * 3 5 4
+ * 3 5 4
7
Eliminating Left Recursion From SDT’s
When the order in which the actions in an SDT is
needed to consider only, the actions are treated as
if they were terminal symbols during transforming
the grammar,
E → E + T {print(‘+’);}
E → T
A → β R
R → α R
R → ε
A → A α
A → β
E → T R
R → + T {print(‘+’);} R
R → ε
8
Eliminating Left Recursion From SDT’s
→ { .a = ( .a, .y) }
A.a = g(g(f(X.x), Y1.y), Y2.y)
Y2
A.a = g(f(X.x), Y1.y)
A ⇒ A Y2
⇒ A Y1 Y2
⇒ X Y1 Y2
A → A Y
A → X
A → A1 Y { A.a = g(A1.a, Y.y) }
A → X { A.a = f(X.x) }
A → X { R.i = f(X.x) } R { A.a = R.s }
R → Y { R1.i = g(R.i, Y.y) } R1 { R.s = R1.s }
R → ε { R.s = R.i }
Y1
X
A.a = f(X.x)
9
Eliminating Left Recursion (Cont.)
X R1.i = f(X.x)
A A → X R
R → Y R
R → ε
A ⇒ X R
⇒ X Y1 R
⇒ X Y1 Y2 R
⇒ X Y1 Y2
R3.i = g(g(f(X.x), Y1.y), Y2.y)
Y2
Y1 R2.i = g(f(X.x), Y1.y)
ε
1. Flow of inherited
attribute values
10
Eliminating Left Recursion (Cont.)
X R1.s = R2.s = g(g(f(X.x), Y1.y), Y2.y)
A.s = R1.s = g(g(f(X.x), Y1.y), Y2.y) A ⇒ X R
⇒ X Y1 R
⇒ X Y1 Y2 R
⇒ X Y1 Y2
R3.s = R3.i = g(g(f(X.x), Y1.y), Y2.y)
Y2
Y1 R2.s = R3.s = g(g(f(X.x), Y1.y), Y2.y)
ε
2. Flow of synthesized
attribute values
SDT's for L-Attributed Definitions
• Assume that the underlying grammar can be parsed
top-down
• The rules for turning an L-attributed SDD into an SDT
are as follows
1. Embed the action that computes the inherited attributes for
11
1. Embed the action that computes the inherited attributes for
a nonterminal A immediately before that occurrence of A
in the body of the production. If several inherited attributes
for A depend on one another in an acyclic fashion, order
the evaluation of attributes so that those needed first are
computed first.
2. Place the actions that compute a synthesized attribute for
the head of a production at the end of the body of that
production.
Example: Typesetting
12
This grammar is ambiguous, but we can still use it to parse
B → B1 B2 | B1 sub B2 | (B1) | text
The input string a sub i sub j b sub k will produce k
i b
a j
Consider the following grammar
This grammar is ambiguous, but we can still use it to parse
bottom-up if we make subscripting and juxtaposition right
associative, with sub taking precedence over juxtaposition.
Constructing larger boxes from smaller ones
SDD for typesetting boxes
13
SDT for typesetting boxes
14
Example: Intermediate Code Generation
15
Consider the following grammar
S → while ( C ) S1
Inherited attributes
S.code
……
……
S1.next C.code
Inherited attributes
S.next
C.true
C.false
synthesized attributes
S.code
C.code
……
if true goto C.true
if false goto C.false
……
……
goto S1.next
C.false
C.true S1.code
Intermediate Code Generation (Cont.)
16
SDD for while-statements
SDT for while-statements
5. Implementing L-Attributed SDD's
Four methods for translation during parsing:
A. Use a recursive-descent parser with one function for each
nonterminal.
– The function for nonterminal A receives the inherited attributes of
A as arguments and returns the synthesized attributes of A.
17
A as arguments and returns the synthesized attributes of A.
B. Generate code on the fly, using a recursive-descent parser.
C. Implement an SDT in conjunction with an LL-parser.
– The attributes are kept on the parsing stack, and the rules fetch the
needed attributes from known locations on the stack.
D. Implement an SDT in conjunction with an LR-parser.
– If the underlying grammar is LL, we can always handle both the
parsing and translation bottom-up.
A. Translation During Recursive-Descent Parsing
18
Example: Implementing while-statement
B. On-The-Fly Intermediate Code Generation
19
Recursive-descent code generation for while-statement
On-The-Fly Code Generation (cont.)
20
SDT for on-the-fly code generation for while statements
Incidentally, we can make the same change to the underlying
SDT: turn the construction of a main attribute into actions
SDT: turn the construction of a main attribute into actions
that emit the elements of that attribute
Figure 5.32: SDT for on-the-fly code generation
for while statements
C. L-Attributed SDD's and LL Parsing
• For an SDT with embedded actions converted from an L-
attributed SDD with an LL-grammar, then the translation
can be performed during LL parsing by extending the
parser stack to hold actions and certain data items needed
for attribute evaluation.
21
for attribute evaluation.
• In addition to records representing terminals and
nonterminals, the parser stack will hold
– Inherited attributes for a nonterminal A are placed inside A
– Action-records to represent actions to be executed are placed
above A
– Synthesize-records to hold the synthesized attributes for a
nonterminals A are placed below A
Example: Implement the SDT of Fig.5.32
22
Just before expanding S
On-the-fly generation
Immediately after expanding S
Example: Implement the SDT of Fig.5.32
(Cont.)
23
After the action above C is performed
On-the-fly generation
Note: L1 = y, L2 = z
D. Bottom-Up Parsing of L-Attributed SDD's
L-attributed SDD on LL grammar can be adapted to compute
the same SDD on the new grammar during an LR parse
1. Start with the SDTwith embedded actions before each
nonterminal to compute its inherited attributes and an action at
the end of the production to compute synthesized attributes.
24
2. Introduce into a distinct marker nonterminal M in place of each
embedded action, and add one production M → ε .
3. Modify the action a if M replaces it in some production
A →α{a}β, and associate with M → ε an action a' that
a) Copies, as inherited attributes of M, any attributes of A or
symbols of α that action a needs.
b) Computes attributes in the same way as a, but makes those
attributes be synthesized attributes of M.
Turn SDT to Operate with LR Parse
25
A → {B.i = f(A.i); } B C
A → M B C
M → ε {M.i = A.i; M.s = f(M.i); }
Example 5.25:
Example 5.26:
S → while ( M C ) N S1 { S.code = label || L1 || C.code || label || L2 || S1.code; }
M → ε { L1 = new(); L2 = new(); C.false = S.next; C.true = L2;}
N → ε { S1.next = L1; }
S → while ( { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; }
C) { S1.next = L1 ; }
S1 { S.code = label || L1 || C.code || label || L2 || S1.code; }
Example 5.26 (Cont.)
26
LR parsing stack after
reduction of ε to M
L1 = new();
L2 = new();
C.true = L2;
C.false = stack[top-3].next;
Code with M→ε
Code with N→ε
S .next = stack[top-3].L1;
reduction of ε to M
Stack just before reduction of the while-production body to S
S1.next = stack[top-3].L1;
Code with reducing
the while body to S.
tempCode = label ||stack[top-4].L1||
stack[top-3].code || label ||
stack[top-4].L2 || stack[top].code;
top = top-6; stack[top].code = tempCode;

More Related Content

Similar to Ch5b.pdf

Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersChris
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptFamiDan
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptJigarThummar1
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptMulugetaGebino
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
compiler design ujjwal matoliya 2nd sem MCA.pptx
compiler design ujjwal matoliya 2nd sem MCA.pptxcompiler design ujjwal matoliya 2nd sem MCA.pptx
compiler design ujjwal matoliya 2nd sem MCA.pptxujjwalmatoliya
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxsrilakshmis17
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docxvenkatapranaykumarGa
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTFutureTechnologies3
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptSatyamVerma61
 

Similar to Ch5b.pdf (20)

Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
compiler design ujjwal matoliya 2nd sem MCA.pptx
compiler design ujjwal matoliya 2nd sem MCA.pptxcompiler design ujjwal matoliya 2nd sem MCA.pptx
compiler design ujjwal matoliya 2nd sem MCA.pptx
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Java 8
Java 8Java 8
Java 8
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 

Recently uploaded

Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptAfnanAhmad53
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
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
 
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 Kuwaitjaanualu31
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsmeharikiros2
 

Recently uploaded (20)

Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
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
 
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
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 

Ch5b.pdf

  • 1. 4. Syntax-Directed Translation Schemes • Any SDT can be implemented by first building a parse tree and then performing the actions in a left-to-right depth-first order. • Some SDT's can be implemented during parsing, 1 • Some SDT's can be implemented during parsing, without building a parse tree. – But, not all SDT's can be implemented during parsing. • Two important classes of SDD's will be considered: – LR-grammar and S-attributed SDD – LL-grammar and L-attributed SDD
  • 2. Postfix Translation Schemes 2 SDT's with all actions at the right ends of the production bodies are called postfix SDT's Example: Postfix SDT implementing the desk calculator Since the underlying grammar is LR, and the SDD is S-attributed, these actions can be correctly performed along with the reduction steps of the parser
  • 3. Parser-Stack Implementation of Postfix SDT's 3 Parser stack Implementing the desk calculator on a bottom-up parsing stack
  • 4. SDT's With Actions Inside Productions • Consider a production: B → X {a} Y – The action a is done after we have recognized X (if X is a terminal) or all the terminals derived from X (if X is a nonterminal) • Insert marker nonterminals to remove the embedded 4 • Insert marker nonterminals to remove the embedded action and to change the SDT to a postfix SDT – Rewrite the product with marker nonterminal M into B → X M Y M → ε {a} • Problems with inserting marker nonterminals – May introduce conflicts in the parse table – How to propagate inherited attributes?
  • 5. Any SDT Can Be Implemented 1. Ignoring the actions, parse the input and produce a parse tree as a result. 2. Then, examine each interior node N, say one for production A → α. Add additional children to N 5 production A → α. Add additional children to N for the actions in α, so the children of N from left to right have exactly the symbols and actions of α. 3. Perform a preorder traversal of the tree, and as soon as a node labeled by an action is visited, perform that action.
  • 6. Parse Tree With Actions Embedded 6 3 * 5 + 4 + * 3 5 4 + * 3 5 4
  • 7. 7 Eliminating Left Recursion From SDT’s When the order in which the actions in an SDT is needed to consider only, the actions are treated as if they were terminal symbols during transforming the grammar, E → E + T {print(‘+’);} E → T A → β R R → α R R → ε A → A α A → β E → T R R → + T {print(‘+’);} R R → ε
  • 8. 8 Eliminating Left Recursion From SDT’s → { .a = ( .a, .y) } A.a = g(g(f(X.x), Y1.y), Y2.y) Y2 A.a = g(f(X.x), Y1.y) A ⇒ A Y2 ⇒ A Y1 Y2 ⇒ X Y1 Y2 A → A Y A → X A → A1 Y { A.a = g(A1.a, Y.y) } A → X { A.a = f(X.x) } A → X { R.i = f(X.x) } R { A.a = R.s } R → Y { R1.i = g(R.i, Y.y) } R1 { R.s = R1.s } R → ε { R.s = R.i } Y1 X A.a = f(X.x)
  • 9. 9 Eliminating Left Recursion (Cont.) X R1.i = f(X.x) A A → X R R → Y R R → ε A ⇒ X R ⇒ X Y1 R ⇒ X Y1 Y2 R ⇒ X Y1 Y2 R3.i = g(g(f(X.x), Y1.y), Y2.y) Y2 Y1 R2.i = g(f(X.x), Y1.y) ε 1. Flow of inherited attribute values
  • 10. 10 Eliminating Left Recursion (Cont.) X R1.s = R2.s = g(g(f(X.x), Y1.y), Y2.y) A.s = R1.s = g(g(f(X.x), Y1.y), Y2.y) A ⇒ X R ⇒ X Y1 R ⇒ X Y1 Y2 R ⇒ X Y1 Y2 R3.s = R3.i = g(g(f(X.x), Y1.y), Y2.y) Y2 Y1 R2.s = R3.s = g(g(f(X.x), Y1.y), Y2.y) ε 2. Flow of synthesized attribute values
  • 11. SDT's for L-Attributed Definitions • Assume that the underlying grammar can be parsed top-down • The rules for turning an L-attributed SDD into an SDT are as follows 1. Embed the action that computes the inherited attributes for 11 1. Embed the action that computes the inherited attributes for a nonterminal A immediately before that occurrence of A in the body of the production. If several inherited attributes for A depend on one another in an acyclic fashion, order the evaluation of attributes so that those needed first are computed first. 2. Place the actions that compute a synthesized attribute for the head of a production at the end of the body of that production.
  • 12. Example: Typesetting 12 This grammar is ambiguous, but we can still use it to parse B → B1 B2 | B1 sub B2 | (B1) | text The input string a sub i sub j b sub k will produce k i b a j Consider the following grammar This grammar is ambiguous, but we can still use it to parse bottom-up if we make subscripting and juxtaposition right associative, with sub taking precedence over juxtaposition. Constructing larger boxes from smaller ones
  • 15. Example: Intermediate Code Generation 15 Consider the following grammar S → while ( C ) S1 Inherited attributes S.code …… …… S1.next C.code Inherited attributes S.next C.true C.false synthesized attributes S.code C.code …… if true goto C.true if false goto C.false …… …… goto S1.next C.false C.true S1.code
  • 16. Intermediate Code Generation (Cont.) 16 SDD for while-statements SDT for while-statements
  • 17. 5. Implementing L-Attributed SDD's Four methods for translation during parsing: A. Use a recursive-descent parser with one function for each nonterminal. – The function for nonterminal A receives the inherited attributes of A as arguments and returns the synthesized attributes of A. 17 A as arguments and returns the synthesized attributes of A. B. Generate code on the fly, using a recursive-descent parser. C. Implement an SDT in conjunction with an LL-parser. – The attributes are kept on the parsing stack, and the rules fetch the needed attributes from known locations on the stack. D. Implement an SDT in conjunction with an LR-parser. – If the underlying grammar is LL, we can always handle both the parsing and translation bottom-up.
  • 18. A. Translation During Recursive-Descent Parsing 18 Example: Implementing while-statement
  • 19. B. On-The-Fly Intermediate Code Generation 19 Recursive-descent code generation for while-statement
  • 20. On-The-Fly Code Generation (cont.) 20 SDT for on-the-fly code generation for while statements Incidentally, we can make the same change to the underlying SDT: turn the construction of a main attribute into actions SDT: turn the construction of a main attribute into actions that emit the elements of that attribute Figure 5.32: SDT for on-the-fly code generation for while statements
  • 21. C. L-Attributed SDD's and LL Parsing • For an SDT with embedded actions converted from an L- attributed SDD with an LL-grammar, then the translation can be performed during LL parsing by extending the parser stack to hold actions and certain data items needed for attribute evaluation. 21 for attribute evaluation. • In addition to records representing terminals and nonterminals, the parser stack will hold – Inherited attributes for a nonterminal A are placed inside A – Action-records to represent actions to be executed are placed above A – Synthesize-records to hold the synthesized attributes for a nonterminals A are placed below A
  • 22. Example: Implement the SDT of Fig.5.32 22 Just before expanding S On-the-fly generation Immediately after expanding S
  • 23. Example: Implement the SDT of Fig.5.32 (Cont.) 23 After the action above C is performed On-the-fly generation Note: L1 = y, L2 = z
  • 24. D. Bottom-Up Parsing of L-Attributed SDD's L-attributed SDD on LL grammar can be adapted to compute the same SDD on the new grammar during an LR parse 1. Start with the SDTwith embedded actions before each nonterminal to compute its inherited attributes and an action at the end of the production to compute synthesized attributes. 24 2. Introduce into a distinct marker nonterminal M in place of each embedded action, and add one production M → ε . 3. Modify the action a if M replaces it in some production A →α{a}β, and associate with M → ε an action a' that a) Copies, as inherited attributes of M, any attributes of A or symbols of α that action a needs. b) Computes attributes in the same way as a, but makes those attributes be synthesized attributes of M.
  • 25. Turn SDT to Operate with LR Parse 25 A → {B.i = f(A.i); } B C A → M B C M → ε {M.i = A.i; M.s = f(M.i); } Example 5.25: Example 5.26: S → while ( M C ) N S1 { S.code = label || L1 || C.code || label || L2 || S1.code; } M → ε { L1 = new(); L2 = new(); C.false = S.next; C.true = L2;} N → ε { S1.next = L1; } S → while ( { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; } C) { S1.next = L1 ; } S1 { S.code = label || L1 || C.code || label || L2 || S1.code; }
  • 26. Example 5.26 (Cont.) 26 LR parsing stack after reduction of ε to M L1 = new(); L2 = new(); C.true = L2; C.false = stack[top-3].next; Code with M→ε Code with N→ε S .next = stack[top-3].L1; reduction of ε to M Stack just before reduction of the while-production body to S S1.next = stack[top-3].L1; Code with reducing the while body to S. tempCode = label ||stack[top-4].L1|| stack[top-3].code || label || stack[top-4].L2 || stack[top].code; top = top-6; stack[top].code = tempCode;