I've recently implemented an LR(1) parser (without epsilon), and have been wondering how I would implement epsilon in the parsing algorithm (note: not the table construction algorithm). This is my grammar:
start -> A b
b -> B | \epsilon
I've followed the steps listed here and got this as a result from my parser (table):
state 0 on symbol start: 1
state 0 on symbol A: shift and goto state 2
state 1 on symbol $: accept
state 2 on symbol b: 3
state 2 on symbol B: shift and goto state 4
state 2 on symbol $: reduce b →
state 3 on symbol $: reduce start → A b
state 4 on symbol $: reduce b → B
The table listed above is correct, but when I try to parse A
, there is no transition:
error: no transition in table for (0, 'b')
This is how my stack looks:
[0]
[0, 'A', 2]
[0, 'b'] # stack during the error
Now, I notice that there is not a state on the top, which is a problem, but I have no clue what to add after it. My paring code is based upon the one over here.