11cs30011

advertisement
SYNTAX DIRECTED
TRANSLATION
11CS30011
Types of Attributes
There are two types of attributes for nonterminals :• Synthesized Attributes : For a non-terminal X
at a parse-tree node T , the synthesized
attribute is defined only in terms of attribute
values at the children of T.
Types of Attributes
• Inherited Attributes : For a non-terminal X at
a parse-tree node T , the synthesized attribute
is defined only in terms of attribute values at
the T’s parent , T itself and T’s siblings.
Example : T -> FT’
T’ -> *FT’
T’ -> ɛ
F -> id
T
T’
T
F
ɛ
id
PARSE TREE
F
*
id
Let val be the synthesized attribute of F and T.
Let inh be the inherited attribute of T’ and let
syn be the synthesized attribute of T’.
Let lexval be the synthesized attribute of id
,which is an integer value returned by the lexical
analyzer.
Let the input string be 4 * 5
The Symantic Rules corresponding to each
production is :T -> FT’
T’.inh = F.val
T.val = T’.syn
T’-> *FT’’ T’’.inh = T’.inh * F.val
T’.syn = T’’.syn
T’-> ɛ
T’.syn = T’.inh
F-> val
F.val = id.lexval
Annotated Parse Tree after applying the
semantic rules:T.val = 4*5
T
T.inh = 4*5
T.syn = 4*5
T.inh = 4*5
T.syn = 4*5
T’
T’
F
F
F.val = 4
id
id.lexval = 4
*
F.val = 5
ɛ
id
id.lexval = 5
S-attributed SDD
A syntax-directed definition (SDD) that involves
only synthesized attributes is called S-attributed
SDD. In an S-attributed SDD, each rule computes
an attribute for the nonterminal at the head of a
production from attributes taken from the body
of the production.
S-attributed SDD
Evaluation of S-attributed Parse Trees:When an SDD is S-attributed , its attributes can
be evaluated in any bottom up order of the
nodes of the parse tree. The attributes can be
evaluated by performing a post-order traversal
of the parse tree and evaluating the attributes at
a node N when the traversal leaves N for the last
time.
L-attributed SDD
In a L-attributed SDD each attribute must be either
synthesised or inherited but there are some conditions:For a production A->X1 X2 X3….Xn
The rule associated with this production used for
computing an inherited attribute Xi.a may use:
a)Inherited attributes associated with the head.
b)Either inherited or synthesized attributes associated
with the occurrences of symbols X1, X2,. . . , Xi-1 located
to the left of Xi.
c) Inherited or synthesized attributes associated with this
occurrence of Xi itself, but only in such a way that there
are no cycles in a dependency graph formed by the
attributes of this Xi.
Download