Presentation on theme: "SYNTAX DIRECTED TRANSLATION 11CS30011. Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal."— Presentation transcript:
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.
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 F * id TF ɛ T’ 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 F * id T’F idɛ T’ T.val = 4*5 F.val = 4 id.lexval = 4 id.lexval = 5 F.val = 5 T.inh = 4*5 T.syn = 4*5 T.inh = 4*5 T.syn = 4*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.