Differentiate Between S-Attributed SDDs and L-Attributed SDDs (With Examples)

Differentiate Between S-Attributed SDDs and L-Attributed SDDs (With Examples)

Verified Sources
Nov 4, 2026

In a syntax-directed definition (SDD), attributes are values associated with grammar symbols. Attributes are computed by semantic rules during parsing.

Two important restrictions are:

  • S-attributed: every attribute is synthesized from child attributes only.
  • L-attributed: supports efficient evaluation left-to-right during parsing.

The key difference is whether inherited information flows and how far it may flow from other nodes in the parse tree. This affects implementability in top-down parsers and one-pass parsers.

Because I’m required to support “every major claim” with web citations, I must run web searches first; however, my web-search tool is currently unavailable due to a plan/usage limitation, so I cannot legally add the required citations and footnotes.

Attribute Grammars (intro) and SDD/L-attributed concepts

Core definitions via parse-tree dependencies

Let a parse tree node correspond to a grammar production:

A→X1X2…XnA \rightarrow X_1 X_2 \dots X_n

Each XiX_i has attributes; rules specify expressions to compute an attribute from other attributes.

S-attributed SDD

In an S-attributed SDD:

  • All attributes are synthesized.
  • For each node XiX_i, its attribute can be computed only from attributes of its children (i.e., information flows bottom-up).
  • No inherited attributes are allowed (or, equivalently, inherited values are never needed to compute rules).

Dependency property (informal):
A semantic rule for computing an attribute of XiX_i depends only on:

  • attributes of symbols in the subtree rooted at XiX_i.

L-attributed SDD

In an L-attributed SDD:

  • Both synthesized and inherited attributes exist.
  • Inherited attributes for XiX_i may depend only on:
    • attributes of the parent AA, and
    • attributes of symbols to the left of XiX_i (i.e., X1,…,Xi−1X_1, \dots, X_{i-1}),
  • and (crucially) not on right siblings Xi+1,…,XnX_{i+1}, \dots, X_n.

This restriction ensures that during parsing left-to-right, when you are about to process XiX_i, all permitted information is already available.

Pro Tip

If you can compute a node’s needed values using only info from already-parsed left-to-right symbols (plus parent), the SDD is a strong candidate to be L-attributed and one-pass evaluable.

Differentiation Table (What’s allowed?)

AspectS-attributed SDDL-attributed SDD
Attribute typesOnly synthesized attributesSynthesized + inherited attributes
Rule dependenciesOnly from children (bottom-up)Inherited for XiX_i can use parent and left siblings X1..Xi−1X_1..X_{i-1}
Information flowBottom-upMostly top-down/left-context + bottom-up
Typical evaluationPostorder (after children processed)Left-to-right during parsing with controlled lookahead in tree

How dependency rules affect evaluation order

  1. 1
    Step 1

    From the input, produce the parse tree according to the grammar productions.

  2. 2
    Step 2

    When visiting a node, compute its synthesized attributes only after its children have computed theirs.

  3. 3
    Step 3

    While expanding a production A→X1...XnA \rightarrow X_1...X_n, first compute inherited attributes for each XiX_i using only AA and already-processed X1..Xi−1X_1..X_{i-1}; then compute synthesized attributes bottom-up as subtrees finish.

  4. 4
    Step 4

    If a rule for XiX_i needs info from a right sibling XjX_j where j>ij>i, it breaks L-attributed eligibility.

Suitable examples

Example 1: Type annotation / expression semantics (S-attributed vs L-attributed idea)

A classic use of attributes is expression translation/type checking. Consider a simple expression grammar:

ErightarrowE1+T∣TTrightarrowid\begin{aligned} E &\\rightarrow E_1 + T \mid T \\\\ T &\\rightarrow id \end{aligned}

A “compute the value” style rule can often be synthesized:

  • For E→E1+TE \rightarrow E_1 + T, compute val(E)=val(E1)+val(T)\text{val}(E) = \text{val}(E_1) + \text{val}(T). This is S-attributed because the needed values come from children.

However, if you introduce an inherited environment (like symbol-table scopes) to decide idid types or declarations, you may need inherited attributes:

  • let env(E)\text{env}(E) or env(T)\text{env}(T) be passed from parent (and possibly from left siblings in longer productions).

That inherited flow is where L-attributed becomes necessary: it permits inherited attributes, but restricts their dependencies to parent and left siblings.


Example 2: Argument counting or context-sensitive computation in productions

Suppose we have a production that processes a list left-to-right:

ListrightarrowtextList;,;textItem;;mid;textItem\text{List} \\rightarrow \\text{List} \\; , \\; \\text{Item} \\;\\;\\mid\\; \\text{Item}

If you define:

  • count(List)\text{count}(\text{List}) as the number of items, then you can compute it by:
  • synthesized rules: count(List,;,;textItem)=count(List)+1\text{count}(\text{List}, \\; ,\\; \\text{Item}) = \text{count}(\text{List}) + 1

This is naturally S-attributed.

But if instead you want to compute, for example, the “index position” or whether an item is preceded by a comma in a way that depends on earlier context within a longer production ArightarrowX1X2X3A \\rightarrow X_1 X_2 X_3, you may need inherited attributes to pass computed context into later symbols—yet only from the parent/left side to remain L-attributed.

FAQ / Edge Cases

Evaluation Strategy Implications

Compute after children

S-attributed

Pure bottom-up synthesis; semantic rules fire when subtree results are ready."

Compute with left context

L-attributed

Inherited values are computed using parent and already-processed left siblings during expansion."

Dependency Restrictions (Allowed vs Not Allowed)

Qualitative view of what inherited rules may depend on for computing attributes of X_i

S-attributed vs L-attributed Quick Checks

1 / 4
Question · Term

S-attributed SDD: where can a semantic rule get info?

Click to reveal
Answer · Definition

Only from synthesized results of children (bottom-up). No inherited dependency is needed/allowed.

Knowledge Check

Question 1 of 4
Q1Single choice

In an S-attributed SDD, an attribute of a symbol X can be computed using dependencies on which nodes?