GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 14(a)

Language of an unequal-count grammar

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

GATE 1998, Q14(a) Descriptive
Report issue

Context-Free Grammars & Languages

Question 14(a), GATE 1998: Find L(G1) for S1→aS1b|aAb|aBb, A→aA|a, B→Bb|b.
Question 14(a), GATE 1998: Find L(G1) for S1→aS1b|aAb|aBb, A→aA|a, B→Bb|b.
Tap to enlarge
Answer & solution

Answer: {a^i b^j : i,j≥1 and i≠j}. (no official key was published)

A generates exactly ar with r≥1: each recursive step adds one a and the final A→a supplies the last. Similarly B generates exactly bs with s≥1. The left recursion in B changes derivation direction, not its generated language or uniqueness for a fixed length.

After t≥0 uses of S1→aS1b, choosing S1→aAb and A⇒ar produces at+r+1bt+1. Both exponents are positive and the number of a's exceeds the number of b's by r≥1. Choosing S1→aBb instead gives at+1bt+s+1, whose b-count exceeds its a-count by s≥1. These are the only terminating choices, so equality of the counts is impossible.

Conversely, for any i>j≥1, choose t=j−1 and r=i−j, yielding ai bj through the A branch. For j>i≥1, choose t=i−1 and s=j−i through the B branch. Therefore every positive unequal pair occurs, proving L(G1)={ai bj:i,j≥1,i≠j}.

This grammar is also unambiguous. The comparison between i and j forces the A or B branch; the smaller exponent forces t; the difference forces r or s. The unary A or B derivation of that length is unique. This fact will be useful when answering the inherent-ambiguity question, but does not need to be assumed to establish the language.

Original paper, page 7