Po pierwsze, dajmy swoim produkcjom numer.
1
2
3
4S→AaAb
S→BbBa
A→ε
B→ε
Obliczmy pierwszy i najpierw wykonajmy zestawy. W przypadku takich małych przykładów wystarczy intuicyjna obsługa tych zestawów.
FIRST(S)={a,b}FIRST(A)={}FIRST(B)={}FOLLOW(A)={a,b}FOLLOW(B)={a,b}
Teraz obliczmy tabelę . Z definicji, jeśli nie dostaniemy konfliktów, gramatyka to .LL(1)LL(1)
a | b |
-----------
S | 1 | 2 |
A | 3 | 3 |
B | 4 | 4 |
Ponieważ nie ma konfliktów, gramatyka to .LL(1)
Teraz tabela . Po pierwsze, automat .SLR(1)LR(0)
state 0S→∙AaAbS→∙BbBaA→∙B→∙A⟹1B⟹5
state 1S→A∙aAba⟹2
state 2S→Aa∙AbA→∙A⟹3
state 3S→AaA∙bb⟹4
state 4S→AaAb∙b
state 5S→B∙bBab⟹6
state 6S→Bb∙BaB→∙B⟹7
state 7S→BbB∙aa⟹8
state 8S→BbBa∙
And then the SLR(1) table (I assume S can be followed by anything).
a | b | A | B |
---------------------------
0 | R3/R4 | R3/R4 | 1 | 5 |
1 | S2 | | | |
2 | R3 | R3 | 3 | |
3 | | S4 | | |
4 | R1 | R1 | | |
5 | | S4 | | |
6 | R4 | R4 | | 7 |
7 | S8 | | | |
8 | R2 | R2 | | |
There are conflicts in state 0, so the grammar is not SLR(1). Note that if LALR(1) was used instead, then both conflicts would be resolved correctly: in state 0 on lookahead a LALR(1) would take R3 and on lookahead b it would take R4.
This gives rise to the interesting question whether there is a grammar that is LL(1) but not LALR(1), which is the case but not easy to find an example of.