53 53 votes $A$ CFG $G$ is given with the following productions where $S$ is the start symbol, $A$ is a non-terminal and a and b are terminals.$S → aS \mid A$$A → aAb \mid bAa \mid \epsilon$For the string "$aabbaab$" how many steps are required to derive the string and how many parse trees are there?$6$ and $1$$6$ and $2$$7$ and $2$$4$ and $2$ Compiler Design gateit-2008 compiler-design parsing normal + – Ishrat Jahan 12.7k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply rahul sharma 5 commented Jan 23, 2018 reply Follow flag Look at second production:- Equal a and b combinations In given,we need 4 a and 3 b From S,if we have two options but if we take second we cannot cant unequal a and b. So we have to start with S->aS, now as we have taken one a.Now needed is 3 a and 3b in some order and there is only one way to get this from A. It seems like we have to explore lot of options ,but there is always something in gate questions that helps to get quick answer:) 23 23 replyShare nithin_9 commented Dec 3, 2025 reply Follow flag Quick tip: a grammar is ambiguous only when it has both left and right recursion in its productions. Since the given grammar contains only one type of recursion, it is unambiguous. Therefore, it allows only a single parse tree, so Option A is correct. 1 1 replyShare studyrj commented Dec 6, 2025 reply Follow flag thanks this was useful 0 0 replyShare panipuri commented Jan 16 reply Follow flag @nithin_9 small addition:Ambiguous grammar can be easily detected if it is both left and right recursive + it derives $pure\ terminal$ production also$S \rightarrow SS \ |\ a$ is ambiguous if grammar is just left and right recursive then it need not be ambiguous 0 0 replyShare dhawalphalak35 commented Aug 11 reply Follow flag @panipuri This is a major misconception. The only reliable test for ambiguity is whether some string generated by the grammar has more than one parse tree (or equivalently, more than one LMD/RMD). 0 0 replyShare panipuri commented Aug 11 reply Follow flag @dhawalphalak35okay give me a Grammar which is in the form i mentioned but is NON-AMBIGUOUS 0 0 replyShare Please log in or register to add a comment.
Best answer 63 63 votes $S\underset{1}{ \rightarrow} aS$ $\quad \underset{2}{\rightarrow} aA$ $ \quad \underset{3}{\rightarrow} aaAb$ $\quad \underset{4}{ \rightarrow} aabAab$ $\quad \underset{5}{ \rightarrow} aabbAaab$ $\quad \underset{6}{ \rightarrow} aabbaab$ Thus $6$ steps are needed and only one way to derive the string so only one parse tree. Correct Answer: $A$ Shreyans Dhankhar answered Oct 29, 2014 • edited Apr 30, 2019 by Naveen Kumar 3 Shreyans Dhankhar comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments SomeEarth commented Jan 15, 2021 reply Follow flag if we look at the Non Terminal, A it is very much clear that we can produce only even length strings out of it. That means our given string which is $'aabbaab'$ (length =7) can not be derived by production $S \rightarrow A$ or solely with $A$. Hence we are bound to start our derivation with production: $S \rightarrow aS$. 1 1 replyShare LIKITH P commented Aug 20, 2021 reply Follow flag Grammar is unambiguous.So only one parse tree is present for a string. 9 9 replyShare Pranavpurkar commented Nov 28, 2022 reply Follow flag LIKITH P nice :) 1 1 replyShare Please log in or register to add a comment.
0 0 votes As the https://gateoverflow.in/3393/gate-it-2008-question-79?show=3398#a3398 answer as he did top-down if also did bottom-up we will use same no.of reductions to make it to S NOW_OR_NEVER answered May 21 NOW_OR_NEVER comment Share Follow 0 reply Please log in or register to add a comment.