Context Free Grammars and Languages

GATE CS previous year questions · 5 questions

GATE CS 2025 Set 1 | Question 19

MCQ · 1 mark · −0.33

Consider the following context-free grammar \(G\), where \(S\), \(A\), and \(B\) are the variables (non-terminals), \(a\) and \(b\) are the terminal symbols, \(S\) is the start variable, and the rules of \(G\) are described as:

\(S \to aaB \mid Abb\)
\(A \to a \mid aA\)
\(B \to b \mid bB\)

Which ONE of the languages \(L(G)\) is accepted by \(G\)?

Options for GATE CS 2025 Set 1 | Question 19

Explanation

Answer: (A)

\(A \to a \mid aA\) generates \(a^{n}\) and \(B \to b \mid bB\) generates \(b^{n}\), for \(n \ge 1\).

So \(S \to aaB\) gives the strings \(a^{2}b^{n}\), and \(S \to Abb\) gives the strings \(a^{n}b^{2}\). Together, \(L(G) = \{a^{2}b^{n} \mid n \ge 1\} \cup \{a^{n}b^{2} \mid n \ge 1\}\).

The other options do not match: for example, \(aabbb \in L(G)\), but it belongs to none of (B), (C) and (D).

Answer: A

GATE CS 2025 Set 1 | Question 45

MCQ · 2 marks · −0.66

Consider the following two languages over the alphabet \(\{a, b, c\}\), where \(m\) and \(n\) are natural numbers.

\[ L_1 = \{a^m b^m c^{m+n} \mid m, n \ge 1\} \]

\[ L_2 = \{a^m b^n c^{m+n} \mid m, n \ge 1\} \]

Which ONE of the following statements is CORRECT?

Options for GATE CS 2025 Set 1 | Question 45

Explanation

Answer: (C)

  • \(L_2\) is context-free: \(a^m b^n c^{m+n} = a^m\,(b^n c^n)\,c^m\) is a nested pattern, generated by \(S \to aSc \mid aAc\), \(A \to bAc \mid bc\). A PDA pushes one symbol for every \(a\) and \(b\) and pops one for every \(c\).
  • \(L_1\) is not context-free: it needs \(\#a = \#b\) and also \(\#c \gt \#a\) (because \(n \ge 1\)), so the same count \(m\) must be compared twice, but the single stack is used up matching the a's against the b's.
  • Pumping lemma check: take \(a^p b^p c^{p+1}\). The pumped part (length \(\le p\)) cannot touch both a's and c's. If it contains a's or b's, pumping up gives \(\#a \ne \#b\) or \(\#a = \#b \ge p + 1 = \#c\); if it contains only c's, pumping down gives \(\#c \le p = \#a\). Either way the string leaves \(L_1\).

Answer: C

GATE CS 2025 Set 2 | Question 30

MSQ · 1 mark

Consider the two lists List I and List II given below:

List IList II
(i) Context free languages(a) Closed under union
(ii) Recursive languages(b) Not closed under complementation
(iii) Regular languages(c) Closed under intersection

For matching of items in List I with those in List II, which of the following option(s) is/are CORRECT?

Options for GATE CS 2025 Set 2 | Question 30

Explanation

Answer: (B), (C)

Test each class against each property:

  • Context-free languages are closed under union but not under intersection or complementation. For example, with \(m, n \ge 0\), \(\{a^{n}b^{n}c^{m}\}\) and \(\{a^{m}b^{n}c^{n}\}\) are context-free, but their intersection \(\{a^{n}b^{n}c^{n}\}\) is not. So (i) fits (a) and (b), not (c).
  • Recursive languages are closed under union, intersection and complementation (swap a decider's accept and reject answers). So (ii) fits (a) and (c), not (b).
  • Regular languages are closed under all three as well, so (iii) fits (a) and (c), not (b).

Option A fails on (ii) – (b) and option D on (iii) – (b), while every pair in B and C is true.

Answer: B, C

GATE CS 2024 Set 1 | Question 59

NAT · 2 marks

Let \(G = (V, \Sigma, S, P)\) be a context-free grammar in Chomsky Normal Form with \(\Sigma = \{a, b, c\}\) and \(V\) containing 10 variable symbols including the start symbol \(S\). The string \(w = a^{30}b^{30}c^{30}\) is derivable from \(S\). The number of steps (application of rules) in the derivation \(S \rightarrow^{*} w\) is _________

Explanation

Answer: 179

In Chomsky Normal Form every rule has the form \(A \rightarrow BC\) or \(A \rightarrow a\), and \(|w| = 90\).

  • Each \(A \rightarrow BC\) step adds one symbol to the sentential form. Growing from the single symbol \(S\) to 90 symbols takes exactly 89 such steps.
  • Each \(A \rightarrow a\) step turns one variable into one terminal, and every one of the 90 terminals is produced this way: 90 steps.

The derivation therefore has \(89 + 90 = 179\) steps. In general a CNF derivation of a string of length \(n \ge 1\) takes \(2n - 1\) steps; the 10 variables do not affect the count.

Answer: 179

GATE CS 2024 Set 2 | Question 52

MSQ · 2 marks

Consider a context-free grammar \(G\) with the following 3 rules.

\(S \rightarrow aS,\ S \rightarrow aSbS,\ S \rightarrow c\)

Let \(w \in L(G)\). Let \(n_a(w),\ n_b(w),\ n_c(w)\) denote the number of times \(a, b, c\) occur in \(w\), respectively. Which of the following statements is/are TRUE?

Options for GATE CS 2024 Set 2 | Question 52

Explanation

Answer: (B), (C)

Follow the counts through a derivation, which starts with one \(S\). The rule \(S \rightarrow aSbS\) adds one \(a\), one \(b\) and one extra \(S\); \(S \rightarrow aS\) adds one \(a\); \(S \rightarrow c\) turns one \(S\) into \(c\). Every \(S\) must finally become a \(c\), so:

  • \(n_c(w) = 1 + n_b(w)\), so (C) is true.
  • Every \(b\) arrives with an \(a\), so \(n_a(w) \ge n_b(w) = n_c(w) - 1 \gt n_c(w) - 2\), and (B) is true.

The string \(w = c\) has \(n_a = n_b = 0\) and \(n_c = 1\). It breaks (A), since \(0 \gt 0\) is false, and (D), since \(1 \ne 0\).

Answer: B, C