Question

Every LR(0) grammar is SLR(1) but vice versa need not necessarily be true, why?

Was it helpful?

Solution

Basically, an SLR(1) grammar can resolve shift-reduce conflicts that exist in a corresponding LR(0) grammar. Take the example grammar on Wikipedia's SLR parser page (which explains this at a lower, more rigorous level):

  1. S → E
  2. E → 1 E
  3. E → 1

When an LR(0) parser is parsing an E and a "1" is the next input symbol, it could recognize E and reduce (rule 3) or it could shift in order to parse a following E (rule 2). Because it can't look ahead, an LR(0) can't determine which to do. This becomes more obvious if we look at the items the LR(0) could be processing when it encounters a "1" (the end-of-string symbol has been added):

  • E → 1 • E $
  • E → 1 • $

The first would require a shift, the second requires a reduce.

With the above grammar, an SLR(1) grammar can look ahead one symbol and determine which action to take. E can only be followed by $, so a reduce action is only valid at the end of string. This corresponds to the second item, where you can see the next symbol is a "$".

For another example grammar that is SLR(1) and not LR(0), see Fegaras' notes for CSE 5317/4305 at the University of Texas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top