Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q43 — Context-free Grammars and Push-down Automata

Explain with AI

Consider the following languages:

  • L1={anbmcn+m:m,n≥1}L_{1}=\left\{a^{n}b^{m}c^{n+m}:m, n\geq 1\right\}
  • L2={anbnc2n:n≥1}L_{2}=\left\{a^{n}b^{n}c^{2n} :n\geq 1\right\}

Which one of the following is TRUE?

Choose one option
View worked solution

Answer: B

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

1. Analysis of L1={anbmcn+m:m,n≥1}L_1 = \{a^n b^m c^{n+m} : m, n \ge 1\}

We can rewrite the condition for the number of cc's as n+mn + m. Since the aa's appear first, followed by bb's, and then cc's, we can match the cc's with the bb's first, and then with the aa's.

PDA Intuition (LIFO Stack):

  • Push all nn aa's onto the stack.
  • Push all mm bb's onto the stack.
  • For each incoming cc, pop a symbol from the stack. The first mm cc's will pop the mm bb's, and the subsequent nn cc's will pop the nn aa's.
  • If the stack becomes empty precisely when the input is fully consumed, the string is accepted.

Since this can be processed using a single stack, L1L_1 is a Context-Free Language (CFL).

Context-Free Grammar (CFG) for L1L_1:

S→aSc∣aAcA→bAc∣bc\begin{aligned} S &\to aSc \mid aAc \\ A &\to bAc \mid bc \end{aligned}

Here, SS generates anAcna^n A c^n and AA generates bmcmb^m c^m, resulting in anbmcm+na^n b^m c^{m+n}. Thus, L1L_1 is context-free.


2. Analysis of L2={anbnc2n:n≥1}L_2 = \{a^n b^n c^{2n} : n \ge 1\}

In L2L_2, there are two independent comparisons that must be maintained simultaneously:

  1. The number of aa's must equal the number of bb's.
  2. The number of cc's must be twice the number of aa's (or bb's).

A single stack cannot keep track of both comparisons. If we use the stack to match aa's and bb's, the stack becomes empty before we can verify the count of cc's.

Using the Pumping Lemma for CFLs:

  • Let pp be the pumping length. Choose the string z=apbpc2p∈L2z = a^p b^p c^{2p} \in L_2.
  • Any partition z=uvwxyz = uvwxy with ∣vwx∣≤p|vwx| \le p and ∣vx∣≥1|vx| \ge 1 cannot contain all three symbols (aa, bb, and cc).
  • If vxvx contains only aa's and bb's, pumping it changes the counts of aa and bb but leaves the count of cc unchanged, violating the c2nc^{2n} relation.
  • If vxvx contains only bb's and cc's, pumping it changes the counts of bb and cc but leaves the count of aa unchanged, violating the anbna^n b^n relation.

Thus, L2L_2 is not context-free.

Therefore, L1L_1 is context-free while L2L_2 is not context-free.

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

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