• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Sequential Or Parser (a || b)</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="../operator.html" title="Parser Operators">
9<link rel="prev" href="sequence.html" title="Sequence Parser (a &gt;&gt; b)">
10<link rel="next" href="../stream.html" title="Stream Parsers">
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="sequence.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="../stream.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="spirit.qi.reference.operator.sequential_or"></a><a class="link" href="sequential_or.html" title="Sequential Or Parser (a || b)">Sequential
28          Or Parser (<code class="computeroutput"><span class="identifier">a</span> <span class="special">||</span>
29          <span class="identifier">b</span></code>)</a>
30</h5></div></div></div>
31<h6>
32<a name="spirit.qi.reference.operator.sequential_or.h0"></a>
33            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.description"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.description">Description</a>
34          </h6>
35<p>
36            The sequential-or operator, <code class="computeroutput"><span class="identifier">a</span>
37            <span class="special">||</span> <span class="identifier">b</span></code>,
38            matches <code class="computeroutput"><span class="identifier">a</span></code> or <code class="computeroutput"><span class="identifier">b</span></code> or <code class="computeroutput"><span class="identifier">a</span></code>
39            followed by <code class="computeroutput"><span class="identifier">b</span></code>. That is,
40            if both <code class="computeroutput"><span class="identifier">a</span></code> and <code class="computeroutput"><span class="identifier">b</span></code> match, it must be in sequence; this
41            is equivalent to <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;&gt;</span>
42            <span class="special">-</span><span class="identifier">b</span>
43            <span class="special">|</span> <span class="identifier">b</span></code>:
44          </p>
45<pre class="programlisting"><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span> <span class="special">||</span> <span class="special">...</span>
46</pre>
47<h6>
48<a name="spirit.qi.reference.operator.sequential_or.h1"></a>
49            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.header"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.header">Header</a>
50          </h6>
51<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/qi/operator/sequential_or.hpp&gt;</span>
52<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_sequential_or</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
53</pre>
54<p>
55            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
56          </p>
57<h6>
58<a name="spirit.qi.reference.operator.sequential_or.h2"></a>
59            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.model_of"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.model_of">Model of</a>
60          </h6>
61<div class="blockquote"><blockquote class="blockquote"><p>
62              <a class="link" href="../parser_concepts/naryparser.html" title="NaryParser"><code class="computeroutput"><span class="identifier">NaryParser</span></code></a>
63            </p></blockquote></div>
64<div class="variablelist">
65<p class="title"><b>Notation</b></p>
66<dl class="variablelist">
67<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code>, <code class="computeroutput"><span class="identifier">b</span></code></span></dt>
68<dd><p>
69                  A <a class="link" href="../parser_concepts/parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>
70                </p></dd>
71</dl>
72</div>
73<h6>
74<a name="spirit.qi.reference.operator.sequential_or.h3"></a>
75            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.expression_semantics"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.expression_semantics">Expression
76            Semantics</a>
77          </h6>
78<p>
79            Semantics of an expression is defined only where it differs from, or
80            is not defined in <a class="link" href="../parser_concepts/naryparser.html" title="NaryParser"><code class="computeroutput"><span class="identifier">NaryParser</span></code></a>.
81          </p>
82<div class="informaltable"><table class="table">
83<colgroup>
84<col>
85<col>
86</colgroup>
87<thead><tr>
88<th>
89                    <p>
90                      Expression
91                    </p>
92                  </th>
93<th>
94                    <p>
95                      Semantics
96                    </p>
97                  </th>
98</tr></thead>
99<tbody><tr>
100<td>
101                    <p>
102                      <code class="computeroutput"><span class="identifier">a</span> <span class="special">||</span>
103                      <span class="identifier">b</span></code>
104                    </p>
105                  </td>
106<td>
107                    <p>
108                      Match <code class="computeroutput"><span class="identifier">a</span></code> or
109                      <code class="computeroutput"><span class="identifier">b</span></code> in sequence.
110                      equivalent to <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="special">-</span><span class="identifier">b</span> <span class="special">|</span>
111                      <span class="identifier">b</span></code>
112                    </p>
113                  </td>
114</tr></tbody>
115</table></div>
116<h6>
117<a name="spirit.qi.reference.operator.sequential_or.h4"></a>
118            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.attributes"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.attributes">Attributes</a>
119          </h6>
120<p>
121            See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.qi.quick_reference.compound_attribute_rules.notation">Compound
122            Attribute Notation</a>.
123          </p>
124<div class="informaltable"><table class="table">
125<colgroup>
126<col>
127<col>
128</colgroup>
129<thead><tr>
130<th>
131                    <p>
132                      Expression
133                    </p>
134                  </th>
135<th>
136                    <p>
137                      Attribute
138                    </p>
139                  </th>
140</tr></thead>
141<tbody><tr>
142<td>
143                    <p>
144                      <code class="computeroutput"><span class="identifier">a</span> <span class="special">||</span>
145                      <span class="identifier">b</span></code>
146                    </p>
147                  </td>
148<td>
149                    <p>
150</p>
151<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;,</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">B</span><span class="special">&gt;</span> <span class="special">&gt;</span>
152<span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
153<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">B</span><span class="special">&gt;</span>
154<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">Unused</span>
155
156<span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">||</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="special">&gt;</span></pre>
157<p>
158                    </p>
159                  </td>
160</tr></tbody>
161</table></div>
162<div class="note"><table border="0" summary="Note">
163<tr>
164<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
165<th align="left">Note</th>
166</tr>
167<tr><td align="left" valign="top">
168<p>
169              The sequential-or parser behaves attribute-wise very similar to the
170              plain sequence parser (<code class="computeroutput"><span class="identifier">a</span>
171              <span class="special">&gt;&gt;</span> <span class="identifier">b</span></code>)
172              in the sense that it exposes the attributes of its elements separately.
173              For instance, if you attach a semantic action to the whole sequential-or:
174</p>
175<pre class="programlisting"><span class="special">(</span><span class="identifier">int_</span> <span class="special">||</span> <span class="identifier">int_</span><span class="special">)[</span><span class="identifier">print_pair</span><span class="special">(</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">_2</span><span class="special">)]</span>
176</pre>
177<p>
178              the function object <code class="computeroutput"><span class="identifier">print_pair</span></code>
179              would be invoked with the attribute of the first <code class="computeroutput"><span class="identifier">int_</span></code>
180              (<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code>)
181              as its first parameter and the attribute of the second <code class="computeroutput"><span class="identifier">int_</span></code> (<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code> as well) as its second parameter.
182            </p>
183</td></tr>
184</table></div>
185<h6>
186<a name="spirit.qi.reference.operator.sequential_or.h5"></a>
187            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.complexity"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.complexity">Complexity</a>
188          </h6>
189<div class="blockquote"><blockquote class="blockquote"><p>
190              The overall complexity of the sequential-or parser is defined by the
191              sum of the complexities of its elements. The complexity of the sequential-or
192              itself is O(N), where N is the number of elements in the sequence.
193            </p></blockquote></div>
194<h6>
195<a name="spirit.qi.reference.operator.sequential_or.h6"></a>
196            <span class="phrase"><a name="spirit.qi.reference.operator.sequential_or.example"></a></span><a class="link" href="sequential_or.html#spirit.qi.reference.operator.sequential_or.example">Example</a>
197          </h6>
198<div class="note"><table border="0" summary="Note">
199<tr>
200<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
201<th align="left">Note</th>
202</tr>
203<tr><td align="left" valign="top"><p>
204              The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.qi.reference.basics.examples">Basics Examples</a>
205              section.
206            </p></td></tr>
207</table></div>
208<p>
209            Some using declarations:
210          </p>
211<p>
212</p>
213<pre class="programlisting"><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">qi</span><span class="special">::</span><span class="identifier">int_</span><span class="special">;</span>
214</pre>
215<p>
216          </p>
217<p>
218            Correctly parsing a number with optional fractional digits:
219          </p>
220<p>
221</p>
222<pre class="programlisting"><span class="identifier">test_parser</span><span class="special">(</span><span class="string">"123.456"</span><span class="special">,</span> <span class="identifier">int_</span> <span class="special">||</span> <span class="special">(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">));</span>  <span class="comment">// full</span>
223<span class="identifier">test_parser</span><span class="special">(</span><span class="string">"123"</span><span class="special">,</span> <span class="identifier">int_</span> <span class="special">||</span> <span class="special">(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">));</span>      <span class="comment">// just the whole number</span>
224<span class="identifier">test_parser</span><span class="special">(</span><span class="string">".456"</span><span class="special">,</span> <span class="identifier">int_</span> <span class="special">||</span> <span class="special">(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">));</span>     <span class="comment">// just the fraction</span>
225</pre>
226<p>
227          </p>
228<p>
229            A naive but incorrect solution would try to do this using optionals (e.g.):
230          </p>
231<pre class="programlisting"><span class="identifier">int_</span> <span class="special">&gt;&gt;</span> <span class="special">-(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">)</span>  <span class="comment">// will not match ".456"</span>
232<span class="special">-</span><span class="identifier">int_</span> <span class="special">&gt;&gt;</span> <span class="special">(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">)</span>  <span class="comment">// will not match "123"</span>
233<span class="special">-</span><span class="identifier">int_</span> <span class="special">&gt;&gt;</span> <span class="special">-(</span><span class="char">'.'</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">)</span> <span class="comment">// will match empty strings! Ooops.</span>
234</pre>
235</div>
236<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
237<td align="left"></td>
238<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
239        Distributed under the Boost Software License, Version 1.0. (See accompanying
240        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>)
241      </p>
242</div></td>
243</tr></table>
244<hr>
245<div class="spirit-nav">
246<a accesskey="p" href="sequence.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="../stream.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
247</div>
248</body>
249</html>
250