Syntax analysis

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 · slides 16–21
  • Lecture reference: Bottom-up Parsing III · slides 5–14

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, $ }).

Closure order

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

Lecture reference: Bottom-up Parsing II · slide 21

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 · slide 15

  • 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 · slide 20

Closure and Transition one item at a time

Closure({ S → •E, $ }): the start state of the parsing DFA.

  1. Closure(Items) =
  2. repeat
  3. for each [X → α•Yβ, a] in Items
  4. for each production Y → γ
  5. for each b ∈ First(βa)
  6. add [Y → •γ, b] to Items
  7. until Items is unchanged

Items 5

  1. S → •E, $
  2. E → •E + ( E ), $
  3. E → •int, $
  4. E → •E + ( E ), +
  5. E → •int, +

Abbreviated 3

  1. S → •E, $
  2. E → •E + ( E ), $/+
  3. E → •int, $/+
Step 23 of 23

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
States and conflicts of each method
MethodLR(1)LALR(1)SLR(1)LR(0)
States12777
Conflicts0000
Drawing
Items
Step 15 of 15

Every state’s transitions are made: 12 states, 13 edges.

12 states
100%

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 >

Tokens separated by spaces; $ is added at the end (I = w $).
  1. Let I = w $
  2. Let j = 0
  3. Let DFA state 0 be the start state
  4. Let stack = < dummy, 0 >
  5. repeat
  6. case action[top_state(stack), I[j]] of
  7. shift k : push < I[j++], k >
  8. reduce X → α :
  9. pop |α| pairs,
  10. push < X, Goto[top_state(stack), X] >
  11. accept: halt normally
  12. 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

  1. 0 int
  2. 1 +
  3. 2 (
  4. 3 int
  5. 4 )
  6. 5 +
  7. 6 (
  8. 7 int
  9. 8 )

stack top on the right

  1. < dummy, 0 >
  2. < E, 2 >

E divider $

Step 36 of 36

accept: halt normally accept

Action/goto table LR(1)

Action/goto table, LR(1)
Stateint+()$E
s1g2
rE→intrE→int
s3accept
s4
s5g6
rE→intrE→int
s8s7
rE→E+(E)rE→E+(E)
s9
s5g10
s8s11
rE→E+(E)rE→E+(E)