
Go backward to Some Preliminaries to LR(1) parsing (cont.)
Go up to Top
Go forward to LR(1) Parsing
SLR(1) Parsing
- Given the notion of the "follow" set, we can
illustrate the use of look-ahead in LR parsing, by considering
the simplest form of look-ahead LR parsing -- SLR(1) parsing (that's
S for simple).
- Recall the grammar
< S >
a < S > b < S > |
- The LR(0) machine for this grammar contains three states with LR(0)
conflicts (two of which were shown above):
- The initial state is composed of the items:
[ < S' >
. < S > $ ]
[ < S >
. a < S > b < S > ]
[ < S >
.
]
- Starting from the initial state on input "a" we
reach the the state:
[ < S >
a . < S > b < S > ]
[ < S >
. a < S > b < S > ]
[ < S >
. ]
- Starting from the initial state on input "aSb" we
reach the the state:
[ < S >
a < S > b . < S > ]
[ < S >
. a < S > b < S > ]
[ < S >
. ]
- For this grammar, a is not in Follow(S). So, we should
never reduce using the production < S >
if the next "input" symbol is a.
Therefore, the three states with LR(0) conflicts really don't have
conflicts at all.
- In general, we will say that a set of LR(0) items contains an SLR(1)
conflict if either:
- It contains two reduce items [ N
1 . ] and
[ M
2 . ] such that the intersection of
Follow(N) and Follow(M) is non-empty, or
- It contains a reduce item [ N
1 . ] and
a shift item [ M
2 . x
2 ] such that
x
Follow(N).
- If the LR(0) machine for a grammar G contains no states with
SLR(1) conflicts we say that G is an SLR(1) grammar.
- An SLR(1) parser for an SLR(1) grammar G behaves as follows in state
when the next input symbol is "x":
- reduce using production N
if
[ N
. ] 
, and
x
Follow(N).
- shift in next input if
contains one or more items of the form
[ N
. x
].
- error otherwise.
Computer Science 434
Department of Computer Science
Williams College
