Unit-4 Syntax Directed Translation
Unit-4 Syntax Directed Translation
Unit-4 Syntax Directed Translation
Unit-4
Syntax‐Directed
Translation
Syntax Directed Definitions, Construction of
syntax tree, S- attributed and L-attributed SDDs
with example.
ERROR Handling during Syntax analysis
• When the stream of tokens coming from lexical analyzer disobeys the
syntactic (grammatical) rules of a language, syntactic error occurs.
• Handling Syntactic Error:
• Report Errors
• Recover from discovered error
• Aim at not slowing down the processing of the remaining program
ERROR Handling during Syntax analysis
• Error Recovery Strategies:
• Panic Mode Recovery: The parser discards the input symbols one at a time
until one of designated set of synchronizing tokens is found.
• Simple and it does not go into loop
• Adequate when the presence of multiple errors in same statement is rare
• Phrase Level Recovery: The parser performs local correction on remaining
input when the error is discovered.
• The parser replaces the prefix of the remaining input by some string that
allows the parser to carry on its execution
• Drawback: Error correction is difficult when actual error occurred before
the point of detection
ERROR Handling during Syntax analysis
• Error Productions: If we know common errors that can be encountered, we
can augment the grammar for the language with productions that generate
erroneous constructs.
• Use the new grammar (with augmented productions) for the parser.
• In case an error production is used by parser, generate appropriate
diagnostic to indicate the errors /erroneous constructs.
• Global Correction: The aim is to make as few changes as possible while
converting an incorrect input string to a valid string.
• Given an incorrect input x, find a parse tree for a related string w(using the
given grammar) such that the number of changes (insertion/deletion)
required to transform x to w is minimum
• Too costly to implement
SDT(Syntax Directed Translation)
• Semantic Analysis
Parser
Parse
tree
Semanti
c
Analysi
s
SDT
• Types of Semantic action performed by Semantic Analyzer:
• Scope resolution
• Type checking
• Array bound checking
• Flow Control Check
SDT(Syntax Directed Translation)
• Syntax directed translation is a generalization of context free grammar
in which each grammar symbol has an associated set of attributes.
• The attributes can be a number, type, memory location, return type
etc….
• Types of attributes are: X .a
X.a denotes that
node X has an
1. Synthesized attributed attribute called ‘a’
2. inherited attributed .b .c
Y Z
• Grammar + Semantic Rules = SDT Value of X.a is found
using semantic rule
for the X Production
id1 id2 used at this node
SDT(Syntax Directed Translation)
• 𝑆 → 𝑆#𝐴|𝐴 {𝑆. 𝑣𝑎𝑙 = 𝑆. 𝑣𝑎𝑙 ∗ 𝐴. 𝑣𝑎𝑙; 𝑆. 𝑣𝑎𝑙 = 𝐴. 𝑣𝑎𝑙; }
• 𝐴 → 𝐴&𝐵|𝐵 {𝐴. 𝑣𝑎𝑙 = 𝐴. 𝑣𝑎𝑙 + 𝐴. 𝑣𝑎𝑙; 𝐴. 𝑣𝑎𝑙 = 𝐵. 𝑣𝑎𝑙; }
• 𝐵 → 𝑖𝑑 {𝐵. 𝑣𝑎𝑙 = 𝑖𝑑. 𝑣𝑎𝑙; }
• Given String: 5#3&4
SDT(Syntax Directed Translation)
S
• 𝑆 → 𝑆#𝐴|𝐴 {𝑆. 𝑣𝑎𝑙 = 𝑆. 𝑣𝑎𝑙 ∗ 𝐴. 𝑣𝑎𝑙; 𝑆. 𝑣𝑎𝑙 = 𝐴. 𝑣𝑎𝑙; }
• 𝐴 → 𝐴&𝐵|𝐵 {𝐴. 𝑣𝑎𝑙 = 𝐴. 𝑣𝑎𝑙 + 𝐴. 𝑣𝑎𝑙; 𝐴. 𝑣𝑎𝑙 = 𝐵. 𝑣𝑎𝑙; }
• 𝐵 → 𝑖𝑑 {𝐵. 𝑣𝑎𝑙 = 𝑖𝑑. 𝑣𝑎𝑙; } S # A
• Given String: 5#3&4
A A & B
B B id
id id
5 3 4
SDT(Syntax Directed Translation)
• Annotated Parse Tree: A Parse Tree with attribute values at
each node, is annotated parse tree. X .a
• Synthesized Attributes: An Attribute is said to be a
synthesized attribute if its value at a Parent Node is .b .c
determined from the attribute values of the children of the Y Z
node.
• Synthesized Attributes can be evaluated during a single
bottom up traversal id1 id2
• Inherited attribute: An inherited value at a node in a parse
tree is computed from the value of attributes at the parent
and/or siblings of the node.
SDT(Syntax Directed Translation)
• Application:
• Executing Arithmetic Expression
• Conversion from infix to postfix
• Conversion from infix to prefix
• Conversion from binary to decimal
• Counting number of reductions
• Creating Syntax Tree
• Generating Intermediate Code
• Type Checking
• Storing type information into symbol
Arithmetic Expression by SDT
• 𝐸 → 𝐸&𝑇{𝐸. 𝑣𝑎𝑙 = 𝐸. 𝑣𝑎𝑙 ∗ 𝑇. 𝑣𝑎𝑙}
• 𝐸 → 𝑇{𝐸. 𝑣𝑎𝑙 = 𝑇. 𝑣𝑎𝑙}
• 𝑇 → 𝑇@𝐹{𝑇. 𝑣𝑎𝑙 = 𝑇. 𝑣𝑎𝑙 − 𝐹. 𝑣𝑎𝑙}
• 𝑇 → 𝐹{𝑇. 𝑣𝑎𝑙 = 𝐹. 𝑣𝑎𝑙}
• 𝐹 → 𝑛𝑢𝑚{𝐹. 𝑣𝑎𝑙 = 𝑛𝑢𝑚}
• Input String: 4&8@5&7@3
Arithmetic Expression
• 𝐸 → 𝐸1 + 𝑇 𝐸. 𝑣𝑎𝑙 → 𝐸1 . 𝑣𝑎𝑙 + 𝑇. 𝑣𝑎𝑙
•𝐸→𝑇 𝐸. 𝑣𝑎𝑙 → 𝑇. 𝑣𝑎𝑙
• 𝑇 →𝑇∗𝐹 𝑇. 𝑣𝑎𝑙 → 𝑇. 𝑣𝑎𝑙 ∗ 𝐹. 𝑣𝑎𝑙
•𝑇→𝐹 𝑇. 𝑣𝑎𝑙 → 𝐹. 𝑣𝑎𝑙
• 𝐹 → (𝐸) 𝐹. 𝑣𝑎𝑙 → 𝐸. 𝑣𝑎𝑙
𝐹. 𝑣𝑎𝑙 → 𝑑𝑖𝑔𝑖𝑡. 𝑙𝑒𝑥𝑣𝑎𝑙
• 𝐹 → 𝑑𝑖𝑔𝑖𝑡
• Input String:2+9*8
Infix to postfix
• 𝐸 → 𝐸1 + 𝑇 𝐸. 𝑥 → 𝐸1 . 𝑥 𝑇. 𝑥 +
• 𝐸 → 𝐸1 − 𝑇 𝐸. 𝑥 → 𝐸1 . 𝑥 𝑇. 𝑥 −
•𝐸→𝑇 𝐸. 𝑥 → 𝑇. 𝑥
•𝑇→0 𝑇→0
•𝑇→1 𝑇→1
:
:
𝑇→9
•𝑇→9
• Input String:1-2+3
Exercise
SDT
• Abstract Syntax Tree (AST): Each node in an AST represents an
operator and the children of the operator are the operands (of this
operation)
• e.g., 1 +2
• There is no unimportant details present.
SDT
• Concrete Syntax Tree: It is a normal parse tree and underlying
grammar is called concrete syntax for the language.
• e.g., 1 -2 + 3
• Left to right evaluation
L
+
L D
- 3
L D
D
1 2 - + 3
1 2
Traversal of a Tree
• Starts at the root and visit each node of the tree in some particular
order.
• Depth First Traversal: start with root => recursively visit the children
of each node in left to right manner.
X
Y Z
1 2 W A
3 4
SDT
• Translation: Given an input string x, when we construct a parse tree for x
and convert it into annotated parse tree (output), this mapping from input
to output is called translation.
• Translation Scheme: It is a context free grammar in which program
pieces/fragments are embedded in RHS of the productions (called
semantic actions)
• Translation Scheme specifies explicitly the evaluation order of semantic
actions.
• Translation scheme generates an output for each string x present in the
language generated by the grammar by executing the actions in the order
in which they appear in depth first traversal of parse tree for x.
SDT(Syntax Directed Translation)
• 𝑆 → 𝐴𝑆 {𝑝𝑟𝑖𝑛𝑡𝑓(3); }
• 𝐴 → 𝐴𝐵 {𝑝𝑟𝑖𝑛𝑡𝑓(5); }
• 𝐴 → 𝑎 {𝑝𝑟𝑖𝑛𝑡𝑓(1); }
• 𝐵 → 𝑏𝐶 {𝑝𝑟𝑖𝑛𝑡𝑓(6); }
• 𝐵 → 𝑑𝐵 {𝑝𝑟𝑖𝑛𝑡𝑓(4); }
• 𝐶 → 𝑐 {𝑝𝑟𝑖𝑛𝑡𝑓(2); }
• Given String: 𝑎𝑎𝑑𝑏𝑐
Infix to postfix
• 𝐸 → 𝐸1 + 𝑇 : print(“+”);
• 𝐸 → 𝐸1 + 𝑇 𝐸. 𝑥 → 𝐸1 . 𝑥 𝑇. 𝑥 +
• 𝐸 → 𝐸1 − 𝑇 : print(“-”);
• 𝐸 → 𝐸1 − 𝑇 𝐸. 𝑥 → 𝐸1 . 𝑥 𝑇. 𝑥 −
•𝐸→𝑇 : print(“”);
•𝐸→𝑇 𝐸. 𝑥 → 𝑇. 𝑥
•𝑇→0 :print(“0”);
•𝑇→0 𝑇 → 0
𝑇 → 1 •𝑇→1 :print(“1”);
•𝑇→1
: :
:
𝑇→9 •𝑇→9 :print(“9”);
•𝑇→9
• Input String:1-2+3
• Input String:1-2+3
Types of Attributes
• S-Attributed: S-attributed definition is one such class of syntax
directed definition with synthesized attributes only.
• Synthesized attributes can be evaluated using bottom up parser only.
• L-Attributed: A syntax directed definition is L-attributed if each
inherited attribute of 𝑋𝑗 , 1 ≤ 𝑗 ≤ 𝑛,on the right side of 𝐴 →
𝑋1 , 𝑋2 , … , 𝑋𝑛 depends only on:
• 1. The attributes of the symbols 𝑋1 , 𝑋2 , … , 𝑋𝑗−1 to the left of 𝑋𝑗 in the
production and
• 2. The inherited attribute of A.
Types of SDT
• S-Attributed SDT
• Based on synthesized Attribute
• Use bottom up parsing
• Semantic rules always written at right most position in RHS
• L-Attributed SDT:
• Based on both synthesized and inherited attributes
• Top down Parsing
• Semantic rules anywhere in RHS
Questions
Questions
• Answer: 2.
Example
• 𝐴 → 𝐿𝑀 {𝐿. 𝑖 = 𝑙(𝐴. 𝑖); 𝑀. 𝑖 = 𝑚(𝐿. 𝑠); 𝐴. 𝑠 = 𝑓(𝑀. 𝑠)}
• 𝐴 → 𝑄𝑅 {𝑅. 𝑖 = 𝑟(𝐴. 𝑖); 𝑄. 𝑖 = 𝑞(𝑅. 𝑠); 𝐴. 𝑠 = 𝑓(𝑄. 𝑠)}
Example
Example
• Answer: B.
• Answer: A.