Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Bottom-up parsing and shift-reduce

Bottom-up (shift-reduce) parsing builds a parse from the input up to the start symbol by repeatedly shifting tokens and reducing recognized substrings (handles); LR-family parsers (LR(0), SLR, LR(1), LALR) implement this with an item automaton and ACTION/GOTO tables.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Bottom-up parsing is used inside a compiler's parser (front end) to recognise program structure when the language has constructs that are naturally left-recursive or difficult for predictive top-down parsers (e.g., require precedence/associativity). Engineers use shift-reduce parsers (LR-family) in parser generators (YACC/Bison) and in building reliable compilers and interpreters.

Key ideas

Start with definitions (each defined term is bold the first time it appears).

  • Bottom-up parsing: a parsing approach that starts from input tokens and builds parse trees by repeatedly replacing substrings (right-hand sides of productions) with the corresponding nonterminals until the start symbol remains.
  • Shift: an action that pushes the next input token onto the parser stack (we say the parser 'shifts' the token).
  • Reduce: an action that replaces a sequence of symbols on the stack that matches the right-hand side of a production by the production's left-hand nonterminal.
  • Handle: the substring (contiguous sequence of terminals and nonterminals) of the rightmost derivation of the string that corresponds to the next reduction. Formally: if S =>* αAw => αβw is a rightmost derivation step where A -> β is applied, then β (occurring in αβw) is a handle.
  • Viable prefix: any prefix of a right-sentential form that can appear on the stack of a shift-reduce parser; equivalently, any prefix that can appear before the unread part of a rightmost derivation. Viable prefixes are exactly the strings that the LR(0) automaton recognises.
  • Item (LR(0) item): a production with a dot marking how much of its RHS has been seen, written A -> α·β. The dot indicates the parser's position. Items are used to build states of a DFA (the canonical collection).
  • Closure: the operation that, given a set of items, adds items for productions whose left-hand side appears immediately after a dot. Algorithm (informal): for every item A -> α·Bβ in the set, add B -> ·γ for each production B -> γ; repeat until no new items.
  • Goto(I, X): for item-set I and grammar symbol X, the set of items you get by moving the dot past X in every item A -> α·Xβ in I, then taking the closure.
  • Canonical collection: the set of item-sets (states) obtained by starting with closure of [S' -> ·S] and repeatedly applying goto for all symbols; this collection is the DFA used by LR parsers.
  • Action and Goto tables: parsing tables. ACTION[state, terminal] says shift sN, reduce rM, accept or error. GOTO[state, nonterminal] gives a next state after a reduction.
  • LR(k) parser: scans input left-to-right, produces a rightmost derivation in reverse using k-symbol lookahead (most practical are k=0 or k=1).
  • SLR (Simple LR): uses LR(0) item automaton plus FOLLOW sets to decide where reductions apply (uses less context than full LR(1)). — LALR(1): formed by merging LR(1) item sets with the same LR(0) core; has about the same number of states as SLR but with LR(1) lookaheads; more powerful than SLR yet smaller than canonical LR(1).
  • Shift-reduce conflict: when a table cell suggests both shift and reduce; reduce-reduce conflict: when two reductions appear. These indicate the grammar is not LR(1)/SLR as required.

Detailed constructive steps universities expect (draw exactly as instructed):

  1. Write the augmented grammar with S' -> S.
  2. Draw state I0: a circle labelled I0 containing items S' -> ·S, plus closure items. (Label each item on separate line inside the circle.)
  3. Draw arrows from I0 labelled by symbols (terminals/nonterminals) to other states; label the target circles I1, I2 ...; continue until no new states.
  4. From this canonical DFA, build ACTION and GOTO: for state i and terminal a, if goto(i,a) = j then ACTION[i,a] = shift j; if item A -> α· is in state i and A ≠ S' then for each a in FOLLOW(A) set ACTION[i,a] = reduce A -> α; if S' -> S· in state i set ACTION[i,$]=accept.

Proof idea often asked: why bottom-up gives the rightmost derivation in reverse. Sketch: invariant — stack always contains a viable prefix; every reduce removes a handle (the rightmost reducible substring); reducing a handle corresponds to applying the reverse of a rightmost derivation step, so sequence of reductions reconstructs the rightmost derivation in reverse.

Formulas

item notation: A -> α·β — A is a nonterminal, α and β are strings of grammar symbols; dot shows parser position. closure(I): repeat { for each item A -> α·Bβ in I and for each production B -> γ add B -> ·γ to I } until no change — algorithm to build closure. goto(I, X) = closure({ A -> αX·β | A -> α·Xβ in I }) — goto definition. ACTION[i, a] = shift j if goto(I_i, a) = I_j — set shift actions from goto transitions on terminals. If A -> α· in I_i and A ≠ S' then for each a in FOLLOW(A) set ACTION[i,a] = reduce A -> α — SLR reduction rule (exclude the augmented start production to avoid conflicting with accept). If S' -> S· in I_i then ACTION[i,$] = accept — accept condition.

(Each symbol: A, B nonterminals; α, β, γ strings of grammar symbols; I, I_i item-sets; a terminal; FOLLOW(A) the set of terminals that may follow A.)

Worked examples

Example 1 — canonical collection, SLR table and parse trace (standard small grammar). Grammar (augmented): 0: S' -> S 1: S -> C C 2: C -> c C 3: C -> d

  1. Build item-sets (list all states — draw each as a circle labelled I0..I6). The canonical collection of LR(0) items (I0..I6) is: I0: S'->·S, S->·C C, C->·c C, C->·d I1: S'->S· I2: S->C·C, C->·c C, C->·d I3: C->c·C, C->·c C, C->·d I4: C->d· I5: C->c C· I6: S->C C·

  2. Goto transitions (describe arrows to draw):

  • From I0: on S -> I1, on C -> I2, on c -> I3, on d -> I4.
  • From I2: on c -> I3, on d -> I4, on C -> I6.
  • From I3: on c -> I3 (self-loop), on d -> I4, on C -> I5.
  • From I5 and I4 and I6 there are no further nontrivial shifts.
  1. Compute FOLLOW sets (needed for SLR reductions): FIRST(C) = {c, d}; from S->C C we get FOLLOW(C) contains {c, d} for the first C and for second C include FOLLOW(S) which is {$}; so FOLLOW(C) = {c, d, $}. FOLLOW(S) = {$}.

  2. Fill ACTION/GOTO table (show as table or list). Key ACTION entries:

  • ACTION[0,c] = shift 3, ACTION[0,d] = shift 4, GOTO[0,S]=1, GOTO[0,C]=2.
  • ACTION[1,$] = accept.
  • ACTION[2,c] = shift 3, ACTION[2,d] = shift 4, GOTO[2,C]=6.
  • ACTION[3,c] = shift 3, ACTION[3,d] = shift 4, GOTO[3,C]=5.
  • ACTION[4,a] = reduce by (3) C -> d for a in FOLLOW(C) = {c,d,$}.
  • ACTION[5,a] = reduce by (2) C -> c C for a in {c,d,$}.
  • ACTION[6,$] = reduce by (1) S -> C C. (No conflicts here — grammar is SLR.)
  1. Parse the input c d c d $ (step-by-step trace). Show a table with columns: step, stack (states & symbols), remaining input, action.
  • Initial: stack [0], input c d c d $
  • 1: state 0, read c -> ACTION[0,c]=s3; push c,3. Stack [0, c, 3].
  • 2: state 3, read d -> ACTION[3,d]=s4; push d,4. Stack [0, c,3, d,4].
  • 3: state 4 -> ACTION[4, lookahead c] = reduce by C->d (prod 3). Pop d,4; push C; goto from state3 on C = 5. Stack [0, c,3, C,5].
  • 4: state 5 -> ACTION[5, lookahead c] = reduce by C->c C (prod 2). Pop c,3,C,5; push C; goto from state0 on C = 2. Stack [0, C,2].
  • 5: state 2, read c -> shift 3. Stack [0, C,2, c,3].
  • 6: state 3, read d -> shift 4. Stack [0, C,2, c,3, d,4].
  • 7: state 4 -> reduce by C->d (prod 3); pop d,4; push C; goto[3,C]=5. Stack [0,C,2,c,3,C,5].
  • 8: state 5 -> reduce by C->c C (prod 2); pop c,3,C,5; push C; goto[2,C]=6. Stack [0,C,2,C,6].
  • 9: state 6 -> ACTION[6,$] = reduce S->C C (prod 1) on lookahead $; pop C,6,C,2; push S; goto[0,S]=1. Stack [0,S,1].
  • 10: state 1 and input $ => ACTION[1,$] = accept. Done.

This trace shows shifts and reductions and that the parser finds S as the start symbol.

Example 2 — conceptual worked question often asked in exams: "Define a handle and show why a reduction always replaces a handle." Model answer: give the definition, draw a short rightmost derivation S =>* αA w => αβ w and indicate β is the handle; explain that bottom-up parser maintains viable prefix invariant and only reduces when the top-of-stack substring matches a handle, therefore each reduce step reverses a single rightmost derivation step.

Common mistakes

  • Forgetting to augment grammar with S' -> S before building items (this breaks accept logic).
  • Mixing production numbering between table and trace (be consistent and state production numbers at start).
  • Building goto/closure wrong: forgetting closure when constructing goto targets or forgetting repeated closure until no change.
  • Not checking lookahead sets: placing a reduce for all terminals instead of only FOLLOW(A) in SLR.

For your exam

Typical question types and what full-marks answers include:

  • Definitions: give precise definitions of handle, viable prefix, item, closure, goto, shift-reduce conflict.
  • Construction problems: build the canonical collection (draw item-sets I0..In), show goto arrows, and produce ACTION/GOTO table (list entries clearly). Remember to augment grammar first and show production numbers.
  • Trace problems: given table, show shift-reduce parse trace on a given input with stack and remaining input and each action step.
  • Short proofs: show why bottom-up parsing yields reversed rightmost derivation (use invariant and handle definition).
  • Conflict diagnosis: show why a grammar causes shift-reduce or reduce-reduce conflict, and suggest fixes (left-factor, rewrite grammar, add precedence rules in parser generator).

Full marks answers must state assumptions (augmented grammar, token stream ends with $), label production numbers, list item-sets completely, show FOLLOW sets when using SLR, and give a correct step-by-step trace.

Quick check

  1. What is a handle?
  2. In SLR, which set decides where a reduction A->α applies?
  3. What two table actions conflict in a shift-reduce conflict?

Answers: 1) The substring that matches a production right side and is the next to be reduced in the rightmost derivation. 2) FOLLOW(A). 3) A shift (shift to some state) and a reduce (reduce by some production).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?