
Go backward to LR(1) Parsing
Go up to Top
Go forward to A Word About LALR(1) Parsing
Building an LR(1) Machine
- First, we need to extend the definitions which we used to define
the transition function for an LR(0) machine to account for the
lookaheads we have added to LR(1) items.
- goto
- Given a set of LR(1) items for a grammar G, we define
goto(
, x ) = { [ N
1 x .
2 , a ] | [ N
1 . x
2 , a] 
}
- closure
- Given a set
of LR(1) items for a grammar G with
productions P, we define closure(
) to be the smallest
set of LR(1) items such that:
- closure(
) 
- if [N1
1 . N2
2,a]
closure(
)
and N2
3
P
then, for each b
First(
2 a ),
[N2
.
3, b]
closure(
)
- With these definitions, it should be obvious, that the next step
is to define the LR(1) finite automaton for a grammar G consisting of:
- A set of states with one state for every subset of LR(1)
items.
- An alphabet consisting of the terminals and non-terminals of G.
- A set of final states consisting of the set of all states except
the state corresponding to the empty set of LR(1) items.
- A transition function defined by:
(
, x) = closure ( goto (
, x ) )
- The state closure( [ S'
. S $,
] ) as its
initial state.
- The notions of a kernel item and a reduce item transfer naturally
from LR(0) items to LR(1) items.
- Note that the language accepted by the LR(1) FSM is the same as
that accepted by the LR(0) machine (i.e. the set of viable prefixes).
The extra states in the machine, however, include information that
can be used to make a better parser.
- Consider what happens when we build the LR(1) machine for the non-SLR(1)
grammar considered yesterday.
E
( L , E )
E
S
L
L , E
L
E
S
ident
S
( S )
- A set of LR(1) items contains a conflict if it contains a reduce
items of the form [ N
1 . , x ] and
either another reduce item of the form [ M
2 . , x ] or
a shift item of the form [ M
alpha. x
2 , y ].
- We say that a grammar is LR(1) if the reachable states in its LR(1)
machine are conflict free.
- Given an LR(1) grammar, its LR(1) parser, shifts in state
with input x if state
contains a shift item of the form
[ N
alpha. x
, a], reduces using production
N
if state
contains a reduce item of
the form [ N
. , x ] and reports error otherwise.
Computer Science 434
Department of Computer Science
Williams College
