Proving That a Grammar Is LL(1) but Not SLR(1)

Proving That a Grammar Is LL(1) but Not SLR(1)

Verified Sources
Sep 11, 2026

Consider the grammar

S→AaAb∣BbBaA→ϵB→ϵ\begin{aligned} S &\rightarrow AaAb \mid BbBa\\ A &\rightarrow \epsilon\\ B &\rightarrow \epsilon \end{aligned}

where aa and bb are terminals, SS is the start symbol, and ϵ\epsilon denotes the empty string.

This section proves two different properties:

  1. The grammar is LL(1) because one input symbol is sufficient to choose every production in a predictive top-down parser.
  2. The grammar is not SLR(1) because its SLR parsing table contains a reduce–reduce conflict.

An LL(1) table is constructed using FIRST and FOLLOW sets. A grammar is LL(1) when no table cell receives more than one production. SLR(1), in contrast, constructs LR(0) item sets and places reductions according to FOLLOW sets; overlapping reductions create conflicts.

Footnotes

  1. Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions. ↩

  2. LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩

Key distinction

LL(1) and SLR(1) use different decision mechanisms. LL(1) chooses productions from the current nonterminal and next input token; SLR(1) chooses shift or reduce actions from LR(0) item states and FOLLOW sets.

Language generated by the grammar

Because both AA and BB derive ϵ\epsilon,

A⇒ϵandB⇒ϵ.A \Rightarrow \epsilon \qquad\text{and}\qquad B \Rightarrow \epsilon.

Therefore,

S⇒AaAb⇒abS \Rightarrow AaAb \Rightarrow ab

and

S⇒BbBa⇒ba.S \Rightarrow BbBa \Rightarrow ba.

Thus,

L(G)={ab,ba}.L(G)=\{ab,ba\}.

The two alternatives begin with different terminals:

  • AaAbAaAb begins with aa, because AA disappears and the next symbol is aa.
  • BbBaBbBa begins with bb, because BB disappears and the next symbol is bb.

This separation is the central reason the grammar is LL(1).

Computing FIRST and FOLLOW Sets

  1. 1
    Step 1

    Since both nonterminals derive epsilon, FIRST(A) = { ε } and FIRST(B) = { ε }.

  2. 2
    Step 2

    A can disappear, so the first visible symbol is terminal a. Therefore FIRST(AaAb) = { a }.

  3. 3
    Step 3

    B can disappear, so the first visible symbol is terminal b. Therefore FIRST(BbBa) = { b }.

  4. 4
    Step 4

    The alternatives have disjoint FIRST sets: FIRST(S) = { a, b }.

  5. 5
    Step 5

    Because S is the start symbol, the end-marker belongs to FOLLOW(S). Thus FOLLOW(S) = { }.

  6. 6
    Step 6

    In AaAb, the first A is followed by terminal a, and the second A is followed by terminal b. Hence FOLLOW(A) = { a, b }.

  7. 7
    Step 7

    In BbBa, the first B is followed by terminal b, and the second B is followed by terminal a. Hence FOLLOW(B) = { a, b }.

FIRST and FOLLOW summary

Nonterminal or productionFIRSTFOLLOW
AA{ϵ}\{\epsilon\}{a,b}\{a,b\}
BB{ϵ}\{\epsilon\}{a,b}\{a,b\}
SS{a,b}\{a,b\}\{\}$
AaAbAaAb{a}\{a\}—
BbBaBbBa{b}\{b\}—

The two alternatives for SS satisfy

FIRST(AaAb)∩FIRST(BbBa)={a}∩{b}=∅.FIRST(AaAb)\cap FIRST(BbBa) = \{a\}\cap\{b\} = \varnothing.

Although both alternatives contain nullable nonterminals, neither alternative itself derives ϵ\epsilon:

ϵ∉FIRST(AaAb),ϵ∉FIRST(BbBa).\epsilon\notin FIRST(AaAb), \qquad \epsilon\notin FIRST(BbBa).

Consequently, there is no FIRST/FIRST conflict and no FIRST/FOLLOW conflict for SS.

Constructing the LL(1) parsing table

For a production X→αX\rightarrow\alpha, place the production in M[X,t]M[X,t] for each t∈FIRST(α)t\in FIRST(\alpha). If ϵ∈FIRST(α)\epsilon\in FIRST(\alpha), place it under every symbol in FOLLOW(X)FOLLOW(X).

The terminals are aa, bb, and $$$.

Nonterminalaabb$$$
SSS→AaAbS\rightarrow AaAbS→BbBaS\rightarrow BbBaerror
AAA→ϵA\rightarrow\epsilonA→ϵA\rightarrow\epsilonerror
BBB→ϵB\rightarrow\epsilonB→ϵB\rightarrow\epsilonerror

Every populated table cell contains exactly one production. Therefore, the grammar is LL(1).

Formally,

FIRST(AaAb)∩FIRST(BbBa)=∅,FIRST(AaAb)\cap FIRST(BbBa)=\varnothing,

and the nullable productions for AA and BB are selected uniquely using their FOLLOW sets. Hence,

G is LL(1).\boxed{G\text{ is LL(1)}}.

Footnotes

  1. Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions. ↩

Testing the LL(1) Parser

  1. 1
    Step 1

    The first input symbol is a. The table selects S → AaAb. Then A → ε is selected because a ∈ FOLLOW(A), the terminal a is matched, the second A → ε is selected because b ∈ FOLLOW(A), and finally b is matched.

  2. 2
    Step 2

    The first input symbol is b. The table selects S → BbBa. Then B → ε is selected because b ∈ FOLLOW(B), the terminal b is matched, the second B → ε is selected because a ∈ FOLLOW(B), and finally a is matched.

  3. 3
    Step 3

    The parser may begin with a or b, but after the first terminal the required opposite terminal is not present. The input is rejected.

LL(1) derivation traces

For input abab:

S⇒AaAb⇒ϵaϵb⇒ab.\begin{aligned} S &\Rightarrow AaAb\\ &\Rightarrow \epsilon a\epsilon b\\ &\Rightarrow ab. \end{aligned}

For input baba:

S⇒BbBa⇒ϵbϵa⇒ba.\begin{aligned} S &\Rightarrow BbBa\\ &\Rightarrow \epsilon b\epsilon a\\ &\Rightarrow ba. \end{aligned}

The first lookahead symbol determines the production for SS:

LookaheadSelected production
aaS→AaAbS\rightarrow AaAb
bbS→BbBaS\rightarrow BbBa
$$$error

This is exactly the one-symbol prediction required by LL(1).

Fast LL(1) test

When alternatives contain nullable prefixes, skip those prefixes while computing FIRST. Here, A disappears before a and B disappears before b, so the alternatives are distinguished immediately by a and b.

Why the SLR(1) analysis is different

To test SLR(1), augment the grammar with a new start production:

S′→S.S'\rightarrow S.

An LR(0) item places a dot at every possible position in a production. For example,

S→AaAbS\rightarrow AaAb

produces

S→∙AaAbS→A∙aAbS→Aa∙AbS→AaA∙bS→AaAb∙.\begin{aligned} S&\rightarrow \bullet AaAb\\ S&\rightarrow A\bullet aAb\\ S&\rightarrow Aa\bullet Ab\\ S&\rightarrow AaA\bullet b\\ S&\rightarrow AaAb\bullet. \end{aligned}

An item with the dot at the end is a completed item and proposes a reduction.

For SLR(1), a completed item X→α∙X\rightarrow\alpha\bullet produces a reduce action only on terminals in FOLLOW(X)FOLLOW(X).

Footnotes

  1. LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩

Constructing the Relevant LR(0) Item Sets

  1. 1
    Step 1

    Add S' → S to identify the accepting configuration.

  2. 2
    Step 2

    The initial item is S' → ·S. Closure adds S → ·AaAb and S → ·BbBa.

  3. 3
    Step 3

    Because A → ε and B → ε, closure adds the completed items A → ε· and B → ε· to the initial state.

  4. 4
    Step 4

    The same LR(0) state contains both completed items A → ε· and B → ε·, while it also contains items that can proceed with terminals a and b.

  5. 5
    Step 5

    Since FOLLOW(A) = {a,b}, place reduce A → ε under a and b. Since FOLLOW(B) = {a,b}, place reduce B → ε under a and b.

  6. 6
    Step 6

    Each of the cells for lookahead a and b receives two reductions. Therefore, the SLR table is not single-valued.

Initial LR(0) item set

The closure of the initial item is

I0={S′→∙S,S→∙AaAb,S→∙BbBa,A→ϵ∙,B→ϵ∙}.\begin{aligned} I_0=\{& S'\rightarrow\bullet S,\\ &S\rightarrow\bullet AaAb,\\ &S\rightarrow\bullet BbBa,\\ &A\rightarrow\epsilon\bullet,\\ &B\rightarrow\epsilon\bullet \}. \end{aligned}

The two completed items are especially important:

A→ϵ∙andB→ϵ∙.A\rightarrow\epsilon\bullet \qquad\text{and}\qquad B\rightarrow\epsilon\bullet.

They both occur in the same LR(0) state.

The SLR reduction rule is:

  • reduce A→ϵA\rightarrow\epsilon on every terminal in FOLLOW(A)FOLLOW(A);
  • reduce B→ϵB\rightarrow\epsilon on every terminal in FOLLOW(B)FOLLOW(B).

Since

FOLLOW(A)=FOLLOW(B)={a,b},FOLLOW(A)=FOLLOW(B)=\{a,b\},

the actions are:

State I0I_0aabb
SLR actionreduce A→ϵA\rightarrow\epsilon and reduce B→ϵB\rightarrow\epsilonreduce A→ϵA\rightarrow\epsilon and reduce B→ϵB\rightarrow\epsilon

Each cell contains a reduce–reduce conflict.

Why this is a reduce–reduce conflict

On lookahead aa, the SLR parser sees two valid reductions:

A→ϵorB→ϵ.A\rightarrow\epsilon \qquad\text{or}\qquad B\rightarrow\epsilon.

On lookahead bb, it again sees two valid reductions:

A→ϵorB→ϵ.A\rightarrow\epsilon \qquad\text{or}\qquad B\rightarrow\epsilon.

Therefore,

FOLLOW(A)∩FOLLOW(B)={a,b}≠∅.FOLLOW(A)\cap FOLLOW(B) = \{a,b\} \neq\varnothing.

Because the completed items occur in the same LR(0) item set, SLR places both reductions in the same ACTION-table cells. This is precisely the SLR reduce–reduce conflict condition.

Thus,

G is not SLR(1).\boxed{G\text{ is not SLR(1)}}.

Footnotes

  1. LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩

Visual comparison of the two analyses

The apparent contrast is not contradictory:

  • Top-down parsing sees the first terminal of the complete SS alternative.
  • SLR parsing sees two completed nullable reductions in the same LR(0) state.
  • SLR uses the coarse FOLLOW approximation for reductions, so both reductions are enabled on both terminals.

Common Points of Confusion

Conflict Comparison

Number of competing actions in the critical parsing-table cells

LL(1) versus SLR(1)

1 / 6
Question · Term

What is FIRST(AaAb)?

Click to reveal
Answer · Definition

{a}\{a\}, because AA derives ϵ\epsilon and the first visible terminal is aa.

Exam-ready conclusion

Do not claim that the grammar is not SLR(1) merely because A and B both derive ε. The decisive proof is the same LR(0) state containing A → ε· and B → ε·, together with FOLLOW(A) ∩ FOLLOW(B) = {a,b}.

Final proof

For LL(1):

FIRST(AaAb)={a},FIRST(BbBa)={b}.FIRST(AaAb)=\{a\}, \qquad FIRST(BbBa)=\{b\}.

Therefore,

FIRST(AaAb)∩FIRST(BbBa)=∅.FIRST(AaAb)\cap FIRST(BbBa)=\varnothing.

Also,

FOLLOW(A)=FOLLOW(B)={a,b},FOLLOW(A)=FOLLOW(B)=\{a,b\},

so the epsilon productions are selected unambiguously. The LL(1) parsing table has no multiply defined entries. Hence,

G is LL(1).\boxed{G\text{ is LL(1)}}.

For SLR(1), the initial LR(0) state contains

A→ϵ∙andB→ϵ∙.A\rightarrow\epsilon\bullet \qquad\text{and}\qquad B\rightarrow\epsilon\bullet.

SLR places the reductions on their FOLLOW sets. Since

FOLLOW(A)∩FOLLOW(B)={a,b}≠∅,FOLLOW(A)\cap FOLLOW(B)=\{a,b\}\neq\varnothing,

both reductions are entered under both aa and bb, producing reduce–reduce conflicts. Hence,

G is not SLR(1).\boxed{G\text{ is not SLR(1)}}.

Therefore, the required result is

The grammar is LL(1) but not SLR(1).\boxed{\text{The grammar is LL(1) but not SLR(1).}}

Knowledge Check

Question 1 of 4
Q1Single choice

What is FIRST(AaAb) for the grammar?