I've wrote following grammar:

S->S ( S ) S
S->e

e stands for "empty string"

So the language this grammar recognizes includes all strings with matching left and right parenthesis, like (), (()), (()()), etc.

And this grammar is not SLR, here is how I construct SLR parse table:

  1. Augment this grammar:

    S1->S S->S(S)S S->e

  2. Then construct LR(0) automaton for it:

    I0: S1->.S S->.S(S)S S->.e

    I1: S1->S. S->S.(S)S

...

Please note that for I0, there is no shift or reduce action for input symbol '(', which is the first token of any string this grammar generates.

So SLR parse table will generate error since on state I0, it doesn't know what to do when parsing string: (()).

My question is:

What is the culprit that makes this grammar NOT SLR? Is it the empty string production? That is: S->e. ?

And in a general sense, can SLR grammar have empty productions? like, S->e in this example. Thanks.

有帮助吗?

解决方案

The answer is OK, if no shift/reduce action is available for current input and there is a shift on empty product, we choose to shift on this empty terminal.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top