1. Gyanastra
  2. Compiler Design
  3. Syntax Directed Definitions
  4. Synthesized vs Inherited Attributes
Free lesson, no account needed 5 min read 13 practice questions after it
Asked in past papers7 times
  • GATE CSE20262025202420192003
  • UGC NET20092008

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.
Figure. A.S receives values upward from B C and D. C.I is one inherited attribute whose value may come from parent A.I or sibling B.I or D.I as alternatives.

How it works

Consider a production of the form A→B C DA \to B\ C\ D, where AA is the parent (head) and BB, CC, DD are its children on the right-hand side.

  1. Synthesized attribute: If AA has an attribute A.SA.S computed as a function of the attributes of BB, CC and DD, the value flows upward from children to parent. A.SA.S is synthesized because the parent's value is built up from the children's values.
  2. Inherited attribute (from parent): If CC has an attribute C.IC.I that takes its value from the parent, C.I=f(A.I)C.I = f(A.I), the value flows downward from parent to child.
  3. Inherited attribute (from sibling): C.IC.I may instead take its value from a sibling, C.I=f(B.I)C.I = f(B.I) or C.I=f(D.I)C.I = f(D.I); the value flows sideways.
  4. 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 A→B C DA \to B\ C\ D:

  • A.S=f(B.attr, C.attr, D.attr)A.S = f(B.attr,\ C.attr,\ D.attr) — synthesized: the parent's attribute is a function of its children's attributes (value flows upward).

  • C.I=f(A.I)C.I = f(A.I) — inherited from parent: the child's attribute is a function of its parent's attribute (value flows downward).

  • C.I=f(B.I)C.I = f(B.I) or C.I=f(D.I)C.I = f(D.I) — inherited from sibling: a node's attribute is a function of a sibling's attribute (value flows sideways).

  • Here attrattr denotes any attribute of the symbol, and ff denotes any function/computation the semantic rule defines.

Worked examples

Production: A→B C DA \to B\ C\ D

  1. Synthesized case: A.SA.S is defined using the attributes of BB, CC and DD. Since AA (the parent) receives its value from its children, A.SA.S is a synthesized attribute.
  2. Inherited from parent: C.IC.I is defined using A.IA.I. Since the child CC receives its value from its parent AA, C.IC.I is an inherited attribute.
  3. Inherited from sibling: C.IC.I is defined using B.IB.I, or C.IC.I is defined using D.ID.I. Since CC receives its value from a sibling, C.IC.I is an inherited attribute.

Comparison

FeatureSynthesized attributeInherited attribute
Value taken fromChildren of the nodeParent or siblings of the node
Direction of value flowUpward (child → parent)Downward (parent → child) or sideways (sibling → sibling)
Example on A→B C DA \to B\ C\ DA.S=f(B.attr, C.attr, D.attr)A.S = f(B.attr,\ C.attr,\ D.attr)C.I=f(A.I)C.I = f(A.I), or C.I=f(B.I)C.I = f(B.I), or C.I=f(D.I)C.I = f(D.I)
Reason for the nameParent's value is synthesized from children's valuesChild 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
Pages 1–2 of 2