<?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>Ikke&#039;s blog &#187; pyparsing</title>
	<atom:link href="http://eikke.com/tag/pyparsing/feed/" rel="self" type="application/rss+xml" />
	<link>http://eikke.com</link>
	<description>&#039;cause this is what I do</description>
	<lastBuildDate>Sun, 13 Feb 2011 14:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Pyparsing introduction: BNF to code</title>
		<link>http://eikke.com/pyparsing-introduction-bnf-to-code/</link>
		<comments>http://eikke.com/pyparsing-introduction-bnf-to-code/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 16:32:51 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bnf]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[pyparsing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/pyparsing-introduction-bnf-to-code/</guid>
		<description><![CDATA[After reading my previous post, you should have a pretty good understanding of what a BNF definition is all about. Let&#8217;s put this theory into practice, and write some basic parsers in Python, using Pyparsing! Pyparsing allows a pretty one-to-one mapping of BNF to Python code: you can define sets and combinations, then parse any [...]]]></description>
			<content:encoded><![CDATA[<p>After reading my <a href="http://eikke.com/text-parsing-formal-grammars-and-bnf-introduction/" title="eikke.com: Formal grammars and BNF introduction">previous post</a>, you should have a pretty good understanding of what a BNF definition is all about. Let&#8217;s put this theory into practice, and write some basic parsers in <a href="http://www.python.org" title="Python">Python</a>, using <strong>Pyparsing</strong>!</p>
<p>Pyparsing allows a pretty one-to-one <strong>mapping of BNF to Python code</strong>: you can define sets and combinations, then parse any text fragment against it. This is something very important to notice: one basic BNF definition can (and should) be reused: if you once wrote a BNF definition for an integer value, you can easily reuse this definition in, eg, a basic integer math expression.</p>
<p>The most basic element using Pyparsing is a Word. In it&#8217;s most basic form this is a set of characters which will match any arbitrary length string, as long as the characters in this string are part of the Word character set.</p>
<p>A little introduction example: let&#8217;s write a parser which accepts words consisting of small-cap characters, or sentences which consist of words separated by spaces. First we define a formal definition using BNF:</p>
<p><span id="more-51"></span></p>
<ul>
<li>character ::= a | b | c | d | &#8230; | y | z</li>
<li>word ::= character | character word</li>
<li>sentence ::= word | sentence &#8221; &#8221; word</li>
</ul>
<p>Let&#8217;s port this formal definition to Python code. First we need to do some imports, as in most Python programs. I&#8217;d encourage the reader to write this code in an interactive interpreter (give iPython a try, it rocks!) and experiment a little with it (tab-completion and &#8216;?&#8217; rock!):</p>
<pre>from pyparsing import Word
from string import lowercase</pre>
<p>Pyparsing includes several useful pre-defined lists of characters, including</p>
<ul>
<li>alphas: a-zA-Z</li>
<li>nums: 0-9</li>
<li>alphanums: alphas + nums</li>
</ul>
<p>These are normal Python strings. In this sample we only want lowercase characters though, so we import this from the string module.</p>
<p>Now we can define one word: a word is a concatenation of lowercase characters.</p>
<pre>word = Word(lowercase)</pre>
<p>Let&#8217;s play around with this:</p>
<pre>print word.parseString('hello')
# returns ['hello']
print word.parseString('Hello')
# raises ParseException: Expected W:(abcd...) (0), (1,1)
print word.parseString('hello world')
# returns ['hello']</pre>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/pyparsing-introduction-bnf-to-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

