Compiler Design · Syntax Directed Definitions
Synthesized vs Inherited Attributes
Overview
In Syntax Directed Translation (SDT), every grammar symbol carries attributes that receive values.
Attributes are classified into two types — synthesized and inherited — based solely on where the value comes from, and this classification in turn determines whether an SDT is S-attributed or L-attributed.
Key concepts
- Attribute: Every variable (grammar symbol) in an SDT carries an attribute that receives a value.
- Synthesized attribute: A node takes its attribute value from its children; the value flows upward and is synthesized (built up) from the children's values.
- Inherited attribute: A node takes its attribute value from its parent or from its siblings; the value flows downward or sideways.
- Classification rule: The type of an attribute depends only on the source of its value, not on the symbol it is attached to.
- SDT classification: Attributes are of exactly two types, and depending on which types appear in an SDT, the SDT itself is classified as S-attributed or L-attributed.
Drawing the diagram…
How it works
Consider a production of the form , where is the parent (head) and , , are its children on the right-hand side.
- Synthesized attribute: If has an attribute computed as a function of the attributes of , and , the value flows upward from children to parent. is synthesized because the parent's value is built up from the children's values.
- Inherited attribute (from parent): If has an attribute that takes its value from the parent, , the value flows downward from parent to child.
- Inherited attribute (from sibling): may instead take its value from a sibling, or ; the value flows sideways.
- Summary: A node taking its value from its children gives a synthesized attribute; a node taking its value from its parent or sibling gives an inherited attribute.
Formulas
-
For the production :
-
— synthesized: the parent's attribute is a function of its children's attributes (value flows upward).
-
— inherited from parent: the child's attribute is a function of its parent's attribute (value flows downward).
-
or — inherited from sibling: a node's attribute is a function of a sibling's attribute (value flows sideways).
-
Here denotes any attribute of the symbol, and denotes any function/computation the semantic rule defines.
Worked examples
Production:
- Synthesized case: is defined using the attributes of , and . Since (the parent) receives its value from its children, is a synthesized attribute.
- Inherited from parent: is defined using . Since the child receives its value from its parent , is an inherited attribute.
- Inherited from sibling: is defined using , or is defined using . Since receives its value from a sibling, is an inherited attribute.
Comparison
| Feature | Synthesized attribute | Inherited attribute |
|---|---|---|
| Value taken from | Children of the node | Parent or siblings of the node |
| Direction of value flow | Upward (child → parent) | Downward (parent → child) or sideways (sibling → sibling) |
| Example on | , or , or | |
| Reason for the name | Parent's value is synthesized from children's values | Child inherits the value from parent/sibling |
Revision summary
- Attributes are of exactly two types: synthesized and inherited.
- Synthesized means value comes from children (flows up).
- Inherited means value comes from parent or sibling (flows down or sideways).
- The classification depends only on the source of the value, not on the symbol the attribute is attached to.
- The types of attributes present in an SDT determine the SDT's classification: S-attributed vs L-attributed.
Previous-year exam insights
- An SDD is S-attributed if every attribute it uses is synthesized; S-attributed SDDs can be evaluated bottom-up during LR parsing.
Asked inGATE 2025GATE 2026 - An SDD is L-attributed if every attribute is either synthesized or inherited with the restriction that an inherited attribute of a symbol on the right-hand side may depend only on attributes of the symbols to its left (and on the head's inherited attributes), never on attributes of symbols to its right; every S-attributed SDD is L-attributed but not conversely.
Asked inGATE 2025GATE 2024GATE 2003 - In bottom-up (LR) evaluation, inherited attributes can be evaluated only if the definition is L-attributed; synthesized attributes alone are always evaluable bottom-up.
Asked inGATE 2003 - Synthesized attributes are naturally simulated by bottom-up parsers (LR grammars), since values are computed when a production is reduced; inherited attributes fit top-down (LL) evaluation.
Asked inUGC NET 2009 - An attribute grammar is an SDD whose semantic-rule functions have no side effects; side effects such as inserting into a symbol table (put) make it an SDT rather than a pure attribute grammar.
Asked inGATE 2024 - Inherited attributes are essential for passing context such as a declared type downward (e.g., from a declaration to the identifiers in its list), which synthesized attributes alone cannot do.
Asked inGATE 2019GATE 2026