Skip to main content

Grammar for aⁿbⁿcⁿ, n ≥ 1

Short answer

The language is not context-free; use a context-sensitive grammar. S → aSBC | aBC, CB → BC, aB → ab, bB → bb, bC → bc, cC → cc.

Construct a grammar to accept the language L = {aⁿbⁿcⁿ : n ≥ 1}.

L = { aⁿbⁿcⁿ : n ≥ 1 } is not context free, so an unrestricted (context sensitive) grammar is needed. Here every production has |left side| ≤ |right side|.

Grammar

G = ({S, B, C}, {a, b, c}, P, S) with productions

1. S  → aSBC
2. S  → aBC
3. CB → BC
4. aB → ab
5. bB → bb
6. bC → bc
7. cC → cc

Idea. Rules 1 and 2 generate n a's followed by n copies of B and C mixed together: aⁿ (BC)ⁿ. Rule 3 sorts them so that all B's come before all C's. Rules 4 to 7 convert the variables into terminals from left to right, and only in the correct order, so the result is always aⁿbⁿcⁿ.

Derivation of aabbcc (n = 2)

S ⇒ aSBC            (1)
  ⇒ aaBCBC          (2)
  ⇒ aaBBCC          (3: CB → BC)
  ⇒ aabBCC          (4: aB → ab)
  ⇒ aabbCC          (5: bB → bb)
  ⇒ aabbcC          (6: bC → bc)
  ⇒ aabbcc          (7: cC → cc)

Why only aⁿbⁿcⁿ is generated

The first B can become b only when it follows an a (rule 4), each later B only after a b (rule 5), and C can become c only after b or c (rules 6 and 7). If a C is turned into c while a B still stands to its right, that B is stuck after a c (there is no rule cB → …), so the derivation can never reach a terminal string. Hence the variables must be sorted into B…BC…C before the conversion finishes. The numbers of a's, B's and C's are always equal, since rules 1 and 2 create them together.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Theory of Computation chapter 4 questions →