<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Balafon &#187; parsing expression grammar</title>
	<atom:link href="http://balafon.net/archives/tag/parsing-expression-grammar/feed" rel="self" type="application/rss+xml" />
	<link>http://balafon.net</link>
	<description>Shouting Into the Void</description>
	<lastBuildDate>Fri, 27 Jan 2012 16:56:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Parsing Expression Grammars and Left Recursion</title>
		<link>http://balafon.net/archives/660</link>
		<comments>http://balafon.net/archives/660#comments</comments>
		<pubDate>Fri, 25 Jul 2008 16:41:20 +0000</pubDate>
		<dc:creator>Gordon</dc:creator>
				<category><![CDATA[Journal]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[parsing expression grammar]]></category>

		<guid isPermaLink="false">http://balafon.net/?p=660</guid>
		<description><![CDATA[Parsing Expression Grammars are a form of context-free grammars with ordered choice. This reduces ambiguity in the grammar and makes writing simple recursive-descent parsers (and parser generators) quite easy. With memoization, you can parse in linear time. The resulting parser is called a Packrat Parser. The only problem is handling left-recursion. If you have grammar [...]]]></description>
			<content:encoded><![CDATA[<p>Parsing Expression Grammars are a form of context-free grammars with ordered choice.  This reduces ambiguity in the grammar and makes writing simple recursive-descent parsers (and parser generators) quite easy.  With memoization, you can parse in linear time.  The resulting parser is called a <a href="http://pdos.csail.mit.edu/~baford/packrat/">Packrat Parser</a>.</p>
<p>The only problem is handling left-recursion.  If you have grammar rules like this:</p>
<p><code>Expression = Expression "+" Term<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| Term<br />
</code></p>
<p>your recursive-descent parser will recurse until it runs out of stack.</p>
<p>I just noticed a paper on detecting and handling left-recursion: <a href="http://www.vpri.org/pdf/packrat_TR-2007-002.pdf">Packrat Parsers can Handle Left Recursion</a>.  It&#8217;s a nifty technique, but it somewhat reduces the simplicity of the resulting program.</p>
<p>For a finger exercise a while ago I wrote a pretty basic packrat parser generator and used it to implement a C preprocessor.  Source is <a href="https://narwhal.svn.sourceforge.net/svnroot/narwhal/branches/old_c_work">here</a>.  When my supply of circular tuits increases I&#8217;ll have to look at implementing the stuff from the paper.</p>
<div class="bottomcontainerBox" style="background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fbalafon.net%2Farchives%2F660&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://balafon.net/archives/660"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://balafon.net/archives/660"  data-text="Parsing Expression Grammars and Left Recursion" data-count="horizontal" data-via="kulibali">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://balafon.net/archives/660" data-counter="right"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div>]]></content:encoded>
			<wfw:commentRss>http://balafon.net/archives/660/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

