Question

Consider the following context-free grammars:

G1:S→aS∣B,B→b∣bBG_1 : S \to aS \mid B, \quad B \to b \mid bB
G2:S→aA∣bB,A→aA∣B∣ε,B→bB∣εG_2 : S \to aA \mid bB, \quad A \to aA \mid B \mid \varepsilon, \quad B \to bB \mid \varepsilon

Which one of the following pairs of languages is generated by G1G_1 and G2G_2, respectively?

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2016 answer key

Solution

Determine the set of strings generated by each grammar by analyzing the production rules from the start symbol.

Analysis of grammar G1G_1:

  • B→b∣bBB \to b \mid bB: The non-terminal BB generates one or more bb's, i.e., L(B)=b+={bn∣n>0}L(B) = b^+ = \{b^n \mid n > 0\}.
  • S→aS∣BS \to aS \mid B: The start symbol generates zero or more aa's followed by a derivation from BB. Thus, L(G1)=a∗b+={ambn∣m≥0 and n>0}L(G_1) = a^* b^+ = \{a^m b^n \mid m \ge 0 \text{ and } n > 0\}.

Analysis of grammar G2G_2:

  • B→bB∣εB \to bB \mid \varepsilon: Non-terminal BB generates zero or more bb's, i.e., L(B)=b∗={bn∣n≥0}L(B) = b^* = \{b^n \mid n \ge 0\}.
  • A→aA∣B∣εA \to aA \mid B \mid \varepsilon: AA generates zero or more aa's followed by BB or ε\varepsilon, which simplifies to L(A)=a∗b∗L(A) = a^* b^*.
  • S→aA∣bBS \to aA \mid bB:
    • The branch S→aAS \to aA generates a(a∗b∗)=a+b∗a(a^* b^*) = a^+ b^*, which is all strings of aa's and bb's with at least one aa (m>0,n≥0m > 0, n \ge 0).
    • The branch S→bBS \to bB generates b(b∗)=b+b(b^*) = b^+, which is all strings with only bb's and at least one bb (m=0,n>0m = 0, n > 0).
    • The union a+b∗∪b+a^+ b^* \cup b^+ contains all strings of the form ambna^m b^n where at least one of mm or nn is strictly positive (m>0 or n>0m > 0 \text{ or } n > 0). Note that the empty string ε\varepsilon cannot be generated by SS.

Comparing the languages:

  • L(G1)={ambn∣m≥0 and n>0}L(G_1) = \{a^m b^n \mid m \ge 0 \text{ and } n > 0\}
  • L(G2)={ambn∣m>0 or n>0}L(G_2) = \{a^m b^n \mid m > 0 \text{ or } n > 0\}
    This matches option D.
Final answer

D

Common trap

Overlooking that S→aAS \to aA requires at least one aa, and S→bBS \to bB requires at least one bb, so ε\varepsilon is not generated by G2G_2.

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
52 of 65, printed as CS Q.42
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.