Syntax Directed Translation
Syntax Directed Translation
1. Syntax-Directed Definitions
A syntax-directed definition is a generalization of a context-
free grammar in which:
– Each grammar symbol is associated with a set of
attributes.
– This set of attributes for a grammar symbol is partitioned
into two subsets called
• synthesized and
• inherited attributes of that grammar symbol.
– Each production rule is associated with a set of semantic
rules.
Syntax-Directed Definition –
Example(calculator)
Production Semantic Rules
L → E return print(E.val)
E → E1 + T E.val = E1.val + T.val
E→T E.val = T.val
T → T1 * F T.val = T1.val * F.val
T→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval
X X.x
Y Y.x
top Z Z.x
Production Code fragment
L->E n print(val[top])
E->E + T val[ntop] :=val[top-2] + val[top]
E->T
E->T*F val[ntop] :=val[top-2] * val[top]
T->F
F->(E) val[ntop] := val[top-1]
F-> digit
3. L-attributed definitions
• These are definitions that define rules for determining
inherited attributes in which each inherited attribute
depends only on
The attributes of the symbols to the left of it in the
production
The inherited attributes of the non-terminal on the left-
side of the production
• Every L-attributed definition is L-attributed, as the rules
stated above apply only to inherited attributes. L-
attributed definitions are said to be L-attributed as
information flows from left to right in the syntax tree.
4. Top down translation
5. Bottom up evaluation of
inherited attributes
Production Semantic Rules
D → T L L.in = T.type
T → int T.type = integer
T → real T.type = real
L → L1 , id L1.in = L.in,
addtype(id.entry,L.in)
L → id
addtype(id.entry,L.in)