0% found this document useful (0 votes)
20 views

Lecture#6 - Chap#2 (Syntax Directed Translator (Part-II) )

Uploaded by

nishafshah68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture#6 - Chap#2 (Syntax Directed Translator (Part-II) )

Uploaded by

nishafshah68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Lecture# 06

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

Infix: 9-5+2 & Postfix: 95-2+


Production Semantic Rule
expr  expr1 + term expr.t := expr1.t // term.t //
expr  expr1 - term “+” expr.t := expr1.t //
expr  term term.t // “-” expr.t := term.t
term  0 term.t := “0”
term  1 term.t := “1”
… …
term  9 term.t := “9”
Note: expr and expr1 are same. // is concatenation, t is
used for string valued
10
Infix: 9- & Postfix: 95-2+ Example Annotated
5+2 Semantic Rule Parse Tree Continued
Production
expr  expr1 + term expr.t := expr1.t // term.t //
expr  expr1 - term “+” expr.t := expr1.t //
expr  term term.t // “-” expr.t := term.t
term  0 term.t := “0”
term  1 term.t := “1” Just in this Evaluation of the
… … value
term  9 term.t := “9”
expr.t = 95-
2+
expr
expr.t = 95- term.t =
2
expr1 term
expr.t = 9 term.t =
expr1 term 5

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

rest  + term { print(“+”) } rest

Embedded
semantic action
rest

Sematic rules ky against kon sa


action perform hota ha osy hm TS + term rest
my dykhtay hen { print(“+”) }
Example Translation Scheme
Translation scheme is used to translate infix to postfix

expr  expr + term { print(“+”) }


expr  expr - term { print(“-”) }
expr  term
term  0 { print(“0”) }
term  1 { print(“1”) }
… …
term  9 { print(“9”) }
Example Translation Scheme (cont’d)
expr { print(“+”) }
expr + term
{ print(“2”) }
{ print(“-”)}
- 2
expr term
{ print(“5”) }
term 5
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term

Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term
{ print(“-”)}
- term
expr

Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term
{ print(“-”)}
- term
expr

term
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term
{ print(“-”)}
- term
expr
{ print(“5”) }
term 5
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term
{ print(“2”) }
{ print(“-”)}
- 2
expr term
{ print(“5”) }
term 5
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
Translation scheme is used to translate infix to postfix Example Translation Scheme
expr  expr + term{ print(“+”) } (cont’d)
expr  expr - term { print(“-”) }
expr  term
term 0 { print(“0”) }
term  1 { print(“1”) }

expr { print(“+”) }

term  9 { print(“9”) }
expr + term
{ print(“2”) }
{ print(“-”)}
- 2
expr term
{ print(“5”) }
term 5
{ print(“9”) }
9
Translates 9-5+2 into postfix 95-2+

The implementation of a translation scheme ensures that semantic actions


are performed in the order they would appear during a postorder traversal of
a parse tree
SDT Question-1
• Construct a syntax-directed translation scheme that
translates arithmetic expressions from infix notation
into prefix notation in which an operator appears
before its operands; e.g. , -xy is the prefix notation
for x - y. Give annotated parse trees for the inputs 9-
5+2 and 9-5*2.
SDT Solution-1
• Construct a syntax-directed translation scheme that translates
arithmetic expressions from infix notation into prefix notation
in which an operator appears before its operands; e.g. , -xy is
the prefix notation for x - y. Give annotated parse trees for the
inputs 9-5+2 and 9-5*2.
Productions Translation Schemes
expr -> expr + term expr -> {print("+")} expr + term
| expr - term | {print("-")} expr - term
| term | term
term -> term * term -> {print("*")} term * factor
factor | {print("/")} term / factor
| term / | factor
factor factor -> digit {print(digit)}
| factor | (expr)
factor -> digit |
(expr)
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
| expr expr / {print(")*(")} expr
| digit {print(")")} *
| {print("(")} expr
{print(")/(")} expr
{print(")")} /
| 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.

Productions Translation Schemes


expr -> expr expr op expr -> {print(op)} expr expr op
| digit | digit {print(digit)}
The
End

You might also like