Building an LR table step by step

This page builds the automaton and the ACTION/GOTO tables that LR parsing step by step uses, from the three rules below. Step forward with Next or the arrow keys.

0.S′ → E1.E → E + n2.E → n

The automaton, as it grows

An item E → E · + n, $ is a rule, a dot (what is read so far) and a lookahead (the token that must follow when the rule is reduced). Blue: the items being looked at. Green: the items just added.

What happens

Worklist (states whose transitions are still to compute)

ACTION and GOTO tables

staten+$GOTO E

The four rules used on this page

  1. Closure. For an item [A → α · B β, a] with a non-terminal B after the dot, add [B → · γ, b] for every rule B → γ and every b in FIRST(β a). Repeat until the state stops growing. FIRST(β a) walks β with the FIRST sets and the nullable set; if all of β is nullable, a is in it.
  2. Goto. goto(I, X) takes the items of I with the dot just before X, moves the dot over X (lookaheads unchanged), then takes the closure. If that set of items already is a state, reuse its number.
  3. Worklist. Start with the closure of [S′ → · E, $]. Take a state off the list, compute a goto for each symbol after a dot, put every new state on the list. Stop when the list is empty.
  4. Tables. A transition on a terminal is a shift in ACTION, on a non-terminal a GOTO. A complete item [A → α ·, x] is a reduce in column x only. [S′ → E ·, $] is accept. Two values in one cell is a conflict.