S – attributed and L – attributed SDTs in Syntax directed translation



Before coming up to S-attributed and L-attributed SDTs, here is a brief intro to Synthesized or Inherited attributes

Types of attributes –
Attributes may be of two types – Synthesized or Inherited.

  1. Synthesized attributes –
    A Synthesized attribute is an attribute of the non-terminal on the left-hand side of a production. Synthesized attributes represent information that is being passed up the parse tree. The attribute can take value only from its children (Variables in the RHS of the production).

    For eg. let’s say A -> BC is a production of a grammar, and A’s attribute is dependent on B’s attributes or C’s attributes than it will be synthesized attribute.

  2. Inherited attributes –
    An attribute of a nonterminal on the right-hand side of a production is called an inherited attribute. The attribute can take value either from its children or from its siblings (variables in the LHS or RHS of the production).

    For example, let’s say A -> BC is a production of a grammar and B’s attribute is dependent on A’s attributes or C’s attributes than it will be inherited attribute.

Now, let’s discuss about S – attribute and L – attribute.



  1. S-attributed SDT –
    If an SDT uses only synthesized attributes, it is called as S-attributed SDT. S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent nodes depend upon the values of the child nodes.

    S-attributed SDT As depicted above, attributes in S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent nodes depend upon the values of the child nodes. These attributes are evaluated using S-attributed SDTs that have their semantic actions written after the production (RHS).

  2. L-attributed SDT –
    If an SDT uses either synthesized attributes or inherited attributes with a restriction that it can inherit values from left siblings only, it is called as L-attributed SDT. Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.

    L-attributed SDT is form of SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings.

    For example,

    A -> XYZ {Y.S = A.S, Y.S = X.S, Y.S = Z.S} 

    is not an L-attributed grammar since Y.S = A.S and Y.S = X.S are allowed but Y.S = Z.S violates the L-attributed SDT definition as attributed is inheriting the value from its right sibling.

    Note – If a definition is S-attributed, then it is also L-attributed but NOT vice-versa.

    Example – Consider the given below SDT.

    P1: S -> MN  {S.val= M.val + N.val}
    P2: M -> PQ  {M.val = P.val * Q.val  and P.val =Q.val} 

    Select the correct option.
    A. Both P1 and P2 are S attributed.
    B. P1 is S attributed and P2 is L-attributed.
    C. P1 is L attributed but P2 is not L-attributed.
    D. None of the above

    Explanation –
    The correct answer is option C as, In P1, S is a synthesized attribute and in L-attribute definition synthesized is allowed. So P1 follows the L-attributed definition. But P2 doesn’t follow L-attributed definition as P is depending on Q which is RHS to it.



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.