• edited by
12,677 views
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?

  1. $5$
  2. $4$
  3. $3$
  4. $2$

4 Answers

Best answer
42 42 votes

$5$ Parse trees are possible

• edited by
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.

• edited by
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.

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/

Answer:
Position:
Show:

Related questions

44 44 votes
3 3 answers
10.4k
10.4k views
Ishrat Jahan asked Nov 3, 2014
10,413 views
Consider the context-free grammar$E \rightarrow E + E$$E \rightarrow (E * E)$$E \rightarrow id$where $E$ is the starting symbol, the set of terminals is $\{id, (,+,),*\...
38 38 votes
3 answers 3 answers
20.1k
20.1k views
go_editor asked Nov 27, 2016
20,132 views
Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.$$\begin{array}{l|l} E\rightarrow numbe...
74 74 votes
7 answers 7 answers
26.9k
26.9k views
Ishrat Jahan asked Nov 3, 2014
26,864 views
Consider a simple graph with unit edge costs. Each node in the graph represents a router. Each node maintains a routing table indicating the next hop router to be used to...
31 31 votes
5 answers 5 answers
9.3k
9.3k views
Ishrat Jahan asked Nov 3, 2014
9,338 views
Consider a simple graph with unit edge costs. Each node in the graph represents a router. Each node maintains a routing table indicating the next hop router to be used to...