Skip to content
Official PYQ · 2016

GATE CS 2016 Set 1 Q42 — Context-free Grammars and Push-down Automata

Explain with AI

Consider the following context-free grammars;

G1:S→aS∣B,B→b∣bBG_1 : S \to aS \mid B, B \to b \mid bB

G2:S→aA∣bB,A→aA∣B∣ε,B→bB∣εG_2 : S \to aA \mid bB, A \to aA \mid B \mid \varepsilon,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
View worked solution

Answer: D

To find the languages generated by the context-free grammars G1G_1 and G2G_2, let us analyze them step-by-step:

1. Analysis of G1G_1

The grammar G1G_1 is given by:

S→aS∣BB→b∣bB\begin{aligned} S &\to aS \mid B \\ B &\to b \mid bB \end{aligned}

  • The variable BB generates one or more bb's: B⇒b+={bn∣n≥1}B \Rightarrow b^+ = \{b^n \mid n \ge 1\}
  • The start symbol SS can generate any number of aa's followed by BB: S⇒a∗B⇒a∗b+S \Rightarrow a^* B \Rightarrow a^* b^+

In set notation, this is represented as: L(G1)={ambn∣m≥0 and n>0}L(G_1) = \{ a^m b^n \mid m \ge 0 \text{ and } n > 0 \} This immediately eliminates options A, B, and C.

2. Analysis of G2G_2

The grammar G2G_2 is given by:

S→aA∣bBA→aA∣B∣εB→bB∣ε\begin{aligned} S &\to aA \mid bB \\ A &\to aA \mid B \mid \varepsilon \\ B &\to bB \mid \varepsilon \end{aligned}

Let us analyze the two production paths from the start symbol SS:

  • Case 1: Starting with S→aAS \to aA
    • BB generates any number of bb's (including zero): B⇒b∗B \Rightarrow b^*.
    • AA generates a∗Ba^* B or a∗a^*, which simplifies to a∗b∗a^* b^*.
    • Thus, S→aA⇒a(a∗b∗)=a+b∗S \to aA \Rightarrow a(a^* b^*) = a^+ b^*. This corresponds to strings ambna^m b^n where m>0m > 0 and n≥0n \ge 0.
  • Case 2: Starting with S→bBS \to bB
    • S→bB⇒b(b∗)=b+S \to bB \Rightarrow b(b^*) = b^+. This corresponds to strings ambna^m b^n where m=0m = 0 and n>0n > 0.

Combining both cases, the language L(G2)L(G_2) contains all strings of the form ambna^m b^n except the empty string ε\varepsilon (where m=0m=0 and n=0n=0). Therefore, at least one of mm or nn must be strictly greater than 00:

L(G2)={ambn∣m>0 or n>0}L(G_2) = \{ a^m b^n \mid m > 0 \text{ or } n > 0 \}

Thus, the correct pair of languages is given in Option D.

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

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