edited by
35,412 views
83 votes
83 votes

What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon and unit-production (i.e., of type $A \rightarrow \epsilon$ and $A \rightarrow a$) to parse a string with $n$ tokens?

  1. $n/2$
  2. $n-1$
  3. $2n-1$
  4. $2^{n}$
edited by

13 Answers

0 votes
0 votes

Since there is no proper mathematical form of reaching at the answer included in here, I am including mine.

For obtaining MAXIMAL number of reduce moves, we have to have 2 terminals / non-terminals from each root.

Something like

                  $A \rightarrow XY$

                  $X \rightarrow MN$

and so on..

Now we have n tokens in the expression. So we must have $\frac{n}{2}$ terminal sets which are reduced to non-terminals, assuming a set of 2 terminals is reduced to one non terminal.

These $\frac{n}{2}$ non terminals will then be reduced to $\frac{n}{4}$ non terminals and so on, till we reach the final 2 non terminals pointing to the one non terminal. The summation thus becomes:

                                       $\sum_{i=1}^{\inf}{\frac{n}{2^{i}}}$.

This becomes an infinite geometric series which can be evaluated as : $(n\frac{\frac{1}{2}}{1-\frac{1}{2}})-1$ as the last non-terminal is a single non-terminal and the root. Hence, option B

0 votes
0 votes

Here the intention of the question is also to exclude the productions of type $A\rightarrow B$.

Actually, if you can see the parse tree of any sentence that belongs to a grammar, every node will have at least 2 children except the leaves.

We can get the minimum number of reductions when a node has a vast number of children. i.e RHS of each production has more symbols.

Ex : $S\rightarrow abcde$, It takes only one reduction for string abcde

           S

     /  /  |   \  \

    a  b  c  d  e

As the number of children of an internal node in the parse tree increases(i.e RHS of each production), the number of reductions decreases.

Therefore, we’ll get the maximum number of reductions when every internal node has exactly 2 children in the parse tree. i.e RHS of every production has exactly 2 symbols.

Then every internal node represents a reduction.

If a string has n tokens, then the parse tree has n leaves

Then number of internal nodes (reductions) = n - 1

Ex :$S\rightarrow AB \\A\rightarrow aa \\B\rightarrow bb$

Parse tree of string aabb is 

                                  S

                                /     \

                              A       B

                            /    \     /   \

                          a      a   b    b

0 votes
0 votes

Lets take aabb and here rule is that  we cant  do reductions like A→ a or A→ epsilon

and they are asking for maximum reduce moves so as unit reduction will be not allowed so to get max lets start reducing 

two terminals  at once so that it doesnot violate above rules and give the chance to maximise

A->aa

B->bb

x->AB

aa reduced to A ---->1

bb reduced to B-------2

now we have AB

AB reduced to x---------3

 

aabb symbols 4

reductions 3

n-1

 

 

 

0 votes
0 votes

Lets take aabb and here rule is that  we cant  do reductions like A→ a or A→ epsilon

and they are asking for maximum reduce moves so as unit reduction will be not allowed so to get max lets start reducing 

two terminals  at once so that it doesnot violate above rules and give the chance to maximise

A->aa

B->bb

x->AB

aa reduced to A ---->1

bb reduced to B-------2

now we have AB

AB reduced to x---------3

 

aabb symbols 4

reductions 3

n-1

 

 

Answer:

Related questions

29 votes
29 votes
2 answers
4