0

I have a doubt about lr(0) parsers. For example I have the follow grammar:

S -> S N
   | 

N -> terminalsymbol

If I try to construct the first state of lr0 automaton, I get the following first state:

S ' -> . S $ 

S -> . S N
S -> . 

So here appears to me a stupid doubt. Since I have "S -> ." in the first state, is this a situation of a shift/reduce in lr0 parser? Cause the parser can go to shift action by nonterminal S, or reduce action by empty transition (I think).

I already searched across the web looking for a example with empty transitions, but I didn't found one.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Afaria
  • 403
  • 5
  • 14

1 Answers1

1

If you start with the augmented grammar, it's not a conflict.

In the initial state, there is no possible shift action, and only one reduce action. So the reduce action must take place, unconditionally.

Once you reduce the S, you end up in the state:

S' -> S . $
S  -> S . N
N  -> . nonterminal

which will either shift the end-of-input mark, or the following non-terminal. Assuming that it's the non-terminal, we end up in:

N  -> nonterminal .

which is another forced reduce, which leads us to

S  -> S N .

and then we're back to:

S' -> . S $
S  -> . S N
S  -> .

However, the original unaugmented grammar is not LR(0). (No language which includes both the empty string and some other string can be LR(0).)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks for the answer. I'm new to this. So there is not a conflict because in the first state there is not a shift action possible, right?when you say " and only one reduce action" is relative to empty transition? – Afaria Jul 09 '13 at 02:02
  • 1
    A shift action is possible in a state if a point (`.`) is just before a nonterminal. A reduction is possible if a point is at the end of a production. If both are true, or if there are two different reductions, then there is a conflict. Otherwise, all you can do is what you can do (shift or reduce, as the case may be), and there is no conflict. – rici Jul 09 '13 at 03:15
  • Thanks again for your help. Only one more question. If I have the follow grammar (S -> num S, S -> ), in the first state i get a shift-reduce conflict? due to the symbol 'num' is a terminal, so we can shift, and we can reduce by the empty transition? am i thinking correct? – Afaria Jul 10 '13 at 21:54