<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
a	O
parser	B-Application
combinator	I-Application
is	O
a	O
higher-order	B-Language
function	I-Language
that	O
accepts	O
several	O
parsers	B-Language
as	O
input	O
and	O
returns	O
a	O
new	O
parser	B-Language
as	O
its	O
output	O
.	O
</s>
<s>
In	O
this	O
context	O
,	O
a	O
parser	B-Language
is	O
a	O
function	O
accepting	O
strings	O
as	O
input	O
and	O
returning	O
some	O
structure	O
as	O
output	O
,	O
typically	O
a	O
parse	O
tree	O
or	O
a	O
set	O
of	O
indices	O
representing	O
locations	O
in	O
the	O
string	O
where	O
parsing	O
stopped	O
successfully	O
.	O
</s>
<s>
Parser	B-Application
combinators	I-Application
enable	O
a	O
recursive	B-Application
descent	I-Application
parsing	I-Application
strategy	O
that	O
facilitates	O
modular	O
piecewise	O
construction	O
and	O
testing	O
.	O
</s>
<s>
This	O
parsing	O
technique	O
is	O
called	O
combinatory	B-Application
parsing	I-Application
.	O
</s>
<s>
Parsers	B-Language
using	O
combinators	O
have	O
been	O
used	O
extensively	O
in	O
the	O
prototyping	O
of	O
compilers	O
and	O
processors	O
for	O
domain-specific	B-Language
languages	I-Language
such	O
as	O
natural-language	B-General_Concept
interfaces	I-General_Concept
to	O
databases	O
,	O
where	O
complex	O
and	O
varied	O
semantic	O
actions	O
are	O
closely	O
integrated	O
with	O
syntactic	O
processing	O
.	O
</s>
<s>
In	O
1989	O
,	O
Richard	O
Frost	O
and	O
John	O
Launchbury	O
demonstrated	O
use	O
of	O
parser	B-Application
combinators	I-Application
to	O
construct	O
natural-language	B-Language
interpreters	O
.	O
</s>
<s>
Graham	O
Hutton	O
also	O
used	O
higher-order	B-Language
functions	I-Language
for	O
basic	O
parsing	O
in	O
1992	O
and	O
monadic	O
parsing	O
in	O
1996	O
.	O
</s>
<s>
S	O
.	O
D	O
.	O
Swierstra	O
also	O
exhibited	O
the	O
practical	O
aspects	O
of	O
parser	B-Application
combinators	I-Application
in	O
2001	O
.	O
</s>
<s>
In	O
2008	O
,	O
Frost	O
,	O
Hafiz	O
and	O
Callaghan	O
described	O
a	O
set	O
of	O
parser	B-Application
combinators	I-Application
in	O
Haskell	B-Language
that	O
solve	O
the	O
long-standing	O
problem	O
of	O
accommodating	O
left	B-Application
recursion	I-Application
,	O
and	O
work	O
as	O
a	O
complete	O
top-down	B-Application
parsing	I-Application
tool	O
in	O
polynomial	O
time	O
and	O
space	O
.	O
</s>
<s>
In	O
any	O
programming	O
language	O
that	O
has	O
first-class	B-Application
functions	I-Application
,	O
parser	B-Application
combinators	I-Application
can	O
be	O
used	O
to	O
combine	O
basic	O
parsers	B-Language
to	O
construct	O
parsers	B-Language
for	O
more	O
complex	O
rules	O
.	O
</s>
<s>
If	O
a	O
simple	O
parser	B-Language
is	O
available	O
for	O
each	O
of	O
these	O
alternatives	O
,	O
a	O
parser	B-Application
combinator	I-Application
can	O
be	O
used	O
to	O
combine	O
each	O
of	O
these	O
parsers	B-Language
,	O
returning	O
a	O
new	O
parser	B-Language
which	O
can	O
recognise	O
any	O
or	O
all	O
of	O
the	O
alternatives	O
.	O
</s>
<s>
In	O
languages	O
that	O
support	O
operator	O
overloading	O
,	O
a	O
parser	B-Application
combinator	I-Application
can	O
take	O
the	O
form	O
of	O
an	O
infix	O
operator	O
,	O
used	O
to	O
glue	O
different	O
parsers	B-Language
to	O
form	O
a	O
complete	O
rule	O
.	O
</s>
<s>
Parser	B-Application
combinators	I-Application
thereby	O
enable	O
parsers	B-Language
to	O
be	O
defined	O
in	O
an	O
embedded	O
style	O
,	O
in	O
code	O
which	O
is	O
similar	O
in	O
structure	O
to	O
the	O
rules	O
of	O
the	O
formal	O
grammar	O
.	O
</s>
<s>
To	O
keep	O
the	O
discussion	O
relatively	O
straightforward	O
,	O
we	O
discuss	O
parser	B-Application
combinators	I-Application
in	O
terms	O
of	O
recognizers	O
only	O
.	O
</s>
<s>
If	O
the	O
input	O
string	O
is	O
of	O
length	O
#input	O
and	O
its	O
members	O
are	O
accessed	O
through	O
an	O
index	O
j	O
,	O
a	O
recognizer	O
is	O
a	O
parser	B-Language
which	O
returns	O
,	O
as	O
output	O
,	O
a	O
set	O
of	O
indices	O
representing	O
positions	O
at	O
which	O
the	O
parser	B-Language
successfully	O
finished	O
recognizing	O
a	O
sequence	O
of	O
tokens	O
that	O
began	O
at	O
position	O
j	O
.	O
</s>
<s>
This	O
parser	B-Language
always	O
succeeds	O
,	O
returning	O
a	O
singleton	O
set	O
containing	O
the	O
current	O
position	O
:	O
</s>
<s>
If	O
the	O
token	O
at	O
position	O
j	O
in	O
the	O
input	O
string	O
is	O
x	O
,	O
this	O
parser	B-Language
returns	O
a	O
singleton	O
set	O
containing	O
j	O
+	O
1	O
;	O
otherwise	O
,	O
it	O
returns	O
the	O
empty	O
set	O
.	O
</s>
<s>
Following	O
the	O
definitions	O
of	O
two	O
basic	O
recognizers	O
p	O
and	O
q	O
,	O
we	O
can	O
define	O
two	O
major	O
parser	B-Application
combinators	I-Application
for	O
alternative	O
and	O
sequencing	O
:	O
</s>
<s>
The	O
‘	O
alternative’	O
parser	B-Application
combinator	I-Application
,	O
⊕	O
,	O
applies	O
both	O
of	O
the	O
recognizers	O
on	O
the	O
same	O
input	O
position	O
j	O
and	O
sums	O
up	O
the	O
results	O
returned	O
by	O
both	O
of	O
the	O
recognizers	O
,	O
which	O
is	O
eventually	O
returned	O
as	O
the	O
final	O
result	O
.	O
</s>
<s>
The	O
sequencing	O
of	O
recognizers	O
is	O
done	O
with	O
the	O
⊛	O
parser	B-Application
combinator	I-Application
.	O
</s>
<s>
Haskell	B-Language
)	O
as	O
s	O
=	O
term	O
‘	O
x’	O
<*> s <*> s <+>	O
empty	O
.	O
</s>
<s>
Parser	B-Application
combinators	I-Application
,	O
like	O
all	O
recursive	B-Application
descent	I-Application
parsers	I-Application
,	O
are	O
not	O
limited	O
to	O
the	O
context-free	O
grammars	O
and	O
thus	O
do	O
no	O
global	O
search	O
for	O
ambiguities	O
in	O
the	O
LL(k )	O
parsing	O
Firstk	O
and	O
Followk	O
sets	O
.	O
</s>
<s>
In	O
such	O
cases	O
,	O
the	O
recursive	B-Application
descent	I-Application
parser	I-Application
may	O
default	O
(	O
perhaps	O
unknown	O
to	O
the	O
grammar	O
designer	O
)	O
to	O
one	O
of	O
the	O
possible	O
ambiguous	O
paths	O
,	O
resulting	O
in	O
semantic	O
confusion	O
(	O
aliasing	O
)	O
in	O
the	O
use	O
of	O
the	O
language	O
.	O
</s>
<s>
The	O
simple	O
implementations	O
of	O
parser	B-Application
combinators	I-Application
have	O
some	O
shortcomings	O
,	O
which	O
are	O
common	O
in	O
top-down	B-Application
parsing	I-Application
.	O
</s>
<s>
Naïve	O
combinatory	B-Application
parsing	I-Application
requires	O
exponential	O
time	O
and	O
space	O
when	O
parsing	O
an	O
ambiguous	O
context-free	O
grammar	O
.	O
</s>
<s>
In	O
1996	O
,	O
Frost	O
and	O
Szydlowski	O
demonstrated	O
how	O
memoization	O
can	O
be	O
used	O
with	O
parser	B-Application
combinators	I-Application
to	O
reduce	O
the	O
time	O
complexity	O
to	O
polynomial	O
.	O
</s>
<s>
Like	O
any	O
top-down	O
recursive	B-Application
descent	I-Application
parsing	I-Application
,	O
the	O
conventional	O
parser	B-Application
combinators	I-Application
(	O
like	O
the	O
combinators	O
described	O
above	O
)	O
will	O
not	O
terminate	O
while	O
processing	O
a	O
left-recursive	B-Application
grammar	I-Application
(	O
e.g.	O
</s>
<s>
This	O
extended	O
algorithm	O
accommodates	O
indirect	O
left	B-Application
recursion	I-Application
by	O
comparing	O
its	O
‘	O
computed	O
context’	O
with	O
‘	O
current	O
context’	O
.	O
</s>
<s>
The	O
same	O
authors	O
also	O
described	O
their	O
implementation	O
of	O
a	O
set	O
of	O
parser	B-Application
combinators	I-Application
written	O
in	O
the	O
Haskell	B-Language
programming	I-Language
language	I-Language
based	O
on	O
the	O
same	O
algorithm	O
.	O
</s>
