Skip to content
Official PYQ · 2007

GATE CS 2007 Q78 — Parsing (LL, LR)

Explain with AI

Consider the CFG with {S,A,B}\left\{S, A, B\right\} as the non-terminal alphabet, {a,b}\{a, b\} as the terminal alphabet, SS as the start symbol and the following set of production rules:

S→aBS \rightarrow aB            S→bAS \rightarrow bA

B→bB \rightarrow b               A→aA \rightarrow a 

B→bSB \rightarrow bS           A→aSA \rightarrow aS

B→aBBB \rightarrow aBB       A→bAAA \rightarrow bAA

Which of the following strings is generated by the grammar?

Choose one option
View worked solution

Answer: C

By analyzing the production rules, we can determine the language generated by each non-terminal:

  • SS generates strings with an equal number of aa's and bb's.
  • AA generates strings with one more aa than bb's.
  • BB generates strings with one more bb than aa's.

We can verify this property from the rules:

  • S→aB∣bAS \rightarrow aB \mid bA: Since BB has one more bb than aa, prepending aa results in an equal number of aa's and bb's. Similarly, prepending bb to AA results in an equal number of aa's and bb's.
  • B→b∣bS∣aBBB \rightarrow b \mid bS \mid aBB: Each alternative guarantees exactly one more bb than aa.
  • A→a∣aS∣bAAA \rightarrow a \mid aS \mid bAA: Each alternative guarantees exactly one more aa than bb.

Now, let's count the number of aa's and bb's in each option:

  • (A) aaaabbaaaabb: 44 aa's, 22 bb's (not equal)
  • (B) aabbbbaabbbb: 22 aa's, 44 bb's (not equal)
  • (C) aabbabaabbab: 33 aa's, 33 bb's (equal)
  • (D) abbbbaabbbba: 22 aa's, 44 bb's (not equal)

Thus, only option C can be generated by the grammar. The derivation for aabbabaabbab is:

S⇒aB⇒aaBB⇒aabB⇒aabbS⇒aabbaB⇒aabbabS \Rightarrow aB \Rightarrow aaBB \Rightarrow aabB \Rightarrow aabbS \Rightarrow aabbaB \Rightarrow aabbab

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

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