In Syntax-Directed Translation (SDT), semantic actions are associated with grammar productions to process semantic information during parsing. Based on attribute evaluation and dependency rules, syntax-directed definitions are classified into S-attributed and L-attributed forms.
Importance of SDTs
- Defines how semantic information is evaluated during compilation.
- Supports semantic analysis and code generation.
- Helps organize attribute evaluation and translation.
S-attributed SDT
An S-attributed SDT uses only synthesized attributes, which are computed from the attributes of child nodes and propagated upward in the parse tree.
- Bottom-Up Evaluation: Attributes are computed from child nodes toward parent nodes.
- Suitable for Bottom-Up Parsing: Commonly used with shift-reduce parsers.
- Simple Evaluation: Does not require inherited attributes.
Example:
Consider the production:
E → E1 + TThe synthesized attribute can be calculated as:
E.val = E1.val + T.valHere, the value of E is computed using the values of its child nodes.
L-attributed SDT
An L-attributed SDT supports both synthesized and inherited attributes. Inherited attributes can depend on the attributes of the parent and symbols to the left of the current symbol in a production.
- Left-to-Right Evaluation: Attributes can be evaluated in a left-to-right traversal of the parse tree.
- Suitable for Top-Down Parsing: Commonly used with recursive-descent parsers.
- Supports Attribute Dependencies: Allows information to be passed from parent nodes and left siblings.
Example:
Consider the production:
S → A BThe inherited attributes can be defined as:
A.inh = f(S.inh)
B.inh = g(S.inh, A.synth)
Here, A.inh depends on the parent S, while B.inh depends on the parent and the synthesized attribute of the left sibling A.

