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 -> є and A -> a) to parse a string with n tokens?
Explanation: Given in the question, a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a).
To get maximum number of Reduce moves, we should make sure than in each sentential form only one terminal is reduced. Since there is no unit production, so last 2 tokens will take only 1 move.
So To Reduce input string of n tokens, first Reduce n-2 tokens using n-2 reduce moves and then Reduce last 2 tokens using production which has . So total of n-2+1 = n-1 Reduce moves.
Suppose the string is abcd. ( n = 4 ).
We can write the grammar which accepts this string as follows:
S->aB B->bC C->cd
The Right Most Derivation for the above is:
S -> aB ( Reduction 3 ) -> abC ( Reduction 2 ) -> abcd ( Reduction 1 )
We can see here that no production is for unit or epsilon. Hence 3 reductions here.
We can get less number of reductions with some other grammar which also does’t produce unit or epsilon productions,
S->abA A-> cd
The Right Most Derivation for the above as:
S -> abA ( Reduction 2 ) -> abcd ( Reduction 1 )
Hence 2 reductions.
But we are interested in knowing the maximum number of reductions which comes from the 1st grammar. Hence total 3 reductions as maximum, which is ( n – 1) as n = 4 here.
Thus, Option B.
Quiz of this Question
Attention reader! Don’t stop learning now. Learn all GATE CS concepts with Free Live Classes on our youtube channel.