44 44 votes Consider the context-free grammar $E\rightarrow E+E$ $E\rightarrow (E *E)$ $E\rightarrow \text{id}$ where $E$ is the starting symbol, the set of terminals is $\{id, (,+,),*\}$, and the set of non-terminals is $\{E\}$. For the terminal string $id + id + id + id$, how many parse trees are possible? $5$ $4$ $3$ $2$ Compiler Design gateit-2005 compiler-design parsing normal + – Ishrat Jahan 12.7k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply ujjwal saini commented Nov 11, 2014 reply Follow flag Can you please elaborate for what string you want to count the parse trees ? As there are many strings possible using the above grammer. 0 0 replyShare Arjun commented Nov 11, 2014 reply Follow flag Added that :) 0 0 replyShare ujjwal saini commented Nov 11, 2014 reply Follow flag Okay :) 1 1 replyShare js__ commented Nov 4, 2025 reply Follow flag . 6 6 replyShare Please log in or register to add a comment.
Best answer 42 42 votes $5$ Parse trees are possible stblue answered Aug 14, 2017 • edited Jul 4, 2019 by Lakshman Bhaiya stblue comment Share Follow See all 3 Comments 3 3 Comments reply s_dr_13 commented Jan 31, 2020 reply Follow flag How to enumerate all such possible parse trees,especially in exam setting where there is lots of tension due to which there is some chance of missing some trees !! How to tackle such questions ? Is there any better solution rather than enumerating all possible trees ? 6 6 replyShare Shiva Sagar Rao commented Feb 4, 2021 reply Follow flag I think red coloured box should be E –> id 2 2 replyShare jatinmittal199510 commented Mar 26, 2021 reply Follow flag One can enumerate atleast this example where we have $4$ $id's$. First thing, we have to use only $E \to E + E$ production. So from $2$ $E's$ we have to create $4$ $E's$. So there are these possible cases, 1. Get 2 E's from one E and other 2 E's from another E. 2. Get 3 E's from first E and leave the another E as it is. 3. Keep first E as it is and get 3 E's from second E. Now, to get 3 E's from 1 E, we again have 2 cases. So, in total we will have 1 case for (1) and 2 cases for (2) and (3) each. 3 3 replyShare Please log in or register to add a comment.
56 56 votes A simpler method is to calculate how many ways the expression can be parsed ((id+id)+(id+id)) ((id+(id+id))+id) (id+((id+id)+id)) (((id+id)+id)+id) (id+(id+(id+id))) Note-The parentheses are given just for better understanding....they are not in the grammar for addition. It's a better time-saving method than drawing parse trees, although parse trees represent the same idea. Edit- Rather than writing all the ways the given expression can be parsed, it can be seen that the answer is 3rd Catalan number i.e number of valid expressions with three sets of parenthesis. tanaya answered Aug 27, 2016 • edited Nov 28, 2019 by tanaya tanaya comment Share Follow See all 3 Comments 3 3 Comments reply shashankrustagi commented Dec 15, 2020 reply Follow flag Correct thinking. 0 0 replyShare Vivek Raj commented Dec 21, 2020 reply Follow flag nice 0 0 replyShare Pranavpurkar commented Nov 28, 2022 reply Follow flag nice approach . 0 0 replyShare Please log in or register to add a comment.
50 50 votes Here number of parse tree is equal to number of ways to parenthesize an expression. It turns out that the number of ways to parenthesize an expression with n+1 terms is Cn, the nth Catalan number. If we set n = 3, we get C3 = 5, confirming that there are five ways to parenthesize four terms. https://www.johndcook.com/blog/2013/10/03/parenthesize-expression-catalan/ lambda answered Dec 19, 2017 lambda comment Share Follow See all 8 Comments 8 8 Comments reply Show 5 previous comments Thadymademe commented Oct 22, 2022 reply Follow flag @Danishgupta read @tanaya answer you will get why n is used as 3. 0 0 replyShare zoy123 commented Nov 10, 2022 reply Follow flag the problem is quite similar to the DP problem of matrix-chain multiplication. there also we find no of ways to parenthesize the matrix multiplications in given expression. parenthesization makes it explicit what what production to use at a given time. So there is no ambiguity 0 0 replyShare js__ commented Nov 4, 2025 reply Follow flag https://gemini.google.com/share/76785d7b581c 2 2 replyShare Please log in or register to add a comment.
13 13 votes 5 parse trees are possible. ujjwal saini answered Nov 11, 2014 ujjwal saini comment Share Follow See all 2 Comments 2 2 Comments reply ankitrokdeonsns commented Nov 22, 2014 reply Follow flag can you elaborate your answer I could count upto 4 parse trees 0 0 replyShare jayendra commented Jan 6, 2015 reply Follow flag this is 5th.. 8 8 replyShare Please log in or register to add a comment.