Skip to content
Official PYQ · 2026

GATE CS 2026 Set 1 Q42 — Regular and Context-free Languages

Explain with AI

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?

Select all correct options
View worked solution

Answer: B;D

To determine the relationship between the number of aa's (n1(s)n_1(s)) and bb's (n2(s)n_2(s)) in any string s∈L(G)s \in L(G), we analyze the contributions of each non-terminal symbol.

1. Analysis of BB:
From the productions B→aBb∣abB \rightarrow a B b \mid a b, every production adds exactly one aa and one bb. Thus, for any terminal string derived from BB: na(B)=nb(B)n_a(B) = n_b(B)

2. Analysis of AA:
Let us analyze the two productions of AA:

  • Base Case: A→bBabaaA \rightarrow b B a b a a
    This production explicitly contains 33 aa's and 22 bb's. Since BB contributes equal numbers of aa's and bb's (say k≥1k \ge 1 of each), we have: na(A)=3+k,nb(A)=2+kn_a(A) = 3 + k, \quad n_b(A) = 2 + k Clearly, na(A)−nb(A)=1n_a(A) - n_b(A) = 1 and na(A)≤2nb(A)n_a(A) \le 2 n_b(A) (since 3+k≤4+2k3 + k \le 4 + 2k for k≥1k \ge 1).
  • Inductive Step: A→aaBBAbA \rightarrow a a B B A b
    This production adds 22 aa's, 11 bb, two BB's (which add equal aa's and bb's), and one child AA. By induction, any terminal string derived from AA satisfies: na(A)>nb(A)andna(A)≤2nb(A)n_a(A) > n_b(A) \quad \text{and} \quad n_a(A) \le 2 n_b(A)

3. Analysis of SS:
The start production is S→abaABAbbaS \rightarrow a b a A B A b b a. The explicit terminals in this production consist of 33 aa's and 33 bb's. Thus, for any string s∈L(G)s \in L(G): n1(s)=3+na(A1)+na(A2)+na(B)n_1(s) = 3 + n_a(A_1) + n_a(A_2) + n_a(B) n2(s)=3+nb(A1)+nb(A2)+nb(B)n_2(s) = 3 + n_b(A_1) + n_b(A_2) + n_b(B) Using our previous results:

  • Since na(A)>nb(A)n_a(A) > n_b(A) and na(B)=nb(B)n_a(B) = n_b(B), we have: n1(s)−n2(s)=(na(A1)−nb(A1))+(na(A2)−nb(A2))≥1+1=2n_1(s) - n_2(s) = (n_a(A_1) - n_b(A_1)) + (n_a(A_2) - n_b(A_2)) \ge 1 + 1 = 2 Thus, n1(s)≥n2(s)n_1(s) \ge n_2(s) is always true (making B true and A false).
  • Since na(A)≤2nb(A)n_a(A) \le 2 n_b(A) and na(B)=nb(B)n_a(B) = n_b(B): n1(s)=3+na(A1)+na(A2)+na(B)≤6+2nb(A1)+2nb(A2)+2nb(B)=2n2(s)n_1(s) = 3 + n_a(A_1) + n_a(A_2) + n_a(B) \le 6 + 2n_b(A_1) + 2n_b(A_2) + 2n_b(B) = 2n_2(s) Thus, n1(s)≤2n2(s)n_1(s) \le 2 n_2(s) is always true (making D true and C false).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Regular and Context-free Languages questions