Skip to content
Official PYQ · 2018

GATE CS 2018 Q35 — Context-free Grammars and Push-down Automata

Explain with AI

Consider the following languages:

  1. {ambncpdq∣m+p=n+q, where m,n,p,q≥0}\{a^mb^nc^pd^q \mid m+p=n+q, \text{ where } m, n, p, q \geq 0 \}
  2. {ambncpdq∣m=n and p=q, where m,n,p,q≥0}\{a^mb^nc^pd^q \mid m=n \text{ and }p=q, \text{ where } m, n, p, q \geq 0 \}
  3. {ambncpdq∣m=n=p and p≠q, where m,n,p,q≥0}\{a^mb^nc^pd^q \mid m=n =p \text{ and } p \neq q, \text{ where } m, n, p, q \geq 0 \}
  4. {ambncpdq∣mn=p+q, where m,n,p,q≥0}\{a^mb^nc^pd^q \mid mn =p+q, \text{ where } m, n, p, q \geq 0 \}

Which of the above languages are context-free?

Choose one option
View worked solution

Answer: B

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

  • Language I: L1={ambncpdq∣m+p=n+q}L_1 = \{a^mb^nc^pd^q \mid m+p=n+q\}

    The condition can be rewritten as m−n=q−pm - n = q - p. We can track this relation using a single stack (PDA):

    • Push for each aa, pop for each bb. If the stack becomes empty and more bb's arrive, push a placeholder for excess bb's.
    • Push for each cc (or pop if excess bb's are on the stack).
    • Pop for each dd.

    Since a single stack is sufficient to verify the net difference, L1L_1 is context-free.

  • Language II: L2={ambncpdq∣m=n and p=q}L_2 = \{a^mb^nc^pd^q \mid m=n \text{ and } p=q\}

    This language can be written as the concatenation of two context-free languages:

    L2={ambm∣m≥0}⋅{cpdp∣p≥0}L_2 = \{a^m b^m \mid m \geq 0\} \cdot \{c^p d^p \mid p \geq 0\}

    Since context-free languages are closed under concatenation, L2L_2 is context-free.

  • Language III: L3={ambncpdq∣m=n=p and p≠q}L_3 = \{a^mb^nc^pd^q \mid m=n=p \text{ and } p \neq q\}

    This requires matching three independent variables simultaneously (m=n=pm=n=p), which is a classic non-context-free structure (similar to {anbncn}\{a^n b^n c^n\}). Thus, L3L_3 is not context-free.

  • Language IV: L4={ambncpdq∣mn=p+q}L_4 = \{a^mb^nc^pd^q \mid mn = p+q\}

    The constraint involves multiplication of independent variables (mnmn), which cannot be tracked by a pushdown automaton. Thus, L4L_4 is not context-free.

Therefore, only languages I and II are 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