GATE CS 2024 Set 2 — Question 52

MSQ+2 / -0MediumContext-Free GrammarsContext-Free LanguagesTheory of Computation

Theory of Computation → Context-Free Languages → Context-Free Grammars

Last updated

Question

Consider a context-free grammar GG with the following 3 rules.SaS,SaSbS,ScS \rightarrow aS, \quad S \rightarrow aSbS, \quad S \rightarrow cLet wL(G)w \in L(G). Let na(w)n_a(w), nb(w)n_b(w), nc(w)n_c(w) denote the number of times a,b,ca, b, c occur in ww, respectively. Which of the following statements is/are TRUE?
A.
na(w)>nb(w)n_a(w) > n_b(w)
B.
na(w)>nc(w)2n_a(w) > n_c(w) - 2
C.
nc(w)=nb(w)+1n_c(w) = n_b(w) + 1
D.
nc(w)=nb(w)2n_c(w) = n_b(w) * 2

Correct answer

(B) nₐ(w) n_c(w) - 2; (C) n_c(w) = n_b(w) + 1

Solution

Let k1,k2,k3k_1, k_2, k_3 be the number of times the rules SaSS \rightarrow aS, SaSbSS \rightarrow aSbS, and ScS \rightarrow c are applied in the derivation of a string ww, respectively.
We track the number of non-terminals SS and the terminals generated:
1.Start: We begin with 1 SS.
2.Rule 1 (SaSS \rightarrow aS): Replaces 1 SS with 1 SS. Net change in SS is 00. Adds 1 aa.
3.Rule 2 (SaSbSS \rightarrow aSbS): Replaces 1 SS with 2 SS's. Net change in SS is +1+1. Adds 1 aa and 1 bb.
4.Rule 3 (ScS \rightarrow c): Replaces 1 SS with 0 SS's. Net change in SS is 1-1. Adds 1 cc.
For a derivation to terminate and produce a string wL(G)w \in L(G), the final number of SS's must be 0. Starting from 1 SS, the total change must be 1-1.0k1+1k2+(1)k3=10 \cdot k_1 + 1 \cdot k_2 + (-1) \cdot k_3 = -1k2k3=1    k3=k2+1k_2 - k_3 = -1 \implies k_3 = k_2 + 1The number of terminals in ww are:
  • na(w)=k1+k2n_a(w) = k_1 + k_2 (since rules 1 and 2 produce 'a')
  • nb(w)=k2n_b(w) = k_2 (only rule 2 produces 'b')
  • nc(w)=k3n_c(w) = k_3 (only rule 3 produces 'c')
Now we evaluate the options:
(C) nc(w)=nb(w)+1n_c(w) = n_b(w) + 1
Substituting the counts: k3=k2+1k_3 = k_2 + 1. This matches our derived constraint exactly. Thus, this statement is TRUE.
(B) na(w)>nc(w)2n_a(w) > n_c(w) - 2
Substitute nc(w)=nb(w)+1n_c(w) = n_b(w) + 1:na(w)>(nb(w)+1)2n_a(w) > (n_b(w) + 1) - 2na(w)>nb(w)1n_a(w) > n_b(w) - 1Since na(w)=k1+k2n_a(w) = k_1 + k_2 and nb(w)=k2n_b(w) = k_2, this becomes:k1+k2>k21    k1>1k_1 + k_2 > k_2 - 1 \implies k_1 > -1Since k1k_1 is a count of rule applications, k10k_1 \ge 0. Therefore, k1>1k_1 > -1 is always true. Thus, this statement is TRUE.
(A) na(w)>nb(w)n_a(w) > n_b(w)k1+k2>k2    k1>0k_1 + k_2 > k_2 \implies k_1 > 0This is not always true. For example, the derivation ScS \rightarrow c has k1=0k_1=0. Thus, this statement is FALSE.
(D) nc(w)=nb(w)2n_c(w) = n_b(w) * 2k2+1=2k2    k2=1k_2 + 1 = 2 k_2 \implies k_2 = 1This is not always true. For example, ScS \rightarrow c has k2=0k_2=0, so 101 \neq 0. Thus, this statement is FALSE.
Correct options are (B) and (C).

More questions on Context-Free Languages

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free