<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
parsing	B-Language
expression	O
grammar	O
(	O
PEG	O
)	O
is	O
a	O
type	O
of	O
analytic	O
formal	O
grammar	O
,	O
i.e.	O
</s>
<s>
The	O
formalism	O
was	O
introduced	O
by	O
Bryan	O
Ford	O
in	O
2004	O
and	O
is	O
closely	O
related	O
to	O
the	O
family	O
of	O
top-down	O
parsing	B-Language
languages	O
introduced	O
in	O
the	O
early	O
1970s	O
.	O
</s>
<s>
by	O
a	O
recursive	B-Application
descent	I-Application
parser	I-Application
.	O
</s>
<s>
Unlike	O
CFGs	O
,	O
PEGs	O
cannot	O
be	O
ambiguous	O
;	O
a	O
string	O
has	O
exactly	O
one	O
valid	O
parse	B-Language
tree	O
or	O
none	O
.	O
</s>
<s>
PEGs	O
are	O
well-suited	O
to	O
parsing	B-Language
computer	O
languages	O
(	O
and	O
artificial	O
human	O
languages	O
such	O
as	O
Lojban	O
)	O
,	O
but	O
not	O
natural	O
languages	O
where	O
the	O
performance	O
of	O
PEG	O
algorithms	O
is	O
comparable	O
to	O
general	O
CFG	O
algorithms	O
such	O
as	O
the	O
Earley	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Formally	O
,	O
a	O
parsing	B-Language
expression	O
grammar	O
consists	O
of	O
:	O
</s>
<s>
A	O
finite	O
set	O
Σ	O
of	O
terminal	O
symbols	O
that	O
is	O
disjoint	B-Algorithm
from	O
N	O
.	O
</s>
<s>
A	O
finite	O
set	O
P	O
of	O
parsing	B-Language
rules	O
.	O
</s>
<s>
Each	O
parsing	B-Language
rule	O
in	O
P	O
has	O
the	O
form	O
A	O
←	O
e	O
,	O
where	O
A	O
is	O
a	O
nonterminal	O
symbol	O
and	O
e	O
is	O
a	O
parsing	B-Language
expression	O
.	O
</s>
<s>
A	O
parsing	B-Language
expression	O
is	O
a	O
hierarchical	O
expression	O
similar	O
to	O
a	O
regular	B-Language
expression	I-Language
,	O
which	O
is	O
constructed	O
in	O
the	O
following	O
fashion	O
:	O
</s>
<s>
An	O
atomic	O
parsing	B-Language
expression	O
consists	O
of	O
:	O
</s>
<s>
Given	O
any	O
existing	O
parsing	B-Language
expressions	O
e	O
,	O
e1	O
,	O
and	O
e2	O
,	O
a	O
new	O
parsing	B-Language
expression	O
can	O
be	O
constructed	O
using	O
the	O
following	O
operators	O
:	O
</s>
<s>
The	O
fundamental	O
difference	O
between	O
context-free	O
grammars	O
and	O
parsing	B-Language
expression	O
grammars	O
is	O
that	O
the	O
PEG	O
's	O
choice	O
operator	O
is	O
ordered	O
.	O
</s>
<s>
Ordered	O
choice	O
is	O
analogous	O
to	O
soft	B-Application
cut	I-Application
operators	O
available	O
in	O
some	O
logic	B-Language
programming	I-Language
languages	I-Language
.	O
</s>
<s>
The	O
consequence	O
is	O
that	O
if	O
a	O
CFG	O
is	O
transliterated	O
directly	O
to	O
a	O
PEG	O
,	O
any	O
ambiguity	O
in	O
the	O
former	O
is	O
resolved	O
by	O
deterministically	O
picking	O
one	O
parse	B-Language
tree	O
from	O
the	O
possible	O
parses	B-Language
.	O
</s>
<s>
By	O
carefully	O
choosing	O
the	O
order	O
in	O
which	O
the	O
grammar	O
alternatives	O
are	O
specified	O
,	O
a	O
programmer	O
has	O
a	O
great	O
deal	O
of	O
control	O
over	O
which	O
parse	B-Language
tree	O
is	O
selected	O
.	O
</s>
<s>
Like	O
boolean	O
context-free	O
grammars	O
,	O
parsing	B-Language
expression	O
grammars	O
also	O
add	O
the	O
and	O
-	O
and	O
not	O
-	O
syntactic	B-Application
predicates	I-Application
.	O
</s>
<s>
Because	O
they	O
can	O
use	O
an	O
arbitrarily	O
complex	O
sub-expression	O
to	O
"	O
look	O
ahead	O
"	O
into	O
the	O
input	O
string	O
without	O
actually	O
consuming	O
it	O
,	O
they	O
provide	O
a	O
powerful	O
syntactic	O
lookahead	O
and	O
disambiguation	O
facility	O
,	O
in	O
particular	O
when	O
reordering	O
the	O
alternatives	O
cannot	O
specify	O
the	O
exact	O
parse	B-Language
tree	O
desired	O
.	O
</s>
<s>
Each	O
nonterminal	O
in	O
a	O
parsing	B-Language
expression	O
grammar	O
essentially	O
represents	O
a	O
parsing	B-Language
function	O
in	O
a	O
recursive	B-Application
descent	I-Application
parser	I-Application
,	O
and	O
the	O
corresponding	O
parsing	B-Language
expression	O
represents	O
the	O
"	O
code	O
"	O
comprising	O
the	O
function	O
.	O
</s>
<s>
Each	O
parsing	B-Language
function	O
conceptually	O
takes	O
an	O
input	O
string	O
as	O
its	O
argument	O
,	O
and	O
yields	O
one	O
of	O
the	O
following	O
results	O
:	O
</s>
<s>
An	O
atomic	O
parsing	B-Language
expression	O
consisting	O
of	O
a	O
single	O
terminal	O
(	O
i.e.	O
</s>
<s>
An	O
atomic	O
parsing	B-Language
expression	O
consisting	O
of	O
the	O
empty	O
string	O
always	O
trivially	O
succeeds	O
without	O
consuming	O
any	O
input	O
.	O
</s>
<s>
An	O
atomic	O
parsing	B-Language
expression	O
consisting	O
of	O
a	O
nonterminal	O
A	O
represents	O
a	O
recursive	O
call	O
to	O
the	O
nonterminal-function	O
A	O
.	O
</s>
<s>
Otherwise	O
,	O
if	O
e1	O
fails	O
,	O
then	O
the	O
choice	O
operator	O
backtracks	B-Algorithm
to	O
the	O
original	O
input	O
position	O
at	O
which	O
it	O
invoked	O
e1	O
,	O
but	O
then	O
calls	O
e2	O
instead	O
,	O
returning	O
e2	O
's	O
result	O
.	O
</s>
<s>
Unlike	O
in	O
context-free	O
grammars	O
and	O
regular	B-Language
expressions	I-Language
,	O
however	O
,	O
these	O
operators	O
always	O
behave	O
greedily	B-Algorithm
,	O
consuming	O
as	O
much	O
input	O
as	O
possible	O
and	O
never	O
backtracking	B-Algorithm
.	O
</s>
<s>
(	O
Regular	B-Language
expression	I-Language
matchers	O
may	O
start	O
by	O
matching	O
greedily	B-Algorithm
,	O
but	O
will	O
then	O
backtrack	O
and	O
try	O
shorter	O
matches	O
if	O
they	O
fail	O
to	O
match	O
.	O
)	O
</s>
<s>
(	O
This	O
range	O
syntax	O
is	O
the	O
same	O
as	O
the	O
syntax	O
used	O
by	O
regular	B-Language
expressions	I-Language
.	O
)	O
</s>
<s>
Note	O
that	O
rules	O
Sum	O
and	O
Product	O
do	O
n't	O
lead	O
to	O
desired	O
left-associativity	O
of	O
these	O
operations	O
(	O
they	O
do	O
n't	O
deal	O
with	O
associativity	O
at	O
all	O
,	O
and	O
it	O
has	O
to	O
be	O
handled	O
in	O
post-processing	O
step	O
after	O
parsing	B-Language
)	O
,	O
and	O
the	O
Power	O
rule	O
(	O
by	O
referring	O
to	O
itself	O
on	O
the	O
right	O
)	O
results	O
in	O
desired	O
right-associativity	O
of	O
exponent	O
.	O
</s>
<s>
The	O
parsing	B-Language
expression	O
matches	O
and	O
consumes	O
the	O
text	O
"	O
foo	O
"	O
but	O
only	O
if	O
it	O
is	O
followed	O
by	O
the	O
text	O
"	O
bar	O
"	O
.	O
</s>
<s>
The	O
parsing	B-Language
expression	O
matches	O
the	O
text	O
"	O
foo	O
"	O
but	O
only	O
if	O
it	O
is	O
not	O
followed	O
by	O
the	O
text	O
"	O
bar	O
"	O
.	O
</s>
<s>
The	O
parsing	B-Language
expression	O
matches	O
and	O
consumes	O
an	O
arbitrary-length	O
sequence	O
of	O
a	O
's	O
and	O
b	O
's	O
.	O
</s>
<s>
The	O
following	O
parsing	B-Language
expression	O
grammar	O
describes	O
the	O
classic	O
non-context-free	O
language	O
:	O
</s>
<s>
Any	O
parsing	B-Language
expression	O
grammar	O
can	O
be	O
converted	O
directly	O
into	O
a	O
recursive	B-Application
descent	I-Application
parser	I-Application
.	O
</s>
<s>
Due	O
to	O
the	O
unlimited	O
lookahead	O
capability	O
that	O
the	O
grammar	O
formalism	O
provides	O
,	O
however	O
,	O
the	O
resulting	O
parser	B-Language
could	O
exhibit	O
exponential	O
time	O
performance	O
in	O
the	O
worst	O
case	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
obtain	O
better	O
performance	O
for	O
any	O
parsing	B-Language
expression	O
grammar	O
by	O
converting	O
its	O
recursive	B-Application
descent	I-Application
parser	I-Application
into	O
a	O
packrat	B-Application
parser	I-Application
,	O
which	O
always	O
runs	O
in	O
linear	O
time	O
,	O
at	O
the	O
cost	O
of	O
substantially	O
greater	O
storage	O
space	O
requirements	O
.	O
</s>
<s>
is	O
a	O
form	O
of	O
parser	B-Language
similar	O
to	O
a	O
recursive	B-Application
descent	I-Application
parser	I-Application
in	O
construction	O
,	O
except	O
that	O
during	O
the	O
parsing	B-Language
process	O
it	O
memoizes	O
the	O
intermediate	O
results	O
of	O
all	O
invocations	O
of	O
the	O
mutually	B-Algorithm
recursive	I-Algorithm
parsing	B-Language
functions	O
,	O
ensuring	O
that	O
each	O
parsing	B-Language
function	O
is	O
only	O
invoked	O
at	O
most	O
once	O
at	O
a	O
given	O
input	O
position	O
.	O
</s>
<s>
Because	O
of	O
this	O
memoization	O
,	O
a	O
packrat	B-Application
parser	I-Application
has	O
the	O
ability	O
to	O
parse	B-Language
many	O
context-free	O
grammars	O
and	O
any	O
parsing	B-Language
expression	O
grammar	O
(	O
including	O
some	O
that	O
do	O
not	O
represent	O
context-free	O
languages	O
)	O
in	O
linear	O
time	O
.	O
</s>
<s>
Examples	O
of	O
memoized	O
recursive	B-Application
descent	I-Application
parsers	I-Application
are	O
known	O
from	O
at	O
least	O
as	O
early	O
as	O
1993	O
.	O
</s>
<s>
This	O
analysis	O
of	O
the	O
performance	O
of	O
a	O
packrat	B-Application
parser	I-Application
assumes	O
that	O
enough	O
memory	O
is	O
available	O
to	O
hold	O
all	O
of	O
the	O
memoized	O
results	O
;	O
in	O
practice	O
,	O
if	O
there	O
is	O
not	O
enough	O
memory	O
,	O
some	O
parsing	B-Language
functions	O
might	O
have	O
to	O
be	O
invoked	O
more	O
than	O
once	O
at	O
the	O
same	O
input	O
position	O
,	O
and	O
consequently	O
the	O
parser	B-Language
could	O
take	O
more	O
than	O
linear	O
time	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
build	O
LL	B-Language
parsers	I-Language
and	O
LR	B-Application
parsers	I-Application
from	O
parsing	B-Language
expression	O
grammars	O
,	O
with	O
better	O
worst-case	O
performance	O
than	O
a	O
recursive	B-Application
descent	I-Application
parser	I-Application
,	O
but	O
the	O
unlimited	O
lookahead	O
capability	O
of	O
the	O
grammar	O
formalism	O
is	O
then	O
lost	O
.	O
</s>
<s>
Therefore	O
,	O
not	O
all	O
languages	O
that	O
can	O
be	O
expressed	O
using	O
parsing	B-Language
expression	O
grammars	O
can	O
be	O
parsed	B-Language
by	O
LL	O
or	O
LR	B-Application
parsers	I-Application
.	O
</s>
<s>
Compared	O
to	O
pure	O
regular	B-Language
expressions	I-Language
(	O
i.e.	O
</s>
<s>
For	O
example	O
,	O
a	O
regular	B-Language
expression	I-Language
inherently	O
cannot	O
find	O
an	O
arbitrary	O
number	O
of	O
matched	O
pairs	O
of	O
parentheses	O
,	O
because	O
it	O
is	O
not	O
recursive	O
,	O
but	O
a	O
PEG	O
can	O
.	O
</s>
<s>
However	O
,	O
a	O
PEG	O
will	O
require	O
an	O
amount	O
of	O
memory	O
proportional	O
to	O
the	O
length	O
of	O
the	O
input	O
,	O
while	O
a	O
regular	B-Language
expression	I-Language
matcher	O
will	O
require	O
only	O
a	O
constant	O
amount	O
of	O
memory	O
.	O
</s>
<s>
Any	O
PEG	O
can	O
be	O
parsed	B-Language
in	O
linear	O
time	O
by	O
using	O
a	O
packrat	B-Application
parser	I-Application
,	O
as	O
described	O
above	O
.	O
</s>
<s>
PEG	O
parsing	B-Language
is	O
typically	O
carried	O
out	O
via	O
packrat	O
parsing	B-Language
,	O
which	O
uses	O
memoization	O
to	O
eliminate	O
redundant	O
parsing	B-Language
steps	O
.	O
</s>
<s>
Packrat	O
parsing	B-Language
requires	O
storage	O
proportional	O
to	O
the	O
total	O
input	O
size	O
,	O
rather	O
than	O
the	O
depth	O
of	O
the	O
parse	B-Language
tree	O
as	O
with	O
LR	B-Application
parsers	I-Application
.	O
</s>
<s>
For	O
some	O
grammars	O
and	O
some	O
inputs	O
,	O
the	O
depth	O
of	O
the	O
parse	B-Language
tree	O
can	O
be	O
proportional	O
to	O
the	O
input	O
size	O
,	O
</s>
<s>
so	O
both	O
an	O
LR	B-Application
parser	I-Application
and	O
a	O
packrat	B-Application
parser	I-Application
will	O
appear	O
to	O
have	O
the	O
same	O
worst-case	O
asymptotic	O
performance	O
.	O
</s>
<s>
A	O
more	O
accurate	O
analysis	O
would	O
take	O
the	O
depth	O
of	O
the	O
parse	B-Language
tree	O
into	O
account	O
separately	O
from	O
the	O
input	O
size	O
.	O
</s>
<s>
This	O
is	O
similar	O
to	O
a	O
situation	O
which	O
arises	O
in	O
graph	O
algorithms	O
:	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
and	O
Floyd	B-Algorithm
–	I-Algorithm
Warshall	I-Algorithm
algorithm	I-Algorithm
appear	O
to	O
have	O
the	O
same	O
running	O
time	O
(	O
)	O
if	O
only	O
the	O
number	O
of	O
vertices	O
is	O
considered	O
.	O
</s>
<s>
However	O
,	O
a	O
more	O
precise	O
analysis	O
which	O
accounts	O
for	O
the	O
number	O
of	O
edges	O
as	O
a	O
separate	O
parameter	O
assigns	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
a	O
time	O
of	O
,	O
which	O
is	O
quadratic	O
for	O
sparse	O
graphs	O
with	O
.	O
</s>
<s>
A	O
PEG	O
is	O
called	O
well-formed	O
if	O
it	O
contains	O
no	O
left-recursive	B-Application
rules	O
,	O
i.e.	O
,	O
rules	O
that	O
allow	O
a	O
nonterminal	O
to	O
expand	O
to	O
an	O
expression	O
in	O
which	O
the	O
same	O
nonterminal	O
occurs	O
as	O
the	O
leftmost	O
symbol	O
.	O
</s>
<s>
For	O
a	O
left-to-right	O
top-down	O
parser	B-Language
,	O
such	O
rules	O
cause	O
infinite	O
regress	O
:	O
parsing	B-Language
will	O
continually	O
expand	O
the	O
same	O
nonterminal	O
without	O
moving	O
forward	O
in	O
the	O
string	O
.	O
</s>
<s>
Therefore	O
,	O
to	O
allow	O
packrat	O
parsing	B-Language
,	O
left	B-Application
recursion	I-Application
must	O
be	O
eliminated	O
.	O
</s>
<s>
However	O
,	O
left-recursive	B-Application
rules	O
can	O
always	O
be	O
rewritten	O
to	O
eliminate	O
left-recursion	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
following	O
left-recursive	B-Application
CFG	O
rule	O
:	O
</s>
<s>
The	O
process	O
of	O
rewriting	O
indirectly	O
left-recursive	B-Application
rules	O
is	O
complex	O
in	O
some	O
packrat	B-Application
parsers	I-Application
,	O
especially	O
when	O
semantic	O
actions	O
are	O
involved	O
.	O
</s>
<s>
With	O
some	O
modification	O
,	O
traditional	O
packrat	O
parsing	B-Language
can	O
support	O
direct	O
left	B-Application
recursion	I-Application
,	O
but	O
doing	O
so	O
results	O
in	O
a	O
loss	O
of	O
the	O
linear-time	O
parsing	B-Language
property	O
which	O
is	O
generally	O
the	O
justification	O
for	O
using	O
PEGs	O
and	O
packrat	O
parsing	B-Language
in	O
the	O
first	O
place	O
.	O
</s>
<s>
Only	O
the	O
OMeta	B-Language
parsing	B-Language
algorithm	O
supports	O
full	O
direct	O
and	O
indirect	O
left	B-Application
recursion	I-Application
without	O
additional	O
attendant	O
complexity	O
(	O
but	O
again	O
,	O
at	O
a	O
loss	O
of	O
the	O
linear	O
time	O
complexity	O
)	O
,	O
whereas	O
all	O
GLR	B-Application
parsers	I-Application
support	O
left	B-Application
recursion	I-Application
.	O
</s>
<s>
PEG	O
packrat	B-Application
parsers	I-Application
cannot	O
recognize	O
some	O
unambiguous	O
nondeterministic	O
CFG	O
rules	O
,	O
such	O
as	O
the	O
following	O
:	O
</s>
<s>
Neither	O
LL(k )	O
nor	O
LR(k )	O
parsing	B-Language
algorithms	O
are	O
capable	O
of	O
recognizing	O
this	O
example	O
.	O
</s>
<s>
However	O
,	O
this	O
grammar	O
can	O
be	O
used	O
by	O
a	O
general	O
CFG	O
parser	B-Language
like	O
the	O
CYK	B-Application
algorithm	I-Application
.	O
</s>
<s>
However	O
,	O
the	O
language	O
in	O
question	O
can	O
be	O
recognised	O
by	O
all	O
these	O
types	O
of	O
parser	B-Language
,	O
since	O
it	O
is	O
in	O
fact	O
a	O
regular	O
language	O
(	O
that	O
of	O
strings	O
of	O
an	O
odd	O
number	O
of	O
x	O
's	O
)	O
.	O
</s>
<s>
It	O
is	O
an	O
open	O
problem	O
to	O
give	O
a	O
concrete	O
example	O
of	O
a	O
context-free	O
language	O
which	O
cannot	O
be	O
recognized	O
by	O
a	O
parsing	B-Language
expression	O
grammar	O
.	O
</s>
<s>
In	O
particular	O
,	O
it	O
is	O
open	O
whether	O
a	O
parsing	B-Language
expression	O
grammar	O
can	O
recognize	O
the	O
language	O
of	O
palindromes	O
.	O
</s>
<s>
LL(k )	O
and	O
LR(k )	O
parser	B-Language
generators	O
will	O
fail	O
to	O
complete	O
when	O
the	O
input	O
grammar	O
is	O
ambiguous	O
.	O
</s>
<s>
A	O
PEG	O
parser	B-Language
generator	O
will	O
resolve	O
unintended	O
ambiguities	O
earliest-match-first	O
,	O
which	O
may	O
be	O
arbitrary	O
and	O
lead	O
to	O
surprising	O
parses	B-Language
.	O
</s>
<s>
A	O
pika	O
parser	B-Language
uses	O
dynamic	O
programming	O
to	O
apply	O
PEG	O
rules	O
bottom-up	O
and	O
right	O
to	O
left	O
,	O
which	O
is	O
the	O
inverse	O
of	O
the	O
normal	O
recursive	B-Application
descent	I-Application
order	O
of	O
top-down	O
,	O
left	O
to	O
right	O
.	O
</s>
<s>
Parsing	B-Language
in	O
reverse	O
order	O
solves	O
the	O
left	B-Application
recursion	I-Application
problem	O
,	O
allowing	O
left-recursive	B-Application
rules	O
to	O
be	O
used	O
directly	O
in	O
the	O
grammar	O
without	O
being	O
rewritten	O
into	O
non-left-recursive	O
form	O
,	O
and	O
also	O
conveys	O
optimal	O
error	O
recovery	O
capabilities	O
upon	O
the	O
parser	B-Language
,	O
which	O
historically	O
proved	O
difficult	O
to	O
achieve	O
for	O
recursive	B-Application
descent	I-Application
parsers	I-Application
.	O
</s>
<s>
Python	B-Language
reference	O
implementation	O
CPython	B-Language
introduced	O
PEG	O
parser	B-Language
in	O
version	O
3.9	O
as	O
an	O
alternative	O
to	O
the	O
LL(1 )	O
parser	B-Language
and	O
uses	O
just	O
PEG	O
from	O
version	O
3.10	O
.	O
</s>
