Recall warm-up
- How many parse trees correspond to one leftmost derivation?
Stuck? Peek at 3.1.1 Context-free grammars, derivations and parse trees
- CFG: A → α, single variable on the left. CFL = L(G) for some CFG. Regular ⊂ CFL.
- Leftmost / rightmost derivations; parse tree with yield = leaves left to right.
- Trees ↔ leftmost derivations ↔ rightmost derivations (one-to-one per string).
- Grammar for palindromes over {a,b}?
Stuck? Peek at 2.3.1 What a grammar is
- G = (V, T, P, S); A → x | y; sentential form; ⇒, ⇒*; L(G) = {w ∈ T* : S ⇒* w}.
- S → aSb | ε gives aⁿbⁿ (not regular) — general grammars exceed DFAs.
- Palindromes: S → aSa | bSb | a | b | ε.
- Why is aⁿbⁿ not regular (pumping view)?
Stuck? Peek at 2.4.3 Pumping lemma for regular languages
- L regular ⇒ ∃p ∀w ∈ L, |w| ≥ p, ∃ xyz: |xy| ≤ p, |y| ≥ 1, ∀i ≥ 0: xyⁱz ∈ L. p = #states.
- Game: they pick p and split; you pick w and i. Beat every split.
- Standard picks: aᵖbᵖ; a^{p²}; aᵖbaᵖb (ww); prime q ≥ p+2 with i = q+1.
- Necessary, not sufficient. Finite languages pass vacuously.
The Hook
GATE regularly shows a grammar and asks “which language does it generate?”, or gives a language and four grammars. The trick is that CFGs build strings with a few patterns: wrap (grow both ends together), concatenate (glue independent parts), choose (union), and repeat. Recognize the pattern and both directions become fast.
Predict First
How would you generate { aⁿb²ⁿ : n ≥ 0 } with one variable?
Core Concept
Prediction check
S → aSbb | ε. Every a placed on the left is paired with two b’s on the right, wrapping from the outside in.
Pattern 1 — wrap (matched counts)
{aⁿbⁿ}: S → aSb | ε. {aⁿbᵐcᵐdⁿ}: S → aSd | T, T → bTc | ε (outer wrap, then inner wrap). Wrapping always matches a block on the left with a block on the right, nested.
Pattern 2 — palindromes
Even: S → aSa | bSb | ε. All: add | a | b for the middle symbol. Palindromes are a wrap where both ends must be the same symbol.
Pattern 3 — balanced parentheses
S → SS | (S) | ε (concatenate balanced pieces, or wrap one). An unambiguous alternative: S → (S)S | ε.
Pattern 4 — equal numbers of a’s and b’s
S → aSbS | bSaS | ε. Idea: look at the first symbol; if it is a, find the b that “balances” it; between them and after them are again balanced strings. Also correct: S → SS | aSb | bSa | ε.
Pattern 5 — unequal counts
{aⁱbʲ : i ≠ j} = {more a’s} ∪ {more b’s}. S → AC | CB, C → aCb | ε (equal core), A → aA | a (extra a’s, at least one), B → bB | b (extra b’s).
Union of grammars: new start S → S₁ | S₂. Concatenation: S → S₁S₂. Star: S → S₁S | ε.
Putting it together
- Four building moves: wrap, glue, choose, repeat.
- Wraps give nested matching; two counts that cross (aⁿbᵐcⁿdᵐ) cannot be wrapped.
- Always test ε and the shortest strings against a grammar.
Visual
language grammar pattern aⁿbⁿ S → aSb | ε wrap aⁿb²ⁿ S → aSbb | ε wrap 1:2 aⁿbᵐcᵐdⁿ S → aSd | T, T → bTc | ε nested wraps aⁿbⁿcᵐdᵐ S → XY, X → aXb | ε, Y → cYd | ε concatenation palindromes S → aSa | bSb | a | b | ε symmetric wrap balanced ( ) S → SS | (S) | ε concat + wrap #a = #b S → aSbS | bSaS | ε split at partner aⁱbʲ, i ≠ j S → AC | CB, C → aCb | ε, A → aA|a, B → bB|b union aⁱbʲcᵏ, i = j or j = k S → XC | AY, X → aXb | ε, Y → bYc | ε, A → aA | ε, C → cC | ε
Worked Examples
CFG for { aⁿ c bⁿ : n ≥ 0 }.
Show the full solution
S → aSb | c. Derive aacbb: S ⇒ aSb ⇒ aaSbb ⇒ aacbb.
CFG for { aⁱbʲcᵏ : i + k = j }.
Show the full solution
Split j into the part matched with a’s and the part matched with c’s: aⁱbⁱ bᵏcᵏ. S → XY, X → aXb | ε, Y → bYc | ε. Check abbc: X ⇒ ab, Y ⇒ bc ✓. Check aab b? a²b²c⁰ ✓; abc: i + k = 2 ≠ 1 ✗ (not derivable since X, Y each give equal counts).
Which language does G generate? S → aSa | aSb | bSa | bSb | c.
Show the full solution
Every step adds one symbol on each side, any symbols, and the middle is c. So L = { x c y : x, y ∈ {a,b}*, |x| = |y| } — strings with c exactly in the middle position (and no other c). Not a palindrome condition: the symbols are independent.
GATE Traps
- “Wrap” matches nested pairs: S → aSb generates aⁿbⁿ, never (ab)ⁿ-style interleavings.
- aSa | aSb | bSa | bSb only matches lengths, not symbols.
- For i ≠ j, don’t forget both sides of the union; each side needs at least one extra symbol.
- Test ε against every grammar: does S → … | ε let it in?
Memory Hook
Grammar patterns are moves in a game of building: wrap (both ends), glue (side by side), choose (union), repeat (star).
Connect the Dots
Some of these grammars give two different trees for the same string (S → SS | (S) | ε does, for ()()()). When a grammar does that, it is ambiguous — the topic of the next lesson, and a real problem for compilers.
Self-Test
- MCQ
Which grammar generates { aⁿbⁿ⁺¹ : n ≥ 0 }?
- S → aSb | b
- S → aSb | ε
- S → aSbb | b
- S → aS | b
- MSQ
S → aSbS | bSaS | ε generates which strings?
- abab
- aabb
- abba
- aab
- MCQ
S → XY, X → aXb | ε, Y → cYd | ε generates:
- aⁿbⁿcⁿdⁿ
- aⁿbⁿcᵐdᵐ
- aⁿbᵐcᵐdⁿ
- aⁿbᵐcⁿdᵐ
- NAT
Number of strings of length 4 generated by S → aSa | bSb | ε?
Write your answer before opening the key.
Rule Card
- Wrap: S → aSb | ε (aⁿbⁿ); ratios S → aSbb.
- Nested: outer wrap → inner wrap. Concatenation: S → XY. Union: S → S₁ | S₂. Star: S → S₁S | ε.
- Palindromes S → aSa | bSb | a | b | ε. Balanced S → SS | (S) | ε. #a = #b: S → aSbS | bSaS | ε.
- i ≠ j: S → AC | CB, C → aCb | ε, A → aA | a, B → bB | b.
Concept Ledger
- wrap pattern
- nested and concatenated grammars
- union / concatenation / star of CFGs
- balanced-parentheses grammar
- equal-count grammar
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- Exactly one.
- S → aSa | bSb | a | b | ε.
- y lies in the a’s, pumping changes only the a-count.
Self-test answers
- A — The final b is the extra one.
- B: Equal counts.
- C: Two b’s per a.
- D: a*b.
- ABC — Exactly the strings with #a = #b.
- D: 2 a’s, 1 b.
- B — Two independent wraps, concatenated.
- A: n and m are independent.
- C: That needs nesting.
- D: Crossing pairs — not even context-free.
- 4 — Even palindromes of length 4: aaaa, abba, baab, bbbb.
Flashcards
- CFG for aⁿbⁿcᵐdᵐ?
Reveal answer
S → XY, X → aXb | ε, Y → cYd | ε - CFG for aⁱbʲ with i ≠ j?
Reveal answer
S → AC | CB, C → aCb | ε, A → aA | a, B → bB | b - CFG for strings with #a = #b?
Reveal answer
S → aSbS | bSaS | ε - CFG for balanced parentheses?
Reveal answer
S → SS | (S) | ε (unambiguous: S → (S)S | ε)