增广文法:
(0) S′→S
(1) S→SS+
(2) B→SS∗
(3) B→a
SLR项集如下:
GOTO 函数如下:
GOTO(I0,a)=I1 , GOTO(I0,S)=I2
GOTO(I2,a)=I1 , GOTO(I2,S)=I3 , GOTO(I2, $ )=accept
GOTO(I3,a)=I1 , GOTO(I3,S)=I3
GOTO(I3,+)=I4 , GOTO(I3,∗)=I5
语法分析表如下:
状态 | ACTION | GOTO | |||
---|---|---|---|---|---|
a | + | * | $ | S | |
0 | S1 | 2 | |||
1 | r3 | r3 | r3 | r3 | |
2 | S1 | accept | 3 | ||
3 | S1 | S4 | S5 | 3 | |
4 | r1 | r1 | r1 | r1 | |
5 | r2 | r2 | r2 | r2 |
无冲突,这显然是一个 SLR 文法
序号 | 栈 | 符号 | 输入 | 动作 |
---|---|---|---|---|
(1) | 0 | aa*a+$ | 移入 | |
(2) | 01 | a | a*a+$ | 按S->a规约 |
(3) | 02 | S | a*a+$ | 移入 |
(4) | 021 | Sa | *a+$ | 按S->a规约 |
(5) | 023 | SS | *a+$ | 移入 |
(6) | 0235 | SS* | a+$ | 按S->SS*规约 |
(7) | 02 | S | a+$ | 移入 |
(8) | 021 | Sa | +$ | 按S->a规约 |
(9) | 023 | SS | +$ | 移入 |
(10) | 0234 | SS+ | $ | 按S->SS+规约 |
(11) | 02 | S | $ | 接受 |
下证该文法是SLR(1)的:
SLR项集:
语法分析表:
状态 | ACTION | GOTO | ||
---|---|---|---|---|
a | $ | S | A | |
0 | S1 | S2 | S3 | |
1 | r3 | r3 | ||
2 | r2 | r2 | ||
3 | S! | acc | S4 | |
4 | r1 | r1 |
因为没有重复的冲突项,故该文法为SLR(1)的。
I0:
[S' -> ·S , $]
[S' -> ·SS+, $], [S' -> ·SS+, a]
[S' -> ·SS*, $], [S' -> ·SS*, a]
[S' -> ·a , $], [S' -> ·a , a]
I1:
[S' -> a· , $], [S' -> a· , a]
I2:
[S' -> S· , $]
[S' -> S·S+, $], [S' -> S·S+, a]
[S' -> S·S*, $], [S' -> S·S*, a]
[S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
[S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
[S' -> ·a , a], [S' -> ·a , *], [S' -> ·a , +]
I3:
[S' -> a· , a], [S' -> a· , *], [S' -> a· , +]
I4:
[S' -> SS·+, $], [S' -> SS·+, a]
[S' -> SS·*, $], [S' -> SS·*, a]
[S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
[S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
[S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
[S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
[S' -> ·a , a], [S' -> ·a , *], [S' -> ·a , +]
I5:
[S' -> SS+·, $], [S' -> SS+·, a]
I6:
[S' -> SS*·, $], [S' -> SS*·, a]
I7:
[S' -> SS·+, a], [S' -> SS·+, *], [S' -> SS·+, +]
[S' -> SS·*, a], [S' -> SS·*, *], [S' -> SS·*, +]
[S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
[S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
[S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
[S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
[S' -> ·a , a], [S' -> ·a , *], [S' -> ·a , +]
I8:
[S' -> SS+·, a], [S' -> SS+·, *], [S' -> SS+·, +]
I9:
[S' -> SS*·, a], [S' -> SS*·, *], [S' -> SS*·, +]
I0:
[S' -> ·S , $]
[S' -> ·SS+, $], [S' -> ·SS+, a]
[S' -> ·SS*, $], [S' -> ·SS*, a]
[S' -> ·a , $], [S' -> ·a , a]
I1:
[S' -> S· , $]
[S' -> S·S+, $], [S' -> S·S+, a]
[S' -> S·S*, $], [S' -> S·S*, a]
[S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
[S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
[S' -> ·a , a], [S' -> ·a , *], [S' -> ·a , +]
I2:
[S' -> SS·+, a], [S' -> SS·+, *], [S' -> SS·+, +], [S' -> SS·+, $]
[S' -> SS·*, a], [S' -> SS·*, *], [S' -> SS·*, *], [S' -> SS·*, $]
[S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
[S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
[S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
[S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
[S' -> ·a , a], [S' -> ·a , *], [S' -> ·a , +]
I3:
[S' -> a· , a], [S' -> a· , *], [S' -> a· , +], [S' -> a· , $]
I4:
[S' -> SS+·, a], [S' -> SS+·, *], [S' -> SS+·, +], [S' -> SS+·, $]
I5:
[S' -> SS*·, a], [S' -> SS*·, *], [S' -> SS*·, +], [S' -> SS*·, $]