GATE Computer ScienceModerateGATE CS (PYQ pattern)

The language generated by S → aSb | bSa | SS | ε is

  1. A. { w : n_a(w) = n_b(w) and every prefix has n_a ≥ n_b }
  2. B. { w w' : w' is w with a and b swapped }
  3. C. { aⁿbⁿ : n ≥ 0 }*
  4. D. { w : n_a(w) = n_b(w) }

Correct answer: D. { w : n_a(w) = n_b(w) }

Any string with equally many a's and b's can be decomposed by splitting at the first point where the counts balance, so the grammar generates all of them; it is not limited to prefix-balanced strings.

More Theory of Computation questions

Exams that ask this topic