Question

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

G1:S→aSb∣T,T→cT∣ϵG_1: S \to a S b \mid T, \quad T \to c T \mid \epsilon
G2:S→bSa∣T,T→cT∣ϵG_2: S \to b S a \mid T, \quad T \to c T \mid \epsilon

The language L(G1)∩L(G2)L(G_1) \cap L(G_2) is

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2017 answer key

Solution

Find the languages generated by grammars G1G_1 and G2G_2, compute their set intersection, and classify the resulting language.

Grammar G1G_1 has productions:

S→aSb∣T,T→cT∣ϵS \to a S b \mid T, \quad T \to c T \mid \epsilon

Here TT generates any string in c∗c^*, and each application of S→aSbS \to a S b adds matched aa's and bb's. Thus:

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

Grammar G2G_2 has productions:

S→bSa∣T,T→cT∣ϵS \to b S a \mid T, \quad T \to c T \mid \epsilon

Similarly, TT generates c∗c^*, and each application of S→bSaS \to b S a adds matched bb's at the beginning and aa's at the end. Thus:

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

Now consider the intersection L=L(G1)∩L(G2)L = L(G_1) \cap L(G_2):

  • If n>0n > 0, any string in L(G1)L(G_1) starts with 'a'. But any string in L(G2)L(G_2) with k>0k > 0 starts with 'b'.
  • If n=0n = 0, strings in L(G1)L(G_1) are purely of the form cmc^m.
  • If k=0k = 0, strings in L(G2)L(G_2) are purely of the form cmc^m.

Therefore, the only strings common to both languages are those with n=0n = 0 and k=0k = 0:

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

The language c∗c^* is infinite and regular (recognized by a single-state DFA with a loop on cc). Hence, the intersection is not finite but regular.

Final answer

B

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

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 1
Question
37 of 65, printed as CS Q.37
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.