<s>
A	O
shift-reduce	B-Application
parser	I-Application
is	O
a	O
class	O
of	O
efficient	O
,	O
table-driven	O
bottom-up	B-Application
parsing	I-Application
methods	O
for	O
computer	O
languages	O
and	O
other	O
notations	O
formally	O
defined	O
by	O
a	O
grammar	O
.	O
</s>
<s>
The	O
parsing	O
methods	O
most	O
commonly	O
used	O
for	O
parsing	O
programming	O
languages	O
,	O
LR	B-Application
parsing	I-Application
and	O
its	O
variations	O
,	O
are	O
shift-reduce	B-Application
methods	O
.	O
</s>
<s>
The	O
precedence	O
parsers	B-Language
used	O
before	O
the	O
invention	O
of	O
LR	B-Application
parsing	I-Application
are	O
also	O
shift-reduce	B-Application
methods	O
.	O
</s>
<s>
All	O
shift-reduce	B-Application
parsers	I-Application
have	O
similar	O
outward	O
effects	O
,	O
in	O
the	O
incremental	O
order	O
in	O
which	O
they	O
build	O
a	O
parse	O
tree	O
or	O
call	O
specific	O
output	O
actions	O
.	O
</s>
<s>
A	O
shift-reduce	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
without	O
backing	O
up	O
.	O
</s>
<s>
The	O
parser	B-Language
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	O
.	O
</s>
<s>
At	O
every	O
point	O
in	O
this	O
pass	O
,	O
the	O
parser	B-Language
has	O
accumulated	O
a	O
list	O
of	O
subtrees	O
or	O
phrases	O
of	O
the	O
input	O
text	O
that	O
have	O
been	O
already	O
parsed	O
.	O
</s>
<s>
Those	O
subtrees	O
are	O
not	O
yet	O
joined	O
together	O
because	O
the	O
parser	B-Language
has	O
not	O
yet	O
reached	O
the	O
right	O
end	O
of	O
the	O
syntax	O
pattern	O
that	O
will	O
combine	O
them	O
.	O
</s>
<s>
These	O
four	O
root	O
nodes	O
are	O
temporarily	O
held	O
in	O
a	O
parse	O
stack	B-Application
.	O
</s>
<s>
A	O
shift-reduce	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
hence	O
the	O
name	O
.	O
</s>
<s>
The	O
parser	B-Language
continues	O
with	O
these	O
steps	O
until	O
all	O
of	O
the	O
input	O
has	O
been	O
consumed	O
and	O
all	O
of	O
the	O
parse	O
trees	O
have	O
been	O
reduced	O
to	O
a	O
single	O
tree	O
representing	O
an	O
entire	O
legal	O
input	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
parse	O
stack	B-Application
,	O
current	O
lookahead	O
symbol	O
,	O
and	O
remaining	O
unscanned	O
text	O
.	O
</s>
<s>
The	O
parser	B-Language
's	O
next	O
action	O
is	O
determined	O
by	O
the	O
rightmost	O
stack	B-Application
symbol(s )	O
and	O
the	O
lookahead	O
symbol	O
.	O
</s>
<s>
The	O
action	O
is	O
read	O
from	O
a	O
table	O
containing	O
all	O
syntactically	O
valid	O
combinations	O
of	O
stack	B-Application
and	O
lookahead	O
symbols	O
.	O
</s>
<s>
Shift-reduce	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>
They	O
only	O
happen	O
as	O
a	O
result	O
of	O
the	O
parser	B-Language
applying	O
some	O
grammar	O
rule	O
.	O
</s>
<s>
The	O
grammar	O
for	O
a	O
shift-reduce	B-Application
parser	I-Application
must	O
be	O
unambiguous	O
itself	O
,	O
or	O
be	O
augmented	O
by	O
tie-breaking	O
precedence	O
rules	O
.	O
</s>
<s>
A	O
table-driven	O
parser	B-Language
has	O
all	O
of	O
its	O
knowledge	O
about	O
the	O
grammar	O
encoded	O
into	O
unchanging	O
data	O
called	O
parser	B-Language
tables	O
.	O
</s>
<s>
The	O
parser	B-Language
's	O
program	O
code	O
is	O
a	O
simple	O
generic	O
loop	O
that	O
applies	O
unchanged	O
to	O
many	O
grammars	O
and	O
languages	O
.	O
</s>
<s>
For	O
LR	O
methods	O
,	O
the	O
complex	O
tables	O
are	O
mechanically	O
derived	O
from	O
a	O
grammar	O
by	O
some	O
parser	B-Language
generator	I-Language
tool	O
like	O
Bison	B-Application
.	O
</s>
<s>
The	O
parser	B-Language
tables	O
are	O
usually	O
much	O
larger	O
than	O
the	O
grammar	O
.	O
</s>
<s>
In	O
other	O
parsers	B-Language
that	O
are	O
not	O
table-driven	O
,	O
such	O
as	O
recursive	B-Application
descent	I-Application
,	O
each	O
language	O
construct	O
is	O
parsed	O
by	O
a	O
different	O
subroutine	O
,	O
specialized	O
to	O
the	O
syntax	O
of	O
that	O
one	O
construct	O
.	O
</s>
<s>
The	O
shift-reduce	B-Application
parser	I-Application
is	O
efficient	O
because	O
it	O
involves	O
no	O
backing	O
up	O
.	O
</s>
<s>
Other	O
parser	B-Language
methods	O
that	O
backtrack	O
may	O
take	O
exponential	O
time	O
when	O
they	O
guess	O
badly	O
.	O
</s>
<s>
To	O
avoid	O
guessing	O
,	O
the	O
shift-reduce	B-Application
parser	I-Application
often	O
looks	O
ahead	O
(	O
to	O
the	O
right	O
in	O
left-to-right	O
text	O
)	O
at	O
the	O
next	O
scanned	O
symbol	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
symbol	O
ahead	O
of	O
the	O
rest	O
of	O
the	O
parser	B-Language
.	O
</s>
<s>
A	O
shift-reduce	B-Application
parser	I-Application
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
parser	B-Language
then	O
acts	O
immediately	O
on	O
the	O
combination	O
instead	O
of	O
waiting	O
any	O
further	O
.	O
</s>
<s>
The	O
decisions	O
how	O
to	O
handle	O
B	O
are	O
based	O
only	O
on	O
what	O
the	O
parser	B-Language
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>
is	O
applied	O
,	O
the	O
stack	B-Application
top	O
holds	O
the	O
parse	O
trees	O
"	O
...	O
Products	O
*	O
Value	O
"	O
.	O
</s>
<s>
If	O
the	O
parser	B-Language
builds	O
complete	O
parse	O
trees	O
,	O
then	O
the	O
three	O
trees	O
for	O
inner	O
Products	O
,	O
*	O
,	O
and	O
Value	O
are	O
combined	O
by	O
a	O
new	O
tree	O
root	O
for	O
the	O
larger	O
Products	O
.	O
</s>
<s>
The	O
parser	B-Language
keeps	O
applying	O
reductions	O
to	O
the	O
top	O
of	O
the	O
parse	O
stack	B-Application
for	O
as	O
long	O
as	O
it	O
keeps	O
finding	O
newly	O
completed	O
examples	O
of	O
grammar	O
rules	O
there	O
.	O
</s>
<s>
When	O
no	O
more	O
rules	O
can	O
be	O
applied	O
,	O
the	O
parser	B-Language
then	O
shifts	O
the	O
lookahead	O
symbol	O
onto	O
the	O
parse	O
stack	B-Application
,	O
scans	O
a	O
new	O
lookahead	O
symbol	O
,	O
and	O
tries	O
again	O
.	O
</s>
<s>
The	O
parser	B-Language
tables	O
show	O
what	O
to	O
do	O
next	O
,	O
for	O
every	O
legal	O
combination	O
of	O
topmost	O
parse	O
stack	B-Application
symbols	O
and	O
lookahead	O
symbol	O
.	O
</s>
<s>
The	O
table	O
details	O
vary	O
greatly	O
between	O
different	O
types	O
of	O
shift-reduce	B-Application
parsers	I-Application
.	O
</s>
<s>
In	O
precedence	O
parsers	B-Language
,	O
the	O
right	O
end	O
of	O
handles	O
are	O
found	O
by	O
comparing	O
the	O
precedence	O
level	O
or	O
grammar	O
tightness	O
of	O
the	O
top	O
stack	B-Application
symbols	O
to	O
that	O
of	O
the	O
lookahead	O
symbol	O
.	O
</s>
<s>
There	O
are	O
different	O
varieties	O
of	O
precedence	O
parsers	B-Language
,	O
each	O
with	O
different	O
ways	O
of	O
finding	O
the	O
handle	O
's	O
left	O
end	O
and	O
choosing	O
the	O
correct	O
rule	O
to	O
apply	O
:	O
</s>
<s>
Operator-precedence	B-Application
parser	I-Application
,	O
a	O
very	O
simple	O
numerical	O
method	O
that	O
works	O
for	O
expressions	O
but	O
not	O
general	O
program	O
syntax	O
.	O
</s>
<s>
Simple	B-Application
precedence	I-Application
parser	I-Application
,	O
uses	O
one	O
large	O
MxN	O
table	O
to	O
find	O
right	O
and	O
left	O
ends	O
.	O
</s>
<s>
Used	O
in	O
PL360	B-Language
.	O
</s>
<s>
Weak	O
precedence	B-Application
parser	I-Application
,	O
uses	O
the	O
precedence	O
table	O
only	O
to	O
find	O
handles	O
 '	O
right	O
ends	O
.	O
</s>
<s>
Extended	O
precedence	B-Application
parser	I-Application
.	O
</s>
<s>
Mixed	O
Strategy	O
Precedence	B-Application
parser	I-Application
,	O
used	O
by	O
the	O
original	O
version	O
of	O
XPL	B-Language
.	O
</s>
<s>
Generally	O
has	O
very	O
large	O
tables	O
even	O
for	O
relatively	O
small	O
languages	O
such	O
as	O
XPL	B-Language
itself	O
on	O
account	O
of	O
the	O
great	O
many	O
"	O
triples	O
"	O
which	O
are	O
required	O
to	O
recognize	O
grammars	O
outside	O
the	O
limits	O
imposed	O
by	O
precedence	O
methods	O
.	O
</s>
<s>
Precedence	O
parsers	B-Language
are	O
limited	O
in	O
the	O
grammars	O
they	O
can	O
handle	O
.	O
</s>
<s>
They	O
ignore	O
most	O
of	O
the	O
parse	O
stack	B-Application
when	O
making	O
decisions	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
are	O
a	O
more	O
flexible	O
form	O
of	O
shift-reduce	B-Application
parsing	I-Application
,	O
handling	O
many	O
more	O
grammars	O
.	O
</s>
<s>
LR	B-Application
parsers	I-Application
function	O
like	O
a	O
state	B-Application
machine	I-Application
,	O
performing	O
a	O
state	O
transition	O
for	O
each	O
shift	O
or	O
reduce	O
action	O
.	O
</s>
<s>
These	O
employ	O
a	O
stack	B-Application
where	O
the	O
current	O
state	O
is	O
pushed	O
(	O
down	O
)	O
by	O
shift	O
actions	O
.	O
</s>
<s>
This	O
stack	B-Application
is	O
then	O
popped	O
(	O
up	O
)	O
by	O
reduce	O
actions	O
.	O
</s>
<s>
This	O
mechanism	O
allows	O
the	O
LR	B-Application
parser	I-Application
to	O
handle	O
all	O
deterministic	O
context-free	O
grammars	O
,	O
a	O
superset	O
of	O
precedence	O
grammars	O
.	O
</s>
<s>
The	O
LR	B-Application
parser	I-Application
is	O
fully	O
implemented	O
by	O
the	O
Canonical	B-Application
LR	I-Application
parser	I-Application
.	O
</s>
<s>
The	O
Look-Ahead	B-Application
LR	I-Application
and	O
Simple	B-Application
LR	I-Application
parsers	I-Application
implement	O
simplified	O
variants	O
of	O
it	O
that	O
have	O
significantly	O
reduced	O
memory	O
requirements	O
.	O
</s>
<s>
Recent	O
research	O
has	O
identified	O
methods	O
by	O
which	O
canonical	B-Application
LR	I-Application
parsers	I-Application
may	O
be	O
implemented	O
with	O
dramatically	O
reduced	O
table	O
requirements	O
over	O
Knuth	O
's	O
table-building	O
algorithm	O
.	O
</s>
<s>
Whether	O
LR	O
,	O
LALR	B-Application
or	O
SLR	O
,	O
the	O
basic	O
state	B-Application
machine	I-Application
is	O
the	O
same	O
;	O
only	O
the	O
tables	O
are	O
different	O
,	O
and	O
these	O
tables	O
are	O
almost	O
always	O
mechanically	O
generated	O
.	O
</s>
<s>
Additionally	O
,	O
these	O
tables	O
are	O
usually	O
implemented	O
such	O
that	O
a	O
REDUCE	O
results	O
in	O
a	O
CALL	O
to	O
a	O
closed	O
subroutine	O
which	O
is	O
external	O
to	O
the	O
state	B-Application
machine	I-Application
and	O
which	O
performs	O
a	O
function	O
which	O
is	O
implied	O
by	O
the	O
semantics	O
of	O
the	O
grammar	O
rule	O
which	O
is	O
being	O
REDUCEd	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
parser	B-Language
is	O
partitioned	O
into	O
an	O
invariant	O
state	B-Application
machine	I-Application
part	O
,	O
and	O
a	O
variant	O
semantics	O
part	O
.	O
</s>
<s>
This	O
fundamental	O
distinction	O
encourages	O
the	O
development	O
of	O
high-quality	O
parsers	B-Language
which	O
are	O
exceptionally	O
reliable	O
.	O
</s>
<s>
Given	O
a	O
specific	O
stack	B-Application
state	O
and	O
lookahead	O
symbol	O
,	O
there	O
are	O
precisely	O
four	O
possible	O
actions	O
,	O
ERROR	O
,	O
SHIFT	O
,	O
REDUCE	O
,	O
and	O
STOP	O
(	O
hereinafter	O
referred	O
to	O
as	O
configurations	O
)	O
.	O
</s>
<s>
The	O
presence	O
of	O
a	O
dot	O
,	O
•	O
,	O
in	O
a	O
configuration	O
represents	O
the	O
current	O
lookahead	O
position	O
,	O
with	O
the	O
lookahead	O
symbol	O
shown	O
to	O
the	O
right	O
of	O
the	O
dot	O
(	O
and	O
which	O
always	O
corresponds	O
to	O
a	O
terminal	O
symbol	O
)	O
,	O
and	O
the	O
current	O
stack	B-Application
state	O
to	O
the	O
left	O
of	O
the	O
dot	O
(	O
and	O
which	O
usually	O
corresponds	O
to	O
a	O
nonterminal	O
symbol	O
)	O
.	O
</s>
<s>
In	O
programming	O
systems	O
which	O
support	O
the	O
specification	O
of	O
values	O
in	O
quaternary	O
numeral	O
system	O
(	O
base	O
4	O
,	O
two	O
bits	O
per	O
quaternary	O
digit	O
)	O
,	O
such	O
as	O
XPL	B-Language
,	O
these	O
are	O
coded	O
as	O
,	O
for	O
example	O
:	O
</s>
<s>
The	O
SHIFT	O
and	O
REDUCE	O
configurations	O
are	O
obvious	O
,	O
from	O
the	O
basic	O
definition	O
of	O
a	O
SHIFT-REDUCE	B-Application
parser	I-Application
.	O
</s>
<s>
STOP	O
,	O
then	O
,	O
represents	O
a	O
configuration	O
where	O
the	O
state	O
at	O
the	O
top	O
of	O
the	O
stack	B-Application
and	O
the	O
lookahead	O
terminal	O
symbol	O
is	O
within	O
the	O
subject	O
grammar	O
,	O
and	O
represents	O
the	O
ending	O
of	O
the	O
program	O
:	O
</s>
<s>
ERROR	O
,	O
then	O
,	O
represents	O
a	O
configuration	O
where	O
the	O
state	O
at	O
the	O
top	O
of	O
the	O
stack	B-Application
,	O
and	O
the	O
lookahead	O
terminal	O
symbol	O
is	O
not	O
within	O
the	O
subject	O
grammar	O
.	O
</s>
<s>
where	O
<program>	O
consists	O
only	O
of	O
a	O
"	B-Language
null	I-Language
statement	I-Language
"	I-Language
)	O
.	O
</s>
<s>
Clearly	O
,	O
such	O
a	O
parser	B-Language
has	O
precisely	O
one	O
(	O
implicit	O
)	O
START	O
configuration	O
and	O
one	O
(	O
explicit	O
)	O
STOP	O
configuration	O
,	O
but	O
it	O
can	O
,	O
and	O
usually	O
does	O
have	O
hundreds	O
of	O
SHIFT	O
and	O
REDUCE	O
configurations	O
,	O
and	O
perhaps	O
thousands	O
of	O
ERROR	O
configurations	O
.	O
</s>
