https://gateoverflow.in/1272/gate2007-78

The Gateway to Computer Science Excellence

+16 votes

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

$S \rightarrow aB$ $S \rightarrow bA$

$B \rightarrow b$ $A \rightarrow a$

$B \rightarrow bS$ $A \rightarrow aS$

$B \rightarrow aBB$ $S \rightarrow bAA$

For the string $aabbab$, how many derivation trees are there?

- $1$
- $2$
- $3$
- $4$

+26 votes

Best answer

$S \rightarrow aB$

$ \rightarrow aaBB$

$ \rightarrow aabB$

$ \rightarrow aabbS$

$ \rightarrow aabbaB$

$ \rightarrow aabbab$

$S \rightarrow aB$

$ \rightarrow aaBB$ (till now, only 1 choice possible)

$ \rightarrow aabSB$ (last time we took $B \rightarrow b$, now taking $B \rightarrow bS$)

$ \rightarrow aabbAB$

$ \rightarrow aabbaB$

$ \rightarrow aabbab$

So, totally $2$ possible derivation trees.

Correct Answer: $B$

$ \rightarrow aaBB$

$ \rightarrow aabB$

$ \rightarrow aabbS$

$ \rightarrow aabbaB$

$ \rightarrow aabbab$

$S \rightarrow aB$

$ \rightarrow aaBB$ (till now, only 1 choice possible)

$ \rightarrow aabSB$ (last time we took $B \rightarrow b$, now taking $B \rightarrow bS$)

$ \rightarrow aabbAB$

$ \rightarrow aabbaB$

$ \rightarrow aabbab$

So, totally $2$ possible derivation trees.

Correct Answer: $B$

+11

sir by using trial and error method for finding number of derivation trees there may be chances that we missed out some. can you suggest something else

+2

I don't think that there will be algorithm to find number of parse tress given a grammar.If it would have been possible, then problem whether a CFG G is ambiguous would be decidable which is already proven to be undecidable.

@Arjun can this be the reasoning for not having any method to derive number of parse trees given a grammar G.

0 votes

the given grammer will generate equal numbee rof a's and b's .The 1st production will generate an extraa a then lead to other production that generate an extraa b thus equalizing number of a's and b's. and vice versa.

To see all possible derivations just try to cnstruct parse tree by taking decision each time the derivation is as follows: S->aB;B->aBB; now 1we can derive the given string by choosing 1st B to derive a b so that other B can derive S which then give ab(since the rest part contains equal number of a's and b;s we generaate it by moving to the starting symbol) uniquely similarly you can give values interchange. so 2 parse tree.

To see all possible derivations just try to cnstruct parse tree by taking decision each time the derivation is as follows: S->aB;B->aBB; now 1we can derive the given string by choosing 1st B to derive a b so that other B can derive S which then give ab(since the rest part contains equal number of a's and b;s we generaate it by moving to the starting symbol) uniquely similarly you can give values interchange. so 2 parse tree.

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.5k
- Others 1.5k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,644 questions

56,517 answers

195,582 comments

101,143 users