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
5
location, number, etc.)
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 its value at a parse-tree node
if is by the parent
determined
parent’s semantic rules) & (by
its siblings
enforcing the
Synthesized
Inherited
9
Attributed Grammar
(SDD for infix to postfix translation)
Syntax Directed Definition
term term.t = 9
+ 2
9 - 11
9 - 5 + 2
Depth-First Traversals
procedure visit(n : node);
begin
for each child m of n, from left to right do
visit(m);
evaluate semantic rules at node n
end
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-
“+” expr.t := expr1.t //
2+
term.t // “-” expr.t := term.t
term.t := “0” expr.t = 95- term.t =
term.t := “1” 2
… expr.t = 9 term.t =
term.t := “9” 5
term.t = 9
+ 2
9 -
5 Note: all attributes are
of the synthesized type
Translation Schemes
• A translation scheme is a CF grammar
embedded with semantic actions
Embedded
semantic action
rest
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-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.