Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

S-attributed and L-attributed definitions

S-attributed definitions use only synthesized attributes and fit bottom-up evaluation; L-attributed definitions allow certain inherited attributes and can be evaluated left-to-right, so they are suitable for recursive-descent (top-down) translators and some one-pass implementations.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two sentences: Compiler writers use S-attributed and L-attributed definitions to attach semantic actions to grammar productions so translation (type checks, expression evaluation, code generation, etc.) can be done in a single pass over the parse tree or while parsing. Choosing the right class (S- or L-attributed) tells you whether the translations can be implemented naturally in a bottom-up parser (LR) or as parameters/returns in a top-down parser (recursive descent / LL).

Key ideas

Start by defining the terms used.

  • Attribute: a named value associated with a grammar symbol in a parse tree (example: the numeric value of an expression node). Attributes hold semantic information.
  • Synthesized attribute: an attribute of a node that is computed from the attribute values of that node’s children in the parse tree. Notation: for a node N representing symbol A, a synthesized attribute is written A.s.
  • Inherited attribute: an attribute of a node that is computed from attributes of the node’s parent or siblings (left siblings typically), i.e., information flows down or across the tree. Notation: A.i.
  • Attribute grammar: a context-free grammar plus a set of semantic rules that define how to compute attributes for each production.
  • Dependency graph: for a given parse tree, a directed graph whose nodes are individual attribute instances; there is an edge X -> Y if the semantic rule computing Y uses X. A cycle in this graph means attributes cannot be evaluated in any order.

Exact definitions used in exams:

  • Definition (S-attributed grammar): An attribute grammar is S-attributed iff every attribute in the grammar is a synthesized attribute (no inherited attributes appear anywhere). Assumption: semantic rules refer only to children and compute values to pass up to parent.

  • Definition (L-attributed grammar): An attribute grammar is L-attributed iff for every production A → X1 X2 ... Xn, each inherited attribute of Xj may depend only on: attributes of A (the parent), on attributes of X1, X2, ..., Xj−1 (the symbols to the left of Xj), and at most on inherited attributes of Xj itself; it must not depend on synthesized attributes of Xj nor on any attributes of Xk for k > j. Synthesized attributes of Xj may depend on any attributes of Xj’s children (normal synthesized rules). Assumption: evaluation proceeds in left-to-right order of children.

Why these definitions matter (derivations commonly required in exams):

  1. S-attributed grammars are evaluable during bottom-up parsing (LR family). Proof sketch: bottom-up parsing reduces a right-hand side to its left-hand side only when children subtrees are fully built; since synthesized attributes only require children, you can compute them at the moment of reduction. Show sequence: build subtree for RHS (children available) → apply semantic rules (compute synthesized attributes) → reduce to parent symbol with its synthesized attribute ready.

  2. L-attributed grammars are evaluable in a single left-to-right traversal of the parse tree. Proof sketch: inheritance for Xj depends only on parent and left siblings which are already visited in a left-to-right traversal; so if you (a) visit parent and children left-to-right and (b) compute each child’s inherited attributes when you begin that child, you can compute all needed inherited attributes before computing synthesized ones.

Implementation consequences (practical rules to remember):

  • Use S-attributed definitions when you want easy bottom-up implementation (postfix generation, many LR-based code emission schemes).
  • Use L-attributed definitions when you want to implement semantic actions as parameters and return values in recursive-descent (top-down) parsers. Also, every S-attributed grammar is L-attributed (a special case with no inherited attributes).
  • Both S- and L-attributed grammars must be acyclic in their dependency graphs for a particular parse tree; otherwise attributes cannot be evaluated.

What to draw in an exam (diagram instructions):

  • Draw a small parse tree (label nodes with grammar symbols). Under each node draw its attributes (e.g., .val, .post). Draw dependency arrows from attribute instances used as inputs to the attribute instance they compute. To prove S-attributed evaluability by bottom-up, show arrow directions go from children up to parent; for L-attributed, show arrows from parent and left siblings to right siblings.

Formulas

A → α — production: nonterminal A produces string α. (A, α are grammar symbols; no units.)

X.attr = f(Y1.attr, Y2.attr, ...) — generic semantic rule: X.attr is computed by function f from listed attribute values. (attr: attribute name; applies in semantic rules in attribute grammars.)

For L-attributed condition (formal statement): For production A → X1 X2 ... Xn, each inherited attribute Xj.inh may depend only on: A.* and X1.* ... X_{j-1}.* and (optionally) on inherited attributes of Xj; it must not depend on synthesized attributes of Xj nor on any attribute of Xk for k > j. — A.* denotes any attribute of A, Xi.* denotes any attribute of Xi. (Applies when deciding if grammar is L-attributed.)

Notation to remember for annotated translations (translation scheme syntax): A → { action0 } X1 { action1 } X2 { action2 } ... Xn { actionn } — action0 executes before parsing X1; actioni executes immediately after Xi is parsed. Place actions so inherited attributes of the next symbol are computed before that symbol is parsed (typical for L-attributed schemes). Applies to translation schemes embedded in grammar.

Worked examples

Example 1 — S-attributed: generate postfix (Reverse Polish) for arithmetic expressions using bottom-up evaluation. Grammar (left-recursive; OK for bottom-up):

  1. E → E '+' T { E.post = E1.post T.post '+' }
  2. E → T { E.post = T.post }
  3. T → T '' F { T.post = T1.post F.post '' }
  4. T → F { T.post = F.post }
  5. F → num { F.post = num.lexeme }
  6. F → '(' E ')' { F.post = E.post }

Task: input tokens num(3) + num(4) * num(5). Show bottom-up reductions and synthesized values. Steps (reductions):

  1. Reduce num(3) to F: F.post = "3".
  2. Reduce F to T (rule 4): T.post = "3".
  3. Reduce T to E (rule 2): E.post = "3".
  4. Next token is '+', then num(4). Reduce num(4) -> F: F.post = "4".
  5. Next token '' means T → T '' F must apply later; reduce F->T (rule 4): T.post = "4".
  6. Next token '*' and then num(5): reduce num(5) -> F: F.post = "5".
  7. Reduce T -> T '' F (rule 3) on T(post="4") '' F(post="5"): T.post = "4 5 *".
  8. Now we have E(post="3") '+' T(post="4 5 *"): reduce E → E '+' T (rule 1): E.post = "3 4 5 * +". Final postfix: 3 4 5 * +.

This example uses only synthesized attributes (E.post, T.post, F.post) and was evaluated at each reduction step in bottom-up parse order.

Example 2 — L-attributed: left-to-right evaluation of value for expression with standard precedence and associativity. Use grammar suited for top-down parsing: Productions and semantic rules (common textbook form):

  1. E → T E' { E'.inh = T.val; E.val = E'.syn }
  2. E' → '+' T E1' { E1'.inh = E'.inh + T.val; E'.syn = E1'.syn }
  3. E' → ε { E'.syn = E'.inh }
  4. T → F T' { T'.inh = F.val; T.val = T'.syn }
  5. T' → '*' F T1' { T1'.inh = T'.inh * F.val; T'.syn = T1'.syn }
  6. T' → ε { T'.syn = T'.inh }
  7. F → num { F.val = num.lexval }
  8. F → '(' E ')' { F.val = E.val }

Trace for input 3 + 4 * 5 (tokens: num(3) + num(4) * num(5)) — show inherited/synthesized flow:

  1. Parse F -> num(3): F.val = 3. Then T'.inh = F.val = 3.
  2. T' sees next token '+', so T' -> ε: T'.syn = T'.inh = 3; then T.val = 3.
  3. E' inh: E'.inh = T.val = 3.
  4. E' -> '+' T E1': parse T for the part after '+'. For this T, F -> num(4): F.val = 4; T'.inh = 4.
  5. For that T', next token is '', so T' -> '' F T1': F -> num(5) gives F.val = 5; then T1'.inh = T'.inh * F.val = 4 * 5 = 20; T1' -> ε so T1'.syn = 20; thus T'.syn = 20; so T.val = 20.
  6. Back to E' semantic action: E1'.inh = E'.inh + T.val = 3 + 20 = 23.
  7. E1' -> ε so E1'.syn = 23; then E'.syn = E1'.syn = 23; finally E.val = E'.syn = 23. Final numeric value: 23.

This example uses inherited attributes (E'.inh, T'.inh) only from parent/left siblings, satisfying the L-attributed condition. It is evaluable in one left-to-right pass and maps to passing parameters to recursive calls in a top-down parser.

Common mistakes

  • Assuming L-attributed grammars always work with bottom-up parsers without extra machinery. Bottom-up parsers can implement L-attributed actions but often require extra temporaries or two-pass algorithms; do not claim free compatibility.
  • Forgetting to ensure acyclicity of dependency graph for a particular parse tree. Even if grammar is L- or S-attributed, circular dependencies in semantic rules can make evaluation impossible.
  • Using left-recursive grammars in recursive-descent without rewriting. Left recursion is language/grammar-level; attribute classification doesn't remove left recursion problems for top-down parsers.

For your exam

Kinds of questions you will be asked:

  • Definitions: define synthesized/inherited attributes, S-attributed and L-attributed grammars and state the L-attributed condition precisely.
  • Short notes: compare S- and L-attributed definitions, when to use which, and relation to LR and LL parsing.
  • Derivations/proofs: show why S-attributed grammars are suitable for bottom-up or why L-attributed can be implemented left-to-right; draw dependency graph and argue acyclicity.
  • Programs/translation schemes: write semantic rules (translation scheme) for expression evaluation or code generation, either S- or L-attributed, and annotate how they execute in a parser.
  • Traces: given an annotated grammar and an input, show step-by-step attribute evaluation and final result.

What a full-marks answer includes:

  • Precise definitions with the exact L-attributed condition.
  • Clear explanation of assumptions (parse tree exists, dependency graph notion).
  • One small diagram of a parse tree with attribute dependencies drawn and labeled, or a step-by-step trace of attribute evaluation on an input showing values at each node.
  • If asked for implementation, show where semantic actions run in the parser (at reductions for bottom-up; as parameter passing/returns or in-line actions for top-down).

Quick check

  1. What is a synthesized attribute? 2) State the L-attributed condition for a production A → X1 ... Xn. 3) Can any S-attributed grammar be evaluated during LR parsing? Answers: 1) An attribute computed from values of a node’s children and passed up. 2) Each inherited attribute of Xj may depend only on attributes of A and on attributes of X1..Xj−1 (left siblings). 3) Yes — S-attributed grammars can be evaluated during bottom-up (LR) parsing because reductions occur after children subtrees are built.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?