Question:

Consider the following context-free grammar \(G\).
\[ S \rightarrow abaABAbba \]\[ A \rightarrow aaBBAb \mid bBabaa \]\[ B \rightarrow aBb \mid ab \]In the above grammar, \(S\) is the start symbol, \(a\) and \(b\) are terminal symbols, and \(A\) and \(B\) are non-terminal symbols.
Let \(L(G)\) be the language generated by the grammar \(G\). For a string \(s \in L(G)\), let \(n_1(s)\) be the number of \(a\)'s in \(s\) and \(n_2(s)\) be the number of \(b\)'s in \(s\).
Which of the following statements is/are true?

Show Hint

B always generates equal a's and b's; track how much surplus of a's over b's each A-production injects, and note that mixing in equal (balanced) material only pulls a ratio toward 1, never away from it.
Updated On: Jul 22, 2026
  • There is a string \(s \in L(G)\) such that \(n_1(s) < n_2(s)\)
  • For every string \(s \in L(G),\ n_1(s) \geq n_2(s)\)
  • There is a string \(s \in L(G)\) such that \(n_1(s) > 2n_2(s)\)
  • For every string \(s \in L(G),\ n_1(s) \leq 2n_2(s)\)
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B, D

Solution and Explanation

Step 1: Concept - analyse each non-terminal's a-count minus b-count contribution.
\(B \rightarrow aBb \mid ab\) is a matched-pair rule: every derivation from \(B\) has the shape \(a^{m}b^{m}\) for some \(m \geq 1\), so B always contributes an EQUAL number of a's and b's, whatever value \(m\) takes. Call this "B is a-b balanced".
Step 2: Compute the a-minus-b balance contributed by A.
Production \(A \rightarrow aaBBAb\) has explicit terminals a,a,b, i.e. 2 a's and 1 b directly (net +1), plus two B's (balanced, net 0 each) plus a recursive A.
Production \(A \rightarrow bBabaa\) has explicit terminals b,a,b,a,a, i.e. 3 a's and 2 b's directly (net +1), plus one B (balanced, net 0), and no recursive A - this is the only way the A-recursion terminates.
So every full derivation of A applies the first production some \(k \geq 0\) times (each contributing net +1) and then applies the second production exactly once to terminate (contributing net +1). Hence for every string derivable from A, \(n_1 - n_2 = (k)(1) + 1 = k+1 \geq 1\). In particular this difference is ALWAYS a positive integer, never zero or negative, for any A-derived substring.
Step 3: Compute the a-minus-b balance contributed by S.
\(S \rightarrow abaABAbba\) has explicit terminals a,b,a,b,b,a i.e. 3 a's and 3 b's directly (net 0), plus two A's and one B. B is balanced (net 0). Each A contributes a value from \(\{1,2,3,\dots\}\) (Step 2), independently for the two occurrences of A. So for any \(s \in L(G)\):
\(n_1(s) - n_2(s) = 0 + (\text{first }A\text{'s net}) + (\text{second }A\text{'s net}) + 0 \geq 1+1 = 2\).
So \(n_1(s) - n_2(s) \geq 2\) for EVERY string in \(L(G)\), with no exceptions.
Step 4: Test options (A) and (B) using Step 3.
Since \(n_1(s) - n_2(s) \geq 2 > 0\) always, we always have \(n_1(s) > n_2(s)\), so \(n_1(s) < n_2(s)\) can NEVER happen: option (A) is FALSE. And \(n_1(s) > n_2(s)\) certainly implies \(n_1(s) \geq n_2(s)\), so option (B) is TRUE for every string.
Step 5: Bound the ratio \(n_1(s)/n_2(s)\) to test options (C) and (D).
Every B contributes equal amounts \(m,m\) to \((n_1,n_2)\); adding equal amounts to both a numerator and denominator that already satisfy numerator > denominator can only pull the ratio DOWN toward 1, never up. So the ratio is maximised by taking every B at its smallest size \(m=1\) (production \(B \to ab\)).
With every B minimal, one application of the first A-production contributes \((2,1)\) directly plus two minimal B's \((1,1)\) each, totalling \((a,b)=(4,3)\); the terminating second A-production contributes \((3,2)\) directly plus one minimal B \((1,1)\), also totalling \((4,3)\). So EVERY "layer" of the A-recursion contributes exactly the pair \((4,3)\), and after \(k\) uses of the first production plus the terminating second production, \(A\) contributes \((4(k+1),\ 3(k+1))\) - a ratio of exactly \(4/3\), for any \(k \geq 0\), whenever all internal B's are minimal.
For the full string from S (with both A's minimal-B, S's own literals \(3,3\), and the middle B minimal \(1,1\)): \(n_1(s) = 4 + 4(k_1+1) + 4(k_2+1)\), \(n_2(s) = 4 + 3(k_1+1) + 3(k_2+1)\), for any \(k_1,k_2 \geq 0\). As \(k_1,k_2\) grow, this ratio increases but only approaches \(4/3\) in the limit, and for every finite string it stays strictly below \(4/3\) (for example \(k_1=k_2=0\) gives \(12/10=1.2\); \(k_1=k_2=1\) gives \(20/16=1.25\)). Making any B larger than minimal only lowers the ratio further.
Step 6: Test options (C) and (D) using Step 5.
Since \(n_1(s)/n_2(s) < 4/3\) for every string, and \(4/3 < 2\), it is impossible to ever get \(n_1(s) > 2n_2(s)\) (that would need ratio \(>2\)). So option (C) is FALSE. Conversely \(n_1(s) \leq 2n_2(s)\) (ratio \(\leq 2\)) holds comfortably for every string, since the true ratio never even reaches \(4/3\). So option (D) is TRUE for every string.
Step 7: Conclusion.
(B) and (D) are always true; (A) and (C) are impossible, because \(n_1(s)\) exceeds \(n_2(s)\) by at least 2 always, yet the ratio \(n_1(s)/n_2(s)\) never reaches even \(4/3\), let alone 2.
\[ \boxed{\text{Correct options: (B) and (D)}} \]
Was this answer helpful?
0
0

Top GATE CS Theory of Computation Questions

View More Questions