• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Parser Grammar</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="../nonterminal.html" title="Nonterminal Parsers">
9<link rel="prev" href="rule.html" title="Parser Rule">
10<link rel="next" href="../numeric.html" title="Numeric 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="rule.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.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="../numeric.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.nonterminal.grammar"></a><a class="link" href="grammar.html" title="Parser Grammar">Parser Grammar</a>
28</h5></div></div></div>
29<h6>
30<a name="spirit.qi.reference.nonterminal.grammar.h0"></a>
31            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.description"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.description">Description</a>
32          </h6>
33<p>
34            The grammar encapsulates a set of <a class="link" href="rule.html" title="Parser Rule">rules</a>
35            (as well as primitive parsers (<a class="link" href="../parser_concepts/primitiveparser.html" title="PrimitiveParser"><code class="computeroutput"><span class="identifier">PrimitiveParser</span></code></a>) and sub-grammars).
36            The grammar is the main mechanism for modularization and composition.
37            Grammars can be composed to form more complex grammars.
38          </p>
39<h6>
40<a name="spirit.qi.reference.nonterminal.grammar.h1"></a>
41            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.header"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.header">Header</a>
42          </h6>
43<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/qi/nonterminal/grammar.hpp&gt;</span>
44<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_grammar</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
45</pre>
46<p>
47            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
48          </p>
49<h6>
50<a name="spirit.qi.reference.nonterminal.grammar.h2"></a>
51            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.namespace"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.namespace">Namespace</a>
52          </h6>
53<div class="informaltable"><table class="table">
54<colgroup><col></colgroup>
55<thead><tr><th>
56                    <p>
57                      Name
58                    </p>
59                  </th></tr></thead>
60<tbody><tr><td>
61                    <p>
62                      <code class="computeroutput"><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">grammar</span></code>
63                    </p>
64                  </td></tr></tbody>
65</table></div>
66<h6>
67<a name="spirit.qi.reference.nonterminal.grammar.h3"></a>
68            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.synopsis"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.synopsis">Synopsis</a>
69          </h6>
70<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">,</span> <span class="keyword">typename</span> <span class="identifier">A1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">A2</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">A3</span><span class="special">&gt;</span>
71<span class="keyword">struct</span> <span class="identifier">grammar</span><span class="special">;</span>
72</pre>
73<h6>
74<a name="spirit.qi.reference.nonterminal.grammar.h4"></a>
75            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.template_parameters"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.template_parameters">Template
76            parameters</a>
77          </h6>
78<div class="informaltable"><table class="table">
79<colgroup>
80<col>
81<col>
82<col>
83</colgroup>
84<thead><tr>
85<th>
86                    <p>
87                      Parameter
88                    </p>
89                  </th>
90<th>
91                    <p>
92                      Description
93                    </p>
94                  </th>
95<th>
96                    <p>
97                      Default
98                    </p>
99                  </th>
100</tr></thead>
101<tbody>
102<tr>
103<td>
104                    <p>
105                      <code class="computeroutput"><span class="identifier">Iterator</span></code>
106                    </p>
107                  </td>
108<td>
109                    <p>
110                      The underlying iterator type that the rule is expected to work
111                      on.
112                    </p>
113                  </td>
114<td>
115                    <p>
116                      none
117                    </p>
118                  </td>
119</tr>
120<tr>
121<td>
122                    <p>
123                      <code class="computeroutput"><span class="identifier">A1</span></code>, <code class="computeroutput"><span class="identifier">A2</span></code>, <code class="computeroutput"><span class="identifier">A3</span></code>
124                    </p>
125                  </td>
126<td>
127                    <p>
128                      Either <code class="computeroutput"><span class="identifier">Signature</span></code>,
129                      <code class="computeroutput"><span class="identifier">Skipper</span></code> or
130                      <code class="computeroutput"><span class="identifier">Locals</span></code> in any
131                      order. See table below.
132                    </p>
133                  </td>
134<td>
135                    <p>
136                      See table below.
137                    </p>
138                  </td>
139</tr>
140</tbody>
141</table></div>
142<p>
143            Here is more information about the template parameters:
144          </p>
145<div class="informaltable"><table class="table">
146<colgroup>
147<col>
148<col>
149<col>
150</colgroup>
151<thead><tr>
152<th>
153                    <p>
154                      Parameter
155                    </p>
156                  </th>
157<th>
158                    <p>
159                      Description
160                    </p>
161                  </th>
162<th>
163                    <p>
164                      Default
165                    </p>
166                  </th>
167</tr></thead>
168<tbody>
169<tr>
170<td>
171                    <p>
172                      <code class="computeroutput"><span class="identifier">Signature</span></code>
173                    </p>
174                  </td>
175<td>
176                    <p>
177                      Specifies the grammar's synthesized (return value) and inherited
178                      attributes (arguments). More on this here: <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
179                    </p>
180                  </td>
181<td>
182                    <p>
183                      <code class="computeroutput"><span class="identifier">unused_type</span></code>.
184                      When <code class="computeroutput"><span class="identifier">Signature</span></code>
185                      defaults to <code class="computeroutput"><span class="identifier">unused_type</span></code>,
186                      the effect is the same as specifying a signature of <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code>
187                      which is also equivalent to <code class="computeroutput"><span class="identifier">unused_type</span><span class="special">()</span></code>
188                    </p>
189                  </td>
190</tr>
191<tr>
192<td>
193                    <p>
194                      <code class="computeroutput"><span class="identifier">Skipper</span></code>
195                    </p>
196                  </td>
197<td>
198                    <p>
199                      Specifies the grammar's skipper parser. Specify this if you
200                      want the grammar to skip white spaces.
201                    </p>
202                  </td>
203<td>
204                    <p>
205                      <code class="computeroutput"><span class="identifier">unused_type</span></code>
206                    </p>
207                  </td>
208</tr>
209<tr>
210<td>
211                    <p>
212                      <code class="computeroutput"><span class="identifier">Locals</span></code>
213                    </p>
214                  </td>
215<td>
216                    <p>
217                      Specifies the grammar's local variables. See <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
218                    </p>
219                  </td>
220<td>
221                    <p>
222                      <code class="computeroutput"><span class="identifier">unused_type</span></code>
223                    </p>
224                  </td>
225</tr>
226</tbody>
227</table></div>
228<h6>
229<a name="spirit.qi.reference.nonterminal.grammar.h5"></a>
230            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.model_of"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.model_of">Model of</a>
231          </h6>
232<div class="blockquote"><blockquote class="blockquote"><p>
233              <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>
234            </p></blockquote></div>
235<div class="variablelist">
236<p class="title"><b>Notation</b></p>
237<dl class="variablelist">
238<dt><span class="term"><code class="computeroutput"><span class="identifier">g</span></code></span></dt>
239<dd><p>
240                  A grammar
241                </p></dd>
242</dl>
243</div>
244<h6>
245<a name="spirit.qi.reference.nonterminal.grammar.h6"></a>
246            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.expression_semantics"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.expression_semantics">Expression
247            Semantics</a>
248          </h6>
249<p>
250            Semantics of an expression is defined only where it differs from, or
251            is not defined in <a class="link" href="../parser_concepts/nonterminal.html" title="Nonterminal"><code class="computeroutput"><span class="identifier">Nonterminal</span></code></a>.
252          </p>
253<div class="informaltable"><table class="table">
254<colgroup>
255<col>
256<col>
257</colgroup>
258<thead><tr>
259<th>
260                    <p>
261                      Expression
262                    </p>
263                  </th>
264<th>
265                    <p>
266                      Semantics
267                    </p>
268                  </th>
269</tr></thead>
270<tbody><tr>
271<td>
272                    <p>
273</p>
274<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-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>
275<span class="keyword">struct</span> <span class="identifier">my_grammar</span> <span class="special">:</span> <span class="identifier">grammar</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">,</span> <span class="identifier">A3</span><span class="special">&gt;</span>
276<span class="special">{</span>
277    <span class="identifier">my_grammar</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">my_grammar</span><span class="special">::</span><span class="identifier">base_type</span><span class="special">(</span><span class="identifier">start</span><span class="special">,</span> <span class="identifier">name</span><span class="special">)</span>
278    <span class="special">{</span>
279        <span class="comment">// Rule definitions</span>
280        <span class="identifier">start</span> <span class="special">=</span> <span class="comment">/* ... */</span><span class="special">;</span>
281    <span class="special">}</span>
282
283    <span class="identifier">rule</span><span class="special">&lt;</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">,</span> <span class="identifier">A3</span><span class="special">&gt;</span> <span class="identifier">start</span><span class="special">;</span>
284    <span class="comment">// more rule declarations...</span>
285<span class="special">};</span>
286</pre>
287<p>
288                    </p>
289                  </td>
290<td>
291                    <p>
292                      Grammar definition. <code class="computeroutput"><span class="identifier">name</span></code>
293                      is an optional string that gives the grammar its name, useful
294                      for debugging and error handling.
295                    </p>
296                  </td>
297</tr></tbody>
298</table></div>
299<div class="note"><table border="0" summary="Note">
300<tr>
301<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
302<th align="left">Note</th>
303</tr>
304<tr><td align="left" valign="top"><p>
305              The template parameters of a grammar and its start rule (the rule passed
306              to the grammar's base class constructor) must match, otherwise you
307              will see compilation errors.
308            </p></td></tr>
309</table></div>
310<h6>
311<a name="spirit.qi.reference.nonterminal.grammar.h7"></a>
312            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.attributes"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.attributes">Attributes</a>
313          </h6>
314<div class="blockquote"><blockquote class="blockquote"><p>
315              The parser attribute of the grammar is <code class="computeroutput"><span class="identifier">T</span></code>,
316              its synthesized attribute. See <a class="link" href="../parser_concepts/nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.attributes"><code class="computeroutput"><span class="identifier">Attribute</span></code></a>
317            </p></blockquote></div>
318<h6>
319<a name="spirit.qi.reference.nonterminal.grammar.h8"></a>
320            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.complexity"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.complexity">Complexity</a>
321          </h6>
322<div class="blockquote"><blockquote class="blockquote"><p>
323              The complexity is defined by the complexity of the its definition.
324            </p></blockquote></div>
325<h6>
326<a name="spirit.qi.reference.nonterminal.grammar.h9"></a>
327            <span class="phrase"><a name="spirit.qi.reference.nonterminal.grammar.example"></a></span><a class="link" href="grammar.html#spirit.qi.reference.nonterminal.grammar.example">Example</a>
328          </h6>
329<div class="note"><table border="0" summary="Note">
330<tr>
331<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
332<th align="left">Note</th>
333</tr>
334<tr><td align="left" valign="top"><p>
335              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>
336              section.
337            </p></td></tr>
338</table></div>
339<p>
340            Some using declarations:
341          </p>
342<p>
343</p>
344<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">ascii</span><span class="special">::</span><span class="identifier">space_type</span><span class="special">;</span>
345<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">int_</span><span class="special">;</span>
346<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">grammar</span><span class="special">;</span>
347<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">rule</span><span class="special">;</span>
348</pre>
349<p>
350          </p>
351<p>
352            Basic grammar usage:
353          </p>
354<p>
355</p>
356<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">num_list</span> <span class="special">:</span> <span class="identifier">grammar</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="identifier">space_type</span><span class="special">&gt;</span>
357<span class="special">{</span>
358    <span class="identifier">num_list</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">start</span><span class="special">)</span>
359    <span class="special">{</span>
360        <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">int_</span><span class="special">;</span>
361        <span class="identifier">num</span> <span class="special">=</span> <span class="identifier">int_</span><span class="special">;</span>
362        <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">num</span> <span class="special">&gt;&gt;</span> <span class="special">*(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">num</span><span class="special">);</span>
363    <span class="special">}</span>
364
365    <span class="identifier">rule</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="identifier">space_type</span><span class="special">&gt;</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">num</span><span class="special">;</span>
366<span class="special">};</span>
367</pre>
368<p>
369          </p>
370<p>
371            How to use the example grammar:
372          </p>
373<p>
374</p>
375<pre class="programlisting"><span class="identifier">num_list</span> <span class="identifier">nlist</span><span class="special">;</span>
376<span class="identifier">test_phrase_parser</span><span class="special">(</span><span class="string">"123, 456, 789"</span><span class="special">,</span> <span class="identifier">nlist</span><span class="special">);</span>
377</pre>
378<p>
379          </p>
380</div>
381<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
382<td align="left"></td>
383<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
384        Distributed under the Boost Software License, Version 1.0. (See accompanying
385        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>)
386      </p>
387</div></td>
388</tr></table>
389<hr>
390<div class="spirit-nav">
391<a accesskey="p" href="rule.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.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="../numeric.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
392</div>
393</body>
394</html>
395