0

enter image description here

S → ( S ) S
|   .

based on the definition, 0, 2 and 4 have shift/reduce conflict. The follow set of S is ")". For SLR(1) in state 2, "(" is not in the follow set of S, but why is this a SLR (1)? Can you also explain the shift / reduce conflict rule for slr(1), I might be confused on something.

user3230613
  • 133
  • 1
  • 5
  • Your question could be phrased better. There is a shift/reduce conflict without lookahead, but one symbol of lookahead resolves it: you shift a '(' and otherwise reduce. What don't you understand? – rici Mar 10 '16 at 21:25
  • the definition in my notes for SLR (1) is "if you want to reduce (x->b) then the next input must be in follow (x). And I don't know how to apply it. follow (s) is ")" and the next input is "(".. – user3230613 Mar 10 '16 at 21:57
  • if the next input is `(`, you cannot reduce. But in that case you can shift the `(`, so that is what you do. – rici Mar 11 '16 at 00:38

1 Answers1

0

Let's start with your grammar:

S → (S)S | ε

To build an SLR(1) parser for this grammar, we need to augment it with a new start rule:

Start → S

S → (S)S | ε

Notice that FOLLOW(S) contains ) and $ and no other symbols.

We can now start building the SLR(1) automaton by building the LR(0) automaton and augmenting each production with its FOLLOW set:

(0)
Start -> .S      [$]
    S -> .(S)S   [$)]
    S -> .       [$)]

(1)
Start -> S.      [$]

(2)
    S -> (.S)S   [$)]
    S -> .(S)S   [$)]
    S -> .       [$)]

(3)
    S -> (S.)S   [$)]

(4)
    S -> (S).S   [$)]
    S -> .(S)S   [$)]
    S -> .       [$)]

(5)
    S -> (S)S.   [$)]

You've claimed that states 0, 2, and 4 have shift/reduce conflicts, but I don't actually think that's the case here. In state (0), we have the completed item S → ., but the lookaheads are $ and ). This means that we only reduce on $ and ). On the other hand, the only shift action here occurs on a ( symbol.

If we didn't have lookaheads here, we'd have a shift/reduce conflict, but because of the lookaheads we know to shift if we see a ( and to reduce if we see ) or $. If you check the other states you've flagged, you'll see that the same logic applies.

As a result, this grammar is indeed SLR(1).

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065