Skip to content
Official PYQ · 2015

GATE CS 2015 Set 3 Q32 — Context-free Grammars and Push-down Automata

Explain with AI

Which of the following languages are context-free?

L1:{ambnanbm∣m,n≥1}L_1: \left\{a^mb^na^nb^m \mid m, n \geq 1\right\}

L2:{ambnambn∣m,n≥1}L_2: \left\{a^mb^na^mb^n \mid m, n \geq 1\right\}

L3:{ambn∣m=2n+1}L_3: \left\{a^mb^n \mid m = 2n +1 \right\}

Choose one option
View worked solution

Answer: B

To determine which of the given languages are context-free, we analyze each language individually:

1. Language L1={ambnanbm∣m,n≥1}L_1 = \{a^m b^n a^n b^m \mid m, n \geq 1\}

In L1L_1, the comparisons are nested (LIFO structure):

  • The inner bb's are matched with the inner aa's (bnb^n with ana^n).
  • The outer aa's are matched with the outer bb's (ama^m with bmb^m).

We can easily design a Pushdown Automaton (PDA) using a single stack: push aa's, push bb's, pop bb's for the next set of aa's, and then pop aa's for the final set of bb's.

Alternatively, we can write a Context-Free Grammar (CFG) for L1L_1:

S→aSb∣aAbA→bAa∣ba\begin{aligned} S &\rightarrow a S b \mid a A b \\ A &\rightarrow b A a \mid ba \end{aligned}

Since a CFG exists, L1L_1 is context-free.

2. Language L2={ambnambn∣m,n≥1}L_2 = \{a^m b^n a^m b^n \mid m, n \geq 1\}

In L2L_2, the comparisons are overlapping/cross-dependent (FIFO structure):

  • The first block of aa's must match the second block of aa's (am…ama^m \dots a^m).
  • The first block of bb's must match the second block of bb's (bn…bnb^n \dots b^n).

A single stack cannot maintain both of these independent, overlapping relationships simultaneously. Thus, L2L_2 is not context-free (it is a classic context-sensitive language).

3. Language L3={ambn∣m=2n+1}L_3 = \{a^m b^n \mid m = 2n + 1\}

For L3L_3, the number of aa's is exactly twice the number of bb's plus one. We can construct a CFG for this language as follows:

S→aAA→aaAb∣ab(for n≥1)\begin{aligned} S &\rightarrow a A \\ A &\rightarrow aaAb \mid ab \quad (\text{for } n \geq 1) \end{aligned}

Since we can generate L3L_3 using a CFG, L3L_3 is context-free.

Thus, only L1L_1 and L3L_3 are context-free languages.

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

Published
Report a correction
Keep exploringMore Context-free Grammars and Push-down Automata questions