GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 84

Grammar for unequal a and b counts

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 2006 paper

GATE 2006, Q84 2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 84, GATE 2006: Which grammar generates precisely {a^i b^j:i,j≥0 and i≠j}? ε denotes the empty string.
Options:
(A) S→AC|CB; C→aCb|a|b; A→aA|ε; B→Bb|ε
(B) S→aS|Sb|a|b
(C) S→AC|CB; C→aCb|ε; A→aA|ε; B→Bb|ε
(D) S→AC|CB; C→aCb|ε; A→aA|a; B→Bb|b
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. In D, C→aCb|ε generates balanced ak bk. A→aA|a adds a positive number of a symbols, so S→AC generates all i>j.

2. B→Bb|b adds a positive number of b symbols, so S→CB generates all j>i. Together these are precisely ai bj with i≠j.

3. A permits cancellation of C’s ±1 imbalance by the other nonterminal: for example S⇒AC⇒aC⇒ab. B also generates ab.

4. C allows A/B→ε and therefore generates equal counts, including ε. Only D excludes every equal-count string while covering every unequal-count string.

5. Teaching check: C supplies equal counts, while A supplies at least one extra a or B at least one extra b. Because A and B cannot derive ε in D, every generated string has unequal counts. Conversely any unequal pair of nonnegative counts decomposes into a common balanced part and one positive excess.

Original paper, page 13