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.

Syntax-directed definitions and translation schemes

Clear, exam-focused notes on syntax-directed definitions (SDDs) and translation schemes: definitions, dependency graphs, evaluation order, S- and L-attributed subsets, and worked examples (value computation, postfix translation, inherited attributes).

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

Compilers must attach meanings (semantics) to source programs while parsing syntax. Syntax-directed definitions and translation schemes are the standard, structured way to specify how to compute values, types, intermediate code or output (like postfix) from a parse. Engineers use these when designing the semantic phase of front ends, when embedding semantic actions in parser generators, and when proving evaluation order for attributes.

Key ideas

Start with clear vocabulary. I define each term before using it.

  • Grammar: a set of productions that define the syntactic structure of a language. Example symbol names: nonterminals (like E, T), terminals (like +, id), start symbol.

  • Parse tree: a tree whose interior nodes are nonterminals and leaves are terminals/ε, showing one application of grammar rules that generates the input string.

  • Attribute: a name attached to a grammar symbol instance in a parse tree that holds semantic information (a value, type, code, etc.). There are two important kinds:

    • Synthesized attribute: an attribute whose value at a node is computed from attribute values of that node's children in the parse tree.
    • Inherited attribute: an attribute whose value at a node is computed from attributes of the node's parent or siblings (left or right) in the parse tree.
  • Syntax-directed definition (SDD): a grammar plus a set of semantic rules (functions) that specify how to compute attributes for each production. Precisely: for every production A -> X1 X2 ... Xn, the SDD gives rules of the form Xi.a := f(...), or A.a := g(...), where f,g use attributes of the symbols Xj appearing on the right-hand side and possibly attributes of A.

  • Dependency graph: for a given parse tree and SDD, construct a directed graph whose nodes are attribute instances (for every occurrence of a symbol in the tree and every attribute of that symbol). Draw an edge from attribute p to attribute q if the semantic rule computing q uses the value of p. Acyclic dependency graph means attributes can be evaluated.

  • Evaluation order: any topological order of the dependency graph yields an order to compute attributes without violating dependencies. If the dependency graph has a cycle, the SDD is not well-defined for that tree (attributes cannot be evaluated without extra constraints).

  • Translation scheme: a grammar where semantic actions (fragments of code) are embedded at certain positions inside productions. Actions are executed during parsing; the placement controls when (in parse-order) they run. Translation schemes are an operational way to implement SDDs.

  • S-attributed definition: an SDD that uses only synthesized attributes. Theorem (exact statement): "If an SDD uses only synthesized attributes, then for any parse tree the attribute values can be computed in a single postorder traversal of the parse tree." Assumption: semantic rules for each node depend only on children's attributes. Proof idea: dependence edges point from children to parent only; a postorder visits children before parent, so computing children's attributes before parent's satisfies all dependencies.

  • L-attributed definition: an SDD where each inherited attribute of a symbol Xj in production A -> X1 X2 ... Xn depends only on

    1. attributes of A (the parent), and
    2. attributes of X1, X2, ..., X_{j-1} (left siblings) and not on any attributes of X_{j+1} ... Xn. Theorem (exact statement): "Attributes of an L-attributed SDD can be evaluated in one left-to-right depth-first traversal of the parse tree." Assumptions: the traversal visits nodes in top-down, left-to-right order, computing inherited attributes before proceeding to children.
  • Practical point: to implement SDDs in real parsers you map semantic rules to actions in a translation scheme. For top-down parsers (LL), put actions where needed to ensure values are available when child nonterminals are expanded. For bottom-up parsers (LR), actions run at reduce time; place actions so that when a reduction occurs, necessary right-hand-side attributes are available.

What to draw (exam diagram instructions):

  • Parse tree: draw a rooted tree. Label each node with its grammar symbol and next to it list its attributes (e.g., E.val). Mark leaf terminals with their lexeme and any token attributes.
  • Dependency graph: for the same tree, draw a node for each attribute instance (e.g., node E1.val, T2.val). Draw directed edges p -> q where q's semantic rule reads p. Label edges with the rule name if helpful.

Formulas

For this programming topic, the items to remember are the syntactic forms of productions and semantic rules:

A -> X1 X2 ... Xn — a grammar production; A, Xi are symbols.

A.a := f(X1.b, X3.c, ...) — a semantic rule assigning attribute a of A from attributes of RHS symbols. a,b,c are attribute names; f is any computable function.

Xi.d := g(A.e, X1.f, ..., X_{i-1}.h) — an inherited attribute rule for symbol Xi in position i, using parent's and left siblings' attributes.

A -> X1 { action } X2 — a translation scheme production: the action is executed when the parser reaches the position of the action during derivation.

Topological order condition (informal): if there is an edge u -> v in dependency graph, then u appears before v in any valid evaluation order.

Worked examples

Example 1 — Value computation (synthesized attributes only) Grammar (classic arithmetic): E -> E + T | T T -> T * F | F F -> ( E ) | id SDD: every nonterminal has a synthesized attribute val. Semantic rules (for productions): E -> E1 + T : E.val := E1.val + T.val E -> T : E.val := T.val T -> T1 * F : T.val := T1.val * F.val T -> F : T.val := F.val F -> id : F.val := id.lexval (lexval is given by lexer) F -> ( E ) : F.val := E.val

Input tokens with lexvals: id1=2, id2=3, id3=4, source: id1 + id2 * id3

Step-by-step (build parse tree, then evaluate in postorder):

  1. Parse tree: root E; left child E->T->F->id1, plus operator, right child T->T*F where T->F->id2 and F->id3.
  2. Postorder evaluation order of nodes: id1, F(id1), T(F->id1), E(left), id2, F(id2), T(left of *), id3, F(id3), T(right of ), T(), E(root).
  3. Compute values:
    • F(id1).val = 2
    • T(left).val = 2
    • E(left).val = 2
    • F(id2).val = 3
    • T(left of *).val = 3
    • F(id3).val = 4
    • T(right of *).val = 3 * 4 = 12
    • T(root-right).val = 12
    • E(root).val = 2 + 12 = 14 Final answer: E.val = 14.

Example 2 — Postfix translation (translation scheme) Goal: translate infix expression to postfix (RPN) while parsing. Grammar and actions (translation scheme): E -> E + T { print('+') } | T T -> T * F { print('*') } | F F -> ( E ) | id { print(id.lexeme) }

Trace on input id1 + id2 * id3 (same lexemes):

  1. When F -> id1 reduces, action prints id1.
  2. While reducing the right-hand side of the multiplication, actions print id2 (at F -> id) then id3 (at F -> id), and then at the reduction of T -> T * F print *.
  3. Finally, at reduce of E -> E + T, print +. Output sequence (printed in order actions executed): id1 id2 id3 * + (postfix). Final output: id1 id2 id3 * +.

Example 3 — Inherited attributes (L-attributed example for declarations) Grammar: D -> T L T -> int | float L -> L , id | id We want to attach the type (from T) as an inherited attribute to each id in L so that we can enter them in symbol table with their type. Attributes: T.typ (synthesized), L.in (inherited), id.entry (use L.in) Rules: T -> int : T.typ := "int" T -> float : T.typ := "float" D -> T L : L.in := T.typ L -> L1 , id : L1.in := L.in ; (here id.type := L.in) L -> id : (id.type := L.in) Evaluation order: when parsing D left-to-right, compute T.typ, then set L.in before processing L; then L passes L.in to leftmost occurrences and sets id types. Result: each id gets the type from T and can be inserted into symbol table.

Common mistakes

  • Assuming postorder always works: postorder works only for S-attributed SDDs (synthesized only).
  • Placing semantic actions at the wrong position in a translation scheme: for LL parsers actions must precede the nonterminal that needs the inherited info; for LR parsers actions at reduce time cannot access sibling attributes that haven’t been reduced.
  • Not checking cycles in the dependency graph — leads to undefined evaluation.

For your exam

Types of questions you will get:

  • Definitions and short notes: define SDD, synthesized/inherited attributes, dependency graph, translation scheme, S-attributed, L-attributed. A full-marks answer gives the formal definition, a short example, and the evaluation-order theorem statement.
  • Derivations/proofs: prove the postorder theorem for S-attributed SDDs or show why L-attributed SDDs can be evaluated in left-to-right depth-first order. A full answer states assumptions, constructs the dependency graph, and shows that traversal order respects edges.
  • Diagrams: draw a parse tree with attributes and its dependency graph; annotate an evaluation order. A full answer must show the parse tree, list attribute nodes, draw directed edges, and provide a topological order.
  • Algorithmic traces: given an SDD or translation scheme and an input, produce attribute values or output (postfix) step-by-step. Full answer shows parse steps, where actions run, and the final attribute values or output.
  • Implementation questions: where to place actions for LL or LR parsers; explain why some placements fail. Full answer connects position of action to when attributes are available.

Quick check

  1. What is a dependency graph?
  2. State the condition that makes an SDD S-attributed.
  3. Where should you place an action to print a synthesized value of a reduced nonterminal in an LR parser?

Answers: 1) A directed graph whose nodes are attribute instances in a parse tree and edges show "used by" dependencies; 2) All attributes are synthesized (no inherited attributes); 3) Place the action in the production so it runs at the reduce of that production (reduce-time action), i.e., immediately after the RHS is reduced to the LHS.

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

Stuck on something here?