LR(1) Parser Builder
Build the LR(1) parsing DFA of a grammar with Closure and Transition, fill in its action/goto table, and run the LR parsing algorithm on a token string.
- Lecture reference: Bottom-up Parsing II
- Lecture reference: Bottom-up Parsing III
Grammar augmented with a new start symbol
Augmented grammar
S → E
E → E + ( E ) | int The new start symbol S has the one production S → E. State 0 is Closure({ S → •E, $ }).
The order in which Closure adds the productions of a non-terminal, and so the order of the items in a state.
Open in LL(1) Parsing Table, Shift-Reduce Parsing or Conflicts & Precedence
The LR parsing algorithm on the input of slides 4–14. The stack holds pairs < sym, state >, starting with < dummy, 0 >; each step looks up action[top_state(stack), I[j]], shifts, or pops |α| pairs and pushes the Goto entry, until accept.
Left string can be implemented as a ?
Show answer
A stack. Shift pushes a terminal; reduce pops 0 or more symbols (α) and pushes a non-terminal (A).
Lecture reference: Bottom-up Parsing II
After shift or reduce action we rerun DFA on entire stack. Wasteful, since most of the work is repeated. Alternative?
Show answer
For each stack element, remember to which state it brings the DFA. The count of DFA transitions beside the stack compares the two.
Lecture reference: Bottom-up Parsing II
Closure and Transition one item at a time
Closure({ S → •E, $ }): the start state of the parsing DFA.
- Closure(Items) =
- repeat
- for each [X → α•Yβ, a] in Items
- for each production Y → γ
- for each b ∈ First(βa)
- add [Y → •γ, b] to Items
- until Items is unchanged
Items 5
- S → •E, $
- E → •E + ( E ), $
- E → •int, $
- E → •E + ( E ), +
- E → •int, +
Abbreviated 3
- S → •E, $
- E → •E + ( E ), $/+
- E → •int, $/+
Items is unchanged: another pass adds nothing. The closure has 5 items. 2 additions found the item already there.
Parsing DFA each state a closed set of items
| Method | LR(1) | LALR(1) | SLR(1) | LR(0) |
|---|---|---|---|---|
| States | 12 | 7 | 7 | 7 |
| Conflicts | 0 | 0 | 0 | 0 |
Every state’s transitions are made: 12 states, 13 edges.
Select a state or an edge label to mark its row or its entry in the table.
LR parsing a stack of pairs < sym, state >
- Let I = w $
- Let j = 0
- Let DFA state 0 be the start state
- Let stack = < dummy, 0 >
- repeat
- case action[top_state(stack), I[j]] of
- shift k : push < I[j++], k >
- reduce X → α :
- pop |α| pairs,
- push < X, Goto[top_state(stack), X] >
- accept: halt normally
- error: halt and report error
DFA transitions so far rerunning the DFA over the stack after each action: 40 reading the state stored in the top pair: 14
I j = 9
- 0 int
- 1 +
- 2 (
- 3 int
- 4 )
- 5 +
- 6 (
- 7 int
- 8 )
- 9 $ (I[j])
stack top on the right
- < dummy, 0 >
- < E, 2 >
E divider $
accept: halt normally accept
Action/goto table LR(1)
| State | int | + | ( | ) | $ | E |
|---|---|---|---|---|---|---|
| s1 | g2 | |||||
| rE→int | rE→int | |||||
| s3 | accept | |||||
| s4 | ||||||
| s5 | g6 | |||||
| rE→int | rE→int | |||||
| s8 | s7 | |||||
| rE→E+(E) | rE→E+(E) | |||||
| s9 | ||||||
| s5 | g10 | |||||
| s8 | s11 | |||||
| rE→E+(E) | rE→E+(E) |