(in simple language with definitions, steps, diagrams, examples, and key exam points)
Bottom-Up Parsing is a parsing technique in which we start from the input string (leaves) and gradually reduce it to the start symbol (root).
👉 It works by:
“Reducing a string to the start symbol using grammar rules.”
Input String → Reductions → Start Symbol (S)
Bottom-up parsing builds the parse tree: 👉 from leaves → root
A reduction is the process of replacing a substring of input (handle) with a non-terminal using a production rule.
E → E + T
E → T
T → id
id + id
We reduce step-by-step:
id + id
⇒ T + id (id → T)
⇒ T + T (id → T)
⇒ E + T (T → E)
⇒ E (E + T → E)
A handle is a substring that:
E → E + T
In:
E + T
👉 Handle = E + T
👉 Finding handle = core of bottom-up parsing
Shift-Reduce Parsing is a bottom-up parsing technique that uses:
Input → [Shift-Reduce Parser Stack] → Output
👉 Move input symbol to stack
Example:
Input: id + id
Stack: id
👉 Replace stack symbols using grammar rule
Example:
id → T
👉 Input successfully parsed
👉 Invalid input
E → E + T
E → T
T → id
id + id
| Step | Stack | Input | Action |
|---|---|---|---|
| 1 | id+id$ | shift | |
| 2 | id | +id$ | reduce id → T |
| 3 | T | +id$ | reduce T → E |
| 4 | E | +id$ | shift + |
| 5 | E + | id$ | shift id |
| 6 | E + id | $ | reduce id → T |
| 7 | E + T | $ | reduce E + T → E |
| 8 | E | $ | ACCEPT |
Bottom-up parsing builds:
Leaves → Root
Example:
E
/ \
E T
|
id
👉 These are handled by LR parsers
| Feature | Bottom-Up (Shift-Reduce) | Top-Down |
|---|---|---|
| Direction | Leaves → Root | Root → Leaves |
| Approach | Reduce input | Expand grammar |
| Method | Handles | Productions |
| Backtracking | Not needed | Possible |
| Power | More powerful | Less powerful |
✔ Handles larger class of grammars ✔ No backtracking ✔ Efficient parsing ✔ Used in real compilers
❌ Complex implementation ❌ Conflicts (shift/reduce) ❌ Hard to debug
👉 Very frequently asked:
| Aspect | Bottom-Up Parsing | Shift-Reduce Parsing |
|---|---|---|
| Definition | Builds tree from input | Stack-based bottom-up parsing |
| Method | Reductions | Shift + Reduce |
| Direction | Leaves → Root | Leaves → Root |
| Key Idea | Reduce string | Use stack operations |
| Components | Handles | Stack, input buffer |
| Complexity | Moderate | Moderate |
| Use | Syntax analysis | Real compiler parsing |
Open this section to load past papers