Compiler Design
Syntax Directed Translation (SDT) / SDT Trace: Infix to Postfix
SDT Trace: Infix to Postfix
Let us trace how an infix expression is converted into postfix notation during syntax analysis using embedded actions.
SDT Rules for Postfix Translation
| Production | Embedded Action |
|---|---|
| E → E_1 + T | { print('+') } |
| E → T | no action |
| T → T_1 * F | { print('*') } |
| T → F | no action |
| F → ( E ) | no action |
| F → id | { print(id.name) } |
| F → num | { print(num.val) } |
Step-by-Step Execution for: a + b * c
Below is the step-by-step reduction trace demonstrating when each print statement is executed:
- First reduction:
F → id(a).
Action executed:print(a).
Output so far:a. - Promote to term:
T → F(No action). - Promote to expression:
E → T(No action). - Second reduction:
F → id(b).
Action executed:print(b).
Output so far:a b. - Promote to left factor:
T_1 → F(No action). - Third reduction:
F → id(c).
Action executed:print(c).
Output so far:a b c. - Product reduction:
T → T_1 * F.
Action executed:print(*).
Output so far:a b c *. - Sum reduction:
E → E_1 + T.
Action executed:print(+).
Final Output:a b c * +.
The numbered circles indicate the order in which nodes are reduced and their semantic actions are triggered:
E [8: print('+')]
/ | \
E + T [7: print('*')]
| / | \
T T * F [6: print('c')]
| | |
F F id(c)
| |
id(a) id(b)
[1:a] [4:b]
Reductions occur in this order:
1. F -> id(a) [Prints 'a']
2. T -> F
3. E -> T
4. F -> id(b) [Prints 'b']
5. T -> F
6. F -> id(c) [Prints 'c']
7. T -> T * F [Prints '*']
8. E -> E + T [Prints '+']