blob: a9d30dbcfb906d65ed8a82bedd9cd46560c71b43 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Complex - A first more complex generator</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.0">
<link rel="home" href="../../../index.html" title="Spirit 2.4.1">
<link rel="up" href="../tutorials.html" title="Tutorials">
<link rel="prev" href="semantic_actions.html" title="Semantic Actions">
<link rel="next" href="karma_easier_complex.html" title="Complex - Made easier">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="semantic_actions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="karma_easier_complex.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="spirit.karma.tutorials.karma_complex"></a><a class="link" href="karma_complex.html" title="Complex - A first more complex generator">Complex - A first
more complex generator</a>
</h4></div></div></div>
<p>
In this section we will develop a generator for complex numbers, allowing
to represent a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span></code> either as <code class="computeroutput"><span class="special">(</span><span class="identifier">real</span><span class="special">,</span> <span class="identifier">imag</span><span class="special">)</span></code>
(where <code class="computeroutput"><span class="identifier">real</span></code> and <code class="computeroutput"><span class="identifier">imag</span></code> are the real and imaginary parts
of the complex number) or as a simple <code class="computeroutput"><span class="identifier">real</span></code>
if the imaginary part happens to be equal to zero. This example will highlight
the power of <span class="emphasis"><em>Spirit.Karma</em></span> allowing to combine compile
time definition of formatting rules with runtime based decisions which
of the rules to apply. Also this time, we're using <a href="../../../../../phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
to do the semantic actions.
</p>
<p>
Our goal is to allow for two different output formats to be applied depending
on whether the imaginary part of the complex number is zero or not. Let's
write both as a set of alternatives:
</p>
<pre class="programlisting"> <span class="char">'('</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="string">", "</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="char">')'</span>
<span class="special">|</span> <span class="identifier">double_</span>
</pre>
<p>
where the first alternative should be used for numbers having a non-zero
imaginary part, while the second is for real numbers. Generally, alternatives
are tried in the sequence of their definition as long until one of the
expressions (as delimited by <code class="computeroutput"><span class="char">'|'</span></code>)
succeeds. If no generator expression succeeds the whole alternative fails.
</p>
<p>
If we left this formatting grammar as is our generator would always choose
the first alternative. We need to add some additional rules allowing to
make the first alternative fail. So, if the first alternative fails the
second one will be chosen instead. The decision about whether to choose
the first alternative has to be made at runtime as only then we actually
know the value of the imaginary part of the complex number. <span class="emphasis"><em>Spirit.Karma</em></span>
provides us with with a primitive generator <code class="computeroutput"><span class="identifier">eps</span><span class="special">()</span></code>, which is usable as a semantic predicate.
It has the property to 'succeed' generating only if its argument is true
(while it never generates any output on its own).
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">imag</span> <span class="special">=</span> <span class="special">...;</span> <span class="comment">// imaginary part
</span>
<span class="identifier">eps</span><span class="special">(</span><span class="identifier">imag</span> <span class="special">!=</span> <span class="number">0</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'('</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="string">", "</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="char">')'</span>
<span class="special">|</span> <span class="identifier">double_</span>
</pre>
<p>
If one of the generator elements of a sequence fails the whole sequence
will fail. This is exactly what we need, forcing the second alternative
to be chosen for complex numbers with imaginary parts equal to zero.
</p>
<p>
Now on to the full example, this time with the proper semantic actions
(the complete cpp file for this example can be found here: <a href="../../../../../example/karma/complex_number.cpp" target="_top">complex_number.cpp</a>).
</p>
<p>
We will use the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span></code> type for this and all subsequent
related examples. And here you can see the full code of the generator allowing
to output a complex number either as a pair of numbers (if the imaginary
part is non-zero) or as a single number (if the complex is a real number):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">OutputIterator</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="identifier">generate_complex</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">sink</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">c</span><span class="special">)</span>
<span class="special">{</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">eps</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">_1</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">generate</span><span class="special">;</span>
<span class="keyword">return</span> <span class="identifier">generate</span><span class="special">(</span><span class="identifier">sink</span><span class="special">,</span>
<span class="comment">// Begin grammar
</span> <span class="special">(</span>
<span class="identifier">eps</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">imag</span><span class="special">()</span> <span class="special">!=</span> <span class="number">0</span><span class="special">)</span> <span class="special">&lt;&lt;</span>
<span class="char">'('</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">real</span><span class="special">()]</span> <span class="special">&lt;&lt;</span> <span class="string">", "</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">imag</span><span class="special">()]</span> <span class="special">&lt;&lt;</span> <span class="char">')'</span>
<span class="special">|</span> <span class="identifier">double_</span><span class="special">[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">real</span><span class="special">()]</span>
<span class="special">)</span>
<span class="comment">// End grammar
</span> <span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
The <code class="computeroutput"><span class="identifier">double_</span></code> generators
have this semantic action attached:
</p>
<pre class="programlisting"><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">n</span>
</pre>
<p>
which passes <code class="computeroutput"><span class="identifier">n</span></code> to the first
element of the s generator's attached semantic action. Remember, semantic
actions in <span class="emphasis"><em>Spirit.Karma</em></span> are called before the corresponding
generator is invoked and they are expected to provide the generator with
the data to be used. The semantic action above assigns the value to be
generated (<code class="computeroutput"><span class="identifier">n</span></code>) to the generator
(actually, the attribute of <code class="computeroutput"><span class="identifier">double_</span></code>).
<code class="computeroutput"><span class="identifier">_1</span></code> is a Phoenix placeholder
referring to the attribute of the semantic action's attached generator.
If you need more information about semantic actions, you may want to read
about them in this section: <a class="link" href="semantic_actions.html" title="Semantic Actions">Semantic
Actions</a>.
</p>
<p>
These semantic actions are easy to understand but have the unexpected side
effect of being slightly less efficient than it could be. In addition they
tend to make the formatting grammar less readable. We will see in one of
the next sections how it is possible to use other, built-in features of
<span class="emphasis"><em>Spirit.Karma</em></span> to get rid of the semantic actions altogether.
When writing your grammars in Spirit you should always try to avoid semantic
actions which is often possible. Semantic actions are really powerful tools
but grammars tend to be more efficient and readable without them.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2001-2010 Joel de Guzman, Hartmut Kaiser<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="semantic_actions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="karma_easier_complex.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>