Which of the following grammars generates the language L? L = {WWR | W ∈ {a,…

2012

Which of the following grammars generates the language L?

L = {WWR | W ∈ {a, b}*}

Answer: B. S → aSa | bSb | εConceptA context-free grammar for even-length palindromes can build strings from the centre outward. Starting with ε, each recursive step must place the same…

  1. A.

    S → aSb | bSa | ε

  2. B.

    S → aSa | bSb | ε

  3. C.

    S → aSb | bSa | aSa | bSb | ε

  4. D.

    S → aSb | bSa | aSa | bSb

Attempted by 7 students.

Show answer & explanation

Correct answer: B

Concept

A context-free grammar for even-length palindromes can build strings from the centre outward.

Starting with ε, each recursive step must place the same terminal on both ends; this preserves reversal symmetry and increases the length by two.

Application

  1. Let W = x1x2…xn. Then WWR = x1x2…xnxn…x2x1, which is an even-length palindrome.

  2. The base derivation S ⇒ ε represents W = ε.

  3. The productions S → aSa and S → bSb add a matching pair around the current string, so every derived terminal string remains an even-length palindrome.

  4. Conversely, remove the equal outer symbols from any nonempty even-length palindrome. Repeating this process reaches ε, so the grammar derives every string of the required form.

Cross-check and contrast

  • Using only aSb and bSa places different symbols at the two ends at every step.

  • Allowing all four wrappers aSb, bSa, aSa, and bSb permits arbitrary pairs of outer symbols and therefore generates strings beyond the target language.

  • A grammar with no ε or other terminal-only base production cannot finish a derivation to a terminal string.

Result

Therefore, the required grammar is S → aSa | bSb | ε.

Explore the full course: Mppsc Assistant Professor Computer Science Paper 2

Loading lesson…