GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 79

Number of parse trees for aabbab

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 2007 paper

GATE 2007, Q79 2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (2 derivations)

1. Approach. The first two a symbols force S→aB→aaBB. Enumerate how the remaining bbab splits between the two B subtrees. Count trees, not different orders of expanding the same tree.

2. Derivation and interpretation. The first two terminals aa force S -> aB and B -> aBB. The remaining bbab must split between the two B children. Each child has one more b than a. Exactly two splits work: b | bab and bba | b. For the first, the right child uniquely derives bab via B -> bS -> baB -> bab. For the second, the left child uniquely derives bba via B -> bS -> bbA -> bba. The single b children use B -> b. Thus there are 2 parse trees, option B; different orders of expanding a fixed tree do not count as additional trees.

Original paper, page 20