Skip to content
Official PYQ · 2007

GATE CS 2007 Q79 — 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

For the string aabbabaabbab, how many derivation trees are there?

Choose one option
View worked solution

Answer: B

To find the number of derivation trees for the string aabbabaabbab, we trace the leftmost derivations step-by-step starting from the start symbol SS:

  1. Since the string starts with aa, the first production must be: S→aBS \rightarrow aB
  2. The second symbol is also aa, so we must expand BB using B→aBBB \rightarrow aBB: S→aB→aaBBS \rightarrow aB \rightarrow aaBB

Now, we need to derive the remaining substring bbabbbab from the two non-terminals BBBB. Each BB must derive a substring wiw_i such that w1w2=bbabw_1 w_2 = bbab. Since any string derived from BB has one more bb than aa, its length must be odd. Thus, the only possible odd-length partitions of the 44-character string bbabbbab are:

  • Case 1: ∣w1∣=1|w_1| = 1 and ∣w2∣=3  ⟹  w1=b|w_2| = 3 \implies w_1 = b and w2=babw_2 = bab.
    • B⇒bB \Rightarrow b (unique derivation)
    • B⇒bS⇒baB⇒babB \Rightarrow bS \Rightarrow baB \Rightarrow bab (unique derivation)
    This yields the first derivation tree.

  • Case 2: ∣w1∣=3|w_1| = 3 and ∣w2∣=1  ⟹  w1=bba|w_2| = 1 \implies w_1 = bba and w2=bw_2 = b.
    • B⇒bS⇒bbA⇒bbaB \Rightarrow bS \Rightarrow bbA \Rightarrow bba (unique derivation)
    • B⇒bB \Rightarrow b (unique derivation)
    This yields the second derivation tree.

Since both cases yield exactly one unique derivation tree, there are a total of 2 derivation trees for the string aabbabaabbab.

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

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