1. Gyanastra
  2. Compiler Design
  3. Syntax Directed Translation Schemes
  4. Worked Example: Converting Infix to Postfix with an SDT Using Top-Down and Bottom-Up Parsers
Free lesson, no account needed 8 min read 11 practice questions after it
Asked in past papers2 times
  • GATE CSE20062003

Compiler Design · Syntax Directed Translation Schemes

Worked Example: Converting Infix to Postfix with an SDT Using Top-Down and Bottom-Up Parsers

Overview

A Syntax Directed Translation (SDT) scheme attaches semantic actions to grammar productions and is executed together with the parser.
This worked example converts the infix expression 2 + 3 * 4 into postfix form, showing that the same SDT can be run by either a top-down or a bottom-up parser, differing only in when the actions fire.

Key concepts

  • SDT scheme: semantic actions attached to the productions of a grammar; here the goal is to print the postfix form, not to compute a final value (unlike the earlier evaluation example).
  • Execution with the parser: the SDT is carried out along with the syntax analyzer — while parsing, the parser performs the actions attached to the productions.
    Since there are two kinds of parsers (top-down and bottom-up), the same SDT can be executed by either.
  • Production with action: each production is treated as its right-hand side followed by a semantic action, e.g. E -> E + T followed by semantic action 1.
  • Top-down parsing: starts at the root and goes down to the children.
  • Bottom-up parsing: starts from the terminals and works up to the root, focusing on reductions (reducing right-hand sides to left-hand-side variables).

The grammar and the SDT (with named semantic actions):

ProductionSemantic action numberAction
E → E + T1printf +
E → T2nothing (empty)
T → T * F3printf *
T → F4nothing (empty)
F → number5printf number.lexicalValue
Figure. Traverse this tree top-down left to right and fire each action met; the prints give 2 3 4 * +. Bottom-up reductions fire the same actions in the same order.

How it works

  1. Write each production as its right-hand side followed by its semantic action, and number the actions (1 to 5 here).
  2. Top-down: the parser takes the SDT, constructs the parse tree, then traverses it top-down, left to right; whenever a semantic action is encountered during this traversal, perform that action (print what it says).
  3. Bottom-up: whenever a reduction is made (a right-hand side reduced to a left-hand-side variable), perform the action attached to that production.

Formulas

  • E -> E + T {action 1: printf +}

  • E -> T {action 2: empty}

  • T -> T * F {action 3: printf *}

  • T -> F {action 4: empty}

  • F -> number {action 5: printf number.lexicalValue}

  • Parse tree for 2 + 3 * 4 with actions

  • The parse tree of 2 + 3 * 4 with each production's semantic action attached, whose top-down left-to-right traversal prints 2 3 4 * +.

Worked examples

Input infix expression: 2 + 3 * 4. Goal: find its postfix expression.

Step 1: Build the parse tree with semantic actions written in place of the actions. Derivation (each production written together with its semantic action number):

  • E derives E + T, followed by semantic action 1
  • T derives T * F, followed by semantic action 3
  • E derives T, followed by semantic action 2
  • T derives F, followed by semantic action 4
  • F derives number, followed by semantic action 5
  • T derives F, followed by semantic action 4
  • F derives number, followed by semantic action 5
  • F derives number, followed by semantic action 5

The three numbers are 2, 3 and 4, so the derived expression is 2 + 3 * 4.

Step 2: Top-down evaluation (traverse top-down, left to right; perform each action met):

Order metSemantic actionAction contentOutput printed
15printf number.lexicalValue (number = 2)2
24nothing—
32nothing—
45printf number.lexicalValue (number = 3)3
54nothing—
65printf number.lexicalValue (number = 4)4
73printf **
81printf ++

Output (postfix): 2 3 4 * +

Step 3: Bottom-up evaluation (perform the action at every reduction):

ReductionProductionActionOutput printed
number → FF → numberprintf number.lexicalValue (2)2
F → TT → Fnothing—
T → EE → Tnothing—
number → FF → numberprintf number.lexicalValue (3)3
F → TT → Fnothing—
number → FF → numberprintf number.lexicalValue (4)4
T * F → TT → T * Fprintf **
E + T → EE → E + Tprintf ++

Output (postfix): 2 3 4 * +

Both the bottom-up method and the top-down method give the same output.

Comparison

AspectTop-down parserBottom-up parser
Direction of parseRoot to childrenTerminals up to root
When action is performedWhile traversing the parse tree top-down, left to right; action performed whenever encounteredWhenever a reduction is made (RHS reduced to LHS)
Basis of action executionPosition of semantic action in the tree traversalThe production used for each reduction

Revision summary

  • This SDT converts infix to postfix; the earlier example computed a value instead.
  • An SDT is executed together with the parser — actions run during parsing.
  • Treat each production as its RHS followed by a semantic action; number the actions (1 to 5 here).
  • Top-down: traverse the parse tree top-down, left to right, and perform every semantic action met.
  • Bottom-up: perform the action of a production each time that production is used in a reduction.
  • SDTs work with both bottom-up and top-down parsers; the only difference is when the actions are carried out.
  • For input 2 + 3 * 4, both methods print the postfix form 2 3 4 * +.

Previous-year exam insights

  • In an SDT the position of an action within a production determines when it fires: actions are performed in the order the parser processes the symbols of the production, so an action placed after a nonterminal fires only after that nonterminal's subtree has been completely processed (bottom-up: at/after its reduction; top-down: after it is fully expanded).
    This ordering, not the parse-tree shape alone, decides the printed output.
    Asked inGATE 2003GATE 2006
  • For E → E + T | T, T → T * F | F, F → number, an SDT that places {print +} at the end of the production (E → E + T {print +}) yields postfix output, while placing the action before the right-hand-side symbols (E → {print +} E + T style, i.e. before the operands are processed) would yield prefix order in a top-down evaluation; placing the print between the operands (E → E + {print +} T) gives the action in infix position relative to the operand outputs.
    Asked inGATE 2003GATE 2006
  • A bottom-up (LR) parser executes semantic actions at the end of a reduction, i.e., after the entire right-hand side has been matched and popped; therefore an action written in the middle of a production cannot be executed at that exact point by an LR parser unless the grammar is transformed (e.g., by introducing a new nonterminal/marker) so the action falls at the end of some production.
    Asked inGATE 2006
  • In a top-down (recursive-descent/LL) parser, semantic actions embedded anywhere in a production are executed in sequence as the parser scans the right-hand side left to right, so mid-production actions are naturally supported.
    Asked inGATE 2003
  • An SDT is not the same as an S-attributed or L-attributed SDD: an SDT only specifies where actions are placed, and its actions may print or perform side effects without computing attributes; the output depends on the parse order (top-down vs bottom-up) only if the action placement is not consistent with the parse order.
  • When tracing output for a given input, first build the parse tree/derivation for that input under the given grammar, then list the actions in the order the chosen parser fires them (left-to-right over the sentential form for LL; in reduction order for LR), and concatenate the printed symbols.
    Asked inGATE 2003GATE 2006
Pages 1–2 of 2