Bottom-up parsing
A bottom-up parser recognises handles and replaces them by production heads. Most numerical questions ask for item sets, reduction counts, table cells or conflicts; distinguish these objects before counting.
3.1 Handles, viable prefixes and reduction counts
Past papers · CS 2014–2026 (23 papers): 3 questions, 3 marks
A handle is a production body at the position where its reduction reverses one step of a rightmost derivation. A matching substring is not necessarily the handle. A viable prefix is a prefix of a right-sentential form that does not extend beyond the right end of its handle. The grammar-symbol part of a successful shift-reduce stack is a viable prefix; its prefixes are also viable prefixes.
The actions are shift (consume a token), reduce (replace the handle), accept and error. LR parsers use states on the stack to identify which action is legal. Top-down parsing starts at the root; bottom-up reductions build towards the root.
3.2 LR(0) items, closure and transitions
Past papers · CS 2014–2026 (23 papers): 4 questions, 7 marks
Augment the grammar with a fresh . An LR(0) item records how much of a production has been recognised. An empty production has one item, ; do not put an epsilon symbol after the dot.
Kernel items have a noninitial dot, plus the augmented initial item. Closure adds the nonkernel items. Equal sets reached by different routes are the same state. Count duplicate items once, even when several parents introduce them.
3.3 LR tables and conflict tests
Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks
ACTION columns are terminals plus the end marker. GOTO columns are original nonterminals. A transition on terminal gives a shift entry; a transition on nonterminal gives a GOTO entry. A completed ordinary production gives reductions in the columns selected by the parser family.
A table cell is conflicting when it contains distinct actions. Two identical items or duplicate insertions of the same reduction do not create a new conflict. “Number of conflicts” may mean conflicting cells, item pairs or states; state the convention. This handbook counts conflicting ACTION cells.
For reduction , pop one grammar symbol and its state for each symbol of , then consult GOTO from the newly exposed state and push with that state. In an alternating symbol/state stack, this pops entries. An empty production pops zero. A reduction never consumes the lookahead.
3.4 Canonical LR(1) and LALR merging
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
An LR(1) item says that after completing this particular production in this context, may follow it. This lookahead is not necessarily the symbol immediately after the dot and is not automatically all of FOLLOW.
To construct LALR(1), group canonical LR(1) states whose LR(0) cores are identical, erase no core items, and union the lookahead sets attached to corresponding items. Merge the corresponding transitions as well. This reduces the state count to the LR(0) collection's count under the usual construction.
3.5 Parser power and grammar versus language
Past papers · CS 2014–2026 (23 papers): 6 questions, 7 marks
For the usual conflict-free table constructions, the strict grammar-class chain is
Canonical LR(1), also called CLR(1), tracks the most precise reduction context of these methods. SLR uses smaller and simpler tables but a coarse FOLLOW test. A common prefix or left recursion does not by itself prevent LR parsing.
| Claim | Correct interpretation |
|---|---|
| An LL(1) grammar is unambiguous | Yes; one table choice determines its parse |
| An LR grammar is unambiguous | Yes, when its table is conflict-free without added disambiguation |
| Every unambiguous grammar is LR(1) | No |
| Every LL(1) grammar is LR(1) | Yes under the standard end-marker convention |
| LL(1) is simply another rung below SLR(1) | No; do not infer a universal containment with SLR or LALR for arbitrary grammars |
| A deterministic context-free language has an LR(1) grammar | Yes; this need not be the grammar printed in the question |
| An ambiguous grammar has no parser of any kind | False; general parsers can handle ambiguity |
For a fixed grammar, deterministic LL/LR parsing takes linear time in token length with standard constant-time table access and bounded per-token reduction behaviour. General CFG membership/parsing has cubic-time algorithms; CYK's time and chart space use a fixed grammar in an appropriate normal form. See Theory of Computation for the CYK method. A parser need not enumerate exponentially many parse trees merely to recognise a string.
Last-minute recall
- LR reductions reverse a rightmost derivation.
- Closure expands only the nonterminal immediately after the dot.
- GOTO moves the dot first, then takes closure.
- SLR reductions use FOLLOW; LR(1) reductions use item lookaheads.
- Epsilon is a completed item, not a shiftable symbol.
- Accept is distinct from reduction by an original production.
- Equal-core LALR merging can introduce reduce/reduce conflicts.
- A grammar's parser class and its language's properties are different questions.