Chapter 2 - Simple Syntax Directed Translator
Chapter 2 - Simple Syntax Directed Translator
A Simple Syntax-Directed
Translator
Chapter 2
2
Syntax Definition
• Context-free grammar is a 4-tuple with
– A set of tokens (terminal symbols)
– A set of nonterminals
– A set of productions
– A designated start symbol
5
Example Grammar
with productions P =
list → list + digit
list → digit
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
6
Derivation
• Given a CF grammar we can determine the
set of all strings (sequences of tokens)
generated by the grammar using derivation
– We begin with the start symbol
– In each step, we replace one nonterminal in the
current sentential form with one of the right-
hand sides of a production for that nonterminal
7
list
list + digit
list - digit + digit
digit - digit + digit
9 - digit + digit
9 - 5 + digit
9-5+2
Parse Trees
• The root of the tree is labeled by the start symbol
• Each leaf of the tree is labeled by a terminal
(=token) or
• Each interior node is labeled by a nonterminal
• If A → X1 X2 … Xn is a production, then node A has
immediate children X1, X2, …, Xn where Xi is a
(non)terminal or ( denotes the empty string)
9
list
list digit
list digit
digit
The sequence of
9 - 5 + 2 leafs is called the
yield of the parse tree
10
Ambiguity
with production P =
string → string + string | string - string | 0 | 1 | … | 9
This grammar is ambiguous, because more than one parse tree
represents the string 9-5+2
11
Ambiguity (cont’d)
string string
9 - 5 + 2 9 - 5 + 2
12
Associativity of Operators
Precedence of Operators
Operators with higher precedence “bind more tightly”
expr → expr + term | term
term → term * factor | factor
factor → number | ( expr )
String 2+3*5 has the same meaning as 2+(3*5)
expr
expr term
term term factor
factor factor number
number number
2 + 3 * 5
14
Syntax of Statements
stmt → id := expr
| if expr then stmt
| if expr then stmt else stmt
| while expr do stmt
| begin opt_stmts end
opt_stmts → stmt ; opt_stmts
|
15
Syntax-Directed Translation
• Uses a CF grammar to specify the syntactic
structure of the language
• AND associates a set of attributes with the
terminals and nonterminals of the grammar
• AND associates with each production a set of
semantic rules to compute values of attributes
• A parse tree is traversed and semantic rules
applied: after the tree traversal(s) are completed,
the attribute values on the nonterminals contain
the translated form of the input
16
Depth-First Traversals
expr.t = “95-2+”
term.t = “9”
Translation Schemes
• A translation scheme is a CF grammar
embedded with semantic actions
Embedded
semantic action
rest
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+
24
Parsing Problem
The parsing Problem: Take a string of symbols in a language (tokens)
and a grammar for that language to construct the parse tree or report that
the sentence is syntactically incorrect.
For correct strings:
Sentence + grammar → parse tree
For a compiler, a sentence is a program:
Program + grammar → parse tree
Types of parsers:
Top-down a.k.a predictive (recursive descent parsing)
Bottom-up parsing.
“We will focus on top-down parsing at present”.
25
Recursive Descent
Procedure E Procedure T Procedure F
begin { E } begin { T } begin { F }
call T call F case token is
call E’ call T’ “(“:
print (“ E found ”) print (“ T found ”) print (“ ( found ”)
end { E } end { T } Get next token
call E
Procedure E’ Procedure T’ if token = “)” then
begin { E’ } begin { T’ } begin { IF }
If token = “+” then If token = “ * ” then print (“ ) found”)
begin { IF } begin { IF } Get next token
print (“ + found “) print (“ * found “) print (“ F found “)
Get next token Get next token end { IF }
call T call F else
call E’ call T’ call ERROR
end { IF } end { IF } “id“:
print (“ E’ found “) print (“ T’ found “) print (“ id found ”)
end { E’ } end { T’ } Get next token
print (“ F found “)
otherwise:
call ERROR
end { F }
27
Indirect left-recursion: A → Ba | C
B → Ab | D
A Ba Aba
29
How It Works
Examples of applying previous syntax
directed translation
Input: 15 - 20 + 7 * 3 / 2
Output: 15 20 - 7 3 * 2 / +
Input: 15 - 20 - 7 + 3 * 2
Output: 15 20 - 7 - 3 2 * +
33
Problems Galore
Examples of applying previous syntax
directed translation
Input: 15 - 20 + 7 * 3 / 2
Output: 15 20 7 3 2 / * + - (In error)
Input: 15 - 20 - 7 + 3 * 2
Output: 15 20 7 3 2 * + - - (In error)
35
Recursive Descent
Procedure E Procedure T Procedure F
begin { E } begin { T } begin { F }
call T call F case token is
call E’ call T’ “(“:
end { E } end { T } nextsy()
call E
Procedure E’ Procedure T’ if token = “)” then
begin { E’ } begin { T’ } nextsy()
If token = “+” then If token = “*” then else
begin { addition } begin { multiply } ERROR()
nextsy nextsy() “id“:
call T call F out( id.val )
out(“ + “) out(“ * “) Get next token
call E’ call T’ otherwise:
end { addition } end { multiply } ERROR()
If token = “-” then If token = “/” then end { F }
begin { subtraction } begin { divide }
nextsy nextsy()
call T call F
out(“ - “) out(“ / “)
call E’ call T’
end { subtraction} end { divide }
end { E’ } end { T’ }
38
Process
• Write left recursive grammar with semantic
actions.
• Rewrite as right recursive with actions
treated as terminals in original rules.
• Develop recursive descent parser.
39
Left Factoring
When have rules like
A → |
which rule to choose is a problem
Factor as
A→X
X→|