GateAI
Context-Free Grammars and PDAs

GATE CSE 2026 Set 1 · Q52

MSQ2 marksMedium

Time

Consider the following context-free grammar GG. S→abaABAbbaS \to abaABAbba A→aaBBAb∣bBabaaA \to aaBBAb \mid bBabaa B→aBb∣abB \to aBb \mid ab 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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: B) For every string s ∈ L(G), n1(s) ≥ n2(s); D) For every string s ∈ L(G), n1(s) ≤ 2n2(s)

Context-Free Grammars and PDAs

Let na(w)n_a(w) and nb(w)n_b(w) denote the number of aa's and bb's in a string or sentential form ww.

  1. For non-terminal BB:

    B→aBb∣abB \to aBb \mid ab

    Any terminal string derived from BB has the form akbka^k b^k where k≥1k \ge 1. Therefore, for any string wBw_B derived from BB:

    na(wB)=nb(wB)≥1n_a(w_B) = n_b(w_B) \ge 1
  2. For non-terminal AA:

    • The production A→aaBBAbA \to aaBBAb introduces 22 terminal aa's, 11 terminal bb, two BB's, and one AA. The direct terminal contribution has Δ(na−nb)=2−1=+1\Delta(n_a - n_b) = 2 - 1 = +1, and the ratio of terminal aa's to bb's is 21=2\frac{2}{1} = 2.

    • The production A→bBabaaA \to bBabaa introduces 33 terminal aa's, 22 terminal bb's, and one BB. The direct terminal contribution has Δ(na−nb)=3−2=+1\Delta(n_a - n_b) = 3 - 2 = +1, and the ratio of terminal aa's to bb's is 32=1.5≤2\frac{3}{2} = 1.5 \le 2.

    Since BB always contributes equal numbers of aa's and bb's (na(wB)=nb(wB)n_a(w_B) = n_b(w_B)), any string wAw_A derived from AA satisfies:

    na(wA)>nb(wA)andna(wA)≤2nb(wA)n_a(w_A) > n_b(w_A) \quad \text{and} \quad n_a(w_A) \le 2 n_b(w_A)
  3. For the start symbol SS:

    S→abaABAbbaS \to abaABAbba

    The terminal prefix and suffix contribute 33 aa's and 33 bb's (na=3,nb=3n_a = 3, n_b = 3). The body contains 22 occurrences of AA and 11 occurrence of BB.

    Therefore, for every string s∈L(G)s \in L(G):

    n1(s)=3+2na(wA)+na(wB)n_1(s) = 3 + 2 n_a(w_A) + n_a(w_B)
    n2(s)=3+2nb(wA)+nb(wB)n_2(s) = 3 + 2 n_b(w_A) + n_b(w_B)
    • Since $n_a(w_A) > n_b(w_A)$ and $n_a(w_B) = n_b(w_B)$:

      n1(s)>n2(s)  ⟹  n1(s)≥n2(s)n_1(s) > n_2(s) \implies n_1(s) \ge n_2(s)

      Hence, (B) is true and (A) is false.

    • Since 3≤2(3)3 \le 2(3), na(wB)≤2nb(wB)n_a(w_B) \le 2 n_b(w_B), and na(wA)≤2nb(wA)n_a(w_A) \le 2 n_b(w_A) for all derived sub-strings:

      n1(s)≤2n2(s)n_1(s) \le 2 n_2(s)

      Hence, (D) is true and (C) is false.

Thus, statements (B) and (D) are true.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Context-Free Grammars and PDAs previous year question is Q52 of the GATE CSE 2026 Set 1 paper. Find more GATE CSE PYQs on Context-Free Grammars and PDAs in Theory of Computation, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Context-Free Grammars and PDAs PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Context-Free Grammars and PDAs in GATE CSE

Context-Free Grammars and PDAs has 26 GATE CSE PYQs across 17 papers from 2011 to 2026. 4 of them are 2-mark MSQs like this one, and GATE CSE 2026 Set 1 has 2.

How GATE tests Context-Free Grammars and PDAs

More from GATE CSE 2026 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 1 · Q52 · MSQ

Consider the following context-free grammar G. S → abaABAbba A → aaBBAb bBabaa B → aBb 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 ∈ L(G), let n1(s) be the number of a's in s and n2(s) be the number of b's in s. Which of the following statements is/are true?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.