Question:hard

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

The Correct Option is B, D

Solution and Explanation

Step 1: Alternative approach - track \((a\text{-count},\ b\text{-count})\) as an explicit pair through the derivation instead of only the difference, and derive both a lower bound on the gap and an upper bound on the ratio side by side.
Step 2: Note first that B always yields matched strings \(a^m b^m\), so every occurrence of B in any derivation contributes an identical count of a's and b's - it can shift the ABSOLUTE size of the string but never the relative imbalance already built up elsewhere.
Step 3: Look at A's two productions as two "generators": generator 1 (\(aaBBAb\)) injects the fixed surplus pair (2 a, 1 b) plus two neutral B blocks plus a further nested call to A; generator 2 (\(bBabaa\)) injects the fixed surplus pair (3 a, 2 b) plus one neutral B block and stops the recursion. Every terminating derivation of A therefore is a chain of zero or more generator-1 calls capped off by exactly one generator-2 call, so the number of "fixed surplus" injections is \(k+1\) for \(k \geq 0\) uses of generator 1, each injection contributing net a-surplus of exactly \(+1\) (2-1 for generator 1, 3-2 for generator 2). Summing, \(a\)-count of A minus \(b\)-count of A is exactly \(k+1\), which can be 1, 2, 3, ... but never 0 or negative, regardless of how large the neutral B blocks are made.
Step 4: S wraps two independent A's inside a-b balanced literals (3 a, 3 b) and one neutral B, so the total surplus of S is the sum of the two A-surpluses, each at least 1, giving a total surplus of at least 2 for every derivable string, with no upper limit (it can be made arbitrarily large by nesting generator 1 many times in either A). This proves n1 always exceeds n2, ruling out option (A), and confirms option (B) (\(n_1 \geq n_2\), in fact strictly greater) unconditionally.
Step 5: For the ratio bound, observe that a fixed surplus pair like (2,1) or (3,2) has ratio 2 or 1.5 respectively, both under 2 already even before mixing with any balanced B material (ratio 1) which can only average the combined ratio down further, never up, since it is impossible to add a 1:1 (equal) quantity to two numbers already in a ratio above 1 and increase that ratio; the combined ratio always moves strictly between the two ratios being mixed, i.e. between 1 and the maximum "pure surplus" ratio, which itself is at most \(4/3\) once the two generators are chained together and their neutral B parts are folded in at minimum size. This directly caps every achievable \(n_1(s)/n_2(s)\) below \(4/3\), which is comfortably below the threshold of 2 used in options (C) and (D). So no string can reach ratio \(>2\) (option (C) fails) while every string safely satisfies ratio \(\leq 2\) (option (D) holds).
Step 6: Both routes - the exact difference bound and the ratio/mixing argument - agree: n1 is always strictly greater than n2 by at least 2, yet never more than roughly 1.33 times n2, so it always stays under twice n2.
\[ \boxed{\text{Correct options: (B) and (D)}} \]
Was this answer helpful?
0


Questions Asked in GATE CS exam