Past exam question: 2073 Bhadra (Regular), Q2(b)
Open in the Padhaiko appQuestion
Show status of stack while converting following infix expression to postfix expression: A+B-(C*D/E+F)-G*H.
Solution
Operator Precedence Rules: *, / +, - (all left-associative).
Tabular Stack Trace:
| Symbol | Stack (Bottom Top) | Postfix Output | Action |
|---|---|---|---|
A | (empty) | A | Output A |
+ | + | A | Push + |
B | + | A B | Output B |
- | - | A B + | Pop +, push - |
( | - ( | A B + | Push ( |
C | - ( | A B + C | Output C |
* | - ( * | A B + C | Push * |
D | - ( * | A B + C D | Output D |
/ | - ( / | A B + C D * | Pop *, push / |
E | - ( / | A B + C D * E | Output E |
+ | - ( + | A B + C D * E / | Pop /, push + |
F | - ( + | A B + C D * E / F | Output F |
) | - | A B + C D * E / F + | Pop +, discard ( |
- | - | A B + C D * E / F + - | Pop -, push - |
G | - | A B + C D * E / F + - G | Output G |
* | - * | A B + C D * E / F + - G | Push * |
H | - * | A B + C D * E / F + - G H | Output H |
| (end) | (empty) | A B + C D * E / F + - G H * - | Pop *, - |
Answer:
Converted postfix expression: A B + C D * E / F + - G H * - (AB+CD*E/F+-GH*-).