Question

Which of the following languages is generated by the given grammar?

S→aS∣bS∣εS \to aS \mid bS \mid \varepsilon
Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2016 answer key

Solution

Trace the language produced by the recursive context-free grammar.

Given the grammar rules with start symbol SS:

S→aS∣bS∣εS \to aS \mid bS \mid \varepsilon

At each derivation step, SS can prepend either the terminal symbol aa or bb, or terminate by producing ε\varepsilon:

  • S  ⟹  εS \implies \varepsilon
  • S  ⟹  aS  ⟹  aε=aS \implies aS \implies a\varepsilon = a
  • S  ⟹  bS  ⟹  bε=bS \implies bS \implies b\varepsilon = b
  • S  ⟹  aS  ⟹  abS  ⟹  abaS  ⟹  abaS \implies aS \implies abS \implies abaS \implies aba

By induction, any arbitrary finite sequence of aa's and bb's can be generated by applying the corresponding production rules followed by S→εS \to \varepsilon. Therefore, the grammar generates the set of all binary strings over {a,b}\{a, b\}, which is {a,b}∗\{a, b\}^* (or (a+b)∗)(a + b)^*).

Final answer

D

Common trap

Assuming the grammar requires aa's before bb's, confusing S→aS∣bS∣εS \to aS \mid bS \mid \varepsilon with S→aS∣B,B→bB∣εS \to aS \mid B, B \to bB \mid \varepsilon.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 1
Question
26 of 65, printed as CS Q.16
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.