Compiler Design · Syntax Directed Translation Schemes
Worked Example: Evaluating an Expression by Traversing the Parse Tree and Applying Semantic Actions
Overview
Syntax Directed Translation (SDT) combines a grammar with semantic rules.
To evaluate an input, derive its parse tree, traverse it bottom-up (post-order, left to right), and execute the semantic action of the production used at every reduction. This example evaluates the expression to .
Key concepts
- Syntax Directed Translation (SDT): a grammar plus semantic rules.
- Lexical value: the actual value a token represents. In the parse tree, leaves labelled
numberare tokens; their lexical values are 2, 3 and 4. - Reduction: a right-hand side being replaced by a non-terminal, e.g.
numberreduced toF. At each reduction, the action of the production used is executed. - Variable: a non-terminal. In this SDT every variable gets an attribute; the attribute associated with
Fisvalue. In general, a variable may get one or more attributes, or zero attributes, depending on the type of SDT. - Subscript marker: when a production has the same non-terminal on both sides (e.g.
T -> T * ForE -> E + T), the right-hand-side occurrence is written with subscript 1 (T1, E1) only to distinguish it from the left-hand-side occurrence.
T1 is the same variable as T.
Drawing the diagram…
How it works
- Derive the parse tree for the input string.
- Traverse the parse tree bottom-up (post-order), left to right.
- Whenever there is a reduction, go to the production used for that reduction and carry out the action associated with that production.
Formulas
-
F -> number: -
T -> F: -
E -> T: -
T -> T * F: -
E -> E + T: -
Parse tree for 2 + 3 * 4
Drawing the diagram…
- The parse tree of 2 + 3 * 4 showing how the root E splits into E + T and the right T splits into T * F, with number leaves 2, 3 and 4.
Worked examples
Input expression: . Expected answer: (, plus ).
Step 1: Derive the parse tree
Grammar productions used:
E -> E + TE -> TT -> T * FT -> FF -> number
Derivation: E derives E + T; the left E derives T, T derives F, F derives number (this number is 2); the T on the right derives T * F; the left T of that derives F, F derives number (3); the F on the right derives number (4).
Parse tree described in words:
- Root E has children: E, +, T.
- Left child E has one child T; that T has one child F; that F has one child number (lexical value 2).
- Right child T has children: T, *, F.
- Its left T has one child F; that F has one child number (lexical value 3).
- Its right F has one child number (lexical value 4).
Step 2: Traverse top-down, left to right and apply actions at each reduction
| Step | Reduction | Production | Semantic action | Value computed |
|---|---|---|---|---|
| 1 | number → F | F → number | F.value = number.lexical value | F.value = 2 |
| 2 | F → T | T → F | T.value = F.value | T.value = 2 |
| 3 | T → E | E → T | E.value = T.value | E.value = 2 (this 2 reaches the left E and waits there) |
| — | E + T seen | — | E + T cannot reduce to E, so no action | — |
| 4 | number → F | F → number | F.value = number.lexical value | F.value = 3 |
| 5 | F → T | T → F | T.value = F.value | T.value = 3 |
| 6 | number → F | F → number | F.value = number.lexical value | F.value = 4 |
| 7 | T * F → T | T → T * F | T.value = T1.value * F.value | T.value = 3 * 4 = 12 |
| 8 | E + T → E | E → E + T | E.value = E1.value + T.value | E.value = 2 + 12 = 14 |
- After step 3, the value 2 has reached the expression E and is waiting there.
- Where E has seen E + T, this is not a reduction because E + T cannot reduce to E at that moment.
- Step 7: the production is
T -> T * F, so the action is . - Step 8: the production is
E -> E + T, so the action is . The two E's are distinguished by writing E1 for the right-hand-side E.
Final output of the SDT on input is . The value sequence is 2, 2, 2, 3, 3, 4, 12, 14.
Revision summary
- SDT = grammar + semantic rules.
- Build the parse tree first, then traverse it bottom-up (post-order), left to right.
- At every reduction, execute the semantic action of the production used for that reduction.
- Tokens such as
numbercarry lexical values; attributes such asF.valuecarry these values up the tree. - Not every SDT gives an attribute to every non-terminal; a variable may have one, more than one, or zero attributes depending on the type of SDT.
- Subscripts (T1, E1) are only markers distinguishing the right-hand-side occurrence; it is the same variable.
E + Tcannot reduce toEat the moment it is seen, so no action is taken there.
Previous-year exam insights
- An S-attributed definition uses only synthesized attributes and can be evaluated bottom-up during parsing (at each reduction); an L-attributed definition allows inherited attributes restricted to depend only on the parent and left siblings, and is evaluated during top-down (LL) parsing.
Asked inGATE 2024GATE 2023GATE 2022 - In a production A → X1 X2 ... Xn, a synthesized attribute of A is computed from attributes of its children; an inherited attribute of a right-hand-side symbol Xi is computed from attributes of the parent A and of the symbols to its left (X1..Xi-1).
Asked inGATE 2023GATE 2022 - Subscripts in an SDD/SDT (E1, T1, F1) merely distinguish different occurrences of the same non-terminal in one production; they are not different variables, and the left-hand-side occurrence has no subscript.
Asked inGATE 2022GATE 2004 - For an expression grammar with left-associative operators, the left-recursive form E → E + T | T, T → T * F | F, F → num gives * higher precedence than + and left associativity; the parse tree groups 2 + 3 * 4 as 2 + (3 * 4).
Asked inGATE 2004GATE 2022 - When a production's semantic rule is absent for some alternative, the attribute value is undefined for that alternative; every alternative of a non-terminal must have a rule for each attribute that is used, otherwise the SDD is inconsistent.
Asked inGATE 2024 - The value of the start symbol's attribute after the whole parse tree is evaluated is the translation (output) of the input string; intermediate attribute values at inner nodes are not the final answer.
Asked inGATE 2024GATE 2004