The Wayback Machine - https://web.archive.org/web/20260119143617/https://www.geeksforgeeks.org/compiler-design/s-attributed-and-l-attributed-sdts-in-syntax-directed-translation/

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

Last Updated : 20 Dec, 2025

In Syntax-Directed Translation (SDT), rules define how semantic information is passed between nodes of a parse tree during the parsing process.

SDTs are derived from context-free grammars, where semantic actions are attached to grammar productions.

These semantic actions are used to perform tasks such as type checking, intermediate code generation, symbol table management, and other semantic processing.

SDT helps connect the syntactic structure of a program with its semantic meaning, making translation systematic and organized.

Importance of SDTs

  • SDTs play a key role in compiler construction by clearly defining how attributes are evaluated.
  • They help in building efficient and well-structured compilers.
  • By controlling attribute flow, SDTs ensure correct semantic analysis during parsing.

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

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)
Comment

Explore