3. 2.3 Syntax-Directed Translation
3
Syntax-directed translation is done by attaching rules or
program fragments to productions in a grammar.
two concepts related to syntax-directed translation:
Attributes. An attribute is any quantity associated with a programming
construct.
(Syntax-directed) translation schemes. A translation scheme is a notation
for attaching program fragments to the productions of a grammar.
We will use this concept
9. 9
Example: 5-(2*1)+1
5 - ( 2 * 1 ) + 1
expr + term
expr - term
term factor
2.3 Syntax-Directed Translation
10. 10
Example: 5-(2*1)+1
2.3 Syntax-Directed Translation
Productions Semantic Rules
expr expr + term
expr expr – term
expr term
term term * f
term term / f
term f
f (expr)
f digit
digit 0
digit 1
digit 9
expr = expr + term
expr = expr – term
expr = term
term = term * f
term = term / f
term = f
f = (expr)
f = digit
digit = 0
digit = 1
digit = 9