1.3k views

Consider the grammar:

$$S \rightarrow (S) \mid a$$

Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be $n_1, n_2$ and $n_3$ respectively. The following relationship holds good:

1. $n_1 < n_2 < n_3$
2. $n_1 = n_3 < n_2$
3. $n_1 = n_2 = n_3$
4. $n_1 \geq n_3 \geq n_2$

ans B

Both in SLR(1) and LALR(1), states are the LR(0) items while in LR(1) the states are LR(1) set of items. Number of LR(0) items can never be greater than number of LR(1) items. So, $n_1 = n_3 \leq n_2$, B choice.  If we construct the states for the grammar we can replace $\leq$ with $<$.

edited by
why
@utsab if you try to draw the SLR(1) you would get 6 states and the given grammar is LR(0) so must be SLR(1),LALR(1),CLR(1)/LR(1) now if you try to draw LR(1) table

you would get 7 states and there 2 states will differ by only look ahead symbols

1) s->(.s), &

s->.(s), )

s->.a, )

2)  s->(.s), )

s->.(s), )

s->.a, )    // you can minimizes the number of states by merging them and minimized CLR(1) is known as                     LALR(1) so you will get same number of states as SLR(1)

@arjun sir  in this type of question where grammer are given and

Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n1 ,n2 and n3 respectively . so as we know in  general n1=n3<n2   is it valid for any type of given grammer in question  ??  or we go on the basis of given grammer and makes the state transition digram and count the no of states for each parser  and then we will give the answer .
n1 = n3 < n2 is  in general true ?
but it could also be n1 = n2 = n3  right ??

We should draw diagram for finding out . Don't we ?
@arjun sir , is SLR(1) posible ?

r : S->(S)

r2: S->a

both will be placed under follow of S ?
Wont there be a reduce reduce conflict ?

@Das there will no conflicts in SLR.  Because reductions  will be in two different states.

Although n1=n3 <=  n2 is the general relationship between states.  We must draw the state diagram for CLR to confirm.  Reason behind selecting CLR is,  if we draw it's diagram then we would guess what would happen in SLR and LALR

SLR -  6 states

CLR - 10 states

LALR -  6 states

@pc  kindly check ur clr  it will having 10 states not 8 . and their is no conflict either S-R or R-R
@rajan What are the 10 states you are talking about ? Can you show the states ? We can discuss. I think It will be 8 states only.  I didn't account for any conflicts here as the question is interested in olny finding number of states .
yr just see   S->(.S),) on S it will go S->(S.) , )  not on S->(S.),\$
@rajan , ys you are  right ! I corrected my mistake . Thanks for pointing it out :)
no problem bro
I think, LALR(1) uses canonical collection of LR(1) items, but you have

mentioned that it uses LR(0) items.