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 + Tfollowed 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):
| Production | Semantic action number | Action |
|---|---|---|
| E → E + T | 1 | printf + |
| E → T | 2 | nothing (empty) |
| T → T * F | 3 | printf * |
| T → F | 4 | nothing (empty) |
| F → number | 5 | printf number.lexicalValue |
Drawing the diagram…
How it works
- Write each production as its right-hand side followed by its semantic action, and number the actions (1 to 5 here).
- 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).
- 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
Drawing the diagram…
- 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 met | Semantic action | Action content | Output printed |
|---|---|---|---|
| 1 | 5 | printf number.lexicalValue (number = 2) | 2 |
| 2 | 4 | nothing | — |
| 3 | 2 | nothing | — |
| 4 | 5 | printf number.lexicalValue (number = 3) | 3 |
| 5 | 4 | nothing | — |
| 6 | 5 | printf number.lexicalValue (number = 4) | 4 |
| 7 | 3 | printf * | * |
| 8 | 1 | printf + | + |
Output (postfix): 2 3 4 * +
Step 3: Bottom-up evaluation (perform the action at every reduction):
| Reduction | Production | Action | Output printed |
|---|---|---|---|
| number → F | F → number | printf number.lexicalValue (2) | 2 |
| F → T | T → F | nothing | — |
| T → E | E → T | nothing | — |
| number → F | F → number | printf number.lexicalValue (3) | 3 |
| F → T | T → F | nothing | — |
| number → F | F → number | printf number.lexicalValue (4) | 4 |
| T * F → T | T → T * F | printf * | * |
| E + T → E | E → E + T | printf + | + |
Output (postfix): 2 3 4 * +
Both the bottom-up method and the top-down method give the same output.
Comparison
| Aspect | Top-down parser | Bottom-up parser |
|---|---|---|
| Direction of parse | Root to children | Terminals up to root |
| When action is performed | While traversing the parse tree top-down, left to right; action performed whenever encountered | Whenever a reduction is made (RHS reduced to LHS) |
| Basis of action execution | Position of semantic action in the tree traversal | The 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