Shift Reduce Parsing/Bottom Up Parsing Simulator
Enter a grammar, check whether it is ambiguous, then parse a string with shift and reduce actions step by step while the parse tree grows from the bottom up.
1. Grammar
One rule per line. Use -> or →, ε for an empty right side, and | for alternatives. The first rule's left side is the start symbol.
- 1E → E + T
- 2E → T
- 3T → T * F
- 4T → F
- 5F → ( E )
- 6F → id
2. Input string
Separate tokens with spaces, or write them together when they are unambiguous (x+y*z). Any variable name (x, count, total_2) is read as the terminal id.
3. Shift-reduce simulation
Move the timeline to reveal one more row of the table and update the tree. Click any row to see the tree at that step.
Parsing table
| Row | Stack | Input | Action |
|---|---|---|---|
| 0 | $ | id + id * id $ | Shift id |
Bottom-up parse tree
Row 0: shift "id" from the input onto the stack (dashed orange).
Drag to move · scroll to zoom
The tree is built from the leaves up: each shift adds a leaf, each reduce joins the handle under a new parent. Dashed tokens are still in the input. Orange = what this row acts on, green = just created.
How shift-reduce parsing works
- Stack and input. The stack starts as $ and the input ends with $. The parser repeatedly either shifts the next input token onto the stack or reduces a handle on top of the stack.
- Handle. A handle is the right side of a rule, on top of the stack, whose reduction is one step of the rightmost derivation in reverse. Reducing replaces it with the rule's left side.
- Accept and reject. When the stack is $ followed by the start symbol and the input is just $, the string is accepted. If no handle can be reduced and the next token cannot be shifted, it is rejected.
- Conflicts. A shift/reduce conflict means both actions look possible; a reduce/reduce conflict means two rules could reduce. Ambiguous grammars always produce conflicts. This simulator resolves them like yacc: shift before reduce, and the earlier rule first.
- How the choice is made here. A reduction by A → β is only made when the next input symbol is in FOLLOW(A), and every stack must be a viable prefix. For an SLR(1) grammar this is exactly the SLR parser.
About This Simulation
This shift-reduce parsing visualizer shows bottom-up parsing step by step. Enter a context-free grammar, see whether it is ambiguous, then parse an input string: the table lists the stack, the remaining input and the action (shift, reduce by a numbered rule, accept or reject) on every row, and the parse tree grows from the leaves upwards as you move the timeline.
Features
- Grammar input with -> or →, ε rules and | alternatives, plus insert buttons for ε and →
- Numbered rules, used in every Reduce action
- Ambiguity check: proves SLR(1) grammars unambiguous, or shows a string with two parse trees
- Parsing table with Row, Stack, Input and Action columns
- Shift/reduce and reduce/reduce conflicts marked on the rows where they happen
- Bottom-up parse tree that grows as the timeline moves; click any row to see its tree
- Six example grammars, some ambiguous and some not
Explanation
Shift-reduce parsing is a bottom-up parsing method. The parser reads the input from left to right, pushing tokens onto a stack (shift) and replacing the right side of a rule on top of the stack with its left side (reduce). It builds the parse tree from the leaves up to the root, which traces a rightmost derivation in reverse.
How it works
- Start with $ on the stack and the input string followed by $.
- Shift: move the next input token onto the top of the stack. The token becomes a leaf of the parse tree.
- Reduce: when the top of the stack matches the right side of a rule A → β (the handle), pop β and push A. The popped symbols become the children of a new A node.
- Accept: when the stack is $ followed by the start symbol and only $ remains in the input, the string is in the language.
- Reject: when no handle can be reduced and the next token cannot be shifted, the string is not in the language.
- When both a shift and a reduce (or two reduces) are possible, the parser has a conflict. Ambiguous grammars always cause conflicts; parser generators such as yacc prefer shift over reduce, and the earlier rule in a reduce/reduce conflict.
Why it matters
Shift-reduce parsing is the basis of the LR family of parsers (LR(0), SLR, LALR and canonical LR) used by most compiler generators. Understanding handles, conflicts and ambiguity is essential in compiler design courses such as CSE420, and explains why grammars are rewritten or given precedence rules before a parser can be built.
Frequently Asked Questions
What is a handle in shift-reduce parsing?
A handle is a substring on top of the stack that matches the right side of a rule and whose reduction is one step of the rightmost derivation in reverse. Reducing the handle replaces it with the rule's left side.
What is a shift/reduce conflict?
A situation where the parser could either shift the next token or reduce the handle on the stack. It usually comes from an ambiguous grammar, such as the dangling else or E → E + E | E * E.
How can you tell if a grammar is ambiguous?
A grammar is ambiguous if some string has two different parse trees (or two different leftmost or rightmost derivations). In general this is undecidable, but every LR grammar, including every SLR(1) grammar, is unambiguous.
Why is shift-reduce parsing called bottom-up?
Because it builds the parse tree from the leaves (the input tokens) up to the root (the start symbol), unlike top-down parsing, which starts at the root.
What is the difference between shift-reduce parsing and SLR parsing?
Shift-reduce parsing is the general method. SLR, LALR and LR parsers are shift-reduce parsers that use a parsing table to decide exactly when to shift and when to reduce.
Topics covered
- Shift-Reduce Parsing
- Bottom-Up Parsing
- Handles
- Ambiguous Grammars
- Shift/Reduce Conflicts
- Parse Trees
- Rightmost Derivation
Tags
- shift reduce parser
- shift reduce parsing example
- bottom up parsing
- bottom up parser
- handle pruning
- shift reduce conflict
- ambiguous grammar checker
- parse tree generator
- compiler design
Related Simulations
Compiler Design
SLR Parser Simulator
Enter a context-free grammar and build its LR(0) automaton, FIRST and FOLLOW sets and SLR parsing table, then parse an input string step by step.
Theory of Computation: Automata and Computability
CFG (Context Free Grammar) Simulator
Interactive Context-Free Grammar parser and derivation visualizer
Artificial Intelligence
Decision Tree
Show how data is split using features.
Artificial Intelligence
Minimax Algorithm
Show game tree evaluation for two-player games.

