Context-Free Grammar for a Nonregular Language
๐ฑ The Classic Nonregular Language: L = { aโฟ bโฟ | n โฅ 0 }
This language contains strings like:
ฮต
ab
aabb
aaabbb
aaaabbbb
...
The rule is simple:
- The number of
as must be equal to the number ofbs. - All the
as must come before all thebs.
On the surface it seems easy, but DFAs cannot remember how many as they have seen.
They have no stack or memory.
But a CFG can build this language beautifully.
๐ฏ A Simple CFG for L = { aโฟ bโฟ }
We can define it with one elegant rule:
S โ aSb | ฮต
Letโs rewrite this in plain English:
- Replace
Switha S bโ this adds one โaโ in the front and one โbโ at the end - Or replace
Swithฮตโ this is the base case when n = 0
With just this one rule, you can generate strings with equal numbers of as and bs.
๐ก How the Grammar Builds Strings Step-by-Step
Letโs generate the string aaabbb.
S
โ aSb
โ aaSbb
โ aaaSbbb
โ aaabbb
Each step adds one a and one b โ perfectly balanced.
๐งฑ Why This Language Is Nonregular
Try to imagine a DFA recognizing this pattern:
- It reads
as first. - It must remember how many
as were seen. - When it switches to
bs, it must check that the number ofbs matches the number ofas.
A DFA has no memory stack.
It cannot โcountโ arbitrary lengths.
A CFG can, because its production rules expand symmetrically โ adding pairs.
๐ผ Parse Tree Diagram for โaabbโ
Below is a clear, easy-to-read parse tree:
S
____|____
a Sb
/ \
a S b
|
ฮต
If we read the leaves from left to right, we get:
a a b b
which forms the string aabb.
๐ Why This Example Is So Important
This language is often used in textbooks, classes, and exams because:
โ It is one of the simplest examples of a nonregular language
It passes the pumping lemma test for nonregularity.
โ It demonstrates the true strength of CFGs
CFGs handle nested, balanced, or counted structuresโthings regular languages cannot do.
โ It connects directly to Pushdown Automata (PDA)
A PDA can accept this language by pushing as onto the stack and popping one for each b.
โ It forms the foundation for understanding programming language syntax
Matching parentheses
Matching begin-end blocks
Matching HTML tags
All require this kind of nested structure.
