Professional Documents
Culture Documents
Introduction
With LALR (lookahead LR) parsing, we attempt to
reduce the number of states in an LR(1) parser by
merging similar states.
Example1
Example..
Some of the LR(1) states look suspiciously similar.
Take I3 and I6 for example.
These two states are virtually identical
they have the same number of items,
the core of each item is identical, and they differ only
in their lookahead sets.
This
observation may make you wonder if it possible to merge
them into one state.
Same is true of I4 and I7, and I8 and I9.
Example…
If we did merge, we would end up replacing those
six states with just these three:
LALR Merge Conflicts
Can merging states in this way ever introduce new conflicts?
A shiftreduce conflict
cannot exist in a merged set unless the conflict was already pr
esent in one of the original LR(1) configurating sets.
When merging, the two sets must have the same core
items. If the merged set has a configuration that shifts on a an
d another that reduces on
a, both configurations must have been present in the original s
ets, and at least one of those sets had a conflict already.
Example:
Example
Example…
We try to merge I6 and I9 since they have the same
core items and they only differ in lookahead:
The merged configurating set allows a reduction to
either B or C when next token is c or d.
This is a reducereduce conflict and can be an
unintended consequence of merging LR(1) states.
When such a conflict arises in doing a
merging, we say the grammar is not LALR(1).