GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 14(b)

Extend the grammar in four rules

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

GATE 1998, Q14(b) Descriptive
Report issue

Context-Free Grammars & Languages

Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Tap to enlarge
Answer & solution

Answer: Add S2→TAB|S1T and T→aTb|ab, taking S2 as the new start symbol: four added alternatives. (no official key was published)

Keep every production of G1. Introduce new nonterminals S2 and T, take S2 as the new start symbol, and add exactly four individual productions:

S2 → TAB
S2 → S1T
T → aTb
T → ab

T generates precisely {an bn:n≥1}. Existing A and B generate a+ and b+, so AB generates all ak bl with k,l≥1. Consequently the first new start production TAB generates exactly the words with i=j and arbitrary positive k,l. The second production S1T generates exactly those with i≠j and k=l, because part (a) established the unequal-count language of S1.

These two cases together are equivalent to i=j or k=l. If the first equality holds, the first branch handles the word whether or not the second equality also holds. If the first equality fails, the language condition forces k=l, so the second branch handles it. Conversely every generated word satisfies one of those required equalities. Positive lengths of all four runs are guaranteed by T,A,B and S1.

The extension uses four alternatives, within the stated limit of five, even when each bar-separated alternative is counted separately. Reusing the unequal-count grammar is deliberate: the simpler-looking union “balanced first or balanced second” would have overlapping branches when both pairs are balanced. Here the two branches are disjoint, which also supplies a clean unambiguity proof for part (c).

Original paper, page 7