Documente Academic
Documente Profesional
Documente Cultură
Syntax-Directed Translation
Semantic Rules
print(E.val)
E.val := E1.val + T.val
E.val := T.val
T.val := T1.val * F.val
T.val := F.val
F.val := E.val
F.val := digit.lexval
E.Val = 19
E.Val = 15
T.Val = 4
T.Val = 15
T.Val = 3
F.Val = 3
digit.lexval = 3
F.Val = 4
F.Val = 5
digit.lexval = 5
digit.lexval = 4
Example
Using inherited
attributes to parse a
declaration and add
type information to
the symbol table.
Production
Semantic Rules
D TL
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)
real
L.in = real
L.in = real
L.In = real
id1
id2
id3
A.A
X.X
Y.y
Z.Z
Top-Down Translation
E.g.
E E1 + T
{E.val := E1.val + T.val}
E E1 T
{E.val := E1.val T.val}
ET
{E.val := T.val}
T (E)
{T.val := E.val}
T num
{T.val := num.val}
To eliminate the left-recursion we add a
nonterminal R to the grammar with an inherited
attribute, R.i and a synthesized attribute, R.s.
The transformed translation scheme is:
Example
Consider three
nonterminals E, R and T.
Write three recursive
functions.
E and T have no
arguments.
Function E handles the
production:
E T {R.i := T.nptr} R
{E.nptr := R.s}
function E : tree_node;
begin e := R (T) end;
E.Nptr
(none)
R.S
R>I
T.Nptr
(none)