Академический Документы
Профессиональный Документы
Культура Документы
Translations
UNIT IV
Syntax-Directed Translations
Translation of languages guided by CFGs
Information associated with programming
language constructs
Attributes attached to grammar symbols
Values of attributes computed by semantic rules
associated with grammar productions
Semantic Rules
Semantic rules perform various activities:
Generation of code
Save information in a symbol table
Issue error messages
Other activities
Conceptual View
SYNTAX DIRECTED
DEFINATIONS
A syntax directed defination is a
generailization of a context free grammar in
which each grammer symbol has an
associated set of attributes, partitioned into
two subsets called Synthesized attributes
and inherited attributes.
Attributes
Each grammar symbol (node in parse tree) has
attributes attached to it ex: astring,a number,a type,a
memory location etc.
Values of a Synthesized attributes at a node is comuted
from the values of attributes at the children of that node
in the parse tree.
Values of a Inherited attributes at a node is comuted
from the values of attributes at the siblings and parent of
that node.
A dependency graph represents dependencies between
attributes
A parse tree showing the values of attributes at each
node is an annotated parse tree
Semantic Rules
Each semantic rule for production A -> has the form
b := f(c1, c2, , ck)
f is a function
b may be a synthesized attribute of A or
b may be an inherited attribute of one of the grammar symbol on
the right side of the production
c1, c2, , ck are attributes belonging to grammar symbols of
production
S-attributed Definitions
Synthesized attributes are used
extensively in practice
S-attributed definition: A syntax-directed
definition using only synthesized attributes
Parse tree can be annotated by evaluation
nodes during a single bottom up pass
Semantic Rules
L E n
print(E.val)
E E1 + T
E T
E.val := T.val
T T1 * F
T F
T.val := F.val
F (E)
F.val := E.val
F digit
F.val := digit.lexval
NOTE
In a syntax directed definations,terminals
are assumed to have
Synthesized attributes only,as the
definations does not provide any semantic
rules for terminals.values for attributes of
terminals are usually supplied by the
lexical analyser.Start symbol is assumed
not to have any inherited attribute
otherwise stated.
Inherited Attributes
Inherited Attributes:
Value at a node in a parse tree depends
on attributes of parent and/or siblings
Convenient for expressing dependencies of
programming language constructs on context
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)
Dependency Graphs
Dependency graph:
Depicts interdependencies among
synthesized and inherited attributes
Includes dummy nodes for procedure calls
Example(inherited attribute)
Syntax Trees
(Abstract) Syntax Trees
Condensed form of parse tree
Useful for representing language constructs
Operators and keywords appear as internal
nodes
Example: a - 4 + c
p1
P2
p3
p4
p5
:=
:=
:=
:=
:=
mkleaf(id, pa);
mkleaf(num, 4);
mknode('-', p1, p2);
mkleaf(id, pc);
mknode('+', p3, p4);
Semantic Rules
E E1 + T
E E1 T
E T
E.np := T.np
T (E)
T.np := E.np
T id
T num
Example: a
+ a * (b c) + (b c) * d
Production
Code Fragment
(1) L E \n
Print(val[top])
(2) E E1 + t
(3) E T
(4) T T1 * F
(5) T F
(6) F (E)
(7) F digit
val[ntop] := val[top-1]
State
Val
3*5+4\n
---
---
Rule
Input
State
Val
Rule
+4\n E
*5+4\n 3
*5+4\n F
(7)
*5+4\n T
(5)
4\n E+
15
(3)
15_
\n E+4
15_4
3_
\n E+F
15_4
(7)
+4\n T*5
3_5
\n E+T
15_4
(5)
+4\n T*F
3_5
(7)
\n E
19
(2)
+4\n T
3_5
(4)
5+4\n T*
E\n
19_
19
(1)
Evaluating Attributes
Possible evaluation orders depend on
order that nodes are created by parser
Depth-first search is very common
evaluation order
L-attributed definitions use this technique
Information appears to flow left-to-right
Can handle all synthesized and some
inherited attributes
Depth-First Evaluation
procedure dfvisit(n: node);
begin
for each child m of n, from left to right
begin
evaluate inherited attributes of m
dfvisit(m)
end;
evaluate synthesized attributes of n
end
L-attributed Definitions
A syntax-directed definition is L-attributed:
If each inherited attribute of Xj, for production
A X1X2Xn (1 <= j <= n), depends on:
X1, X2, , Xj-1 to the left of XJ
in the production
The inherited attributes of A
Non-L-Attributed Example
Production
Semantic Rule
L.i := l(A.i)
A L M
M.i := m(L.s)
A.s := f(M.s)
R.i := r(A.i)
A Q R
Q.i := q(R.s)
A.s := f(q.s)
Translation Schemes
Semantic actions are inserted within the right
side of productions
Placement indicates order of evaluation
If we are dealing with both inherited and
synthesized attributes:
Each inherited attribute must be computed by action
before symbol appears on right side of production
No action may refer to a synthesized attribute of a
symbol to the right of the action
Any synthesized attribute of nonterminal on left must
be computed after computing all referenced attributes
B B1 B2
B B1 sub B2
B text
Semantic Rules
B.ps := 10
S.ht := B.ht
B1.ps := B.ps
B2.ps := B.ps
B.ht := max(B1.ht, B2.ht)
B1.ps := B.ps
B2.ps := shrink(B.ps)
B.ht := disp(B1.ht, B2.ht)
B.ht := text.h * B.ps
{B.ps := 10}
{S.ht := B.ht}
B1
B2
{B1.ps := B.ps}
{B2.ps := B.ps}
{B.ht := max(B1.ht, B2.ht)}
B
B1
sub
B2
{B1.ps := B.ps}
{B2.ps := shrink(B.ps)}
{B.ht := disp(B1.ht, B2.ht)}
E
R
R
R
T
T
T
Copy Rules
Consider reduction: A X Y
Suppose X has synthesized attribute X.s
X.s will already be on stack before any
reductions take place in subtree below Y
Therefore, this value can be inherited by Y
Define attribute Y.i using a copy rule:
Y.i = X.s
D
T
T
L
L
T {L.in := T.type} L
int {T.type := integer}
real {T.type := real}
{L1.in := L.in} L1, id {addtype(id.entry, L.in)}
id {addtype(id.entry, L.in)}
State
---
Production Used
p, q, r real
p, q, r T
T real
,q, r T id
,q, r T L
L id
q, r T L ,
, r T L , id
, r T L
L L , id
r T L ,
T L , id
T L
L L , id
D T L
Code Fragment
val[ntop] := integer
T real
L L, id
L id
val[ntop] := real
addtype(val[top], val[top-3])
addtype(val[top], val[top-1])
Semantic Rules
C.i := A.s
S bABC
C.i := A.s
C c
C.s := g(C.i)
Markers
Marker nonterminals generating are
inserted into the grammar
Each embedded action is replaced by a
marker with the action attached
Actions in the transformed translation
scheme terminate productions
Markers can often be used to move all
actions to the right side of productions
Markers Example
E T R
R + T {print('+')} R | - T {print('-')} R |
T num {print(num.val)}
E
R
M
N
T
T R
+ T M R | - T N R |
{print('+')}
{print('+')}
num {print(num.val)}
Semantic Rules
C.i := A.s
Production
S aAC
Semantic Rules
C.i := A.s
S bABMC
M
C c
C.i := A.s
C.s := g(C.i)
Semantic Rules
C.i := f(A.s)
Production
S aANC
Semantic Rules
N.i := A.s; C.i := N.s
N.s := f(N.i)
D id L
L , id L | : T
T integer | real