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

Bottom-up parsing (shift–reduce and LR family) builds a parse from input to start symbol using handles, viable prefixes and parser tables (ACTION/GOTO); engineers use it when writing compilers, interpreters, linkers and other language processors to implement efficient syntax analysis.

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 the technique compilers use to recognise program phrases (sentences) from the input and build a parse tree while allowing the most general deterministic context-free grammars. Engineers implement bottom-up (LR) parsers in the syntax analysis phase of a compiler or interpreter (see the subject list for Top-down parsing and Lexical analysis). Bottom-up parsers are used in real-world compiler tools, assembler back-ends, and in automatic parser generators used for language processing.

Key ideas

Start by defining terms in plain language.

  • Grammar: a set of productions (rules) of the form A → α where A is a nonterminal and α is a string of terminals and nonterminals.
  • Sentential form: any string of terminals and nonterminals that can appear during a derivation.
  • Rightmost derivation: a derivation that at each step replaces the rightmost nonterminal.
  • Handle: for a rightmost derivation of a sentence w, a handle is a substring β of a sentential form αβγ that matches the RHS of some production A → β and whose reduction to A (αAγ) is a step in the rightmost derivation. In bottom-up parsing we repeatedly find and reduce handles.
  • Viable prefix: a prefix of a right-sentential form that can appear on the stack of a shift–reduce parser; equivalently, a prefix that can appear before a handle (never extends past the right end of the handle).
  • Item: a production with a dot marking a position, written A → α·β. The dot shows how much of the RHS has been seen on the parser stack.
  • LR(0) item: an item without any lookahead. LR(1) item attaches a lookahead terminal a: written [A → α·β, a].
  • Closure(I): operation that, given a set of items I, adds items for productions whose left side is the nonterminal immediately after a dot in I (repeat until no new items). For LR(1) closure we also compute lookaheads.
  • Goto(I, X): function giving the set of items you get by moving the dot past symbol X in all items of I that have X after the dot, then taking closure.
  • Canonical collection: the set of item-sets reachable from the start item-set via Goto; each item-set becomes a parser state.

Exact algorithm statements (the things you will be asked to derive or show in university answers):

  1. Closure (LR(0)) — precise algorithm:
    • Start with a set of items I.
    • Repeat: for any item A → α·Bβ in I (dot before nonterminal B), for every production B → γ, add item B → ·γ to I if not already present. Continue until no new items are added.
    • This yields Closure(I). For LR(1) items: for each item [A → α·Bβ, a] in I and each production B → γ, for each b in FIRST(βa) add [B → ·γ, b]; repeat until no new items are added.
  2. Goto(I, X):
    • For all items A → α·Xβ in Closure(I), form item A → αX·β (move dot past X).
    • Take Closure of that set; result is Goto(I, X).
  3. Canonical collection construction:
    • Start item: S' → ·S (S' is augmented start nonterminal).
    • I0 = Closure({S' → ·S}).
    • For each state I and each grammar symbol X, if Goto(I, X) is non-empty and not already a state, add it to the collection and repeat.
  4. Parser table (LR(0) / SLR / LR(1)):
    • Each canonical item-set becomes a parser state.
    • ACTION[state, a] for terminal a: if item A → α·aβ in state and Goto(state, a) = t, set ACTION[state, a] = shift t.
    • If item A → α· in state (dot at right end) and A ≠ S', then for each terminal a in FOLLOW(A) (SLR) set ACTION[state, a] = reduce A → α. For LR(1) use item lookaheads instead of FOLLOW.
    • If item S' → S· in state, set ACTION[state, $] = accept.
    • GOTO[state, A] for nonterminal A is the state t = Goto(state, A).
  5. LALR(1) construction (state merging): build the canonical LR(1) item sets, then merge states that have the same LR(0) core (i.e., same items without lookaheads), uniting their lookahead sets; use the merged states to build ACTION/GOTO as in LR(1). Note: merging can introduce reduce–reduce conflicts that were absent in the canonical LR(1) collection.

Important theorem statements (exam-style):

  • LR(1) parsers recognise exactly the deterministic context-free languages (DCFLs). The canonical LR(1) construction accepts any grammar that is LR(1).
  • Relationship: LR(0) ⊆ SLR(1) ⊆ LALR(1) ⊆ LR(1) (each inclusion may be strict). Assumption: the grammar is context-free and not ambiguous for LR(1) parsing to succeed without conflicts.

What to draw in an exam when asked to show parser action:

  • Draw a two-column table labelled Stack | Input. Stack shows alternating states and grammar symbols, top at right. Input shows remaining input with $ at end. Show the ACTION/GOTO table (small) and show each step: current state = top state, action chosen, new stack after shift/reduce, and consumed input. For item-sets, draw each item as a line with a dot and list transitions labelled by grammar symbols to other item-sets.

Formulas

For parser implementation, memorise the table entry syntax and core algorithms as lines: ACTION[s, a] = shift t — when state s has an item A → α·aβ and Goto(s, a) = t. ACTION[s, a] = reduce A -> α — when state s has item A -> α· (dot at right) and a in LOOKAHEAD(A, s) (FOLLOW(A) for SLR). ACTION[s, $] = accept — when state s contains S' -> S·. GOTO[s, A] = t — for nonterminal A, the state reached from s after a reduction when A has just been pushed on the stack. Closure algorithm (pseudocode): Closure(I): repeat for each item A -> α·Bβ in I, for each production B -> γ add B -> ·γ to I, until no change. Goto algorithm (pseudocode): Goto(I, X): J = { A -> αX·β | A -> α·Xβ in I }; return Closure(J).

Worked examples

Example 1 — very small grammar, build SLR(1) table and parse ab Grammar: S' -> S S -> a A A -> b

  1. Items and states: I0: S'->·S, S->·aA Goto(I0, a) = I1 with S->a·A and A->·b Goto(I1, b) = I2 with A->b· Goto(I1, A) = I3 with S->aA· Goto(I0, S) = I4 with S'->S·
  2. ACTION/GOTO table (states 0..4):
  • State0: ACTION[a]=shift 1; GOTO[0,S]=4
  • State1: ACTION[b]=shift 2; GOTO[1,A]=3
  • State2: ACTION[$]=reduce A->b (FOLLOW(A) = {$})
  • State3: ACTION[$]=reduce S->aA (FOLLOW(S) = {$})
  • State4: ACTION[$]=accept
  1. Parse ab$: steps (stack | input)
  • 0 | a b $ shift a -> stack 0 a 1 | b $
  • 1 | b $ shift b -> stack 0 a 1 b 2 | $
  • 2 | $ reduce A->b: pop b,2; push A; goto GOTO[1,A]=3 -> stack 0 a 1 A 3 | $
  • 3 | $ reduce S->aA: pop A,3 and a,1; push S; goto GOTO[0,S]=4 -> stack 0 S 4 | $
  • 4 | $ accept.

Example 2 — classical SLR example, grammar and full trace Grammar (augmented S'): S' -> S S -> C C C -> c C C -> d Goal: build SLR table (sketch) and parse input c d d. Canonical item-sets (state names and key items, sketch): I0: S'->·S; S->·C C; C->·cC; C->·d I1: S'->S· I2: S->C·C; C->·cC; C->·d I3: C->c·C; C->·cC; C->·d I4: C->d· I5: C->cC· I6: S->C C· Transitions (important ones): From 0: on c -> 3, on d -> 4, on C -> 2, on S -> 1 From 2: on c -> 3, on d -> 4, on C -> 6 From 3: on c -> 3, on d -> 4, on C -> 5 From 5: reduce C -> c C From 4: reduce C -> d From 6: reduce S -> C C FOLLOW sets: FOLLOW(S) = {$}; FOLLOW(C) = {c, d, $} ACTION and GOTO (summary):

  • ACTION[0,c]=shift3; ACTION[0,d]=shift4; GOTO[0,C]=2; GOTO[0,S]=1
  • ACTION[2,c]=shift3; ACTION[2,d]=shift4; GOTO[2,C]=6
  • ACTION[3,c]=shift3; ACTION[3,d]=shift4; GOTO[3,C]=5
  • ACTION[4,a] = reduce C->d for a in FOLLOW(C) (a=c,d,$)
  • ACTION[5,a] = reduce C->cC for a in FOLLOW(C)
  • ACTION[6,$] = reduce S->CC Parsing c d d $ step-by-step (stack shows states and symbols):
  1. [0] | c d d $ ACTION[0,c] = shift 3 => push c,3 -> stack: 0 c 3 | d d $
  2. Top state=3, ACTION[3,d]=shift 4 => push d,4 -> stack: 0 c 3 d 4 | d $
  3. Top state=4, ACTION[4,d] = reduce C->d (d in FOLLOW(C)). Reduce: pop d,4 and push C then GOTO[3,C]=5 -> stack: 0 c 3 C 5 | d
  4. Top state=5, ACTION[5,d] = reduce C->cC. Reduce: pop (c,3) and (C,5) then push C and GOTO[0,C]=2 -> stack: 0 C 2 | d
  5. Top state=2, ACTION[2,d] = shift 4 -> stack: 0 C 2 d 4 | $
  6. Top state=4, ACTION[4,$] = reduce C->d -> pop d,4 and push C goto GOTO[2,C]=6 -> stack: 0 C 2 C 6 | $
  7. Top state=6, ACTION[6,$] = reduce S->CC -> pop C6 and C2 and push S goto GOTO[0,S]=1 -> stack: 0 S 1 | $
  8. Top state=1, ACTION[1,$] = accept. Parsing succeeds. This trace is exactly the shift–reduce cycle: shift until you can reduce, reduce, repeat.

Common mistakes

  • Building Closure incorrectly: forgetting to add items B -> ·γ for every production of B when you see A -> α·Bβ.
  • Mislabeling Goto transitions or confusing terminals and nonterminals when creating ACTION vs GOTO entries.
  • Forgetting to put accept entry at state containing S'->S· with $ in ACTION.
  • While tracing, forgetting that on reduce you must pop 2×|RHS| stack entries (symbols and their states), then consult GOTO.

For your exam

Typical question forms:

  • Definitions and short notes: define handle, viable prefix, LR(0) item, closure and goto.
  • Derivations and algorithms: derive closure and goto; show canonical collection for a given small grammar; construct ACTION/GOTO table for LR(0)/SLR/LALR; explain differences between LR variants.
  • Trace problems: give a table and ask to parse a specific input showing stack and input column by column.
  • Conflict analysis: given a grammar show why shift–reduce or reduce–reduce conflict arises and how lookahead (SLR vs LR(1)) can resolve or not. A full-marks answer includes: precise definitions, the closure/goto algorithm (step-by-step), labelled item-sets (I0, I1, ...), the completed ACTION and GOTO tables, and a full trace table of stack vs input showing each shift/reduce/accept step.

Quick check

  1. What is a handle? 2) In LR(0) closure, when do you add a new item B -> ·γ? 3) For SLR reductions you use which set to decide in which terminals to place a reduce action? Answers: A handle is the RHS substring whose reduction is the next step in a rightmost derivation; you add B->·γ when an item has the dot immediately before B (A->α·Bβ in the set); use FOLLOW(A) (FOLLOW of the LHS) for SLR reduce entries.

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

Stuck on something here?