Skip to content
Official PYQ · 2009

GATE CS 2009 Q12 — Context-free Grammars and Push-down Automata

Explain with AI

S→aSa∣bSb∣a∣bS \to aSa \mid bSb\mid a\mid b
The language generated by the above grammar over the alphabet {a,b}\{a,b\} is the set of:

Choose one option
View worked solution

Answer: B

To find the language generated by the given context-free grammar, let us analyze the production rules:

S→aSa∣bSb∣a∣bS \to aSa \mid bSb \mid a \mid b

1. Base Cases:

The non-recursive productions are:

  • S→aS \to a (length 1, palindrome)
  • S→bS \to b (length 1, palindrome)

These generate all palindromes of length 11 over the alphabet {a,b}\{a, b\}.

2. Recursive Steps:

The recursive productions are:

  • S→aSaS \to aSa
  • S→bSbS \to bSb

Each application of these rules wraps a previously generated string with the same character on both the left and right ends. This operation preserves the palindromic property (i.e., the string reads the same forwards and backwards) and increases the length of the string by exactly 22.

3. Conclusion:

  • Since we start with a base string of length 11 (which is odd) and repeatedly add 22 to the length at each step, any derived string must have an odd length.
  • Because of the symmetric wrapping, every derived string is a palindrome.

Therefore, the grammar generates the set of all odd-length palindromes.

Elimination of other options:

  • Option A: Incorrect, because even-length palindromes like aaaa or abbaabba cannot be generated.
  • Option C: Incorrect, because a string like aabaab begins and ends with different symbols, and even strings like abaaabaa (which begin and end with aa) are not palindromes and cannot be generated.
  • Option D: Incorrect, because all generated strings have an odd length.

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

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