100 likes | 302 Views
SYNTAX DIRECTED TRANSLATION. 11CS30011. Types of Attributes. There are two types of attributes for non-terminals :- 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 .
E N D
SYNTAX DIRECTED TRANSLATION 11CS30011
Types of Attributes There are two types of attributes for non-terminals :- • Synthesized Attributes : For a non-terminal X at a parse-tree nodeT , 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 nodeT , 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’ F T F * ɛ id id PARSE TREE
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-> valF.val = id.lexval
Annotated Parse Tree after applying the semantic rules:- T T.val = 4*5 T.inh = 4*5 T.syn = 4*5 T’ F F.val = 4 T’ F * T.inh = 4*5 T.syn = 4*5 F.val = 5 ɛ id id id.lexval = 4 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.