Skip to content

For LR Parsing Algorithm, LR(0) items, SLR Parsing Table Construction

LR Parsing Algorithm

  1. Put the special symbol $ at the end of the input.
  2. Put state 0 at the bottom of the stack.
  3. In each iteration, suppose the current configuration is<0X1S1XmSmaian$>,the current state is Sm and the next input token is ai
    1. If ACTION[Sm,ai] is "shift S", then the next configuration is<0X1S1XmSmaiSan$>
    2. If ACTION[Sm,ai] is "reduce Aβ", then the next configuration is<0X1S1XmrSmrASan$>,where r=|β| and S=GOTO[Smr,A]. At the same time, the parser outputs AβSmr 就是下一个栈顶元素,换句话说就是当前状态的上一个状态 首先先判断归约的 Production Rule,可以知道当前的token 会被归约成哪个token 然后根据 Smr token,去找到下一个状态S
    3. If ACTION[Sm,ai] is "accept" and the current configuration is<0XS1$>,Where X is the start symbol of the grammar, the parser accepts the input.
    4. For all other cases, like ACTION[Sm,ai] is blank, the parser finds a syntax error and switch to error recovery.

Example

Stateid+*()$ETF
0s5s4123
1s6acc
2r2s7r2r2
3r4r4r4r4
4s5s4823
5r6r6r6r6
6s5s493
7s5s410
8s6s11
9r1s7r1r1
10r3r3r3r3
11r5r5r5r5
StackInputOutput
0id + id * id$
0id 5+ id * id$shift 5
0F 3+ id * id$reduce F → id
0T 2+ id * id$reduce T → F
0E 1+ id * id$reduce E → T
0E 1 + 6id * id$shift 6
0E 1 + 6 id 5* id$shift 5
0E 1 + 6 F 3* id$reduce F → id
0E 1 + 6 T 9* id$reduce T → F
0E 1 + 6 T 9 * 7id$shift 7
0E 1 + 6 T 9 * 7 id 5$shift 5
0E 1 + 6 T 9 * 7 F 10$reduce F → id
0E 1 + 6 T 9$reduce T → T * F
0E 1$reduce E → E + T
0E 1$accept