Question

Consider a context-free grammar GG with the following 3 rules.

S→aS,S→aSbS,S→cS \rightarrow aS, \quad S \rightarrow aSbS, \quad S \rightarrow c

Let w∈L(G)w \in L(G). Let na(w),nb(w),nc(w)n_a(w), n_b(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?

Choose every correct option
Official answer and solution
Official answer
B and C
From
Official GATE 2024 answer key

Solution

Count how many times each production rule is applied in any derivation of w∈L(G)w \in L(G).

In any derivation starting from SS, suppose the rule S→aSS \rightarrow aS is applied m≥0m \ge 0 times, and the rule S→aSbSS \rightarrow aSbS is applied k≥0k \ge 0 times:

  • Each application of S→aSbSS \rightarrow aSbS increases the number of nonterminal SS instances by 11, while S→aSS \rightarrow aS keeps the number of SS instances unchanged. Starting with 11 instance of SS, the total number of SS instances that eventually terminate via S→cS \rightarrow c is k+1k + 1.
  • Counting terminal occurrences in ww:
    • nb(w)=kn_b(w) = k
    • nc(w)=k+1=nb(w)+1  ⟹  n_c(w) = k + 1 = n_b(w) + 1 \implies (C) is TRUE (and (D) is FALSE).
    • na(w)=k+m≥k=nb(w)n_a(w) = k + m \ge k = n_b(w).
  • Checking (A): When m=0m = 0 (e.g., w=cw = c, where k=0,m=0k = 0, m = 0), na(w)=0=nb(w)n_a(w) = 0 = n_b(w), so na(w)>nb(w)n_a(w) > n_b(w) is FALSE.
  • Checking (B): Since na(w)=k+m≥kn_a(w) = k + m \ge k and nc(w)−2=(k+1)−2=k−1n_c(w) - 2 = (k + 1) - 2 = k - 1, we always have na(w)≥k>k−1=nc(w)−2n_a(w) \ge k > k - 1 = n_c(w) - 2, so (B) is TRUE.
Final answer

Options (B), (C)

Common trap

Forgetting the base case w=cw = c (where na(w)=nb(w)=0n_a(w) = n_b(w) = 0) and mistakenly selecting (A).

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

Source

Paper
GATE 2024 Computer Science and Information Technology - Set 2 (CS2)
Question
52 of 65
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.