parsinglr-grammarbottom-up

What do the different kinds of LR Parsers use as lookahead?


  1. Is it correct, that LR(0)-Parsers simply reduces, if there is no transition for the next input-symbol (because it has no lookahead) ?
  2. Is it correct, that SLR(1)-Parsers use the FOLLOW-Set of the productions as lookahead?
  3. Is it correct, that LR(1)-Parsers use the FIRST-, not the FOLLOW-Set as lookahead?

The algorithm for closure clearly uses FIRST

closure(S)
For each item [A → α ⋅ B β, t] in S,
  For each production B → γ in G,
    For each token b in FIRST(βt),
      Add [B → ⋅ γ, b] to S

Then again, I am confused about this.

Under paragraph 4.7.1 Canonical LR(1) Items the Dragon Book says:

Thus, we are compelled to reduce by A → α only on those input symbols a for which [A → α·, a] is an LR(1) item in the state on top of the stack. The set of such a's will always be a subset of FOLLOW(A), but it could be a proper subset, as in Example 4.51.


Solution

    1. An LR(0) parser has no lookahead, so it cannot decide between reduce and shift actions if both would be possible in a given parser state. So a reduce state cannot have shift transitions.

    2. Yes, the SLR algorithm overestimates the lookahead set for each reduction action by just using the FOLLOW set of the non-terminal being reduced.

    3. No. The canonical LR algorithm computes a lookahead set for each action based on the parser context (that is, the state). To compute this set, it is useful to know the FIRST set of each non-terminal (and be able to compute the FIRST set of any sentential form) but the computed lookahead set is not the FIRST set of any non-terminal. As indicated in the textbook, it is a subset of the FOLLOW set of the non-terminal being reduced, and in some states in some grammars, it will be a proper subset. This implies that the "same" reduction in two different states may have different lookahead sets, because the two states are reached in different contexts. The textbook provides an example, as you note, and it is worth studying this example in detail.