Differentiate Between S-attributed SDT and L-attributed SDT (With Examples)

Differentiate Between S-attributed SDT and L-attributed SDT (With Examples)

Verified Sources
Nov 4, 2026

Syntax-directed translation (SDT) associates semantic actions with grammar productions, using attributes on grammar symbols. Two important restrictions on attribute dependencies are S-attributed and L-attributed forms, which determine whether translation can be performed in a single left-to-right pass with a parser.

In an S-attributed SDT, all inherited attributes (if any) of a production’s symbols may depend only on:

  1. attributes of the production head (the left-hand side nonterminal), and
  2. attributes of symbols to the left in the production’s right-hand side (including the case where a symbol’s inherited attribute depends on multiple earlier siblings).

In an L-attributed SDT, inherited attributes have an even stricter dependency: each inherited attribute of a symbol may depend only on:

  1. attributes of the immediately preceding grammar symbols (through “left-to-right” accessibility), and
  2. attributes of the production head, but still only in a way that enables left-to-right evaluation.

A key relationship (often stated in compiler-construction texts) is that every L-attributed SDT is S-attributed, but not every S-attributed SDT is L-attributed—because S-attributed allows some dependency patterns that break left-to-right evaluation.

Syntax-Directed Translation: L-attributed vs S-attributed (concept overview)

Core Definitions and Dependency Rules

keyword

keyword

keyword

keyword

We consider a production like:

A→X1 X2 … XnA \rightarrow X_1\,X_2\,\dots\,X_n

Let:

  • AA be the head (left-hand side),
  • X1,…,XnX_1,\dots,X_n be the body (right-hand side symbols).
  • Each symbol may carry inherited attributes (typically written as Xi.inhX_i.inh) and synthesized attributes (typically Xi.synX_i.syn).

S-attributed SDT

For any production A→X1…XnA \rightarrow X_1 \dots X_n, the SDT is S-attributed if:

  • Any inherited attribute of XiX_i depends only on:
    • attributes of AA (the head), and
    • attributes of symbols X1,…,Xi−1X_1,\dots,X_{i-1} (symbols to the left of XiX_i).
  • Any synthesized attribute can depend on the children’s attributes in the usual upward way (there’s no special left-to-right restriction on synthesized attributes; the main restriction is about inherited dependencies).

L-attributed SDT

For the same production, the SDT is L-attributed if:

  • The inherited attributes can be evaluated in a left-to-right manner such that when you reach XiX_i you already know all values required to compute XiX_i’s inherited attributes.
  • Formally, inherited dependencies are constrained so that XiX_i can use information that has been computed by the parser so far (from the left side and the head), enabling a single-pass evaluation strategy (typical with LR/LL parsing variants under constraints).

Practical intuition

If your inherited attribute dependencies can be satisfied while scanning the RHS from left to right, you’re likely in L-attributed territory. If dependencies only require “some left context” but not necessarily the immediate left-to-right computability, you may still be S-attributed.

How to test if an SDT is S-attributed vs L-attributed (dependency check)

  1. 1
    Step 1

    List every inherited attribute dependency as “Xk.attr depends on (set of attributes)”.

  2. 2
    Step 2

    Verify every inherited attribute of Xi depends only on attributes of A and attributes of X1…X(i−1). If any inherited dependency uses attributes of X(i+1)…Xn, it violates S-attributed.

  3. 3
    Step 3

    For each inherited attribute of Xi, ensure the required attributes are available when Xi is processed in a left-to-right traversal. If an inherited attribute needs info that can only be computed from a symbol to the right, then it’s not L-attributed (even if it’s still S-attributed).

  4. 4
    Step 4

    If you can show an evaluation ordering where LTR fails (because an inherited attribute needs a future value), then the SDT is not L-attributed; otherwise it is.

Example 1: An L-attributed SDT (therefore also S-attributed)

Consider building the string representation with parentheses removal, or more standardly: computing type checking or constant folding with inherited context from the left.

A classic, simple L-attributed example is computing the prefix-based inherited context such as “the value of a variable’s scope to the left” in a left-to-right grammar. Here’s a compact SDT pattern for computing an expression’s value under operator precedence using synthesized attributes and a left-to-right inherited setup for something like an accumulated result.

Example grammar (toy)

Assume a grammar for left-associative addition:

E→E+T∣TE \rightarrow E + T \mid T

Use synthesized attributes for EE (common and doesn’t violate either restriction). To illustrate inheritance, consider:

E→A  BE \rightarrow A\;B

where AA carries a starting inherited value and BB uses it.

SDT sketch (L-attributed)

Let:

  • Each nonterminal SS carry synthesized attribute val.
  • For an inherited attribute, let AA compute something that must be available for the next symbol.

For production:

S→A  BS \rightarrow A\;B

Let:

  • AA has no inherited attribute,
  • B.inhB.inh depends only on S.synS.syn (head) and attributes from AA (left sibling).

Because BB’s inherited information depends only on values known after processing AA (left-to-right), this is consistent with L-attributed constraints.

Dependency reasoning

  • Inherited for BB uses only: head SS and left symbol AA.
  • Therefore it is allowed for S-attributed, and importantly computable left-to-right ⇒ L-attributed.

Example 2: S-attributed but NOT L-attributed

To show S-attributed but not L-attributed, we need an SDT where:

  • inherited attributes satisfy the “only head + left symbols” rule (so it is S-attributed),
  • but the particular dependency requires an evaluation order that cannot be produced by left-to-right attribute evaluation.

Key idea

L-attributed SDTs require that when visiting XiX_i left-to-right, all attributes needed for Xi.inhX_i.inh are already known. If an inherited attribute of XiX_i depends on a value that, although ultimately coming from the left side, is only computable using information that would require completing evaluation of later parts, then L-attributed fails but S-attributed might still hold structurally.

Concrete SDT pattern (conceptual)

Consider a grammar production:

A→B  C  DA \rightarrow B\;C\;D

Define:

  • BB has synthesized attribute bb
  • CC has inherited attribute cinhc_{inh}
  • DD provides some information needed to compute bb (but bb is synthesized of BB that depends on DD through its internal subtree evaluation).

Now impose:

  • cinhc_{inh} depends on bb and A.inhA.inh (head + left sibling)
    So it remains within S-attributed rules: C.inhC.inh depends on head AA and left symbol BB. But:
  • if bb itself requires completion of DD’s subtree (even though BB is left of CC), then when parsing left-to-right you cannot compute cinhc_{inh} at the moment you reach CC—because bb is not yet available.

This yields S-attributed (dependency graph respects head+left only) but violates the left-to-right evaluation requirement of L-attributed.

Why this breaks L-attributed evaluation

  • In a left-to-right evaluation, by the time you reach CC, you would need bb.
  • But bb cannot be computed until after evaluating DD’s subtree, creating a “future dependency”.
  • L-attributed SDTs are designed to prevent exactly this sort of computability issue for inherited attributes.

Summary of Differences (What to remember)

keyword

keyword

keyword

keyword

AspectS-attributed SDTL-attributed SDT
Inherited dependenciesOnly depend on head AA and symbols left of XiX_iSame spirit, but restricted to ensure left-to-right computability
Evaluation strategyMay require more flexible evaluation orderSupports evaluation in left-to-right manner
Expressive powerMore general than L-attributedStrict subset of S-attributed
RelationshipCan include cases not computable in one left-to-right passEvery L-attributed SDT is S-attributed

Common Exam/Practice Questions

Quick Check: S-attributed vs L-attributed

1 / 4
Question · Term

In S-attributed SDT, what can Xi.inh depend on?

Click to reveal
Answer · Definition

Only on attributes of the production head A and symbols left of Xi: X1…X(i−1).

Expressive Constraints (Qualitative)

Higher indicates stricter left-to-right constraint (not runtime cost).

Knowledge Check

Question 1 of 3
Q1Single choice

Which dependency restriction defines an S-attributed SDT?