<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Boost.Spirit</title>
	
	<link>http://boost-spirit.com/home</link>
	<description>Home of The Boost.Spirit Library</description>
	<lastBuildDate>Tue, 09 Mar 2010 15:20:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Boost-Spirit" /><feedburner:info uri="boost-spirit" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Integer to String Conversion: Karma fastest again</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/pfpmImvqBus/</link>
		<comments>http://boost-spirit.com/home/2010/03/09/integer-to-string-conversion-karma-fastest-again/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 13:24:01 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Karma]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=1031</guid>
		<description><![CDATA[Tino Didriksin published yet another speed comparison of different ways to convert an integer into a string (see his blog A howl on the Wind&#8230;). And again, Karma turns out to be the leader of the pack by being upto 10 times faster than the other tested methods. This nicely supports our own measurements (see here). Thanks [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>Tino Didriksin published yet another speed comparison of different ways to convert an integer into a string (see his blog <a title="A howl on the Wind..." href="http://tinodidriksen.com/2010/02/07/cpp-convert-int-to-string-speed/">A howl on the Wind&#8230;</a>). And again, <em>Karma</em> turns out to be the leader of the pack by being upto 10 times faster than the other tested methods. This nicely supports our own measurements (see <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/karma/performance_measurements/numeric_performance/int_performance.html">here</a>). Thanks Tino!</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=pfpmImvqBus:uVtx7X7QF8I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/pfpmImvqBus" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/03/09/integer-to-string-conversion-karma-fastest-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/03/09/integer-to-string-conversion-karma-fastest-again/</feedburner:origLink></item>
		<item>
		<title>Tracking the Input Position While Parsing</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/MSajD0k-i8E/</link>
		<comments>http://boost-spirit.com/home/2010/03/05/tracking-the-input-position-while-parsing/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 16:21:53 +0000</pubDate>
		<dc:creator>Peter Schüller</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Qi Example]]></category>
		<category><![CDATA[MultiPass]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=1026</guid>
		<description><![CDATA[The following article is about tracking the parsing position with Spirit V2. This is useful for generating error messages which tell the user exactly where an error has occurred. We also show how to use Spirit V2 to parse from an input stream without first reading the whole stream into a std::string.
Continue reading »
Rating: 5.0/5 [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>The following article is about tracking the parsing position with <em>Spirit</em> V2. This is useful for generating error messages which tell the user exactly where an error has occurred. We also show how to use <em>Spirit</em> V2 to parse from an input stream without first reading the whole stream into a std::string.</p>
<p><a href="http://boost-spirit.com/home/articles/qi-example/tracking-the-input-position-while-parsing/">Continue reading »</a></p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=MSajD0k-i8E:DoNoblc6ySg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/MSajD0k-i8E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/03/05/tracking-the-input-position-while-parsing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/03/05/tracking-the-input-position-while-parsing/</feedburner:origLink></item>
		<item>
		<title>The Anatomy of Semantic Actions in Qi</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/-4CzohooKNM/</link>
		<comments>http://boost-spirit.com/home/2010/03/03/the-anatomy-of-semantic-actions-in-qi/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 18:04:35 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Karma]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=1010</guid>
		<description><![CDATA[The concept of Spirit&#8217;s semantic actions seems to be easy enough to understand as most people new to the library prefer their usage over applying the built-in attribute propagation rules. That is not surprising. The idea of attaching a function to any point of a grammar which is called whenever the corresponding parser matched is [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>The concept of <em>Spirit&#8217;s</em> semantic actions seems to be easy enough to understand as most people new to the library prefer their usage over applying the built-in attribute propagation rules. That is not surprising. The idea of attaching a function to any point of a grammar which is called whenever the corresponding parser matched is straighforward to grasp. Earlier versions of <em>Spirit</em> required a semantic action to conform to a very specific interface. Today&#8217;s semantic actions are more flexible and more powerful. Recently, a couple of people asked questions about them. So I decided dedicating this Tip of the Day to the specifics and the usage model of semantic actions in <em>Spirit Qi</em>.</p>
<p><span id="more-1010"></span></p>
<p>All three of <em>Spirit&#8217;s</em> sub-libraries &#8211; <em>Qi</em>, <em>Karma</em>, and <em>Lex</em> – support semantic actions. In each case they are different and have some specifics. Today I will highlight semantic actions in <em>Qi</em>. But I will dedicate later Tips of the Day to semantic actions in <em>Karma</em> and  <em>Lex</em>.</p>
<p>Semantic actions are functions or function objects attached to some specific part of a grammar. In <em>Qi</em> they are invoked <em>after</em> the corresponding parser successfully recognizes a portion of the input. Here the semantic action receives the attribute value of the matching parser.</p>
<h5>Semantic Actions – a General View</h5>
<p>A semantic action <span style="font-family: Courier New;">f</span> are attached to a <em>Qi</em> parser <span style="font-family: Courier New;">p</span> by simply writing:</p>
<pre class="brush: cpp;">
p[f]
</pre>
<p>The function (or function object) <span style="font-family: Courier New;">f</span> has to expose a certain interface allowing <em>Spirit</em> to pass the proper argument types. In the simplest case this can be a global function taking no arguments at all.</p>
<pre class="brush: cpp;">
void func()
{
    std::cout &lt;&lt; &quot;Matched an integer!\n&quot;;
}

std::string input(&quot;1234&quot;);
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
qi::parse(begin, end, qi::int_[func]);     // this will call func
</pre>
<p>Most of the time this is not sufficient as a semantic action is expected to receive the matched attribute value. This is possible by writing:</p>
<pre class="brush: cpp;">
void func(int attribute)
{
    std::cout &lt;&lt; &quot;Matched integer: &quot; &lt;&lt; attribute &lt;&lt; &quot;\n&quot;;
}
</pre>
<p>The type of the expected parameter (in this case the <span style="font-family: Courier New;">int</span>) depends on the parser the semantic action is attached to. The attribute type exposed by the parser has to be convertible to the argument type.</p>
<p>There are actually 2 more arguments being passed: the parser context and a reference to a boolean &#8216;hit&#8217; parameter. The parser context is meaningful only if the semantic action is attached somewhere to the right hand side of a rule. We will see more information about this shortly. The boolean value can be set to false inside the semantic action invalidates the match in retrospective, making the parser fail. <em>Qi</em> allows us to bind a nullary or a single argument function, like above. The other arguments are simply ignored.</p>
<p>It is feasible to bind any function object (such as generated by <a href="http://www.boost.org/doc/libs/1_42_0/libs/bind/index.html">Boost.Bind</a> or <a href="http://www.boost.org/doc/libs/1_42_0/libs/lambda/index.html">Boost.Lambda</a>) as an semantic action. Even if the documentation shows a couple of examples (see <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/qi/tutorials/semantic_actions.html#spirit.qi.tutorials.semantic_actions.examples_of_semantic_actions">here</a>), I would not recommend using those libraries in this context. For me the preferred method of writing semantic actions is to employ <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/index.html">Boost.Phoenix</a> &#8211; a companion library bundled with <em>Spirit</em>. It is like <a href="http://www.boost.org/libs/lambda/index.html">Boost.Lambda</a> on steroids, with special custom features that make it easy to integrate semantic actions with Spirit. If your requirements go beyond simple parsing, I suggest that you use this library. All the following examples in this article will use <a href="http://www.boost.org/phoenix/doc/html/index.html">Boost.Phoenix</a> for semantic actions. But whatever method you use, please let me highlight the following:</p>
<blockquote><p>The three libraries allow you to utilize special placeholders to control parameter placement (<code>_1</code>, <code>_2</code>, etc.). Unfortunately, each of those libraries has it&#8217;s own implementation of the placeholders, all in different namespaces. You have to make sure not to mix placeholders with a library they don&#8217;t belong to and not to use different libraries while writing a semantic action.</p>
<p>Generally, for <a href="http://www.boost.org/libs/bind/index.html">Boost.Bind</a>, use <code>::_1</code>, <code>::_2</code>, etc. (yes, these placeholders are defined in the global namespace).</p>
<p>For <a href="http://www.boost.org/libs/lambda/index.html">Boost.Lambda</a> use the placeholders defined in the namespace <code>boost::lambda</code>.</p>
<p>For semantic actions written using <a href="http://www.boost.org/phoenix/doc/html/index.html">Boost.Phoenix</a> use the placeholders defined in the namespace <code>boost::spirit</code>. Please note that all existing placeholders for your convenience are also available from the namespace <code>boost::spirit::qi</code>.</p></blockquote>
<p>The current version of Spirit (V2.2) does not yet support binding a native C++0x lambda function as a semantic action, but this is something we are currently working on. You can expect this to be possible in the near future.</p>
<h5>Writing Phoenix based Semantic Actions</h5>
<p>Writing a semantic action with Phoenix is beneficial as <em>Spirit</em>  &#8216;knows&#8217; about Phoenix. If you write them with the help of Phoenix you can utilize special placeholders <em>Spirit</em> provides you with. Those placeholders refer to elements in the context of the current parser execution such as attributes, local variables and inherited attributes of rules, etc. None of the other means of writing semantic actions (using Bind, Lambda, or had written function objects) gives you direct access to those elements. The following table lists all available placeholders exposed by Spirit (as mentioned earlier, all are defined in the namespace <span style="font-family: Courier New;">boost::spirit::qi</span>). Again, please note, these are only available inside a semantic action and only if the semantic action is written utilizing Phoenix.</p>
<table border="1" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td width="206" valign="top"><strong>Placeholder</strong></td>
<td width="394" valign="top"><strong>Description</strong></td>
</tr>
<tr>
<td width="206" valign="top"><code>_1, _2, ... , _N</code></td>
<td width="394" valign="top">Nth attribute of the parser <code>p</code></td>
</tr>
<tr>
<td width="206" valign="top">
<dt><code>_pass</code></dt>
</td>
<td width="394" valign="top">Assign <code>false</code> to <code>_pass</code> to force a generator failure.</td>
</tr>
<tr>
<td width="206" valign="top">
<dt><code>_val</code></dt>
</td>
<td width="394" valign="top">The enclosing rule&#8217;s synthesized attribute.</td>
</tr>
<tr>
<td width="206" valign="top">
<dt><code>_r1, _r2, ... , _rN</code></dt>
</td>
<td width="394" valign="top">The enclosing rule&#8217;s Nth inherited attribute.</td>
</tr>
<tr>
<td width="206" valign="top">
<dt><code>_a, _b, ... , _j</code></dt>
</td>
<td width="394" valign="top">The enclosing rule&#8217;s local variables (<code>_a</code> refers to the first).</td>
</tr>
<tr>
<td width="206" valign="top"> </td>
<td width="394" valign="top"> </td>
</tr>
</tbody>
</table>
<p>Obviously, the placeholders listed in the last three rows of the table are meaningful only if used in a rule definition. As an example, let us rewrite the semantic action from above with Phoenix:</p>
<pre class="brush: cpp;">
std::string input(&quot;1234&quot;);
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
qi::parse(begin, end,
    qi::int_
    [
        std::cout &lt;&lt; &quot;Matched integer: &quot; &lt;&lt; qi::_1 &lt;&lt; &quot;\n&quot;;
    ]
);
</pre>
<p>One problem with earlier versions of Spirit (i.e. <em>Spirit.Classic</em>) was that while parsing sequences of things it was difficult to avoid calling a semantic action prematurely. For instance, in a parser sequence of two integer parsers (<span style="font-family: Courier New;">int_[f1] &gt;&gt; &#8216;,&#8217; &gt;&gt; int_[f2]</span>) the function <span style="font-family: Courier New;">f1</span> got called immediately after the first integer matched, and even if the second integer parser would fail later on. In the current version of Spirit this is not an issue anymore as it is possible to attach a semantic action to the whole sequence while still referring to the single attributes of the different sequence elements:</p>
<pre class="brush: cpp;">
std::string input(&quot;1234,2345&quot;);
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
qi::parse(begin, end,
    (qi::int_ &gt;&gt; ',' &gt;&gt; qi::int_)
    [
        std::cout &lt;&lt; &quot;Matched integers: &quot;
              &lt;&lt; qi::_1 &lt;&lt; &quot; and &quot; &lt;&lt; qi::_2 &lt;&lt; &quot;\n&quot;;
    ]
);
</pre>
<p>Here, <span style="font-family: Courier New;">qi::_1</span> refers to the attribute matched by the first integer parser, and <span style="font-family: Courier New;">qi::_2</span> to the second one.</p>
<p>Initially I was planning to additionally describe the internal interface of a semantic action. Utilizing this interface allows you to write your own function objects and still to get access to the elements of the parser context mentioned above (attributes, the rule&#8217;s local variables and inherited attributes, etc.). But this post already got longer as anticipated, which is why I defer this discussion to a second Tip of the Day. Stay tuned!</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=-4CzohooKNM:yZ2nqOi2wDQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/-4CzohooKNM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/03/03/the-anatomy-of-semantic-actions-in-qi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/03/03/the-anatomy-of-semantic-actions-in-qi/</feedburner:origLink></item>
		<item>
		<title>Parsing Skippers and Skipping Parsers</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/C0IiLz3rHuI/</link>
		<comments>http://boost-spirit.com/home/2010/02/24/parsing-skippers-and-skipping-parsers/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 13:32:08 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Qi Example]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=989</guid>
		<description><![CDATA[Spirit supports skipper based parsing since its very invention. So this is definitely not something new to Spirit V2. Nevertheless, the recent discussion on the Spirit mailing list around the semantics of Qi&#8217;s lexeme[] directive shows the need for some clarification. Today I try to answer questions like: &#8220;What does it mean to use a [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p><em>Spirit</em> supports skipper based parsing since its very invention. So this is definitely not something new to Spirit V2. Nevertheless, the recent discussion on the <a href="http://boost-spirit.com/home/feedback-and-support/">Spirit mailing list</a> around the semantics of <em>Qi&#8217;s</em> <span style="font-family: Courier New;">lexeme[]</span> directive shows the need for some clarification. Today I try to answer questions like: &#8220;What does it mean to use a skipper while parsing?&#8221;, or &#8220;When do I want to use a skipper and when not?&#8221;.</p>
<p><span id="more-989"></span></p>
<p>While parsing some formatted data stream it is very often desirable to ignore some parts of the input. A common example would be the need to skip whitespace and comments while parsing some computer language. Certainly it is possible to explicitly account for the tokens to skip (such as the whitespace or the comments) while writing the grammar. But this can get very tedious as those tokens are valid to appear at any point in the input.</p>
<p>For the sake of simplicity, let us assume we want to parse a simple key/value expression: <span style="font-family: Courier New;">key=value</span>, where we want to allow for any number of space characters before, in between, or after the <span style="font-family: Courier New;">key</span> or the <span style="font-family: Courier New;">value</span>. A naive grammar matching the plain key/value pair without whitespace skipping would look like (see <a href="http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/">Parsing a List of Key-Value Pairs Using Spirit.Qi</a> for more details):</p>
<pre class="brush: cpp;">
pair  =  key &gt;&gt; '=' &gt;&gt; value;
key   =  qi::char_(&quot;a-zA-Z_&quot;) &gt;&gt; *qi::char_(&quot;a-zA-Z_0-9&quot;);
value = +qi::char_(&quot;a-zA-Z_0-9&quot;);
</pre>
<p>If we want to explicitly accommodate the rule <span style="font-family: Courier New;">pair</span> to match any interspersed space characters we get:</p>
<pre class="brush: cpp;">
pair  = *space &gt;&gt; key &gt;&gt; *space &gt;&gt; '=' *space &gt;&gt; value &gt;&gt; *space;
</pre>
<p>which, while it produces the desired result, is not only error prone, but additionally difficult to write, to understand, and to maintain. If we look closer we see, that the process of skipping the whitespace tokens is easily automated. It seems to be sufficient to insert a repeated invocation of the <span style="font-family: Courier New;">space</span> parser (or generally, any skip parser) in between the elements of the user defined parser expression sequences.</p>
<p>In fact, that is exactly what <em>Spirit</em> can do for you! The library invokes any supplied skip parser upon entry to the parse member function of any parser conforming to the <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/qi/reference/parser_concepts/primitiveparser.html"><span style="font-family: Courier New;">PrimitiveParser</span></a> concept. The skip parser has to be supplied by calling a special API function: <span style="font-family: Courier New;">phrase_parse:</span></p>
<pre class="brush: cpp;">
namespace qi = boost::spirit::qi;
typedef std::string::const_iterator iterator;

qi::rule&lt;iterator, qi::space_type&gt; pair = key &gt;&gt; '=' &gt;&gt; value;
qi::rule&lt;iterator&gt; key = qi::char_(&quot;a-zA-Z_&quot;) &gt;&gt; *qi::char_(&quot;a-zA-Z_0-9&quot;);
qi::rule&lt;iterator&gt; value = +qi::char_(&quot;a-zA-Z_0-9&quot;);

std::string input(&quot; key = value &quot;);
iterator_type begin = input.begin();
iterator_type end = input.end();
qi::phrase_parse(begin, end, pair, qi::space);
</pre>
<p>This code snippet illustrates several important things:</p>
<ul>
<li>The function <span style="font-family: Courier New;">qi::phrase_parse</span> is equivalent to the API function <span style="font-family: Courier New;">qi::parse</span> except for its additional parameter, the skip parser. Our example utilizes <span style="font-family: Courier New;">qi::space</span>, but it is possible to use any other, even more complex parser expression as the skipper instead.</li>
<li>All rules which we want to perform the skip parsing need to be declared with the type of the skip parser they are going to be used with. Our example specifies the type of the <span style="font-family: Courier New;">qi::space</span> parser expression, which is <span style="font-family: Courier New;">qi::space_type</span>. For more complex parser expressions you might want to use a (mini) grammar or take advantage of <span style="font-family: Courier New;">BOOST_TYPEOF</span> to let the compiler deduce the actual type.</li>
<li>All rules which should not perform skip parsing have to be declared without an additional skip parser type. These rules behave like an implicit <span style="font-family: Courier New;">lexeme[]</span> directive (for more information about <span style="font-family: Courier New;">lexeme[]</span>, see below), they inhibit the invocation of the skip parser even if they are executed as part of a rule with an associated skipper.</li>
</ul>
<p>In the example above we suppressed skipping while matching either the <span style="font-family: Courier New;">key</span> or the <span style="font-family: Courier New;">value,</span> otherwise our grammar would match any additional <span style="font-family: Courier New;">space</span> character inside the <span style="font-family: Courier New;">key</span> or <span style="font-family: Courier New;">value</span> as well. Remember, the expression <span style="font-family: Courier New;">char_</span> conforms to the <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/qi/reference/parser_concepts/primitiveparser.html">PrimitiveParser</a> concept, it will execute the skip parser for each of its invocations. In this case any skip parser would be executed in between any two of the matched characters.</p>
<p>Sometimes it is necessary to turn of skipping for a smaller part of the grammar only. For this purpose Spirit implements the <span style="font-family: Courier New;">lexeme[]</span> directive. This directive inhibits skipping during the execution of the embedded parser. For instance, parsing a quoted string of alphanumeric characters would look like this:</p>
<pre class="brush: cpp;">
string = lexeme['&quot;' &gt;&gt; *alnum &gt;&gt; '&quot;'];
</pre>
<p>Here the lexeme directive disables skipping while matching the string, which avoids &#8216;loosing&#8217; characters otherwise matched by the skipper. Please note: <span style="font-family: Courier New;">lexeme[]</span> performs a pre-skip step, even if it is not a <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/qi/reference/parser_concepts/primitiveparser.html">PrimitiveParser</a> itself (it is essentially considered to be a logical primitive by design). If this is undesired, you can utilize the <span style="font-family: Courier New;">no_skip[]</span> directive instead:</p>
<pre class="brush: cpp;">
string = '&quot;' &gt;&gt; no_skip[*alnum] &gt;&gt; '&quot;';
</pre>
<p>This parser will match all the characters in between the quotes, even if the string starts with a character sequence matched by the applied skip parser. The <span style="font-family: Courier New;">no_skip[]</span> directive is semantically equivalent to <span style="font-family: Courier New;">lexeme[]</span> except it does not perform a pre-skip before executing the embedded parser. Note: as the <span style="font-family: Courier New;">no_skip[]</span> directive has been added only recently. It will be available starting with the next release (Boost V1.43).</p>
<p>This short article would not be complete without mentioning the <span style="font-family: Courier New;">skip[]</span> directive. This directive is the counterpart to <span style="font-family: Courier New;">lexeme[]</span>. It  enables skipping for the embedded parser. Without any argument it can be used inside a lexeme or no_skip directive only. In this case it just re-enables the outer skipper:</p>
<pre class="brush: cpp;">
string = lexeme['&quot;' &gt;&gt; *(alpha | skip[digit]) &gt;&gt; '&quot;'];
</pre>
<p>This (purely hypothetical) parser would enable skipping inside a string as long as it matches digits. But the skip directive can do more. It may take an additional argument allowing to specify a new skipper, for instance:</p>
<pre class="brush: cpp;">
skip(qi::space)[*alnum]
</pre>
<p>which will skip spaces while executing the embedded <span style="font-family: Courier New;">*alnum</span> parser. This form of the directive can be applied for two purposes. It can be used either for changing the current skip parser or to establish skipping inside a context otherwise not doing skipping at all (even if invoked with the <span style="font-family: Courier New;">qi::parse()</span> API function).</p>
<p>For more detailed information about all the mentioned directives please see the corresponding documentation.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=C0IiLz3rHuI:nGymg7UgoTY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/C0IiLz3rHuI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/02/24/parsing-skippers-and-skipping-parsers/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/02/24/parsing-skippers-and-skipping-parsers/</feedburner:origLink></item>
		<item>
		<title>Parsing Arbitrary Things in Any Sequence</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/VLV1Bi10Al4/</link>
		<comments>http://boost-spirit.com/home/2010/02/17/parsing-arbitrary-things-in-any-sequence/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 15:45:25 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Qi Example]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=977</guid>
		<description><![CDATA[Recently, there have been a couple of questions on the Spirit mailing list asking how to parse as set of things known in advance in any sequence and any combination. A simple example would be a list of key/value pairs with known keys but the keys may be ordered in any sequence. This use case [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>Recently, there have been a couple of questions on the <em><a href="http://boost-spirit.com/home/info/mailing-list/">Spirit mailing list</a></em> asking how to parse as set of things known in advance in any sequence and any combination. A simple example would be a list of key/value pairs with known keys but the keys may be ordered in any sequence. This use case seems to be quite common. Fortunately Spirit provides you with a predefined parser component designed for exactly that purpose: the permutation parser.</p>
<p><span id="more-977"></span></p>
<p><em>Spirit&#8217;s</em> permutation parser <span style="font-family: Courier New;">a ^ b</span> matches either <span style="font-family: Courier New;">a</span>, <span style="font-family: Courier New;">b</span>, <span style="font-family: Courier New;">a &gt;&gt; b</span>, or <span style="font-family: Courier New;">b &gt;&gt; a</span>, where <span style="font-family: Courier New;">a</span> and <span style="font-family: Courier New;">b</span> can be arbitrary parser expressions. Just like normal sequences this operator can be utilized to combine more than two operands. For instance, the expression <span style="font-family: Courier New;">a ^ b ^ c</span> will match <span style="font-family: Courier New;">a</span> or <span style="font-family: Courier New;">b</span> or <span style="font-family: Courier New;">c</span> (or an combination thereof) in any sequence. The attribute propagation rule for the permutation parser is</p>
<pre class="brush: cpp;">
a: A, b: B --&gt; (a ^ b): tuple&lt;optional&lt;A&gt;, optional&lt;B&gt; &gt;
</pre>
<p>As usual, if one or more operand of the expression do not expose any attribute (expose <span style="font-family: Courier New;">unused_type</span> as their attribute, which is equivalent), this operand disappears from attribute handling:</p>
<pre class="brush: cpp;">
a: A, b: Unused --&gt; (a ^ b): optional&lt;A&gt;;
</pre>
<p>The permutation parser works out of the box whenever you do not require to match all of the elements in the input. But what if you want strict permutation (operands get matched exactly once)? You have two possibilities, as often, one simple and less versatile and one more complex but universally applicable solution. The simple solution is to parse the input and to check afterward whether all optionals in the resulting attribute have been filled. I will leave that solution as an exercise for the reader.</p>
<p>If we assume the attribute to be a (<em>Fusion</em>) tuple of optionals, containing one optional for each of the parser components in the permutation parser we can write the following code (thanks to Carl Barron for the initial idea).</p>
<p>This code defines a <em>Phoenix</em> function (a lazy function encapsulating some custom functionality) checking whether one or more of the optionals in a given <em>Fusion</em> sequence are empty. The <em>Fusion</em> algorithm <span style="font-family: Courier New;">find_if</span> iterates over the given sequence of optionals, invoking the <span style="font-family: Courier New;">option_empty::operator()</span> for each of the elements. <span style="font-family: Courier New;">fusion::find_if</span> stops iterating on the first invocation returning <span style="font-family: Courier New;">true</span> and returns the iterator to the element it stopped on. This is very similar to the well known <span style="font-family: Courier New;">std::find_if</span> algorithm.</p>
<pre class="brush: cpp;">
namespace phoenix = boost::phoenix;
namespace fusion = boost::fusion;
namespace qi = boost::spirit::qi;

class no_empties_impl
{
    // helper function object to be invoked by fusion::find_if
    struct optional_empty
    {
        template &lt;typename T&gt;
        bool operator ()(T const&amp; val) const
        {
            return !val;  // return true if 'val' is empty.
        }
    };

public:
    template &lt;typename T&gt;
    struct result { typedef bool type; };

    // This operator will get called from the semantic action attached
    // to the permutation parser. The parameter refers to its overall
    // attribute: the fusion tuple of optionals.
    template &lt;typename T&gt;
    bool operator ()(T const&amp; t) const
    {
        // look for an empty optional, if any return false.
        return fusion::find_if&lt;optional_empty&gt;(t) ==
               fusion::end(t);
    }
};

// define the Phoenix function
phoenix::function&lt;no_empties_impl&gt; const no_empties = no_empties_impl();
</pre>
<p>The overall Phoenix function <span style="font-family: Courier New;">no_empties</span> will return <span style="font-family: Courier New;">false</span> if we found at least one non-initialized optional in the passed sequence. The following code snippet illustrates how everything fits together:</p>
<pre class="brush: cpp;">
std::string input (&quot;BCA&quot;);
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
qi::parse(begin, end,
    (qi::char_('A') ^ 'B' ^ 'C')[qi::_pass = no_empties(qi::_0)]);
</pre>
<p>We assign the result of the invocation of <span style="font-family: Courier New;">no_empties</span> to Qi&#8217;s predefined placeholder <span style="font-family: Courier New;">_pass</span>. If we assign <span style="font-family: Courier New;">false</span>, then the parser the semantic action is attached to will be forced to fail in retrospective (even if it matched the input successfully before). As a result the overall parser expression will succeed as long as a) the permutation parser matches its input and b) the <em>Phoenix</em> function inside the semantic action returns <span style="font-family: Courier New;">true</span>.</p>
<p>For more information about the permutation parser please consult its documentation <a title="Permutation parser documentation" href="http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/spirit/qi/reference/operator/permutation.html">here</a>. Overall, this example is a bit more complex than the average parser you might usually write. It utilizes three libraries: <em>Spirit</em>, <em>Phoenix</em>, and <em>Fusion</em> in a seamless manner. But for sure, once you understand the idea, it will be easier for you to come up with similar solutions. <em>Spirit</em> has been designed with <em>Phoenix</em> and <em>Fusion</em> in mind, and in fact it relies on <em>Fusion</em> heavily itself. As a result, the integration of those libraries is almost perfect.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=VLV1Bi10Al4:j2O1K6ykwfg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/VLV1Bi10Al4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/02/17/parsing-arbitrary-things-in-any-sequence/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/02/17/parsing-arbitrary-things-in-any-sequence/</feedburner:origLink></item>
		<item>
		<title>How to Adapt Templates as a Fusion Sequence</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/_9J9-0Wl_jw/</link>
		<comments>http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:59:13 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Qi Example]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=971</guid>
		<description><![CDATA[Here is another question raised from time to time: &#8220;I know how to use a plain struct as an attribute for a sequence parser in Qi by adapting it with BOOST_FUSION_ADAPT_STRUCT. Unfortunately this does not work if the struct is a template. What can I do in this case?&#8221;.
There have been plans for a while to [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>Here is another question raised from time to time: &#8220;I know how to use a plain <span style="font-family: Courier New;">struct</span> as an attribute for a sequence parser in <em>Qi</em> by adapting it with <span style="font-family: Courier New;">BOOST_FUSION_ADAPT_STRUCT</span>. Unfortunately this does not work if the <span style="font-family: Courier New;">struct</span> is a template. What can I do in this case?&#8221;.</p>
<p>There have been plans for a while to create a separate Fusion facility <span style="font-family: Courier New;">BOOST_FUSION_ADAPT_TPL_STRUCT</span> allowing to adapt templated data types, but this is not in place yet. Today I will describe a trick you can apply to adapt your templates into &#8216;proper&#8217; Fusion sequences anyway.</p>
<p><span id="more-971"></span></p>
<p>We will use the fact that a <em>Qi</em> grammar is already a template in most cases, and even if it is not a template yet, it can be easily converted into one. Further we will use the built-in capability of rule&#8217;s to invoke a custom attribute transformation if the attribute type of the right hand side does not exactly match the left hand side&#8217;s attribute type.</p>
<p>Let us assume this to be our data structure we want to fill while parsing:</p>
<pre class="brush: cpp;">
template &lt;typename A, typename B&gt;
struct data
{
    A a;
    B b;
};
</pre>
<p>We would like to be able to directly utilize this template type as an attribute for our grammar. A possible way of adapting the template type to make it usable as a Fusion sequence is to define a <span style="font-family: Courier New;">fusion::vector&lt;A&amp;, B&amp;&gt;</span> and initialize it with the references to the data members of our template type. If we the pass this Fusion vector as the attribute to the actual parser expression we effectively supply our original data members as the attributes to the parsing process.</p>
<pre class="brush: cpp;">
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;

template &lt;typename Iterator, typename A, typename B&gt;
struct data_grammar : qi::grammar&lt;Iterator, data&lt;A, B&gt;()&gt;
{
    data_grammar() : data_grammar::base_type(start)
    {
        // the implicit attribute transformation 'adapts' data&lt;&gt; to
        // the Fusion vector
        start = real_start;

        // do the actual parsing of the data&lt;&gt; members
        real_start = qi::auto_ &gt;&gt; ',' &gt;&gt; qi::auto_;
    }

    qi::rule&lt;Iterator, data&lt;A, B&gt;()&gt; start;
    qi::rule&lt;Iterator, fusion::vector&lt;A&amp;, B&amp;&gt;()&gt; real_start;
};
</pre>
<p>The signature of the grammar&#8217;s start rule has to match the signature of the grammar itself. To accommodate for this we introduce a second rule &#8216;real_start&#8217; dedicated to the parsing of our data members. At the same time this allows us to inject the needed transformation of our <span style="font-family: Courier New;">data&lt;&gt;</span> attribute to the Fusion vector. As the left hand side&#8217;s and right hand side&#8217;s attribute types do not match, the parser expression <span style="font-family: Courier New;">start = real_start</span> will invoke <em>Spirit&#8217;s</em> customization point <span style="font-family: Courier New;">transform_attribute</span>. But since the default implementation of this customization point does not handle our special data types the way we want, we are required to implement our own specialization:</p>
<pre class="brush: cpp;">
namespace boost { namespace spirit { namespace traits
{
    template &lt;typename A, typename B&gt;
    struct transform_attribute&lt;data&lt;A, B&gt;, fusion::vector&lt;A&amp;, B&amp;&gt; &gt;
    {
        typedef fusion::vector&lt;A&amp;, B&gt; type;
        static type pre(data&lt;A, B&gt;&amp; val) { return type(val.a, val.b); }
        static void post(data&lt;A, B&gt;&amp;, fusion::vector&lt;A&amp;, B&amp;&gt; const&amp;) {}
        static void fail(data&lt;A, B&gt;&amp;) {}
    };
}}}
</pre>
<p>The function <span style="font-family: Courier New;">pre()</span> is called before the right hand side parser expression is invoked. It gets passed the left hand side&#8217;s attribute (the <span style="font-family: Courier New;">data&lt;&gt;</span> instance) and is required to return the attribute to be passed to the rule&#8217;s right hand side expression. The returned Fusion vector is initialized with the references to the data members of our original <span style="font-family: Courier New;">data&lt;&gt;</span> instance. The functions <span style="font-family: Courier New;">post()</span> and <span style="font-family: Courier New;">fail()</span> can be left empty in our case. For more information about this customization point please see the corresponding documentation <a href="http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/spirit/advanced/customize/transform.html">here</a>.</p>
<p>I added a new example to <em>Spirit</em> demonstrating this technique. Currently, it can be accessed from the Boost SVN only (see <a href="http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/adapt_template_struct.cpp">adapt_template_struct.cpp</a>), but in the future it will be released as part of <em>Spirit</em>.</p>
<p>Just in case you were wondering: yes, this trick works equally well for <em>Karma</em> generators. The only difference is that the members of the created Fusion vector will have to be constant references instead.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (1 vote cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=_9J9-0Wl_jw:Ej8i-xhHtgg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/_9J9-0Wl_jw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence/</feedburner:origLink></item>
		<item>
		<title>BoostCon 2010</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/cb4kzmwzaWo/</link>
		<comments>http://boost-spirit.com/home/2010/02/05/boostcon-2010/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 05:21:29 +0000</pubDate>
		<dc:creator>Joel de Guzman</dc:creator>
				<category><![CDATA[BoostCon]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=960</guid>
		<description><![CDATA[Promising to be the main face-to-face event for all things Boost, BoostCon 2010 opens the door to your C++ future. From using the Boost libraries to writing and maintaining them, from evangelizing to deploying Boost within your organization, from infrastructure and process to vision and mission, and from TR1 to TR2, BoostCon brings together the [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>Promising to be <strong>the</strong> main face-to-face event for all things <a href="http://boost.org/">Boost</a>, <em>BoostCon 2010</em> opens the door to your C++ future. From using the Boost libraries to writing and maintaining them, from evangelizing to deploying Boost within your organization, from infrastructure and process to vision and mission, and from TR1 to TR2, BoostCon brings together the sessions, the colleagues, and the inspiration to support your work with Boost for the next year.</p>
<p>To reflect the breadth of the Boost community, the conference includes sessions aimed at two constituencies: Boost end-users and hard-core Boost library and tool developers. The program fosters interaction and engagement within <em>and across</em> those two groups, with an emphasis on hands-on, participatory sessions.</p>
<p><span id="more-960"></span></p>
<p>We&#8217;d love to see you all there! BoostCon, from 2007, has always been a meeting place of Spirit folks. It has been the place where we meet eye to eye and discuss Spirit related events, future plans and road maps, using the library, etc. This year, 3 Spirit related talks were accepted:</p>
<h4>Using Spirit 2.1 : Qi and Karma</h4>
<p>By Michael Caisse</p>
<p>Machinery, sensors, equipment, client/server communications, even file formats&#8230; Parsing and producing communication streams is everywhere you look. Often these tasks are simple or small enough to tempt ad-hoc solutions. The Spirit 2.1 library provides a model that is simple enough to tackle those &#8220;quick hacks&#8221; and easily scales for full-featured AST generation.</p>
<p>This session will explore real-life experiences with the parser and generator (Qi/Karma) portions of the Spirit library. As we look at various small and medium-sized parsers/generators employed in various products we will establish some &#8220;rules-of-thumb&#8221; and guidelines for tackling the parser/generator domain with Qi/Karma. The session will end with the implementation of a usable XML parser and a simplified XPath-like node extractor.</p>
<p>The session will include some lecture and a lot of tutorial. Attendees will walk away with the knowledge and tools to begin parsing and generating with Spirit Qi/Karma.</p>
<h4>RAD Spirit</h4>
<p>By Joel de Guzman and Hartmut Kaiser</p>
<p>One allure of Boost Spirit Parser, compared to traditional parser generators, is its being embedded in C++. The user of the library specifies a parser&#8217;s grammar directly in C++ code using expression templates. There are distinct advantages with this approach, but there are problems as well. The most glaring drawbacks are 1) long compile times and 2) difficult to understand error messages and 3) Difficulty in debugging and testing parsers. For small parsing tasks, these can be tolerated. Yet, after 8 years in active deployment, we&#8217;ve come to a point where Spirit is being used in increasingly more complex parsing tasks. 2 and 3 can somehow be alleviated by adding more &#8220;smarts&#8221; to the expression template engine (using Proto), but that would increase compile times further to the point where the libary will no longer be useful.</p>
<p>It would be interesting to have a tool (both stand-alone or library based) that accepts textual EBNF/PEG expressions and outputs either parsers that are immediately executable or C++ Boost Spirit code. The RAD tool can have all sorts of smarts to make writing parsers as painless as possible (e.g. debugging, error handling, analysis, etc). After the development of the parser is &#8220;set in stone&#8221;, one can opt to have the tool emit C++ code that can then be included in an application. For more complex parsers, this will be a definite boon.</p>
<p>We would like to present the design and development of such a tool. Obviously, this &#8220;Dynamic Spirit&#8221; tool will be written using &#8220;static&#8221; Spirit. This would be a practical real world example using Spirit.</p>
<h4>Spirit History and Evolution</h4>
<p>By Joel de Guzman and Hartmut Kaiser</p>
<p>This year, we celebrate Spirit&#8217;s 10th anniversary from its early beginnings as an offshoot of a much larger GUI library in the 90s and debuted into Boost in May 2001 in the typical &#8220;Is there interest in this library?&#8221; fashion like all would be Boost libraries. From a humble 7 header file library, Spirit has grown to be one of the most sophisticated Boost libraries and along the way became the incubator of other Boost libraries such as Boost.Fusion, Boost.Phoenix, Boost.Wave and Boost.Proto.</p>
<p>We would like to present Spirit (and the libraries it inspired) in a historical perspective. The presentation will aim to provide a lighter, more intimate perspective into the development of at least 4 libraries with almost a decade&#8217;s worth of experience being Boost authors and bonafide crazy template metaprogrammers who abuse operators like Mad Scientists. Of course, we can&#8217;t help it if we show off some C++ tricks here and there, but we&#8217;ll try to keep it as light as we can.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=cb4kzmwzaWo:g5FFuizIQLw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/cb4kzmwzaWo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/02/05/boostcon-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/02/05/boostcon-2010/</feedburner:origLink></item>
		<item>
		<title>What is the Attribute Type Exposed by a Parser?</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/a7JEacFypFs/</link>
		<comments>http://boost-spirit.com/home/2010/01/31/what-is-the-attribute-type-exposed-by-a-parser/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 23:16:59 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Qi Example]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=949</guid>
		<description><![CDATA[Most questions on the main Spirit mailing list are centered around attributes and how to correctly utilize Spirit&#8217;s attribute propagation rules. We discussed the related basics in several posts already, but many people still have problems to understand the rules. Somebody recently asked on the mailing list whether it would be possible to print the attribute [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>Most questions on the <a href="http://boost-spirit.com/home/info/mailing-list/">main Spirit mailing list</a> are centered around attributes and how to correctly utilize <em>Spirit&#8217;s</em> attribute propagation rules. We discussed the related basics in several posts already, but many people still have problems to understand the rules. Somebody recently asked on the mailing list whether it would be possible to print the attribute type exposed by an arbitrary parser expression. I answered by posting a sketchy code snippet (see <a href="http://article.gmane.org/gmane.comp.parsers.spirit.general/18387">here</a>), but afterwards I realized it might be a good topic for yet another &#8216;Tip of the Day&#8217;.</p>
<p><span id="more-949"></span></p>
<p>Before you read on, please be aware that the interfaces described in this post are not set in stone and may change in the future without attempting to be backwards compatible. I&#8217;ll write about the current implementation anyway as I think those are important. Generally, understanding attribute handling is crucial to understanding <em>Spirit</em>. Nuff said.</p>
<p>Naturally, <em>Spirit</em> internally implements the means of retrieving the attribute type of any parser expression. So all we need to do is to invoke those and wrap them into a nicely reusable tool.</p>
<p>Let us start by briefly describing some implementation details related to parser construction and attribute composition. As you might already know, any parser expression provided by the user (such as for instance <span style="font-family: Courier New;">qi::int_ &gt;&gt; qi::double_</span>) is not something you could directly use to parse input. The <span style="font-family: Courier New;">qi::int_</span>, <span style="font-family: Courier New;">qi::double_</span>, etc. are just placeholder symbols denoting the type of parser component required to match the input. These parser expressions have to be compiled into real parser instances responsible for the actual matching of the input. Spirit has a built in facility <span style="font-family: Courier New;">boost::spirit::compile</span> which performs the conversion of the parser expression into the corresponding parser instance. Here is the (simplified) prototype:</p>
<pre class="brush: cpp;">
namespace boost { namespace spirit
{
    template &lt;typename Domain, typename Expr&gt;
    typename result_of::compile&lt;Domain, Expr&gt;::type
    compile(Expr const&amp; expr);
}}
</pre>
<p>where <span style="font-family: Courier New;">Domain</span> is a tag type which in our case is <span style="font-family: Courier New;">qi::domain</span>, thus identifying <em>Qi</em> (i.e. telling the function we want to create a parser), and <span style="font-family: Courier New;">Expr</span> is the parser expression to compile. As most (meta-) template facilities in Spirit the <span style="font-family: Courier New;">compile</span> construct consists out of two parts: the meta-calculation of the resulting type of the parser instance (<span style="font-family: Courier New;">result_of::compile</span>) and the actual function doing the runtime compilation. For the purpose of this post we are interested in the resulting type of the parser instance only.</p>
<p>Among other things, after compiling the parser expression, the member function <span style="font-family: Courier New;">parse()</span> is called on the returned parser instance. In the article about <a href="http://boost-spirit.com/home/articles/qi-example/creating-your-own-parser-component-for-spirit-qi/">Creating Your Own Parser Component for Spirit.Qi</a> we saw that the parser instance additionally exposes its attribute type. <em>Spirit</em> implements another facility we can invoke to extract this attribute type for a given parser instance type:</p>
<pre class="brush: cpp;">
namespace boost { namespace spirit { namespace traits
{
    template &lt;typename Component, typename Context = unused_type
      , typename Iterator = unused_type&gt;
    struct attribute_of;
}}}
</pre>
<p>where <span style="font-family: Courier New;">Component</span> is the type of the parser expression to query for its attribute type, the <span style="font-family: Courier New;">Context</span> is not relevant in our case and <span style="font-family: Courier New;">Iterator</span> is the iterator type used to do the actual matching. Most parser components are independent of any iterator type, but components as the <span style="font-family: Courier New;">raw[]</span> directives are. So most of the time we can leave out supplying an iterator type.</p>
<p>These two Spirit facilities are all what we need! Let us combine the two in order to get the type of the attribute exposed by a parser created from a given parser expression.</p>
<pre class="brush: cpp;">
template &lt;typename Expr, typename Iterator = spirit::unused_type&gt;
struct attribute_of_parser
{
    typedef typename spirit::result_of::compile&lt;
        spirit::qi::domain, Expr
    &gt;::type parser_expression_type;

    typedef typename spirit::traits::attribute_of&lt;
        parser_expression_type, spirit::unused_type, Iterator
    &gt;::type type;
};
</pre>
<p>Now as we have a tool to get the attribute type, we can use a simple utility function to print it out to the console (in this example we ignore the iterator type):</p>
<pre class="brush: cpp;">
template &lt;typename T&gt;
void display_attribute_of_parser(T const&amp;)
{
    typedef typename attribute_of_parser&lt;T&gt;::type attribute_type;
    std::cout &lt;&lt; typeid(attribute_type).name() &lt;&lt; std::endl;
}

// this will print something like: boost::fusion::vector2&lt;int, double&gt;
display_attribute_of_parser(qi::int_ &gt;&gt; qi::double_);
</pre>
<p>That&#8217;s it! We created a nice, reusable function printing the attribute type of an arbitrary parser expression. I added this as an example to the Boost SVN <a href="http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/display_attribute_type.hpp">here</a> and <a href="http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/display_attribute_type.cpp">here</a>, allowing you to reuse it for your needs. The last thing to mention is that a similar utility can be easily written for <em>Karma</em> generators. The only thing to change is the passed <span style="font-family: Courier New;">Domain</span> which needs to be <span style="font-family: Courier New;">spirit::karma::domain</span>.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=a7JEacFypFs:249v2bcRgGc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/a7JEacFypFs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/01/31/what-is-the-attribute-type-exposed-by-a-parser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/01/31/what-is-the-attribute-type-exposed-by-a-parser/</feedburner:origLink></item>
		<item>
		<title>Can the Classic Dynamic Parsers be Ported to Qi?</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/g-JOQlHjARo/</link>
		<comments>http://boost-spirit.com/home/2010/01/28/can-the-classic-dynamic-parsers-be-ported-to-qi/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 11:27:29 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Qi]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=929</guid>
		<description><![CDATA[After writing about local variables for rules here I would like to get back to some information I have had lying around for some time already. Today&#8217;s topic seems to be a nice fit as porting the dynamic parsers from Spirit.Classic requires to utilize local variables. This allows me to give you some more examples [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (2 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>After writing about local variables for rules <a href="http://boost-spirit.com/home/2010/01/21/what-are-rule-bound-semantic-actions/">here</a> I would like to get back to some information I have had lying around for some time already. Today&#8217;s topic seems to be a nice fit as porting the dynamic parsers from <em>Spirit.Classic</em> requires to utilize local variables. This allows me to give you some more examples for this facility.</p>
<p>Previous versions of <em>Spirit</em>, which are the versions we refer to as <em>Spirit.Classic</em> today, implemented special dynamic parsers allowing to insert control statements into the parsing process (such as <span style="font-family: Courier New;">if_p</span>, <span style="font-family: Courier New;">while_p</span>, and <span style="font-family: Courier New;">for_p</span>). These dynamic parsers are not available anymore in <em>Qi</em>. But it is easy enough to achieve the same behavior using existing <em>Qi</em> components. This &#8216;Tip of the Day&#8217; describes those techniques, which have been developed and contributed by Carl Barron. Thanks Carl!</p>
<p><span id="more-929"></span></p>
<p>Generally, <em>Spirit.Classic</em> allowed to employ the dynamic parser in two ways. The condition to check could be either a function or function object, or a parser expression. Functions or function objects are expected to return values convertible to <span style="font-family: Courier New;">bool</span>. When the evaluation of the function or function object yields true it will be considered as meeting the condition. When the parser matches the condition is met as well.</p>
<h5>General Notation</h5>
<p>In the tables below we use the following notation:</p>
<ul>
<li><span style="font-family: Courier New;">c</span>: A nullary function or function object evaluating to <span style="font-family: Courier New;">bool</span></li>
<li><span style="font-family: Courier New;">p, p1, p2, …</span>: Arbitrary parser expressions</li>
<li><span style="font-family: Courier New;">init, step</span>: Arbitrary <a href="http://www.boost.org/doc/libs/1_41_0/libs/spirit/phoenix/doc/html/index.html">Phoenix</a> nullary functions or function objects</li>
<li><span style="font-family: Courier New;">_a</span>: a local variable of the rule the expression is assigned to (the type of the local variable should be <span style="font-family: Courier New;">bool</span>)</li>
</ul>
<p>All symbols are assumed to be referenced from their respective namespaces (i.e. <span style="font-family: Courier New;">boost::spirit::classic</span> or <span style="font-family: Courier New;">boost::spirit::qi</span>).</p>
<h5>Conditional Parsing (<span style="font-family: Courier New;">if_p</span>)</h5>
<table border="1" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td width="220" valign="top">Spirit.Classic Expression</td>
<td width="380" valign="top">Equivalent Spirit.Qi Expression</td>
</tr>
<tr>
<td width="220" valign="top"><span style="font-family: Courier New;">if_p(p)[p1]</span></td>
<td width="380" valign="top"><span style="font-family: Courier New;">p &gt;&gt; p1 | eps</span></td>
</tr>
<tr>
<td width="220" valign="top"><span style="font-family: Courier New;">if_p(p)[p1].else_p[p2]</span></td>
<td width="380" valign="top"><span style="font-family: Courier New;">p &gt;&gt; p1 | p2 </span></td>
</tr>
<tr>
<td width="220" valign="top"> </td>
<td width="380" valign="top"> </td>
</tr>
</tbody>
</table>
<p>The <em>Qi</em> constructs as shown exactly reproduce the behavior if the <span style="font-family: Courier New;">if_p</span> construct: if the condition <span style="font-family: Courier New;">&#8216;p&#8217;</span> is a parser it will consume input in case it matches. If this is not required you may utilize predicates instead (see the post about <a href="http://boost-spirit.com/home/2010/01/17/whats-the-difference-between-qis-and/">What’s the Difference Between Qi’s ‘!’ and ‘~’?</a> for more details).</p>
<h5>Parsing With Loops (<span style="font-family: Courier New;">while_p</span>, <span style="font-family: Courier New;">do_p</span>, and <span style="font-family: Courier New;">for_p</span>)</h5>
<p>All looping constructs require a local Boolean variable to be defined in the rule the expression is assigned to. It does not necessarily have to be the local variable <span style="font-family: Courier New;">_a</span>, which in the expressions below is used as a placeholder for any local variable (<span style="font-family: Courier New;">_a</span>, <span style="font-family: Courier New;">_b</span>, …<span style="font-family: Courier New;">_j</span>). At the same time this restricts the function objects <span style="font-family: Courier New;">&#8216;init&#8217;</span> and <span style="font-family: Courier New;">&#8217;step&#8217;</span> to be <a href="http://www.boost.org/doc/libs/1_41_0/libs/spirit/phoenix/doc/html/index.html">Phoenix</a> function objects (remember, Phoenix expressions do not mix well with non-Phoenix function objects).</p>
<table border="1" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td width="220" valign="top">Spirit.Classic Expression</td>
<td width="380" valign="top">Equivalent Spirit.Qi Expression</td>
</tr>
<tr>
<td width="220" valign="top"><span style="font-family: Courier New;">while_p(p)[p1]</span></td>
<td width="380" valign="top"><span style="font-family: Courier New;">    eps[_a = true]<br />
&gt;&gt; *(p &gt;&gt; (p1 | eps[_a = false]))<br />
&gt;&gt;  eps(_a) </span></td>
</tr>
<tr>
<td width="220" valign="top"> </td>
<td width="380" valign="top"> </td>
</tr>
<tr>
<td width="220" valign="top"><span style="font-family: Courier New;">do_p[p1].while_p(p)</span></td>
<td width="380" valign="top"><span style="font-family: Courier New;">    eps[_a = true]<br />
&gt;&gt; *((p1 | eps[_a = false]) &gt;&gt; p)<br />
&gt;&gt;  eps(_a)</span></td>
</tr>
<tr>
<td width="220" valign="top"> </td>
<td width="380" valign="top"> </td>
</tr>
<tr>
<td width="220" valign="top"><span style="font-family: Courier New;">for_p(init, p, step)[p1]</span></td>
<td width="380" valign="top"><span style="font-family: Courier New;">    eps[init, _a = true]<br />
&gt;&gt; *(p &gt;&gt; (p1 | eps[_a = false]) &gt;&gt; eps[step])<br />
<span style="font-family: Courier New;">&gt;&gt;  eps(_a)</span> </span></td>
</tr>
<tr>
<td width="220" valign="top"> </td>
<td width="380" valign="top"> </td>
</tr>
</tbody>
</table>
<p>All Qi constructs in the table above employ the same trick as shown in the last installment about local variables (<a href="http://boost-spirit.com/home/2010/01/21/what-are-rule-bound-semantic-actions/">What are Rule Bound Semantic Actions?</a>). By using the <span style="font-family: Courier New;">eps</span> component we inject arbitrary semantic actions into the parser execution. But please don&#8217;t confuse this with the <span style="font-family: Courier New;">eps(_a)</span> component, which will propagate the current value of <span style="font-family: Courier New;">_a</span> to its own return code. It will succeed parsing unless the current value of <span style="font-family: Courier New;">_a</span> is <span style="font-family: Courier New;">false</span>. In our case this forces the whole loop construct to fail parsing if the body parser <span style="font-family: Courier New;">&#8216;p1&#8242;</span> failed.</p>
<p>If you replace the <span style="font-family: Courier New;">&#8216;p&#8217;</span> with <span style="font-family: Courier New;">&#8216;c&#8217;</span> in any of the <em>Classic</em> expressions above (i.e. you want to use a function object as the condition instead of a parser), the <span style="font-family: Courier New;">&#8216;p&#8217;</span> in the equivalent <em>Qi</em> expression needs to be replaced by <span style="font-family: Courier New;">&#8216;eps(c)&#8217;</span>.</p>
<p>Needless to say, all of the above expressions require you to add the proper <span style="font-family: Courier New;">#include</span> statements to your code. Please consult the related documentation to see what&#8217;s necessary.</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (2 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=g-JOQlHjARo:i_XDR7PF0i4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/g-JOQlHjARo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/01/28/can-the-classic-dynamic-parsers-be-ported-to-qi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/01/28/can-the-classic-dynamic-parsers-be-ported-to-qi/</feedburner:origLink></item>
		<item>
		<title>What’s the Difference Between Karma’s ‘!’ and ‘~’?</title>
		<link>http://feedproxy.google.com/~r/Boost-Spirit/~3/XwpPoUje2UE/</link>
		<comments>http://boost-spirit.com/home/2010/01/26/whats-the-difference-between-karmas-and/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 11:41:57 +0000</pubDate>
		<dc:creator>Hartmut Kaiser</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Tip of the Day]]></category>
		<category><![CDATA[Karma]]></category>

		<guid isPermaLink="false">http://boost-spirit.com/home/?p=943</guid>
		<description><![CDATA[A couple of days ago I promised to get back to this topic (if you want to refresh your memory, here is the discussion of those operators in Qi). Today we will discuss Karma&#8217;s unary operators &#8216;!&#8217; and &#8216;~&#8217;. These have very similar semantics as their counterparts in Qi, but as usual, we have to [...]<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br />]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I promised to get back to this topic (if you want to refresh your memory, <a href="http://boost-spirit.com/home/2010/01/17/whats-the-difference-between-qis-and/">here</a> is the discussion of those operators in <em>Qi</em>). Today we will discuss <em>Karma&#8217;s</em> unary operators <span style="font-family: Courier New;">&#8216;!&#8217;</span> and <span style="font-family: Courier New;">&#8216;~&#8217;</span>. These have very similar semantics as their counterparts in <em>Qi</em>, but as usual, we have to turn things inside out in order to make them fit to output generation.</p>
<p><span id="more-943"></span></p>
<p>The one commonality of the two operators is the same as in <em>Qi</em>: both negate whether the component they are being used with succeeds generating. If the component <span style="font-family: Courier New;">&#8216;c&#8217;</span> succeeds, both compound constructs, <span style="font-family: Courier New;">&#8216;!c&#8217;</span> and <span style="font-family: Courier New;">&#8216;~c&#8217;</span> will fail, and similarly, if <span style="font-family: Courier New;">&#8216;c&#8217;</span> fails, the execution of the components <span style="font-family: Courier New;">&#8216;!c&#8217;</span> and <span style="font-family: Courier New;">&#8216;~c&#8217;</span> will succeed.</p>
<p>Similar to its counterpart in <em>Qi</em>, the unary <em>Karma</em> operator <span style="font-family: Courier New;">&#8216;~&#8217;</span> is applicable to character and character class generators only. It negates the set of characters a generator will be allowed to emit. Let me explain. The <em>Karma</em> character set generators, such as <span style="font-family: Courier New;">char_(&#8220;a-z&#8221;)</span> or <span style="font-family: Courier New;">digit</span> will emit their attribute only if the attribute value belongs to the character set described. Consequently, applying the operator &#8216;~&#8217; will negate the character set the generator it is attached to. Here are some examples:</p>
<table border="1" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td width="109" valign="top"><strong>Expression</strong></td>
<td width="491" valign="top"><strong>Description</strong></td>
</tr>
<tr>
<td width="109" valign="top"><span style="font-family: Courier New;">~char<span style="font-family: Courier New;">_(&#8220;a-z&#8221;)</span> </span></td>
<td width="491" valign="top">will emit character values outside the character range spanned by &#8216;a&#8217; and &#8216;z&#8217;</td>
</tr>
<tr>
<td width="109" valign="top"><span style="font-family: Courier New;">~digit</span></td>
<td width="491" valign="top">will emit non-digits only</td>
</tr>
<tr>
<td width="109" valign="top"><span style="font-family: Courier New;">~char_(&#8216;a&#8217;)</span></td>
<td width="491" valign="top">will emit everything but an <span style="font-family: Courier New;">&#8216;a&#8217;</span></td>
</tr>
</tbody>
</table>
<p> </p>
<p>If a generator can&#8217;t emit its attribute it will fail. This is very similar to a failing parser component. The possibility for a generator component to fail is very useful. This can be utilized for alternatives, predicates and other constructs. But this is beyond today&#8217;s topic and will be discussed in a different installment of the &#8216;Tip of the Day&#8217;.</p>
<p>The generators created by the operator <span style="font-family: Courier New;">&#8216;~&#8217;</span> do not wrap the underlying generator. The operator rather modifies the behavior of the component it is attached to. This means there is no performance difference if compared to the plain character generators.</p>
<p>The unary operator <span style="font-family: Courier New;">&#8216;!&#8217;</span> creates a not-predicate generator. Again, similar to its counterpart in <em>Qi</em>, it can be attached to any (arbitrarily complex) generator. The not-predicate generator will succeed if the associated generator fails, and it will fail if its generator succeeds. It invokes the generator it is attached to but the emitted output will not show up in the overall output stream. So effectively, the not-predicate generator will never emit any output. The following example will succeed emitting a floating point number if the first attribute is false (which will make the <span style="font-family: Courier New;">true_</span> generator fail), otherwise it will not emit anything and will fail all together:</p>
<pre class="brush: cpp;">
namespace karma = boost::spirit::karma;
std::string output;
std::back_insert_iterator&lt;std::string&gt; sink(output);
karma::generate(sink, !true_ &lt;&lt; double_, false, 1.0); // will emit: 1.0
</pre>
<p>This example highlights another difference if compared to <em>Qi&#8217;s</em> not-predicate. The <em>Karma</em> not-predicate will always consume an attribute. More accurately, it will expose the attribute of the generator it is associated with (while in <em>Qi</em> the not-predicate never exposes any attribute). As mentioned earlier, we utilize the <span style="font-family: Courier New;">true_</span> generator&#8217;s ability to fail to control what output is generated (or if any output is generated at all as in our case).</p>
<br /><div><img src="http://boost-spirit.com/home/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Boost-Spirit?a=XwpPoUje2UE:0RjyJrDwNws:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Boost-Spirit?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Boost-Spirit/~4/XwpPoUje2UE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://boost-spirit.com/home/2010/01/26/whats-the-difference-between-karmas-and/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://boost-spirit.com/home/2010/01/26/whats-the-difference-between-karmas-and/</feedburner:origLink></item>
	</channel>
</rss>
