Compiler Design
Bottom-Up Parsing (LR)
LR parsing is the most frequently tested part of Compiler Design: item sets, state counts, conflicts and the SLR / LALR / CLR comparison. Build the automaton for one standard grammar until you can do it from memory.
GATE CSE bottom-up parsing — shift-reduce, handles, LR(0) items and automata, SLR(1), CLR(1), LALR(1) tables, conflicts, state counts and the parser hierarchy, with sample questions, flashcards and drills.
Learning outcomes
- Explain shift-reduce parsing, handles and viable prefixes
- Construct LR(0) item sets with closure and goto, and count states and items
- Build SLR(1), CLR(1) and LALR(1) tables and identify shift-reduce / reduce-reduce conflicts
- Compare the four LR parsers by power, table size and lookahead, and answer containment questions
Shift-reduce parsing and handles
- A bottom-up parser builds the parse tree from the leaves up. It reduces the input string to the start symbol by repeatedly replacing a substring that matches the right-hand side of a production with its left-hand side. This traces a RIGHTMOST derivation in reverse.
- A shift-reduce parser has a stack (grammar symbols and states) and an input buffer. Actions: SHIFT (push the next input symbol), REDUCE (pop the right-hand side of a production and push its left-hand side), ACCEPT, and ERROR.
- A HANDLE is a substring that matches the RHS of a production and whose reduction is one step of the reverse rightmost derivation. Handle pruning — repeatedly finding and reducing the handle — gives the parse.
- A VIABLE PREFIX is a prefix of a right-sentential form that can appear on the stack (no prefix extends beyond the right end of the handle). LR(0) items are exactly the viable-prefix recognisers: the DFA over viable prefixes is the LR(0) automaton.
- Conflicts: a SHIFT-REDUCE conflict means a state can both shift and reduce on the same lookahead; a REDUCE-REDUCE conflict means two different reductions are possible on the same lookahead. Both mean the grammar is not suitable for that parser type.
- LR(k) means: scan Left to right, build a Rightmost derivation in reverse, with k lookahead symbols. LR parsers are table-driven with an ACTION table (terminals) and a GOTO table (non-terminals).
- Advantages over LL: LR parsers handle left-recursive grammars, a larger class of grammars (all deterministic CFLs) and detect syntax errors as early as possible (valid-prefix property).
- Move counting: a parse of t tokens whose tree has r internal nodes makes exactly t shifts, r reductions and 1 accept, so t + r + 1 moves. For id * id the trace has 3 + 5 + 1 = 9 rows, as in the figure.
- Not every right-hand-side match is a handle. In the stack T with * next, T matches E → T, but reducing gives E * id, which is not a right-sentential form; the parser must SHIFT. A handle is a match whose reduction stays on a rightmost derivation.
- The handle always appears at the TOP of the stack, never buried inside it. That is what lets a shift-reduce parser look only at the top of the stack and the next input symbol.
- Operator-precedence parsing is a simple bottom-up method for operator grammars (no ε-productions and no two adjacent non-terminals); it uses precedence relations between terminals and ignores non-terminals, so it handles a smaller class than LR.
| Action | Stack effect | Input effect |
|---|---|---|
| shift | push next symbol (and new state) | advance one token |
| reduce A → β | pop |β| symbols, push A | unchanged |
| accept | — | input is $ and stack is start symbol |
| error | — | no valid action |
Worked example
Problem. Show the handles when reducing id * id with E → E + T | T ; T → T * F | F ; F → id.
Solution. Right-sentential forms in reverse: id * id → F * id (handle id → F) → T * id (handle F → T) → T * F (handle id → F) → T (handle T * F → T) → E (handle T → E). Each handle is the leftmost substring that is the RHS of a production and is reduced next.
LR(0) items, closure and the canonical collection
- An LR(0) ITEM is a production with a dot in the right-hand side showing how much has been seen: A → α · β. A production of length n has n + 1 items; A → ε has the single item A → ·.
- Augment the grammar with S′ → S so there is exactly one accepting item S′ → S ·.
- CLOSURE(I): start with I; for each item A → α · B β in the set and each production B → γ, add B → · γ; repeat until no change. GOTO(I, X): move the dot over X in all items with the dot before X, then take the closure.
- The canonical collection is built by starting with I0 = closure({S′ → · S}) and repeatedly applying GOTO for every grammar symbol. Each distinct set is a DFA state of the LR(0) automaton.
- Kernel items: S′ → · S and all items whose dot is not at the left end. Non-kernel items come from the closure (dot at the left end).
- LR(0) parser: in a state with a complete item A → α · reduce by A → α on EVERY input symbol; if the state also contains a shift item, there is a conflict. A grammar is LR(0) iff no state has a reduce item together with another item (shift or reduce).
- Reading the automaton: I0 has 4 items, I2 and I3 have 3 each, and I1, I4, I5, I6 have one each (4 + 1 + 3 + 3 + 1 + 1 + 1 = 14 item occurrences, although the augmented grammar has only 10 distinct items). I3 has a self-loop on c because C → c · C closes to C → · c C.
- goto(I0, d), goto(I2, d) and goto(I3, d) all give the same set I4 = {C → d ·}: when counting states, compare item sets, do not create a new state for every transition.
- Total number of LR(0) items of the augmented grammar = Σ over productions (|RHS| + 1). The number of states of the LR(0) automaton is not the item count; it is the number of distinct item sets.
Formulas to remember
-
items in a production A → X1 … Xn = n + 1 ; A → ε has 1 item -
LR(0) conflict-free ⇔ every state is “all shifts” or exactly one complete item
| State | Items |
|---|---|
| I0 | S′ → ·S ; S → ·CC ; C → ·cC ; C → ·d |
| I1 = goto(I0, S) | S′ → S· |
| I2 = goto(I0, C) | S → C·C ; C → ·cC ; C → ·d |
| I3 = goto(I0, c) = goto(I2, c) = goto(I3, c) | C → c·C ; C → ·cC ; C → ·d |
| I4 = goto(I0, d) = goto(I2, d) = goto(I3, d) | C → d· |
| I5 = goto(I2, C) | S → CC· |
| I6 = goto(I3, C) | C → cC· |
Worked example
Problem. For the grammar S → C C ; C → c C | d, build the LR(0) automaton. How many states does it have, and is the grammar LR(0)?
Solution. Augment with S′ → S. The automaton has 7 states I0 … I6 as in the table. States I1, I4, I5, I6 contain a single complete item and I0, I2, I3 contain only non-complete items, so there is no conflict and the grammar is LR(0). The item count is 2 + 3 + 3 + 2 = 10.
SLR(1), CLR(1) and LALR(1)
- SLR(1): use the LR(0) automaton, but reduce A → α only on terminals in FOLLOW(A). A grammar is SLR(1) if this removes all conflicts. It is simple, but FOLLOW(A) is a coarse over-approximation of when a reduction is valid.
- CLR(1) = canonical LR(1): items carry a lookahead, [A → α · β, a]. CLOSURE adds [B → · γ, b] for every b ∈ FIRST(β a). Reduce [A → α ·, a] only on the exact lookahead a. It is the most powerful single-token LR method, but the automaton is large.
- LALR(1): build the CLR(1) automaton, then merge states that have the SAME CORE (same LR(0) items), unioning the lookaheads. The number of states equals the LR(0)/SLR(1) automaton, but lookaheads are more precise than FOLLOW sets.
- Merging can create REDUCE-REDUCE conflicts that did not exist in CLR(1), but it can never create a new shift-reduce conflict (shift actions depend only on the core).
- State counts: SLR(1) = LALR(1) = LR(0) number of states ≤ CLR(1) number of states. For the grammar S → CC ; C → cC | d, LR(0)/SLR/LALR have 7 states and CLR(1) has 10 states.
- Power: LR(0) < SLR(1) < LALR(1) < CLR(1). Table size (number of states): LR(0) = SLR = LALR ≤ CLR. Yacc/Bison generate LALR(1) tables because they are compact but nearly as powerful as CLR(1).
- Size ranking: CLR(1) states ≥ LALR(1) states = SLR(1) states = LR(0) states. Table construction cost follows the same order; for a real programming language the LALR table has hundreds of states where the canonical LR(1) table has thousands.
- Implications: SLR(1) ⇒ LALR(1) ⇒ CLR(1) for grammars. If a grammar is not CLR(1) it is not LALR(1), SLR(1) or LR(0) either. If it is CLR(1) it is LALR(1) exactly when merging same-core states creates no reduce-reduce conflict.
- Table sizes: ACTION has (states × (terminals + 1)) cells and GOTO has (states × non-terminals) cells. Reduce entries are the only difference between the four methods; shift and goto entries are identical for the LR(0)-based tables.
- Any shift-reduce conflict can be resolved by a precedence/associativity declaration in Yacc; by default Yacc prefers shift on shift-reduce conflicts and the earlier production on reduce-reduce conflicts.
- Every LL(1) grammar is LR(1); every LR(1) grammar is unambiguous; and LR(1) parsers recognise exactly the deterministic context-free languages.
| Parser | Items | Reduce A → α on | States | Power |
|---|---|---|---|---|
| LR(0) | LR(0) items | every terminal | n | weakest |
| SLR(1) | LR(0) items | FOLLOW(A) | n (same as LR(0)) | > LR(0) |
| LALR(1) | LR(1) with merged cores | merged lookaheads | n (same as LR(0)) | > SLR(1) |
| CLR(1) | LR(1) items | exact lookahead | ≥ n (often much larger) | strongest |
Worked example
Problem. Show that S → L = R | R ; L → * R | id ; R → L is not SLR(1) but is LALR(1)/CLR(1).
Solution. The LR(0) state reached after L from I0 contains S → L · = R and R → L ·. The reduce R → L is done on FOLLOW(R) = {=, $} (= follows L in S → L = R, and L → * R makes FOLLOW(R) ⊇ FOLLOW(L)), so the lookahead = allows both shifting (S → L · = R) and reducing: a shift-reduce conflict, so the grammar is not SLR(1). In LR(1) the item [R → L ·, $] has lookahead only $, so the conflict disappears: the grammar is LALR(1) and CLR(1).
Worked example — SLR(1) table and a parse
- Number the productions: (1) S → C C, (2) C → c C, (3) C → d. FOLLOW(S) = {$}; FOLLOW(C) = {c, d, $} because S → C C puts FIRST(C) = {c, d} after the first C and FOLLOW(S) after the second.
- ACTION entries: shift on terminals from goto transitions; reduce A → α in state with complete item on every terminal of FOLLOW(A); accept when the item S′ → S · is present and the lookahead is $. GOTO entries come from transitions on non-terminals.
- State 4 has C → d · so r3 on c, d, $. State 6 has C → c C · so r2 on c, d, $. State 5 has S → C C · so r1 on $. State 1 has S′ → S · so accept on $.
- Parse of dd: stack and input are shown in the table. We perform 2 shifts, 3 reductions (C → d, C → d, S → CC) and 1 accept. The number of reductions equals the number of internal nodes of the parse tree.
- For every parse, the number of shifts equals the number of terminals (tokens) in the input, and the number of reduce steps equals the number of internal nodes of the tree (productions applied).
| State | c | d | $ | S | C |
|---|---|---|---|---|---|
| 0 | s3 | s4 | 1 | 2 | |
| 1 | accept | ||||
| 2 | s3 | s4 | 5 | ||
| 3 | s3 | s4 | 6 | ||
| 4 | r3 | r3 | r3 | ||
| 5 | r1 | ||||
| 6 | r2 | r2 | r2 |
Worked example
Problem. Parse the input d d using the table above. List the stack/input/action sequence.
Solution. 0 | dd$ | shift 4 → 0 d 4 | d$ | reduce C → d → 0 C 2 | d$ | shift 4 → 0 C 2 d 4 | $ | reduce C → d → 0 C 2 C 5 | $ | reduce S → C C → 0 S 1 | $ | accept. Total: 2 shifts, 3 reductions.
LR conflicts — diagnosis and resolution
- A SHIFT-REDUCE conflict arises in a state containing A → α · a β (shift on a) and a complete item B → γ · whose reduce lookahead set also contains a. A REDUCE-REDUCE conflict arises when two complete items B → γ · and C → δ · have overlapping lookahead sets (FOLLOW sets in SLR(1), exact lookaheads in CLR(1)).
- E → E + E | id: the state {E → E + E ·, E → E · + E} has a shift-reduce conflict on + because + ∈ FOLLOW(E). The cause is ambiguity, not weak lookahead. Yacc resolves it with declarations: %left + reduces (left associative), %right + shifts, and with no declaration it shifts.
- S → A | B ; A → a ; B → a: after shifting a the state has A → a · and B → a ·. FOLLOW(A) = FOLLOW(B) = {$}, so the reduce-reduce conflict survives in SLR(1) and even in CLR(1), because both items carry lookahead $. The string a has two parse trees: the grammar is ambiguous and so not LR(k) for any k.
- Ambiguous grammars are never LR(k). Conflicts caused only by coarse lookahead (FOLLOW instead of exact lookahead) disappear when moving to a stronger parser; conflicts caused by ambiguity never do.
- Quick exam shortcuts: “Is it LR(0)?” — inspect the states, no FOLLOW needed. “Is it SLR(1)?” — compute FOLLOW sets and compare them with shift terminals in conflicting states. “LALR but not SLR?” — a grammar such as S → L = R | R ; L → * R | id ; R → L.
| Conflict | Items in the state | Reduce lookahead overlaps with | Typical cause |
|---|---|---|---|
| Shift-reduce | A → α · a β and B → γ · | the shift terminal a | ambiguity; coarse FOLLOW in SLR(1) |
| Reduce-reduce | B → γ · and C → δ · | the other reduce lookahead | ambiguity; LALR merging; coarse FOLLOW |
| None (accept) | S′ → S · | lookahead is $ | accepting state |
Worked example
Problem. For S → A | B ; A → a ; B → a, is the grammar LR(0), SLR(1) or LR(1)?
Solution. The state reached by shifting a contains the two complete items A → a · and B → a ·, a reduce-reduce conflict, so the grammar is not LR(0). FOLLOW(A) = FOLLOW(B) = {$}, so the reduce lookaheads overlap and it is not SLR(1). In LR(1) both items have lookahead $, so the conflict remains: the grammar is not LR(1). The reason is ambiguity — a has two parse trees.
CLR(1) vs LALR(1) — the 10 versus 7 states example
- LR(1) closure of I0 = {[S′ → ·S, $], [S → ·CC, $], [C → ·cC, c/d], [C → ·d, c/d]} — the lookahead c/d appears because FIRST(C $) = {c, d}.
- The CLR(1) automaton for S → C C ; C → c C | d has 10 states: I0, I1, I2 (goto C: [S → C·C, $] with lookahead $), I3 and I6 (both C → c·C, with lookaheads c/d and $), I4 and I7 (C → d·, lookahead c/d and $), I5, I8 and I9 (C → cC·, lookahead c/d and $).
- States I3 and I6 have the same core {C → c·C, C → ·cC, C → ·d}, I4 and I7 have the same core {C → d·}, and I8 and I9 have the same core {C → cC·}. LALR merges these three pairs: 10 − 3 = 7 states, equal to the LR(0) automaton.
- After merging, the lookahead of the merged states is the UNION: C → d · has lookahead {c, d, $}. In this example no conflict arises, so the grammar is LALR(1).
- LALR merging is the source of occasional reduce-reduce conflicts. Classic example: S → a A d | b B d | a B e | b A e ; A → c ; B → c is LR(1) but not LALR(1): the merged state has [A → c ·, d/e] and [B → c ·, d/e], a reduce-reduce conflict.
| CLR(1) states | Core | Lookahead | LALR merges with |
|---|---|---|---|
| I3 | C → c·C ... | c / d | I6 |
| I6 | C → c·C ... | $ | I3 |
| I4 | C → d· | c / d | I7 |
| I7 | C → d· | $ | I4 |
| I8 | C → cC· | c / d | I9 |
| I9 | C → cC· | $ | I8 |
Worked example
Problem. Compute the number of states of the CLR(1) and LALR(1) automata for S → C C ; C → c C | d.
Solution. CLR(1) has 10 states (I0 … I9) and LALR(1) merges the three same-core pairs (I3/I6, I4/I7, I8/I9) into 7 states. The number of LALR states always equals the number of LR(0) states.
Exam tips
- Item count = Σ (|RHS| + 1) over productions of the augmented grammar; states = distinct item sets (use closure and goto).
- LR(0): one complete item and nothing else in a state. SLR: reduce on FOLLOW. CLR: exact lookahead. LALR: merge same-core CLR states.
- LALR states = LR(0) states = SLR states ≤ CLR states.
- LALR can create reduce-reduce conflicts but never shift-reduce ones.
- Power: LR(0) < SLR(1) < LALR(1) < CLR(1); LL(1) ⊂ LR(1); all LR(1) parsers accept all DCFLs.
10 GATE-style MCQs for Bottom-Up Parsing (LR) with full explanations. Attempt each question before revealing the answer.
For the grammar S → C C ; C → c C | d, the number of states in the LR(0) automaton (after augmenting) is:
Show answer & explanation
Answer: C
I0 … I6 as constructed above: 7 states.
For the same grammar, the number of states in the canonical LR(1) and LALR(1) automata respectively is:
Show answer & explanation
Answer: A
CLR(1) has 10 states; LALR merges three same-core pairs, leaving 7.
Which of the following is the correct relation among the parser classes?
Show answer & explanation
Answer: B
Each class adds precision to the reduce decisions: LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1).
Merging states of a canonical LR(1) parser with identical cores to obtain LALR(1) can:
Show answer & explanation
Answer: B
Shift actions depend only on the core, so no new shift-reduce conflict appears, but merged lookahead sets can overlap between two reductions.
A bottom-up parser generates:
Show answer & explanation
Answer: B
Shift-reduce parsing performs reductions in the reverse order of a rightmost derivation.
Consider S → L = R | R ; L → * R | id ; R → L. Which statement is TRUE?
Show answer & explanation
Answer: C
FOLLOW(R) contains = (because R ⇒ L), so SLR reduces R → L on =, clashing with the shift of = in S → L · = R. LR(1) lookaheads remove the conflict.
An SLR(1) parser reduces by A → α when the lookahead is in:
Show answer & explanation
Answer: C
SLR(1) reduces on terminals in FOLLOW of the left-hand side.
The total number of LR(0) items of the augmented grammar S′ → S ; S → C C ; C → c C | d is:
Show answer & explanation
Answer: B
S′ → S has 2 items, S → C C has 3, C → c C has 3, C → d has 2: total 10.
The number of reduce moves performed while parsing the string d d with S → C C ; C → c C | d is:
Show answer & explanation
Answer: C
Two reductions C → d and one S → C C: the parse tree has 3 internal nodes.
In an LR(0) automaton, a state containing the items A → α · and B → β · a γ has:
Show answer & explanation
Answer: B
The LR(0) parser would reduce by A → α on every symbol, including a, while the second item asks to shift a.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
The number of LR(0) items in the augmented grammar S′ → S ; S → a S b | ε is:
Show answer & explanation
Answer: C) 7
S′ → S has 2 items, S → a S b has 4 items, S → ε has 1 item: 2 + 4 + 1 = 7.
Which parser needs the largest number of states for the same grammar?
Show answer & explanation
Answer: D) CLR(1)
CLR(1) keeps states with identical cores separate when their lookaheads differ, so it has the most states.
If a grammar is LALR(1), then it is certainly:
Show answer & explanation
Answer: D) CLR(1)
LALR(1) ⊂ CLR(1). Nothing implies LR(0), SLR(1) or LL(1).
In the shift-reduce parsing of id + id with E → E + E | id, the first reduction is:
Show answer & explanation
Answer: B) E → id
After shifting the first id, the handle is id and it is reduced to E.
A grammar has the LR(1) property but is NOT LALR(1). The failure is due to:
Show answer & explanation
Answer: B) reduce-reduce conflicts after merging
Merging same-core states can union lookaheads of two different reductions in one state.
Number of shift moves needed to parse a string of n tokens by any shift-reduce parser (no ε-productions):
Show answer & explanation
Answer: B) n
Each input token is shifted exactly once; reductions do not consume input.
In a canonical LR(1) parser, the item [A → α ·, a] in a state means the parser:
Show answer & explanation
Answer: B) reduces by A → α only when the next input symbol is a
The lookahead stored in an LR(1) item is exact, which is what makes CLR(1) more precise than SLR(1).
Operator-precedence parsing is applicable to:
Show answer & explanation
Answer: B) operator grammars: no ε-productions and no two adjacent non-terminals
It is a restricted bottom-up method that relies on precedence relations between terminals.
Topic FAQs
What is the difference between SLR(1), LALR(1) and CLR(1)?
All three use the same kind of shift-reduce machine. SLR(1) reduces on FOLLOW(A) (coarse), CLR(1) reduces only on the exact lookahead stored in the item (most precise, many states), and LALR(1) merges CLR(1) states with the same LR(0) core, so it has the size of SLR(1) but nearly the power of CLR(1).
Why can LALR merging not create shift-reduce conflicts?
Shift actions depend only on the core (terminals after the dot), which is identical in merged states. If a merged state had a shift-reduce conflict, one of the original CLR states would already have had it.
How do I count the states of an LR(0) automaton quickly?
Start from I0 = closure({S′ → ·S}), compute goto on each symbol after a dot, and add only new item sets. Remember that goto on the same symbol from different states can lead to the same state (e.g. I3 above).
Can a grammar be LR(1) but not LALR(1)?
Yes. A merged LALR state may contain two complete items whose lookahead sets overlap after merging, causing a reduce-reduce conflict. S → aAd | bBd | aBe | bAe ; A → c ; B → c is the standard example.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.