Question:

Consider the following context-free grammar 𝐺.
π‘†β†’π‘Žπ‘π‘Žπ΄π΅π΄π‘π‘π‘Ž
π΄β†’π‘Žπ‘Žπ΅π΅π΄π‘ | π‘π΅π‘Žπ‘π‘Žπ‘Ž
π΅β†’π‘Žπ΅π‘ | π‘Žπ‘
In the above grammar, 𝑆 is the start symbol, π‘Ž and 𝑏 are terminal symbols, and 𝐴 and
𝐡 are non-terminal symbols.
Let 𝐿(𝐺) be the language generated by the grammar 𝐺. For a string π‘ βˆˆπΏ(𝐺), let
𝑛1(𝑠) be the number of π‘Žβ€™s in 𝑠 and 𝑛2(𝑠) be the number of 𝑏’s in 𝑠.
Which of the following statements is/are true?

Show Hint

First prove B always has an equal number of a's and b's by induction on \(B \to aBb \mid ab\). Then show A always contributes at least one extra a over b (from \(A \to aaBBAb \mid bBabaa\)), and bound how large that excess can get relative to A's b-count. Finally substitute two copies of A and one B into S's production \(S \to abaABAbba\) to get bounds on \(n_1(s)-n_2(s)\) and \(n_1(s)-2n_2(s)\).
Updated On: Jul 7, 2026
  • There is a string π‘ βˆˆπΏ(𝐺) such that 𝑛1(𝑠) < 𝑛2(𝑠)
  • For every string π‘ βˆˆπΏ(𝐺), 𝑛1(𝑠) β‰₯𝑛2(𝑠)
  • There is a string π‘ βˆˆπΏ(𝐺) such that 𝑛1(𝑠) > 2𝑛2(𝑠)
  • For every string π‘ βˆˆπΏ(𝐺), 𝑛1(𝑠) ≀2𝑛2(𝑠)
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B, D

Solution and Explanation

Step 1: B always produces equal a's and b's.

\(B \to aBb \mid ab\). The base case \(ab\) has one \(a\) and one \(b\). Each application of \(aBb\) wraps an already-balanced string with one more \(a\) and one more \(b\). So by induction every derivation of \(B\) is balanced: if \(n_1(B)\) and \(n_2(B)\) denote its a-count and b-count, \(n_1(B) = n_2(B) = k\) for some \(k \ge 1\).

Step 2: A always has strictly more a's than b's.

\(A \to aaBBAb \mid bBabaa\).

Using \(bBabaa\): direct terminals are \(b,a,b,a,a\) (3 a's, 2 b's) plus one balanced \(B\). So \(n_1(A) - n_2(A) = 3-2 = 1\).

Using \(aaBBAb\): direct terminals are \(a,a,b\) (2 a's, 1 b) plus two balanced B's (they cancel in the difference) plus a nested \(A'\). So \(n_1(A) - n_2(A) = 1 + [n_1(A') - n_2(A')]\).

So if the recursive rule is used \(m \ge 0\) times before the base rule fires, \(n_1(A) - n_2(A) = m+1 \ge 1\). Hence every derivation of A produces strictly more a's than b's.

Step 3: A also satisfies \(n_1(A) \le \frac{4}{3} n_2(A)\).

Tracking \(n_2(A)\) with minimal B's (\(k=1\)) at each level: base case gives \(n_2(A) = 2+1 = 3\), and every recursive level adds \(1 + 1 + 1 = 3\) to \(n_2(A)\) but only \(1\) to the excess computed in Step 2. So \(n_2(A) = 3(m+1) = 3[n_1(A)-n_2(A)]\), which gives \(n_1(A) = \frac{4}{3}n_2(A)\) in this extreme case; using larger B's only dilutes this ratio further toward 1. So for every derivation of A: \[ n_2(A) < n_1(A) \le \frac{4}{3}n_2(A) \]

Step 4: Combine for S.

\(S \to abaABAbba\). Direct terminals \(a,b,a,b,b,a\) give 3 a's and 3 b's (equal contribution), plus two copies of \(A\) and one balanced \(B\). So: \[ n_1(s) - n_2(s) = [n_1(A_1)-n_2(A_1)] + [n_1(A_2)-n_2(A_2)] \ge 1+1 = 2 \] This proves \(n_1(s) \ge n_2(s)\) (in fact strictly greater) for every string in \(L(G)\) -- so statement (B) is true and (A), which claims a string with \(n_1 < n_2\), is false.

Also, using \(n_1(A_i) \le \frac{4}{3}n_2(A_i)\) with \(n_2(A_i) \ge 3\): \[ n_1(s) - 2n_2(s) = -3 + [n_1(A_1)-2n_2(A_1)] + [n_1(A_2)-2n_2(A_2)] + [n_1(B)-2n_2(B)] \] Each A-bracket is \(\le -\frac{2}{3}n_2(A_i) \le -2\), and the B-bracket is \(-k \le -1\). So \(n_1(s) - 2n_2(s) \le -3-2-2-1 = -8 < 0\), i.e. \(n_1(s) < 2n_2(s)\) always. So statement (D) is true and (C), which claims a string with \(n_1 > 2n_2\), is false.

Final Answer:

\[ \boxed{\text{Options B and D are correct}} \]

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions