L- ATTRIBUTE Name: Khushboo Jethwa Enrollment no. : 140950107028 Subject: Compiler Design Subject Guide: Avneet Saluja
Attribute Grammar • Attribute grammar is a special form of context-free grammar where some additional information (attributes) are appended to one or more of its non- terminals in order to provide context-sensitive information. Each attribute has well-defined domain of values, such as integer, float, character, string, and expressions. • Attribute grammar is a medium to provide semantics to the context-free grammar and it can help specify the syntax and semantics of a programming language. Attribute grammar (when viewed as a parse-tree) can pass values or information among the nodes of a tree.
EXAMPLE E → E + T { E.value = E.value + T.value } • The right part of the CFG contains the semantic rules that specify how the grammar should be interpreted. Here, the values of non-terminals E and T are added together and the result is copied to the non-terminal E. • Semantic attributes may be assigned to their values from their domain at the time of parsing and evaluated at the time of assignment or conditions. Based on the way the attributes get their values, they can be broadly divided into two categories : synthesized attributes and inherited attributes.
Synthesized attributes • These attributes get values from the attribute values of their child nodes. To illustrate, assume the following production: S → ABC • If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized attribute, as the values of ABC are synthesized to S. • As in our previous example (E → E + T), the parent node E gets its value from its child node. Synthesized attributes never take values from their parent nodes or any sibling nodes.
Inherited attributes • In contrast to synthesized attributes, inherited attributes can take values from parent and/or siblings. As in the following production, S → ABC • A can get values from S, B and C. B can take values from S, A, and C. Likewise, C can take values from S, A, and B.
L-attributed SDT • This form of SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings. • In L-attributed SDTs, a non-terminal can get values from its parent, child, and sibling nodes. As in the following production S → ABC • S can take values from A, B, and C (synthesized). A can take values from S only. B can take values from S and A. C can get values from S, A, and B. No non-terminal can get values from the sibling to its right. • Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.
All S-attributes are L-attribute but all S- arrtibute may or may not be L-attribute.
EXAMPLE State whether the following are l attribute or not. 1) A-> QR { R.i = f(A.i), Q.i =f(R.i) } checking one by one semantic actions. • for R.i = f(A.i), it’s a l-attribute as it’s a inherited attibute from the parent. • for Q.i =f(R.i), it’s not a l-attribute as it’s inherited from the right sibling.
2) A-> BC { B.S = A.S } checking one by one semantic actions. • for B.S = A.S, it’s a l-attribute as it’s a inherited attibute from the parent.
THANK YOU

L attribute in compiler design

  • 1.
    L- ATTRIBUTE Name: KhushbooJethwa Enrollment no. : 140950107028 Subject: Compiler Design Subject Guide: Avneet Saluja
  • 2.
    Attribute Grammar • Attributegrammar is a special form of context-free grammar where some additional information (attributes) are appended to one or more of its non- terminals in order to provide context-sensitive information. Each attribute has well-defined domain of values, such as integer, float, character, string, and expressions. • Attribute grammar is a medium to provide semantics to the context-free grammar and it can help specify the syntax and semantics of a programming language. Attribute grammar (when viewed as a parse-tree) can pass values or information among the nodes of a tree.
  • 3.
    EXAMPLE E → E+ T { E.value = E.value + T.value } • The right part of the CFG contains the semantic rules that specify how the grammar should be interpreted. Here, the values of non-terminals E and T are added together and the result is copied to the non-terminal E. • Semantic attributes may be assigned to their values from their domain at the time of parsing and evaluated at the time of assignment or conditions. Based on the way the attributes get their values, they can be broadly divided into two categories : synthesized attributes and inherited attributes.
  • 4.
    Synthesized attributes • Theseattributes get values from the attribute values of their child nodes. To illustrate, assume the following production: S → ABC • If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized attribute, as the values of ABC are synthesized to S. • As in our previous example (E → E + T), the parent node E gets its value from its child node. Synthesized attributes never take values from their parent nodes or any sibling nodes.
  • 5.
    Inherited attributes • Incontrast to synthesized attributes, inherited attributes can take values from parent and/or siblings. As in the following production, S → ABC • A can get values from S, B and C. B can take values from S, A, and C. Likewise, C can take values from S, A, and B.
  • 6.
    L-attributed SDT • Thisform of SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings. • In L-attributed SDTs, a non-terminal can get values from its parent, child, and sibling nodes. As in the following production S → ABC • S can take values from A, B, and C (synthesized). A can take values from S only. B can take values from S and A. C can get values from S, A, and B. No non-terminal can get values from the sibling to its right. • Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.
  • 7.
    All S-attributes areL-attribute but all S- arrtibute may or may not be L-attribute.
  • 8.
    EXAMPLE State whether thefollowing are l attribute or not. 1) A-> QR { R.i = f(A.i), Q.i =f(R.i) } checking one by one semantic actions. • for R.i = f(A.i), it’s a l-attribute as it’s a inherited attibute from the parent. • for Q.i =f(R.i), it’s not a l-attribute as it’s inherited from the right sibling.
  • 9.
    2) A-> BC{ B.S = A.S } checking one by one semantic actions. • for B.S = A.S, it’s a l-attribute as it’s a inherited attibute from the parent.
  • 10.