Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: gh-pages
Fetching contributors…

Cannot retrieve contributors at this time

234 lines (233 sloc) 10.501 kb
<link rel="alternate" title="Ocean of Awareness RSS" type="application/rss+xml" title="RSS" href="" />
The Marpa parser
<style type="text/css">
strong {font-weight: 700;}
<h1>The Marpa parser</h1>
marpa_r2_html_fmt --no-added-tag-comment --no-ws-ok-after-start-tag
<div style="margin:0;padding:10px 30px 10px 10px;width:150px;float:left;border-right:2px solid #38B0C0">
<h2>This is Jeffrey Kegler's website for Marpa, a parsing algorithm</h2>
<a href="">The official Marpa
starting page</a>.
<a href="" class="twitter-follow-button" data-show-count="false">
Follow @jeffreykegler</a>
<p style="text-align:center">
<!-- Place this code where you want the badge to render. -->
<a href="//" rel="publisher" style="text-decoration:none;">
<img src="//" alt="Google+" style="border:0;width:32px;height:32px;"/></a>
The Ocean of Awareness blog:
<a href="">
home page</a>,
<a href="">
chronological index</a>,
<a href="">
annotated index</a>.
Marpa::R2 (active distribution):
<a href="">
<a href="">
<a href=";fromgroups#%21forum/marpa-parser">
Mailing List</a>
<a href="">
Github repository</a></p>
<a href="">
Jeffrey Kegler's personal website
<div style="margin-left:190px;border-left:2px solid #38B0C0;padding:25px;">
is a parsing algorithm.
It is new, but very much based
on earlier work by Jay Earley, Joop Leo, John Aycock and R. Nigel Horspool.
Marpa is intended to replace, and to go well beyond,
recursive descent and the yacc family of parsers.
Marpa is fast. It parses in linear time:
<li>all the grammar classes that recursive descent parses;</li>
<li>the grammar class that the yacc family parses;</li>
<li>in fact, any unambiguous grammars,
with a couple of exceptions that are not likely to
be an issue in practice (see <a href="#quibbles">quibbles</a>); and
ambiguous grammars that are unions of a finite set of any of the above grammars.</li>
Marpa is powerful. Marpa will parse anything that can be
written in BNF.
This includes any mixture of left, right and middle recursions.
<li>Marpa is convenient.
Unlike recursive descent, you do not have to write a parser --
Marpa generates one from BNF.
Unlike PEG or yacc, parser generation is unrestricted and exact.
Marpa converts any grammar which can be written as BNF
into a parser which recognizes everything
in the language described by that BNF, and which rejects everything that is
not in that language.
The programmer is not forced to make arbitrary choices while parsing.
If a rule has several alternatives,
all of the alternatives are considered for as long as they might yield a valid parse.
Marpa is flexible. Like recursive descent, Marpa allows you to stop and
do your own custom processing. Unlike recursive descent, Marpa makes available
to you detailed information about the parse so far --
which rules and symbols have been recognized, with their locations,
and which rules and symbols are expected next.
<h2>Learning about Marpa</h2>
What you are looking at is the web site maintained by the author of Marpa
(Jeffrey Kegler).
It is
the best page for starting to learn about Marpa.
Good places to do that are:
<li><a href="">Marpa's official starting page</a>,
which is maintained by Ron Savage.
</li><li>The documentation of
<a href="">
Marpa's current stable release.
<h2>Other Marpa resources</h2>
Discussion of Marpa currently centers around
<a href="">
the "marpa parser" Google Group</a>
and the IRC channel:
Most of the posts on
<a href="">
Ocean of Awareness</a>,
my blog,
are about Marpa.
To get oriented in my blog,
start at its
<a href="">
annotated list of the most interesting Marpa posts</a>.
</p><p>If you are interested in tutorials,
<li>My blog contains
<a href="">
several tutorials</a>.</li>
<li>Peter Stuifzand has written another as part of
<a href="">
the Marpa Guide</a>.</li>
<li>And amon has written this
<a href="">
one for Stackoverflow</a>.</li>
For those interested in the mathematics behind Marpa, there's
<a href="" target="_blank">
a paper with pseudocode, and proofs of correctness and of my complexity claims</a>.
<h2>Marpa internals</h2>
<a href="">
is a C library, and is the core of Marpa.
<a href="">
Marpa internals</a>:
These are resources of interest only
to those working on the internals of Marpa itself --
"bleeding edge" documentation, etc.
<h2>Quibbles</h2><a name="quibbles">
<p>I mentioned above that Marpa parses unambiguous grammars in linear time,
with a couple of exceptions,
and claimed that those were unlikely to be bothersome in practice.
Here are the details.
For an unambiguous grammar to be parsed in linear time,
it must
<li>be free of unmarked middle recursions; and
<li>be free of ambiguous right recursions.
<h3>Unmarked middle recursions?</h3>
Unmarked middle recursions are what they sound like:
recursions that are not left and right, but in the middle of
a rule, and for which there is no "marker".
What's a marker?
That gets tricky.
<p>The marker of a middle recursion is anything that allows the parser to find the middle.
Since you can encode a Turing machine into the marker, that means the exact question
of what a marker is, is undecidable.
But, for practical purposes, if you can spot the middle by eyeball, the middle
recursion is "marked".
If you can't, the middle recursion might be unmarked.
<h3>Ambiguous right recursions</h3>
How does an unambiguous grammar manage to include an ambiguous right recursion?
The answer is not very easily, but you can sneak an ambiguous right recursion into
an unambigious grammar,
by having two different right recursive rules,
both of which recurse on the same symbol.
I call these ambiguities right recursive symches -- "symches"
because they are ambiguous due to a choice between symbols.
A right recursion can also be ambiguous because it is a "factoring" --
that is, it divides the input up differently
among its symbols.
But a factoring will make the whole grammar ambiguous, while a symch does not necessarily do so.
<p>Right recursive symches are very easy to avoid.
You just rewrite the rules so that they recurse on different symbols.
Preserving the semantics is no problem in this case --
you simply make sure both of the new symbols
have the same semantics as the original one.
<div id="footer" style="border-top:thick solid #38B0C0;clear:left;padding:1em;">
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);;js.src="//";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-33430331-1");
} catch(err) {}
Jump to Line
Something went wrong with that request. Please try again.