Skip to content
Official PYQ · 2004

GATE CS 2004 Q88 — Parsing (LL, LR)

Explain with AI

Consider the following grammar G:

S→bS∣aA∣bS \rightarrow bS \mid aA \mid b

A→bA∣aBA \rightarrow bA \mid aB

B→bB∣aS∣aB \rightarrow bB \mid aS \mid a

Let Na(w)N_a(w) and Nb(w)N_b(w) denote the number of a’s and b’s in a string ω\omega respectively.

The language L(G)L(G) over  {a,b}+\left\{a, b\right\}^+ generated by GG is

Choose one option
View worked solution

Answer: C

We can analyze the grammar by tracking how the number of aa's, denoted by Na(w)N_a(w), changes during derivations. The grammar is right-linear, which can be modeled as a state transition diagram where SS, AA, and BB represent states, and bb-transitions do not change the state:

  • From state SS, generating an aa transitions to state AA (S→aAS \rightarrow aA).
  • From state AA, generating an aa transitions to state BB (A→aBA \rightarrow aB).
  • From state BB, generating an aa transitions back to state SS (B→aSB \rightarrow aS) or terminates with an aa (B→aB \rightarrow a).

Thus, the states track the number of aa's modulo 3:

  • State SS corresponds to Na(w)≡0(mod3)N_a(w) \equiv 0 \pmod 3.
  • State AA corresponds to Na(w)≡1(mod3)N_a(w) \equiv 1 \pmod 3.
  • State BB corresponds to Na(w)≡2(mod3)N_a(w) \equiv 2 \pmod 3.

A derivation can only terminate in one of two ways:

  1. Using S→bS \rightarrow b, which occurs when the count of aa's is a multiple of 3 (Na(w)=3kN_a(w) = 3k).
  2. Using B→aB \rightarrow a, which adds one final aa to a state where Na(w)≡2(mod3)N_a(w) \equiv 2 \pmod 3, resulting in Na(w)=3kN_a(w) = 3k.

Since bb-productions (S→bSS \rightarrow bS, A→bAA \rightarrow bA, B→bBB \rightarrow bB) do not change the state, there is no restriction on the number of bb's. Thus, the language generated is {w∣Na(w)=3k,k∈{0,1,2,… }}\{w \mid N_a(w) = 3k, k \in \{0, 1, 2, \dots\}\}.

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

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions