
(in simple language with definitions, steps, diagrams, examples, and key exam points)
(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 view and download past papers
Click on Show Past Papers to load available exam papers.