Skip to content
Official PYQ · 2017

GATE CS 2017 Set 1 Q37 — Context-free Grammars and Push-down Automata

Explain with AI

Consider the context-free grammars over the alphabet {a,b,c}\left \{ a, b, c \right \} given below. SS and TT are non-terminals.

G1:S→aSb∣T,T→cT∣ϵG_{1}:S\rightarrow aSb \mid T, T \rightarrow cT \mid \epsilon

G2:S→bSa∣T,T→cT∣ϵG_{2}:S\rightarrow bSa \mid T, T \rightarrow cT \mid \epsilon

The language L(G1)∩L(G2)L\left ( G_{1} \right )\cap L(G_{2}) is

Choose one option
View worked solution

Answer: B

To find the language L(G1)∩L(G2)L(G_1) \cap L(G_2), let us first determine the languages generated by the individual grammars G1G_1 and G2G_2.

1. Analyzing Grammar G1G_1

The productions are:

S→aSb∣TS \rightarrow aSb \mid T T→cT∣ϵT \rightarrow cT \mid \epsilon

The non-terminal TT generates any number of cc's, i.e., L(T)=c∗L(T) = c^*.

The non-terminal SS recursively wraps TT with an equal number of aa's on the left and bb's on the right. Thus:

L(G1)={ancmbn∣n≥0,m≥0}L(G_1) = \{a^n c^m b^n \mid n \ge 0, m \ge 0\}

2. Analyzing Grammar G2G_2

The productions are:

S→bSa∣TS \rightarrow bSa \mid T T→cT∣ϵT \rightarrow cT \mid \epsilon

Similarly, TT generates c∗c^*, and SS wraps it with an equal number of bb's on the left and aa's on the right. Thus:

L(G2)={bkcjak∣k≥0,j≥0}L(G_2) = \{b^k c^j a^k \mid k \ge 0, j \ge 0\}

3. Finding the Intersection L(G1)∩L(G2)L(G_1) \cap L(G_2)

Let w∈L(G1)∩L(G2)w \in L(G_1) \cap L(G_2). Then ww must satisfy the formats of both languages simultaneously:

w=ancmbn=bkcjakw = a^n c^m b^n = b^k c^j a^k
  • Case 1: n>0n > 0
    If n>0n > 0, the string ww starts with the symbol aa. For ww to also belong to L(G2)L(G_2), the string bkcjakb^k c^j a^k must start with aa. This is only possible if k=0k = 0 (so it doesn't start with bb) and j>0j > 0 with the first symbol being aa, which is impossible because cjc^j contains only cc's. Thus, no string with n>0n > 0 can belong to the intersection.
  • Case 2: n=0n = 0
    If n=0n = 0, then w=cmw = c^m. For this to match bkcjakb^k c^j a^k, we must have k=0k = 0, which gives w=cjw = c^j. This is perfectly consistent for any m=j≥0m = j \ge 0.

Therefore, the intersection contains only strings of the form cmc^m for m≥0m \ge 0:

L(G1)∩L(G2)={cm∣m≥0}=c∗L(G_1) \cap L(G_2) = \{c^m \mid m \ge 0\} = c^*

Conclusion

The language c∗c^* is infinite and can be represented by a regular expression, meaning it is regular. Thus, the language is not finite but regular.

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

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