
CS143 Handout 11
Summer 2012 July 9st, 2012
SLR and LR(1) Parsing
Handout written by Maggie Johnson and revised by Julie Zelenski.
LR(0) Isn’t Good Enough
LR(0) is the simplest technique in the LR family. Although that makes it the easiest to
learn, these parsers are too weak to be of practical use for anything but a very limited set
of grammars. The examples given at the end of the LR(0) handout show how even small
additions to an LR(0) grammar can introduce conflicts that make it no longer LR(0). The
fundamental limitation of LR(0) is the zero, meaning no lookahead tokens are used. It is
a stifling constraint to have to make decisions using only what has already been read,
without even glancing at what comes next in the input. If we could peek at the next
token and use that as part of the decision-making, we will find that it allows for a much
larger class of grammars to be parsed.
SLR(1)
We will first consider SLR(1) where the S stands for simple . SLR(1) parsers use the
same LR(0) configurating sets and have the same table structure and parser operation,
so everything you've already learned about LR(0) applies here. The difference comes in
assigning table actions, where we are going to use one token of lookahead to help
arbitrate among the conflicts. If we think back to the kind of conflicts we encountered in
LR(0) parsing, it was the reduce actions that cause us grief. A state in an LR(0) parser
can have at most one reduce action and cannot have both shift and reduce instructions.
Since a reduce is indicated for any completed item, this dictates that each completed
item must be in a state by itself. But let's revisit the assumption that if the item is
complete, the parser must choose to reduce. Is that always appropriate? If we peeked at
the next upcoming token, it may tell us something that invalidates that reduction. If the
sequence on top of the stack could be reduced to the non-terminal A, what tokens do we
expect to find as the next input? What tokens would tell us that the reduction is not
appropriate? Perhaps Follow(A) could be useful here!
The simple improvement that SLR(1) makes on the basic LR(0) parser is to reduce only if
the next input token is a member of the follow set of the non-terminal being reduced.
When filling in the table, we don't assume a reduce on all inputs as we did in LR(0), we
selectively choose the reduction only when the next input symbols in a member of the
follow set. To be more precise, here is the algorithm for SLR(1) table construction (note
all steps are the same as for LR(0) table construction except for 2a)