Skip to main content

GATE CSE 2026Theory of Computation › Context-Free Languages & Pushdown AutomataVerified

GATE CSE 2026 PYQ: Consider the following context-free grammar G G G . S → a b a A B A b b a A → a a B B A b ∣ b B a b a a B →…

Consider the following context-free grammar GG.

S→abaABAbbaA→aaBBAb∣bBabaaB→aBb∣ab \begin{array}{l} S \rightarrow a b a A B A b b a \\ A \rightarrow a a B B A b \mid b B a b a a \\ B \rightarrow a B b \mid a b \end{array}

In the above grammar, SS is the start symbol, aa and bb are terminal symbols, and AA and BB are non-terminal symbols.

Let L(G)L(G) be the language generated by the grammar GG. For a string s∈L(G)s \in L(G), let n1(s)n_{1}(s) be the number of aa 's in ss and n2(s)n_{2}(s) be the number of bb 's in ss.

Which of the following statements is/are true?

  1. A
    There is a string s∈L(G)s \in L(G) such that n1(s)<n2(s)n_{1}(s) < n_{2}(s)
  2. B
    For every string s∈L(G),n1(s)≥n2(s)s \in L(G), n_{1}(s) \geq n_{2}(s)
    Correct answer
  3. C
    There is a string s∈L(G)s \in L(G) such that n1(s)>2n2(s)n_{1}(s)>2 n_{2}(s)
  4. D
    For every string s∈L(G),n1(s)≤2n2(s)s \in L(G), n_{1}(s) \leq 2 n_{2}(s)
    Correct answer

Answer

(B, D) For every string s ∈ L ( G ) , n 1 ( s ) ≥ n 2 ( s ) s \in L(G), n_{1}(s) \geq n_{2}(s) s ∈ L ( G ) , n 1 ​ ( s ) ≥ n 2 ​ ( s ); For every string s ∈ L ( G ) , n 1 ( s ) ≤ 2 n 2 ( s ) s \in L(G), n_{1}(s) \leq 2 n_{2}(s) s ∈ L ( G ) , n 1 ​ ( s ) ≤ 2 n 2 ​ ( s )

Approach

Track the difference n1(s)-n2(s). The grammar is designed so B always generates equal a and b counts, each A contributes at least one extra a over b, and S combines two A's with balanced surrounding terminals and a balanced B. Then test each option using count inequalities.

The only true statements are B and D. B holds because each A-string has more a's than b's by at least 1, and S has two A's plus only balanced B and balanced terminals. D holds because the b-count grows at least as fast as the excess a-count, giving n1 &lt;= 2n2 for all generated strings.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.