Lecture#6 - Chap#2 (Syntax Directed Translator (Part-II) )
Lecture#6 - Chap#2 (Syntax Directed Translator (Part-II) )
Compiler Construction
A Simple Syntax-Directed Translator,
(Part-II)
by Abdul Rehman
Topics
• Syntax-Directed Translation (Postfix Notation, Synthesized Attributes, Inherited Attributes
• Translation, Infix to postfix Translation, Attributed Grammars, Depth-First Traversals, SDD
• Translation Schemes, SDT Questions/Tasks)
Overview
• This chapter contains introductory material
• Building a simple compiler
– Syntax Definition
– Syntax-Directed Translation
– Parsing
– A Translator for Simple Expressions
– The Lexical Analyzer
2
Syntax Directed Translation (SDT)
Grammar + Semantic Rule = SDT
Informal Notations
3
Syntax Directed Translation (SDT)
Grammar + Semantic Rule = SDT
Informal Notations
4
Syntax Directed Translation (SDT)
SDT uses a CFG to specify the syntactic structure of the language &
associates a set of attributes with (non)terminals.
Further, with each production, associates a set of semantic rules for
computing values for the attributes.
The attributes contain the translated form of the input after the
computations are completed.
In SDT, every non-terminal can get 0 or more attributes.
In semantic rule, attribute can be a value (string, memory location,
number, etc.)
5
Syntax for Statements
stmt id:= expr
|if expr then stmt
|if expr then stmt else stmt
|while expr do stmt
|begin opt_stmts end
Ambiguous Grammar?
6
Syntax-Directed Translation
• Associate Attributes With Grammar Rules and Translate as
Parsing occurs
• The translation will follow the parse tree structure (and as a
result the structure and form of the parse tree will affect the
translation).
• First example: Inductive Translation.
• Infix to Postfix Notation Translation for Expressions
– Translation defined inductively as: Postfix(E) where E is an
Expression.
Rules
1. If E is a variable or constant then Postfix(E) = E
2. If E is E1 op E2 then Postfix(E)
= Postfix(E1 op E2) = Postfix(E1) Postfix(E2) op
3. If E is (E1) then Postfix(E) = Postfix(E1)
7
Examples
Postfix( ( 9 – 5 ) + 2 )
= Postfix( ( 9 – 5 ) ) Postfix( 2 ) +
= Postfix( 9 – 5 ) Postfix( 2 ) +
= Postfix( 9 ) Postfix( 5 ) - Postfix( 2 ) +
=95–2+
Postfix(9 – ( 5 + 2 ) )
= Postfix( 9 ) Postfix( ( 5 + 2 ) ) -
= Postfix( 9 ) Postfix( 5 + 2 ) –
= Postfix( 9 ) Postfix( 5 ) Postfix( 2 ) + –
=952+–
8
Synthesized and Inherited Attributes
• An attribute is said to be …
– synthesized if its value at a parse-tree node is
determined from the attribute values at the
children of the node
– inherited if its value at a parse-tree node is
determined by the parent (by enforcing the
parent’s semantic rules) & its siblings
Synthesized
Inherited
9
Attributed Grammar
(SDD for infix to postfix translation)
Syntax Directed Definition
Note:
Synthesized attributes are evaluated after visiting and
Inherited attributes are evaluated at first occurrence
Depth-First Traversals (Example)
Semantic Rule
expr.t := expr1.t // term.t // “+” expr.t = 95-2+
expr.t := expr1.t // term.t // “-”
expr.t := term.t
term.t := “0” expr.t = 95- term.t = 2
term.t := “1”
… expr.t = 9 term.t = 5
term.t := “9”
term.t = 9
9 - 5 + 2
Embedded
semantic action
rest
expr { print(“+”) }
expr + term { print(“2”) }
{ print(“-”)}
- term 2
expr
{ print(“5”) }
term 5
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+
term
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+
NOTE: Construct annotated parse trees for the inputs 9-5+2 and 9-5*2 by yourself
SDT Question-2
• Construct a syntax-directed translation scheme that
translates arithmetic expressions from postfix
notation into infix notation. Give annotated parse
trees for the inputs 95-2 and 952-.
SDT Solution-2
• Construct a syntax-directed translation scheme that
translates arithmetic expressions from postfix
notation into infix notation. Give annotated parse
trees for the inputs 95-2 and 952-.
Productions Translation Schemes
expr -> expr expr + expr -> expr {print("+")} expr +
| expr expr – | expr {print("-")} expr –
| expr expr * | {print("(")} expr {print(")*(")} expr {print(")")} *
| expr expr / | {print("(")} expr {print(")/(")} expr {print(")")} /
| digit | digit {print(digit)}
NOTE: Construct annotated parse trees for the inputs 9-5+2 and 9-5*2 by yourself
SDT Question-3
• Construct a syntax-directed translation scheme that
translates arithmetic expression from postfix
notation into equivalent prefix notation.
SDT Questions-3
• Construct a syntax-directed translation scheme that
translates arithmetic expression from postfix
notation into equivalent prefix notation.