• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Sum - adding numbers</title>
5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Spirit 2.5.8">
8<link rel="up" href="../tutorials.html" title="Tutorials">
9<link rel="prev" href="complex___our_first_complex_parser.html" title="Complex - Our first complex parser">
10<link rel="next" href="number_list___stuffing_numbers_into_a_std__vector.html" title="Number List - stuffing numbers into a std::vector">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="complex___our_first_complex_parser.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="number_list___stuffing_numbers_into_a_std__vector.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="spirit.qi.tutorials.sum___adding_numbers"></a><a class="link" href="sum___adding_numbers.html" title="Sum - adding numbers">Sum - adding
28        numbers</a>
29</h4></div></div></div>
30<p>
31          Here's a parser that sums a comma-separated list of numbers.
32        </p>
33<p>
34          Ok we've glossed over some details in our previous examples. First, our
35          includes:
36        </p>
37<p>
38</p>
39<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">qi</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
40<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">phoenix_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
41<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">phoenix_operator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
42<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
43<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
44</pre>
45<p>
46        </p>
47<p>
48          Then some using directives:
49        </p>
50<p>
51</p>
52<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">qi</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">;</span>
53<span class="keyword">namespace</span> <span class="identifier">ascii</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">;</span>
54<span class="keyword">namespace</span> <span class="identifier">phoenix</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">phoenix</span><span class="special">;</span>
55
56<span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
57<span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">_1</span><span class="special">;</span>
58<span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">;</span>
59<span class="keyword">using</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">ref</span><span class="special">;</span>
60</pre>
61<p>
62        </p>
63<div class="informaltable"><table class="table">
64<colgroup>
65<col>
66<col>
67</colgroup>
68<thead><tr>
69<th>
70                  <p>
71                    Namespace
72                  </p>
73                </th>
74<th>
75                  <p>
76                    Description
77                  </p>
78                </th>
79</tr></thead>
80<tbody>
81<tr>
82<td>
83                  <p>
84                    boost::phoenix
85                  </p>
86                </td>
87<td>
88                  <p>
89                    All of phoenix
90                  </p>
91                </td>
92</tr>
93<tr>
94<td>
95                  <p>
96                    boost::spirit
97                  </p>
98                </td>
99<td>
100                  <p>
101                    All of spirit
102                  </p>
103                </td>
104</tr>
105<tr>
106<td>
107                  <p>
108                    boost::spirit::qi
109                  </p>
110                </td>
111<td>
112                  <p>
113                    All of spirit.qi
114                  </p>
115                </td>
116</tr>
117<tr>
118<td>
119                  <p>
120                    boost::spirit::ascii
121                  </p>
122                </td>
123<td>
124                  <p>
125                    ASCII version of <code class="computeroutput"><span class="identifier">char_</span></code>
126                    and all char related parsers. Other encodings are also provided
127                    (e.g. also an ISO8859.1)
128                  </p>
129                </td>
130</tr>
131<tr>
132<td>
133                  <p>
134                    boost::spirit::arg_names
135                  </p>
136                </td>
137<td>
138                  <p>
139                    Special phoenix placeholders for spirit
140                  </p>
141                </td>
142</tr>
143</tbody>
144</table></div>
145<div class="note"><table border="0" summary="Note">
146<tr>
147<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../images/note.png"></td>
148<th align="left">Note</th>
149</tr>
150<tr><td align="left" valign="top"><p>
151            If you feel uneasy with using whole namespaces, feel free to qualify
152            your code, use namespace aliases, etc. For the purpose of this tutorial,
153            we will be presenting unqualified names for both Spirit and <a href="../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>.
154            No worries, we will always present the full working code, so you won't
155            get lost. In fact, all examples in this tutorial have a corresponding
156            cpp file that QuickBook (the documentation tool we are using) imports
157            in here as code snippets.
158          </p></td></tr>
159</table></div>
160<p>
161          Now the actual parser:
162        </p>
163<p>
164</p>
165<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
166<span class="keyword">bool</span> <span class="identifier">adder</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&amp;</span> <span class="identifier">n</span><span class="special">)</span>
167<span class="special">{</span>
168    <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span>
169
170        <span class="comment">//  Begin grammar</span>
171        <span class="special">(</span>
172            <span class="identifier">double_</span><span class="special">[</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">_1</span><span class="special">]</span> <span class="special">&gt;&gt;</span> <span class="special">*(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span><span class="special">[</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">_1</span><span class="special">])</span>
173        <span class="special">)</span>
174        <span class="special">,</span>
175        <span class="comment">//  End grammar</span>
176
177        <span class="identifier">space</span><span class="special">);</span>
178
179    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">first</span> <span class="special">!=</span> <span class="identifier">last</span><span class="special">)</span> <span class="comment">// fail if we did not get a full match</span>
180        <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
181    <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
182<span class="special">}</span>
183</pre>
184<p>
185        </p>
186<p>
187          The full cpp file for this example can be found here: <a href="../../../../../example/qi/sum.cpp" target="_top">../../example/qi/sum.cpp</a>
188        </p>
189<p>
190          This is almost like our original numbers list example. We're incrementally
191          building on top of our examples. This time though, like in the complex
192          number example, we'll be adding the smarts. There's an accumulator (<code class="computeroutput"><span class="keyword">double</span><span class="special">&amp;</span> <span class="identifier">n</span></code>) that adds the numbers parsed. On a
193          successful parse, this number is the sum of all the parsed numbers.
194        </p>
195<p>
196          The first <code class="computeroutput"><span class="identifier">double_</span></code> parser
197          attaches this action:
198        </p>
199<pre class="programlisting"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">_1</span>
200</pre>
201<p>
202          This assigns the parsed result (actually, the attribute of <code class="computeroutput"><span class="identifier">double_</span></code>) to <code class="computeroutput"><span class="identifier">n</span></code>.
203          <code class="computeroutput"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span></code> tells
204          <a href="../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
205          that <code class="computeroutput"><span class="identifier">n</span></code> is a mutable reference.
206          <code class="computeroutput"><span class="identifier">_1</span></code> is a <a href="../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
207          placeholder for the parsed result attribute.
208        </p>
209<p>
210          The second <code class="computeroutput"><span class="identifier">double_</span></code> parser
211          attaches this action:
212        </p>
213<pre class="programlisting"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">_1</span>
214</pre>
215<p>
216          So, subsequent numbers add into <code class="computeroutput"><span class="identifier">n</span></code>.
217        </p>
218<p>
219          That wasn't too bad, was it :-) ?
220        </p>
221</div>
222<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
223<td align="left"></td>
224<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
225        Distributed under the Boost Software License, Version 1.0. (See accompanying
226        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>)
227      </p>
228</div></td>
229</tr></table>
230<hr>
231<div class="spirit-nav">
232<a accesskey="p" href="complex___our_first_complex_parser.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="number_list___stuffing_numbers_into_a_std__vector.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
233</div>
234</body>
235</html>
236