1. Gyanastra
  2. Compiler Design
  3. Syntax Directed Translation Schemes
  4. Worked Example: Evaluating an Expression by Traversing the Parse Tree and Applying Semantic Actions
Free lesson, no account needed 7 min read 15 practice questions after it
Asked in past papers5 times
  • GATE CSE20242023202220142004

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 2+3×42 + 3 \times 4 to 1414.

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 number are tokens; their lexical values are 2, 3 and 4.
  • Reduction: a right-hand side being replaced by a non-terminal, e.g. number reduced to F. 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 F is value. 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 * F or E -> 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.
Figure. Traverse bottom-up left to right. The leaves give 2, 3 and 4; the T node computes 3 times 4 as 12; the root E adds 2 and 12 to give 14.

How it works

  1. Derive the parse tree for the input string.
  2. Traverse the parse tree bottom-up (post-order), left to right.
  3. 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: F.value=number.lexical valueF.value = number.lexical\ value

  • T -> F: T.value=F.valueT.value = F.value

  • E -> T: E.value=T.valueE.value = T.value

  • T -> T * F: T.value=T1.value×F.valueT.value = T_1.value \times F.value

  • E -> E + T: E.value=E1.value+T.valueE.value = E_1.value + T.value

  • Parse tree for 2 + 3 * 4

  • 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: 2+3×42 + 3 \times 4. Expected answer: 1414 (3×4=123 \times 4 = 12, plus 22).

Step 1: Derive the parse tree

Grammar productions used:

  • E -> E + T
  • E -> T
  • T -> T * F
  • T -> F
  • F -> 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

StepReductionProductionSemantic actionValue computed
1number → FF → numberF.value = number.lexical valueF.value = 2
2F → TT → FT.value = F.valueT.value = 2
3T → EE → TE.value = T.valueE.value = 2 (this 2 reaches the left E and waits there)
—E + T seen—E + T cannot reduce to E, so no action—
4number → FF → numberF.value = number.lexical valueF.value = 3
5F → TT → FT.value = F.valueT.value = 3
6number → FF → numberF.value = number.lexical valueF.value = 4
7T * F → TT → T * FT.value = T1.value * F.valueT.value = 3 * 4 = 12
8E + T → EE → E + TE.value = E1.value + T.valueE.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 T.value=T1.value×F.value=3×4=12T.value = T_1.value \times F.value = 3 \times 4 = 12.
  • Step 8: the production is E -> E + T, so the action is E.value=E1.value+T.value=2+12=14E.value = E_1.value + T.value = 2 + 12 = 14. The two E's are distinguished by writing E1 for the right-hand-side E.

Final output of the SDT on input 2+3×42 + 3 \times 4 is 1414. 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 number carry lexical values; attributes such as F.value carry 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 + T cannot reduce to E at 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
Pages 1–2 of 2