I was told that without the added rule S->S' we might accept words that doesn't exists in the grammar language, can someone think on a good example to this? Moreover, do you have an example for a reduction in SLR parsing that goes to more than one state?
1 Answers
The main reason why you need this extra production rule is to be able to determine when you've actually finished reading the input string. If you don't include it, you can get some false positives.
As an example, consider this grammar:
S → aS | b
Let's imagine that we don't augment this grammar with the extra start production and instead start building an LR(0) parser for it. We'll get back these states
(1)
S -> .aS
S -> .b
(2)
S -> b.
(3)
S -> a.S
S -> .aS
S -> .b
(4)
S -> aS.
Now, imagine that we use this LR(0) parser to parse the string abb. Notice that this string is not in the language of the grammar, so we shouldn't accept it. Here's a trace of what happens:
Stack | State | Input | Action
-------------------+-------+------------------+----------
| (1) | abb | Shift, go to (3)
a | (3) | bb | Shift, go to (2)
ab | (2) | b | ???
So here's the issue. At this state, we have a reduce item S → b. Now, do we do the reduce here and continue parsing? Or are we done with the parse? If we choose to stop here, we're going to incorrectly report that the string is in the language even though it isn't actually there. If we don't choose to stop here, then we will correctly parse the input, but it means that if we had gotten a different input (say, just the character b), then we might reduce in a case where we really should just stop parsing. This problem isn't solved by using an SLR(1) parser here because the lookaheads don't help us differentiate between these cases.
By adding in the extra production at the start, we make it possible to unambiguously differentiate between "I've matched something with the start symbol" from "I'm completely done" because the parser will only consider that production if it's finished reducing an S and the parsing stack is empty.

- 362,284
- 104
- 897
- 1,065