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…
- A.
S → aSb | bSa | ε - B.
S → aSa | bSb | ε - C.
S → aSb | bSa | aSa | bSb | ε - 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
Let
W = x1x2…xn. ThenWWR= x1x2…xnxn…x2x1, which is an even-length palindrome.The base derivation
S ⇒ εrepresentsW = ε.The productions
S → aSaandS → bSbadd a matching pair around the current string, so every derived terminal string remains an even-length palindrome.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
aSbandbSaplaces different symbols at the two ends at every step.Allowing all four wrappers
aSb,bSa,aSa, andbSbpermits 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