Open In App

S - Attributed and L - Attributed SDTs in Syntax Directed Translation

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Syntax-Directed Translation (SDT), the rules are those that are used to describe how the semantic information flows from one node to the other during the parsing phase. SDTs are derived from context-free grammars where referring semantic actions are connected to grammar productions. Such action can be used in issues such as code generation like intermediate code, type checking, and so on.

There are two main types of SDTs based on how attributes are associated with grammar symbols and how information is propagated: S-attributed SDTs and L-attributed SDTs At this point, the audience of an S-attributed SDT is just the set of instructions within the same statement that uses the variable, while the audience of an L-Attributed SDT is everyone within the statement but the first instruction that uses the variable. In general, these forms of SDTs are important in the construction of efficient compilers particularly in defining how attributes are computed in the parse tree.

What is S-attributed SDT?

An S-attributed SDT (Synthesized Attributed SDT) is one of the Syntax-Directed Translation schemes in which all attributes are synthesized. Predictive attributes are calculated as a result of attributes of the parse tree children nodes, their values are defined. Normally, the value of a synthesized attribute is produced at the leaf nodes and then passed up to the root of the parse tree.

Key Features:

  • Bottom-Up Evaluation: Similarly, synthesized attributes are assessed in the bottom-up approach.
  • Suitable for Bottom-Up Parsing: Thus, S-attributed SDTs are more suitable to the approaches to bottom-up parsing, including the shift-reduce parsers.
  • Simple and Efficient: As all attributes are generated there are no inherited attributes involved thus making it easier to implement.

Example:

Let us consider a production role such that.

E → E1 + T

Hence, the synthesized attribute E.val can be calculated as:

E.val = E1.val + T.val

What is L-attributed SDT?

An L-Attributed SDT (Left-Attributed SDT) also permits synthesized attributes as well as inherited attributes. Some of these attributes are forced attributes, which are inherited from the parent node, other attributes are synthetic attributes, which are calculated like S-attributed SDTs. L-attributed SDTs is an algebra of system design and the key feature of the algebra is that attributes can only be inherited on the left side of a particular production rule.

Key Features:

  • Top-Down Evaluation: Evaluations of the inherited attributes are carried out in a manner that is top-down while those of the synthesized attributes are bottom–up.
  • Suitable for Top-Down Parsing: L-attributed SDTs are typical for the top-down approaches to parsing such as the recursive descent parsers.
  • Allows More Complex Dependencies: Since a language that has the capability of supporting both, inherent as well as synthesized attributes for its terms define more sophisticated semantic rules, then it is appropriate for a semantic network.

Example:

Consider a production rule.

S → A B

Now, the inherited attribute A.inh can be calculated as.

A.inh = f(S.inh)

likewise, B can also have synthesized attributes based on A :

B.synth = g(A.synth)

Conclusion

In particular, S-attributed and L-attributed SDTs are now known to have important roles within the design of compilers. S-attributed SDTs are quite basic and are more effective with the bottom-up parsing while L-attributed SDTs are quite flexible because they allow for attributes to be inherited or synthesized and therefore are best utilized in the top-down parsing. It is therefore important to distinguish between these different forms of SDTs in order to devise the appropriate translation scheme in compilers.


Next Article

Similar Reads