Chapter 3

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 S′→SS'\to S. An LR(0) item A→α∙βA\to\alpha\mathbin{\bullet}\beta records how much of a production has been recognised. An empty production has one item, A→∙A\to\mathbin{\bullet}; 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 aa gives a shift entry; a transition on nonterminal AA 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 A→βA\to\beta, pop one grammar symbol and its state for each symbol of β\beta, then consult GOTO from the newly exposed state and push AA with that state. In an alternating symbol/state stack, this pops 2∣β∣2|\beta| 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 [A→α∙β,a][A\to\alpha\mathbin{\bullet}\beta,a] says that after completing this particular production in this context, aa may follow it. This lookahead is not necessarily the symbol immediately after the dot and is not automatically all of FOLLOW(A)(A).

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

LR(0)⊊SLR(1)⊊LALR(1)⊊LR(1).\mathrm{LR}(0)\subsetneq\mathrm{SLR}(1)\subsetneq\mathrm{LALR}(1)\subsetneq\mathrm{LR}(1).

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.

ClaimCorrect interpretation
An LL(1) grammar is unambiguousYes; one table choice determines its parse
An LR grammar is unambiguousYes, 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) grammarYes; this need not be the grammar printed in the question
An ambiguous grammar has no parser of any kindFalse; 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 O(n3)O(n^3) time and O(n2)O(n^2) 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.

Revise offline: the whole Compiler Design handbook as one PDF

43 pages · 2.0 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections