<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
LR	B-Application
parsers	I-Application
are	O
a	O
type	O
of	O
bottom-up	B-Application
parser	I-Application
that	O
analyse	O
deterministic	O
context-free	O
languages	O
in	O
linear	O
time	O
.	O
</s>
<s>
There	O
are	O
several	O
variants	O
of	O
LR	B-Application
parsers	I-Application
:	O
SLR	B-Application
parsers	I-Application
,	O
LALR	B-Application
parsers	I-Application
,	O
Canonical	O
LR(1 )	O
parsers	O
,	O
Minimal	O
LR(1 )	O
parsers	O
,	O
and	O
GLR	B-Application
parsers	I-Application
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
can	O
be	O
generated	O
by	O
a	O
parser	B-Language
generator	I-Language
from	O
a	O
formal	O
grammar	O
defining	O
the	O
syntax	O
of	O
the	O
language	O
to	O
be	O
parsed	O
.	O
</s>
<s>
They	O
are	O
widely	O
used	O
for	O
the	O
processing	O
of	O
computer	B-Language
languages	I-Language
.	O
</s>
<s>
An	O
LR	B-Application
parser	I-Application
(	O
Left-to-right	O
,	O
Rightmost	O
derivation	O
in	O
reverse	O
)	O
reads	O
input	O
text	O
from	O
left	O
to	O
right	O
without	O
backing	O
up	O
(	O
this	O
is	O
true	O
for	O
most	O
parsers	O
)	O
,	O
and	O
produces	O
a	O
rightmost	O
derivation	O
in	O
reverse	O
:	O
it	O
does	O
a	O
bottom-up	B-Application
parse	I-Application
–	O
not	O
a	O
top-down	B-Application
LL	I-Application
parse	I-Application
or	O
ad-hoc	O
parse	O
.	O
</s>
<s>
To	O
avoid	O
backtracking	B-Algorithm
or	O
guessing	O
,	O
the	O
LR	B-Application
parser	I-Application
is	O
allowed	O
to	O
peek	O
ahead	O
at	O
k	O
lookahead	O
input	O
symbols	O
before	O
deciding	O
how	O
to	O
parse	O
earlier	O
symbols	O
.	O
</s>
<s>
The	O
name	O
LR	O
is	O
often	O
preceded	O
by	O
other	O
qualifiers	O
,	O
as	O
in	O
SLR	B-Application
and	O
LALR	B-Application
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
are	O
deterministic	O
;	O
they	O
produce	O
a	O
single	O
correct	O
parse	O
without	O
guesswork	O
or	O
backtracking	B-Algorithm
,	O
in	O
linear	O
time	O
.	O
</s>
<s>
This	O
is	O
ideal	O
for	O
computer	B-Language
languages	I-Language
,	O
but	O
LR	B-Application
parsers	I-Application
are	O
not	O
suited	O
for	O
human	O
languages	O
which	O
need	O
more	O
flexible	O
but	O
inevitably	O
slower	O
methods	O
.	O
</s>
<s>
Some	O
methods	O
which	O
can	O
parse	O
arbitrary	O
context-free	O
languages	O
(	O
e.g.	O
,	O
Cocke	B-Application
–	I-Application
Younger	I-Application
–	I-Application
Kasami	I-Application
,	O
Earley	B-Algorithm
,	O
GLR	B-Application
)	O
have	O
worst-case	O
performance	O
of	O
O(3 )	O
time	O
.	O
</s>
<s>
The	O
above	O
properties	O
of	O
L	O
,	O
R	O
,	O
and	O
k	O
are	O
actually	O
shared	O
by	O
all	O
shift-reduce	B-Application
parsers	I-Application
,	O
including	O
precedence	O
parsers	O
.	O
</s>
<s>
But	O
by	O
convention	O
,	O
the	O
LR	O
name	O
stands	O
for	O
the	O
form	O
of	O
parsing	O
invented	O
by	O
Donald	O
Knuth	O
,	O
and	O
excludes	O
the	O
earlier	O
,	O
less	O
powerful	O
precedence	O
methods	O
(	O
for	O
example	O
Operator-precedence	B-Application
parser	I-Application
)	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
can	O
handle	O
a	O
larger	O
range	O
of	O
languages	O
and	O
grammars	O
than	O
precedence	O
parsers	O
or	O
top-down	O
LL	B-Language
parsing	I-Language
.	O
</s>
<s>
This	O
is	O
because	O
the	O
LR	B-Application
parser	I-Application
waits	O
until	O
it	O
has	O
seen	O
an	O
entire	O
instance	O
of	O
some	O
grammar	O
pattern	O
before	O
committing	O
to	O
what	O
it	O
has	O
found	O
.	O
</s>
<s>
An	O
LL	B-Language
parser	I-Language
has	O
to	O
decide	O
or	O
guess	O
what	O
it	O
is	O
seeing	O
much	O
sooner	O
,	O
when	O
it	O
has	O
only	O
seen	O
the	O
leftmost	O
input	O
symbol	O
of	O
that	O
pattern	O
.	O
</s>
<s>
An	O
LR	B-Application
parser	I-Application
scans	O
and	O
parses	O
the	O
input	O
text	O
in	O
one	O
forward	O
pass	O
over	O
the	O
text	O
.	O
</s>
<s>
The	O
parser	O
builds	O
up	O
the	O
parse	O
tree	O
incrementally	O
,	O
bottom	O
up	O
,	O
and	O
left	O
to	O
right	O
,	O
without	O
guessing	O
or	O
backtracking	B-Algorithm
.	O
</s>
<s>
These	O
three	O
root	O
nodes	O
are	O
temporarily	O
held	O
in	O
a	O
parse	O
stack	B-Application
.	O
</s>
<s>
As	O
with	O
other	O
shift-reduce	B-Application
parsers	I-Application
,	O
an	O
LR	B-Application
parser	I-Application
works	O
by	O
doing	O
some	O
combination	O
of	O
Shift	O
steps	O
and	O
Reduce	O
steps	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
differ	O
from	O
other	O
shift-reduce	B-Application
parsers	I-Application
in	O
how	O
they	O
decide	O
when	O
to	O
reduce	O
,	O
and	O
how	O
to	O
pick	O
between	O
rules	O
with	O
similar	O
endings	O
.	O
</s>
<s>
Much	O
of	O
the	O
LR	B-Application
parser	I-Application
's	O
efficiency	O
is	O
from	O
being	O
deterministic	O
.	O
</s>
<s>
To	O
avoid	O
guessing	O
,	O
the	O
LR	B-Application
parser	I-Application
often	O
looks	O
ahead	O
(	O
rightwards	O
)	O
at	O
the	O
next	O
scanned	O
symbol	O
,	O
before	O
deciding	O
what	O
to	O
do	O
with	O
previously	O
scanned	O
symbols	O
.	O
</s>
<s>
The	O
lexical	B-Application
scanner	I-Application
works	O
one	O
or	O
more	O
symbols	O
ahead	O
of	O
the	O
parser	O
.	O
</s>
<s>
Like	O
other	O
shift-reduce	B-Application
parsers	I-Application
,	O
an	O
LR	B-Application
parser	I-Application
lazily	O
waits	O
until	O
it	O
has	O
scanned	O
and	O
parsed	O
all	O
parts	O
of	O
some	O
construct	O
before	O
committing	O
to	O
what	O
the	O
combined	O
construct	O
is	O
.	O
</s>
<s>
The	O
decisions	O
for	O
how	O
to	O
handle	O
A	O
are	O
based	O
only	O
on	O
what	O
the	O
parser	O
and	O
scanner	B-Application
have	O
already	O
seen	O
,	O
without	O
considering	O
things	O
that	O
appear	O
much	O
later	O
to	O
the	O
right	O
.	O
</s>
<s>
So	O
the	O
list	O
of	O
already-parsed	O
things	O
acts	O
like	O
a	O
stack	B-Application
.	O
</s>
<s>
This	O
parse	O
stack	B-Application
grows	O
rightwards	O
.	O
</s>
<s>
The	O
base	O
or	O
bottom	O
of	O
the	O
stack	B-Application
is	O
on	O
the	O
left	O
and	O
holds	O
the	O
leftmost	O
,	O
oldest	O
parse	O
fragment	O
.	O
</s>
<s>
(	O
This	O
accumulative	O
parse	O
stack	B-Application
is	O
very	O
unlike	O
the	O
predictive	O
,	O
leftward-growing	O
parse	O
stack	B-Application
used	O
by	O
top-down	B-Application
parsers	I-Application
.	O
)	O
</s>
<s>
This	O
matches	O
the	O
stack	B-Application
top	O
holding	O
the	O
parsed	O
phrases	O
"	O
...	O
Products	O
*	O
Value	O
"	O
.	O
</s>
<s>
In	O
LR	B-Application
parsers	I-Application
,	O
the	O
shift	O
and	O
reduce	O
decisions	O
are	O
potentially	O
based	O
on	O
the	O
entire	O
stack	B-Application
of	O
everything	O
that	O
has	O
been	O
previously	O
parsed	O
,	O
not	O
just	O
on	O
a	O
single	O
,	O
topmost	O
stack	B-Application
symbol	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
do	O
this	O
with	O
constant	O
speed	O
,	O
by	O
summarizing	O
all	O
the	O
relevant	O
left	O
context	O
information	O
into	O
a	O
single	O
number	O
called	O
the	O
LR(0 )	O
parser	O
state	O
.	O
</s>
<s>
Besides	O
holding	O
the	O
already-parsed	O
symbols	O
,	O
the	O
parse	O
stack	B-Application
also	O
remembers	O
the	O
state	O
numbers	O
reached	O
by	O
everything	O
up	O
to	O
those	O
points	O
.	O
</s>
<s>
At	O
every	O
parse	O
step	O
,	O
the	O
entire	O
input	O
text	O
is	O
divided	O
into	O
a	O
stack	B-Application
of	O
previously	O
parsed	O
phrases	O
,	O
a	O
current	O
look-ahead	O
symbol	O
,	O
and	O
the	O
remaining	O
unscanned	O
text	O
.	O
</s>
<s>
The	O
parser	O
's	O
next	O
action	O
is	O
determined	O
by	O
its	O
current	O
LR(0 )	O
(	O
rightmost	O
on	O
the	O
stack	B-Application
)	O
and	O
the	O
lookahead	O
symbol	O
.	O
</s>
<s>
In	O
the	O
steps	O
below	O
,	O
all	O
the	O
black	O
details	O
are	O
exactly	O
the	O
same	O
as	O
in	O
other	O
non-LR	O
shift-reduce	B-Application
parsers	I-Application
.	O
</s>
<s>
LR	B-Application
parser	I-Application
stacks	B-Application
add	O
the	O
state	O
information	O
in	O
purple	O
,	O
summarizing	O
the	O
black	O
phrases	O
to	O
their	O
left	O
on	O
the	O
stack	B-Application
and	O
what	O
syntax	O
possibilities	O
to	O
expect	O
next	O
.	O
</s>
<s>
Users	O
of	O
an	O
LR	B-Application
parser	I-Application
can	O
usually	O
ignore	O
state	O
information	O
.	O
</s>
<s>
an	O
empty	O
section	O
on	O
the	O
parse	O
stack	B-Application
,	O
</s>
<s>
The	O
parse	O
stack	B-Application
begins	O
by	O
holding	O
only	O
initial	O
state	O
0	O
.	O
</s>
<s>
When	O
state	O
0	O
sees	O
the	O
lookahead	O
id	O
,	O
it	O
knows	O
to	O
shift	O
that	O
id	O
onto	O
the	O
stack	B-Application
,	O
and	O
scan	O
the	O
next	O
input	O
symbol	O
*	O
,	O
and	O
advance	O
to	O
state	O
9	O
.	O
</s>
<s>
The	O
current	O
,	O
rightmost	O
state	O
on	O
the	O
stack	B-Application
is	O
state	O
5	O
.	O
</s>
<s>
When	O
state	O
5	O
sees	O
the	O
lookahead	O
int	O
,	O
it	O
knows	O
to	O
shift	O
that	O
int	O
onto	O
the	O
stack	B-Application
as	O
its	O
own	O
phrase	O
,	O
and	O
scan	O
the	O
next	O
input	O
symbol	O
+	O
,	O
and	O
advance	O
to	O
state	O
8	O
.	O
</s>
<s>
by	O
combining	O
the	O
stack	B-Application
's	O
rightmost	O
three	O
phrases	O
for	O
Sums	O
,	O
+	O
,	O
and	O
Products	O
into	O
one	O
thing	O
.	O
</s>
<s>
This	O
consulting	O
of	O
older	O
states	O
is	O
why	O
they	O
are	O
kept	O
on	O
the	O
stack	B-Application
,	O
instead	O
of	O
keeping	O
only	O
the	O
current	O
state	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
are	O
constructed	O
from	O
a	O
grammar	O
that	O
formally	O
defines	O
the	O
syntax	O
of	O
the	O
input	O
language	O
as	O
a	O
set	O
of	O
patterns	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
use	O
a	O
context-free	O
grammar	O
that	O
deals	O
just	O
with	O
local	O
patterns	O
of	O
symbols	O
.	O
</s>
<s>
The	O
grammar	O
's	O
terminal	O
symbols	O
are	O
the	O
multi-character	O
symbols	O
or	O
'	O
tokens	O
 '	O
found	O
in	O
the	O
input	O
stream	O
by	O
a	O
lexical	B-Application
scanner	I-Application
.	O
</s>
<s>
Any	O
given	O
computer	B-Language
language	I-Language
can	O
be	O
described	O
by	O
several	O
different	O
grammars	O
.	O
</s>
<s>
The	O
grammar	O
for	O
an	O
LR	B-Application
parser	I-Application
must	O
be	O
unambiguous	O
itself	O
,	O
or	O
must	O
be	O
augmented	O
by	O
tie-breaking	O
precedence	O
rules	O
.	O
</s>
<s>
LR	B-Application
parsing	I-Application
is	O
not	O
a	O
useful	O
technique	O
for	O
human	O
languages	O
with	O
ambiguous	O
grammars	O
that	O
depend	O
on	O
the	O
interplay	O
of	O
words	O
.	O
</s>
<s>
Human	O
languages	O
are	O
better	O
handled	O
by	O
parsers	O
like	O
Generalized	B-Application
LR	I-Application
parser	I-Application
,	O
the	O
Earley	B-Algorithm
parser	I-Algorithm
,	O
or	O
the	O
CYK	B-Application
algorithm	I-Application
that	O
can	O
simultaneously	O
compute	O
all	O
possible	O
parse	O
trees	O
in	O
one	O
pass	O
.	O
</s>
<s>
Most	O
LR	B-Application
parsers	I-Application
are	O
table	O
driven	O
.	O
</s>
<s>
The	O
parser	O
's	O
program	B-Language
code	I-Language
is	O
a	O
simple	O
generic	O
loop	O
that	O
is	O
the	O
same	O
for	O
all	O
grammars	O
and	O
languages	O
.	O
</s>
<s>
So	O
they	O
are	O
mechanically	O
derived	O
from	O
the	O
grammar	O
by	O
some	O
parser	B-Language
generator	I-Language
tool	O
like	O
Bison	B-Application
.	O
</s>
<s>
Depending	O
on	O
how	O
the	O
states	O
and	O
parsing	O
table	O
are	O
generated	O
,	O
the	O
resulting	O
parser	O
is	O
called	O
either	O
a	O
SLR	B-Application
(	O
simple	O
LR	O
)	O
parser	O
,	O
LALR	B-Application
(	O
look-ahead	B-Application
LR	I-Application
)	O
parser	O
,	O
or	O
canonical	B-Application
LR	I-Application
parser	I-Application
.	O
</s>
<s>
LALR	B-Application
parsers	I-Application
handle	O
more	O
grammars	O
than	O
SLR	B-Application
parsers	I-Application
.	O
</s>
<s>
Canonical	B-Application
LR	I-Application
parsers	I-Application
handle	O
even	O
more	O
grammars	O
,	O
but	O
use	O
many	O
more	O
states	O
and	O
much	O
larger	O
tables	O
.	O
</s>
<s>
The	O
example	O
grammar	O
is	O
SLR	B-Application
.	O
</s>
<s>
The	O
table	O
column	O
"	O
Current	O
Rules	O
"	O
documents	O
the	O
meaning	O
and	O
syntax	O
possibilities	O
for	O
each	O
state	O
,	O
as	O
worked	O
out	O
by	O
the	O
parser	B-Language
generator	I-Language
.	O
</s>
<s>
Individual	O
table	O
cells	O
must	O
not	O
hold	O
multiple	O
,	O
alternative	O
actions	O
,	O
otherwise	O
the	O
parser	O
would	O
be	O
nondeterministic	O
with	O
guesswork	O
and	O
backtracking	B-Algorithm
.	O
</s>
<s>
The	O
LR	B-Application
parser	I-Application
begins	O
with	O
a	O
nearly	O
empty	O
parse	O
stack	B-Application
containing	O
just	O
the	O
start	B-Architecture
state	I-Architecture
0	O
,	O
and	O
with	O
the	O
lookahead	O
holding	O
the	O
input	O
stream	O
's	O
first	O
scanned	O
symbol	O
.	O
</s>
<s>
The	O
topmost	O
state	O
on	O
the	O
parse	O
stack	B-Application
is	O
some	O
state	O
s	O
,	O
and	O
the	O
current	O
lookahead	O
is	O
some	O
terminal	O
symbol	O
t	O
.	O
Look	O
up	O
the	O
next	O
parser	O
action	O
from	O
row	O
s	O
and	O
column	O
t	O
of	O
the	O
Lookahead	O
Action	O
table	O
.	O
</s>
<s>
Shift	O
the	O
matched	O
terminal	O
t	O
onto	O
the	O
parse	O
stack	B-Application
and	O
scan	O
the	O
next	O
input	O
symbol	O
into	O
the	O
lookahead	O
buffer	O
.	O
</s>
<s>
Push	O
next	O
state	O
n	O
onto	O
the	O
parse	O
stack	B-Application
as	O
the	O
new	O
current	O
state	O
.	O
</s>
<s>
Remove	O
the	O
matched	O
topmost	O
L	O
symbols	O
(	O
and	O
parse	O
trees	O
and	O
associated	O
state	O
numbers	O
)	O
from	O
the	O
parse	O
stack	B-Application
.	O
</s>
<s>
Push	O
the	O
symbol	O
and	O
tree	O
for	O
Lhs	O
onto	O
the	O
parse	O
stack	B-Application
.	O
</s>
<s>
Push	O
next	O
state	O
n	O
onto	O
the	O
parse	O
stack	B-Application
as	O
the	O
new	O
current	O
state	O
.	O
</s>
<s>
If	O
the	O
state	O
stack	B-Application
contains	O
just	O
the	O
start	B-Architecture
state	I-Architecture
report	O
success	O
.	O
</s>
<s>
LR	B-Application
parser	I-Application
stack	B-Application
usually	O
stores	O
just	O
the	O
LR(0 )	O
automaton	O
states	O
,	O
as	O
the	O
grammar	O
symbols	O
may	O
be	O
derived	O
from	O
them	O
(	O
in	O
the	O
automaton	O
,	O
all	O
input	O
transitions	O
to	O
some	O
state	O
are	O
marked	O
with	O
the	O
same	O
symbol	O
,	O
which	O
is	O
the	O
symbol	O
associated	O
with	O
this	O
state	O
)	O
.	O
</s>
<s>
This	O
section	O
of	O
the	O
article	O
can	O
be	O
skipped	O
by	O
most	O
users	O
of	O
LR	B-Application
parser	I-Application
generators	O
.	O
</s>
<s>
The	O
parser	B-Language
generator	I-Language
adds	O
additional	O
rules	O
or	O
items	O
for	O
all	O
the	O
possible	O
next	O
steps	O
in	O
building	O
up	O
the	O
expected	O
Products	O
:	O
</s>
<s>
They	O
form	O
a	O
finite	B-Architecture
state	I-Architecture
machine	I-Architecture
(	O
FSM	O
)	O
.	O
</s>
<s>
An	O
FSM	O
is	O
a	O
simple	O
engine	O
for	O
parsing	O
simple	O
unnested	O
languages	O
,	O
without	O
using	O
a	O
stack	B-Application
.	O
</s>
<s>
In	O
this	O
LR	O
application	O
,	O
the	O
FSM	O
's	O
modified	O
"	O
input	O
language	O
"	O
has	O
both	O
terminal	O
and	O
nonterminal	O
symbols	O
,	O
and	O
covers	O
any	O
partially	O
parsed	O
stack	B-Application
snapshot	O
of	O
the	O
full	O
LR	O
parse	O
.	O
</s>
<s>
The	O
parse	O
stack	B-Application
shows	O
a	O
series	O
of	O
state	O
transitions	O
,	O
from	O
the	O
start	B-Architecture
state	I-Architecture
0	O
,	O
to	O
state	O
4	O
and	O
then	O
on	O
to	O
5	O
and	O
current	O
state	O
8	O
.	O
</s>
<s>
The	O
symbols	O
on	O
the	O
parse	O
stack	B-Application
are	O
the	O
shift	O
or	O
goto	O
symbols	O
for	O
those	O
transitions	O
.	O
</s>
<s>
Another	O
way	O
to	O
view	O
this	O
,	O
is	O
that	O
the	O
finite	B-Architecture
state	I-Architecture
machine	I-Architecture
can	O
scan	O
the	O
stream	O
"	O
Products*int+1	O
"	O
(	O
without	O
using	O
yet	O
another	O
stack	B-Application
)	O
and	O
find	O
the	O
leftmost	O
complete	O
phrase	O
that	O
should	O
be	O
reduced	O
next	O
.	O
</s>
<s>
How	O
can	O
a	O
mere	O
FSM	O
do	O
this	O
when	O
the	O
original	O
unparsed	O
language	O
has	O
nesting	O
and	O
recursion	O
and	O
definitely	O
requires	O
an	O
analyzer	O
with	O
a	O
stack	B-Application
?	O
</s>
<s>
The	O
trick	O
is	O
that	O
everything	O
to	O
the	O
left	O
of	O
the	O
stack	B-Application
top	O
has	O
already	O
been	O
fully	O
reduced	O
.	O
</s>
<s>
In	O
SLR	B-Application
parsers	I-Application
,	O
these	O
lookahead	O
sets	O
are	O
determined	O
directly	O
from	O
the	O
grammar	O
,	O
without	O
considering	O
the	O
individual	O
states	O
and	O
transitions	O
.	O
</s>
<s>
For	O
each	O
nonterminal	O
S	O
,	O
the	O
SLR	B-Application
generator	O
works	O
out	O
Follows(S )	O
,	O
the	O
set	O
of	O
all	O
the	O
terminal	O
symbols	O
which	O
can	O
immediately	O
follow	O
some	O
occurrence	O
of	O
S	O
.	O
In	O
the	O
parse	O
table	O
,	O
each	O
reduction	O
to	O
S	O
uses	O
Follow(S )	O
as	O
its	O
LR(1 )	O
lookahead	O
set	O
.	O
</s>
<s>
Such	O
follow	O
sets	O
are	O
also	O
used	O
by	O
generators	O
for	O
LL	O
top-down	B-Application
parsers	I-Application
.	O
</s>
<s>
A	O
grammar	O
that	O
has	O
no	O
shift/reduce	O
or	O
reduce/reduce	O
conflicts	O
when	O
using	O
Follow	O
sets	O
is	O
called	O
an	O
SLR	B-Application
grammar	O
.	O
</s>
<s>
LALR	B-Application
parsers	I-Application
have	O
the	O
same	O
states	O
as	O
SLR	B-Application
parsers	I-Application
,	O
but	O
use	O
a	O
more	O
complicated	O
,	O
more	O
precise	O
way	O
of	O
working	O
out	O
the	O
minimum	O
necessary	O
reduction	O
lookaheads	O
for	O
each	O
individual	O
state	O
.	O
</s>
<s>
Depending	O
on	O
the	O
details	O
of	O
the	O
grammar	O
,	O
this	O
may	O
turn	O
out	O
to	O
be	O
the	O
same	O
as	O
the	O
Follow	O
set	O
computed	O
by	O
SLR	B-Application
parser	I-Application
generators	O
,	O
or	O
it	O
may	O
turn	O
out	O
to	O
be	O
a	O
subset	O
of	O
the	O
SLR	B-Application
lookaheads	O
.	O
</s>
<s>
Some	O
grammars	O
are	O
okay	O
for	O
LALR	B-Application
parser	I-Application
generators	O
but	O
not	O
for	O
SLR	B-Application
parser	I-Application
generators	O
.	O
</s>
<s>
This	O
happens	O
when	O
the	O
grammar	O
has	O
spurious	O
shift/reduce	O
or	O
reduce/reduce	O
conflicts	O
using	O
Follow	O
sets	O
,	O
but	O
no	O
conflicts	O
when	O
using	O
the	O
exact	O
sets	O
computed	O
by	O
the	O
LALR	B-Application
generator	O
.	O
</s>
<s>
The	O
grammar	O
is	O
then	O
called	O
LALR(1 )	O
but	O
not	O
SLR	B-Application
.	O
</s>
<s>
An	O
SLR	B-Application
or	O
LALR	B-Application
parser	I-Application
avoids	O
having	O
duplicate	O
states	O
.	O
</s>
<s>
Canonical	B-Application
LR	I-Application
parsers	I-Application
use	O
duplicated	O
(	O
or	O
"	O
split	O
"	O
)	O
states	O
to	O
better	O
remember	O
the	O
left	O
and	O
right	O
context	O
of	O
a	O
nonterminal	O
's	O
use	O
.	O
</s>
<s>
This	O
splitting	O
of	O
states	O
can	O
also	O
be	O
done	O
manually	O
and	O
selectively	O
with	O
any	O
SLR	B-Application
or	O
LALR	B-Application
parser	I-Application
,	O
by	O
making	O
two	O
or	O
more	O
named	O
copies	O
of	O
some	O
nonterminals	O
.	O
</s>
<s>
A	O
grammar	O
that	O
is	O
conflict-free	O
for	O
a	O
canonical	B-Application
LR	I-Application
generator	O
but	O
has	O
conflicts	O
in	O
an	O
LALR	B-Application
generator	O
is	O
called	O
LR(1 )	O
but	O
not	O
LALR(1 )	O
,	O
and	O
not	O
SLR	B-Application
.	O
</s>
<s>
SLR	B-Application
,	O
LALR	B-Application
,	O
and	O
canonical	B-Application
LR	I-Application
parsers	I-Application
make	O
exactly	O
the	O
same	O
shift	O
and	O
reduce	O
decisions	O
when	O
the	O
input	O
stream	O
is	O
the	O
correct	O
language	O
.	O
</s>
<s>
When	O
the	O
input	O
has	O
a	O
syntax	O
error	O
,	O
the	O
LALR	B-Application
parser	I-Application
may	O
do	O
some	O
additional	O
(	O
harmless	O
)	O
reductions	O
before	O
detecting	O
the	O
error	O
than	O
would	O
the	O
canonical	B-Application
LR	I-Application
parser	I-Application
.	O
</s>
<s>
And	O
the	O
SLR	B-Application
parser	I-Application
may	O
do	O
even	O
more	O
.	O
</s>
<s>
This	O
happens	O
because	O
the	O
SLR	B-Application
and	O
LALR	B-Application
parsers	I-Application
are	O
using	O
a	O
generous	O
superset	O
approximation	O
to	O
the	O
true	O
,	O
minimal	O
lookahead	O
symbols	O
for	O
that	O
particular	O
state	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
can	O
generate	O
somewhat	O
helpful	O
error	O
messages	O
for	O
the	O
first	O
syntax	O
error	O
in	O
a	O
program	O
,	O
by	O
simply	O
enumerating	O
all	O
the	O
terminal	O
symbols	O
that	O
could	O
have	O
appeared	O
next	O
instead	O
of	O
the	O
unexpected	O
bad	O
lookahead	O
symbol	O
.	O
</s>
<s>
In	O
the	O
yacc	B-Application
and	O
bison	B-Application
parser	I-Application
generators	I-Application
,	O
the	O
parser	O
has	O
an	O
ad	O
hoc	O
mechanism	O
to	O
abandon	O
the	O
current	O
statement	O
,	O
discard	O
some	O
parsed	O
phrases	O
and	O
lookahead	O
tokens	O
surrounding	O
the	O
error	O
,	O
and	O
resynchronize	O
the	O
parse	O
at	O
some	O
reliable	O
statement-level	O
delimiter	O
like	O
semicolons	O
or	O
braces	O
.	O
</s>
<s>
Some	O
LR	B-Application
parsers	I-Application
attempt	O
to	O
detect	O
and	O
automatically	O
repair	O
these	O
common	O
cases	O
.	O
</s>
<s>
(	O
This	O
requires	O
backtracking	B-Algorithm
to	O
snapshots	O
of	O
the	O
parse	O
stack	B-Application
and	O
input	O
stream	O
,	O
normally	O
unneeded	O
by	O
the	O
parser	O
.	O
)	O
</s>
<s>
Repair	O
of	O
syntax	O
errors	O
is	O
easiest	O
to	O
do	O
consistently	O
in	O
parsers	O
(	O
like	O
LR	O
)	O
that	O
have	O
parse	O
tables	O
and	O
an	O
explicit	O
data	O
stack	B-Application
.	O
</s>
<s>
The	O
LR	B-Application
parser	I-Application
generator	O
decides	O
what	O
should	O
happen	O
for	O
each	O
combination	O
of	O
parser	O
state	O
and	O
lookahead	O
symbol	O
.	O
</s>
<s>
Some	O
LR	B-Application
parser	I-Application
generators	O
create	O
separate	O
tailored	O
program	B-Language
code	I-Language
for	O
each	O
state	O
,	O
rather	O
than	O
a	O
parse	O
table	O
.	O
</s>
<s>
In	O
the	O
recursive	B-Application
ascent	I-Application
parser	I-Application
variation	O
,	O
the	O
explicit	O
parse	O
stack	B-Application
structure	O
is	O
also	O
replaced	O
by	O
the	O
implicit	O
stack	B-Application
used	O
by	O
subroutine	O
calls	O
.	O
</s>
<s>
So	O
recursive	B-Application
ascent	I-Application
parsers	I-Application
are	O
generally	O
slower	O
,	O
less	O
obvious	O
,	O
and	O
harder	O
to	O
hand-modify	O
than	O
recursive	B-Application
descent	I-Application
parsers	I-Application
.	O
</s>
<s>
Another	O
variation	O
replaces	O
the	O
parse	O
table	O
by	O
pattern-matching	O
rules	O
in	O
non-procedural	O
languages	O
such	O
as	O
Prolog	B-Language
.	O
</s>
<s>
GLR	B-Application
Generalized	B-Application
LR	I-Application
parsers	I-Application
use	O
LR	O
bottom-up	O
techniques	O
to	O
find	O
all	O
possible	O
parses	O
of	O
input	O
text	O
,	O
not	O
just	O
one	O
correct	O
parse	O
.	O
</s>
<s>
The	O
multiple	O
valid	O
parse	O
trees	O
are	O
computed	O
simultaneously	O
,	O
without	O
backtracking	B-Algorithm
.	O
</s>
<s>
GLR	B-Application
is	O
sometimes	O
helpful	O
for	O
computer	B-Language
languages	I-Language
that	O
are	O
not	O
easily	O
described	O
by	O
a	O
conflict-free	O
LALR(1 )	O
grammar	O
.	O
</s>
<s>
LC	O
Left	B-Application
corner	I-Application
parsers	I-Application
use	O
LR	O
bottom-up	O
techniques	O
for	O
recognizing	O
the	O
left	O
end	O
of	O
alternative	O
grammar	O
rules	O
.	O
</s>
<s>
LC	O
parsers	O
have	O
smaller	O
parse	O
tables	O
than	O
LALR	B-Application
parsers	I-Application
and	O
better	O
error	O
diagnostics	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
were	O
invented	O
by	O
Donald	O
Knuth	O
in	O
1965	O
as	O
an	O
efficient	O
generalization	O
of	O
precedence	O
parsers	O
.	O
</s>
<s>
Knuth	O
proved	O
that	O
LR	B-Application
parsers	I-Application
were	O
the	O
most	O
general-purpose	O
parsers	O
possible	O
that	O
would	O
still	O
be	O
efficient	O
in	O
the	O
worst	O
cases	O
.	O
</s>
<s>
The	O
canonical	B-Application
LR	I-Application
parsers	I-Application
described	O
by	O
Knuth	O
had	O
too	O
many	O
states	O
and	O
very	O
big	O
parse	O
tables	O
that	O
were	O
impractically	O
large	O
for	O
the	O
limited	O
memory	O
of	O
computers	O
of	O
that	O
era	O
.	O
</s>
<s>
LR	B-Application
parsing	I-Application
became	O
practical	O
when	O
Frank	O
DeRemer	O
invented	O
SLR	B-Application
and	O
LALR	B-Application
parsers	I-Application
with	O
much	O
fewer	O
states	O
.	O
</s>
<s>
For	O
full	O
details	O
on	O
LR	O
theory	O
and	O
how	O
LR	B-Application
parsers	I-Application
are	O
derived	O
from	O
grammars	O
,	O
see	O
The	O
Theory	O
of	O
Parsing	O
,	O
Translation	O
,	O
and	O
Compiling	O
,	O
Volume	O
1	O
(	O
Aho	O
and	O
Ullman	O
)	O
.	O
</s>
<s>
Earley	B-Algorithm
parsers	I-Algorithm
apply	O
the	O
techniques	O
and	O
notation	O
of	O
LR	B-Application
parsers	I-Application
to	O
the	O
task	O
of	O
generating	O
all	O
possible	O
parses	O
for	O
ambiguous	O
grammars	O
such	O
as	O
for	O
human	O
languages	O
.	O
</s>
<s>
This	O
example	O
of	O
LR	B-Application
parsing	I-Application
uses	O
the	O
following	O
small	O
grammar	O
with	O
goal	O
symbol	O
E	O
:	O
</s>
<s>
After	O
a	O
reduction	O
,	O
the	O
next	O
state	O
is	O
found	O
by	O
looking	O
up	O
the	O
goto	O
table	O
entry	O
for	O
top	O
of	O
the	O
stack	B-Application
(	O
i.e.	O
</s>
<s>
Here	O
the	O
state	O
refers	O
to	O
the	O
element	O
at	O
the	O
top	O
of	O
the	O
stack	B-Application
(	O
the	O
right-most	O
element	O
)	O
,	O
and	O
the	O
next	O
action	O
is	O
determined	O
by	O
referring	O
to	O
the	O
action	O
table	O
above	O
.	O
</s>
<s>
The	O
parser	O
starts	O
out	O
with	O
the	O
stack	B-Application
containing	O
just	O
the	O
initial	O
state	O
( 	O
 '	O
0	O
 '	O
)	O
:	O
</s>
<s>
To	O
find	O
the	O
next	O
action	O
(	O
shift	O
,	O
reduce	O
,	O
accept	O
or	O
error	O
)	O
,	O
the	O
action	O
table	O
is	O
indexed	O
with	O
the	O
current	O
state	O
(	O
the	O
"	O
current	O
state	O
"	O
is	O
just	O
whatever	O
is	O
on	O
the	O
top	O
of	O
the	O
stack	B-Application
)	O
,	O
which	O
in	O
this	O
case	O
is	O
0	O
,	O
and	O
the	O
current	O
input	O
symbol	O
,	O
which	O
is	O
'	O
1	O
 '	O
.	O
</s>
<s>
The	O
action	O
table	O
specifies	O
a	O
shift	O
to	O
state	O
2	O
,	O
and	O
so	O
state	O
2	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
(	O
again	O
,	O
all	O
the	O
state	O
information	O
is	O
in	O
the	O
stack	B-Application
,	O
so	O
"	O
shifting	O
to	O
state	O
2	O
"	O
is	O
the	O
same	O
as	O
pushing	O
2	O
onto	O
the	O
stack	B-Application
)	O
.	O
</s>
<s>
where	O
the	O
top	O
of	O
the	O
stack	B-Application
is	O
2	O
.	O
</s>
<s>
For	O
the	O
sake	O
of	O
explaining	O
the	O
symbol	O
(	O
e.g.	O
,	O
'	O
1	O
 '	O
,	O
B	O
)	O
is	O
shown	O
that	O
caused	O
the	O
transition	O
to	O
the	O
next	O
state	O
,	O
although	O
strictly	O
speaking	O
it	O
is	O
not	O
part	O
of	O
the	O
stack	B-Application
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
parser	O
writes	O
5	O
to	O
the	O
output	O
stream	O
,	O
pops	O
one	O
state	O
from	O
the	O
stack	B-Application
(	O
since	O
the	O
right-hand	O
side	O
of	O
the	O
rule	O
has	O
one	O
symbol	O
)	O
,	O
and	O
pushes	O
on	O
the	O
stack	B-Application
the	O
state	O
from	O
the	O
cell	O
in	O
the	O
goto	O
table	O
for	O
state	O
0	O
and	O
B	O
,	O
i.e.	O
,	O
state	O
4	O
.	O
</s>
<s>
The	O
resulting	O
stack	B-Application
is	O
:	O
</s>
<s>
So	O
it	O
writes	O
3	O
to	O
the	O
output	O
stream	O
,	O
pops	O
one	O
state	O
from	O
the	O
stack	B-Application
,	O
and	O
finds	O
the	O
new	O
state	O
in	O
the	O
goto	O
table	O
for	O
state	O
0	O
and	O
E	O
,	O
which	O
is	O
state	O
3	O
.	O
</s>
<s>
The	O
resulting	O
stack	B-Application
:	O
</s>
<s>
The	O
resulting	O
stack	B-Application
can	O
be	O
interpreted	O
as	O
the	O
history	O
of	O
a	O
finite	B-Architecture
state	I-Architecture
automaton	I-Architecture
that	O
has	O
just	O
read	O
a	O
nonterminal	O
E	O
followed	O
by	O
a	O
terminal	O
'	O
+	O
 '	O
.	O
</s>
<s>
Just	O
as	O
the	O
previous	O
'	O
1	O
 '	O
this	O
one	O
is	O
reduced	O
to	O
B	O
giving	O
the	O
following	O
stack	B-Application
:	O
</s>
<s>
The	O
stack	B-Application
corresponds	O
with	O
a	O
list	O
of	O
states	O
of	O
a	O
finite	B-Architecture
automaton	I-Architecture
that	O
has	O
read	O
a	O
nonterminal	O
E	O
,	O
followed	O
by	O
a	O
'	O
+	O
 '	O
and	O
then	O
a	O
nonterminal	O
B	O
.	O
</s>
<s>
The	O
top	O
3	O
states	O
on	O
the	O
stack	B-Application
correspond	O
with	O
the	O
3	O
symbols	O
in	O
the	O
right-hand	O
side	O
of	O
rule	O
2	O
.	O
</s>
<s>
These	O
transitions	O
will	O
be	O
determined	O
as	O
if	O
we	O
are	O
considering	O
a	O
finite	B-Architecture
automaton	I-Architecture
that	O
can	O
read	O
terminals	O
as	O
well	O
as	O
nonterminals	O
.	O
</s>
<s>
The	O
finite	B-Architecture
automaton	I-Architecture
,	O
with	O
item	O
sets	O
as	O
its	O
states	O
is	O
shown	O
below	O
.	O
</s>
<s>
Refinements	O
to	O
the	O
LR(0 )	O
table	O
construction	O
procedure	O
(	O
such	O
as	O
SLR	B-Application
and	O
LALR	B-Application
)	O
are	O
capable	O
of	O
constructing	O
reduce	O
actions	O
that	O
do	O
not	O
occupy	O
entire	O
rows	O
.	O
</s>
<s>
However	O
,	O
when	O
reduce	O
actions	O
are	O
added	O
to	O
the	O
action	O
table	O
it	O
can	O
happen	O
that	O
the	O
same	O
cell	O
is	O
filled	O
with	O
a	O
reduce	O
action	O
and	O
a	O
shift	O
action	O
(	O
a	O
shift-reduce	B-Application
conflict	O
)	O
or	O
with	O
two	O
different	O
reduce	O
actions	O
(	O
a	O
reduce-reduce	O
conflict	O
)	O
.	O
</s>
<s>
A	O
classic	O
real-world	O
example	O
of	O
a	O
shift-reduce	B-Application
conflict	O
is	O
the	O
dangling	O
else	O
problem	O
.	O
</s>
<s>
A	O
small	O
example	O
of	O
a	O
non-LR(0 )	O
grammar	O
with	O
a	O
shift-reduce	B-Application
conflict	O
is	O
:	O
</s>
<s>
There	O
is	O
a	O
shift-reduce	B-Application
conflict	O
in	O
this	O
item	O
set	O
:	O
when	O
constructing	O
the	O
action	O
table	O
according	O
to	O
the	O
rules	O
above	O
,	O
the	O
cell	O
for	O
[	O
item	O
set	O
1	O
,	O
terminal	O
'	O
1' ]	O
contains	O
s1	O
(	O
shift	O
to	O
state	O
1	O
)	O
and	O
r2	O
(	O
reduce	O
with	O
grammar	O
rule	O
2	O
)	O
.	O
</s>
<s>
Both	O
examples	O
above	O
can	O
be	O
solved	O
by	O
letting	O
the	O
parser	O
use	O
the	O
follow	O
set	O
(	O
see	O
LL	B-Language
parser	I-Language
)	O
of	O
a	O
nonterminal	O
A	O
to	O
decide	O
if	O
it	O
is	O
going	O
to	O
use	O
one	O
of	O
As	O
rules	O
for	O
a	O
reduction	O
;	O
it	O
will	O
only	O
use	O
the	O
rule	O
A	O
→	O
w	O
for	O
a	O
reduction	O
if	O
the	O
next	O
symbol	O
on	O
the	O
input	O
stream	O
is	O
in	O
the	O
follow	O
set	O
of	O
A	O
.	O
</s>
<s>
This	O
solution	O
results	O
in	O
so-called	O
Simple	B-Application
LR	I-Application
parsers	I-Application
.	O
</s>
