• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Nonterminal</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="../parser_concepts.html" title="Parser Concepts">
9<link rel="prev" href="naryparser.html" title="NaryParser">
10<link rel="next" href="../basics.html" title="Parser Basics">
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="naryparser.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../parser_concepts.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="../basics.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.parser_concepts.nonterminal"></a><a class="link" href="nonterminal.html" title="Nonterminal">Nonterminal</a>
28</h5></div></div></div>
29<h6>
30<a name="spirit.qi.reference.parser_concepts.nonterminal.h0"></a>
31            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.description"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.description">Description</a>
32          </h6>
33<p>
34            A Nonterminal is a symbol in a <a class="link" href="../../../abstracts/parsing_expression_grammar.html" title="Parsing Expression Grammar">Parsing
35            Expression Grammar</a> production that represents a grammar fragment.
36            Nonterminals may self reference to specify recursion. This is one of
37            the most important concepts and the reason behind the word "recursive"
38            in recursive descent parsing.
39          </p>
40<h6>
41<a name="spirit.qi.reference.parser_concepts.nonterminal.h1"></a>
42            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.refinement_of"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.refinement_of">Refinement
43            of</a>
44          </h6>
45<div class="blockquote"><blockquote class="blockquote"><p>
46              <a class="link" href="parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>
47            </p></blockquote></div>
48<h6>
49<a name="spirit.qi.reference.parser_concepts.nonterminal.h2"></a>
50            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.signature"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.signature">Signature</a>
51          </h6>
52<p>
53            Nonterminals can have both synthesized and inherited attributes. The
54            Nonterminal's <span class="emphasis"><em>Signature</em></span> specifies both the synthesized
55            and inherited attributes. The specification uses the function declarator
56            syntax:
57          </p>
58<pre class="programlisting"><span class="identifier">RT</span><span class="special">(</span><span class="identifier">A0</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="special">...,</span> <span class="identifier">AN</span><span class="special">)</span>
59</pre>
60<p>
61            where <code class="computeroutput"><span class="identifier">RT</span></code> is the Nonterminal's
62            synthesized attribute and <code class="computeroutput"><span class="identifier">A0</span></code>
63            ... <code class="computeroutput"><span class="identifier">AN</span></code> are the Nonterminal's
64            inherited attributes.
65          </p>
66<p>
67            The default value is <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code> (no synthesized and inherited attributes).
68          </p>
69<h6>
70<a name="spirit.qi.reference.parser_concepts.nonterminal.h3"></a>
71            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.attributes"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.attributes">Attributes</a>
72          </h6>
73<p>
74            The Nonterminal models a C++ function. The Nonterminal's synthesized
75            attribute is analogous to the function return value and its inherited
76            attributes are analogous to function arguments. The inherited attributes
77            (arguments) can be passed in just like any <a class="link" href="../basics.html#spirit.qi.reference.basics.lazy_argument">Lazy
78            Argument</a>, e.g.:
79          </p>
80<pre class="programlisting"><span class="identifier">r</span><span class="special">(</span><span class="identifier">expr</span><span class="special">)</span> <span class="comment">// Evaluate expr at parse time and pass the result to the Nonterminal r</span>
81</pre>
82<h6>
83<a name="spirit.qi.reference.parser_concepts.nonterminal.h4"></a>
84            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___val__phrase___code_"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___val__phrase___code_"><code class="computeroutput"><span class="identifier">_val</span></code></a>
85          </h6>
86<p>
87            The <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">_val</span></code> placeholder can be used in <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a> semantic
88            actions anywhere in the Nonterminal's definition. This <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
89            placeholder refers to the Nonterminal's (synthesized) attribute. The
90            <code class="computeroutput"><span class="identifier">_val</span></code> placeholder acts
91            like a mutable reference to the Nonterminal's attribute.
92          </p>
93<div class="note"><table border="0" summary="Note">
94<tr>
95<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
96<th align="left">Note</th>
97</tr>
98<tr><td align="left" valign="top"><p>
99              Starting with <a href="http://boost-spirit.com" target="_top">Spirit</a> V2.5
100              (distributed with Boost V1.47) the placeholder <code class="computeroutput"><span class="identifier">_val</span></code>
101              can be used in semantic actions attached to top level parser components
102              as well. See <a class="link" href="../parse_api.html" title="Parser API">The Parse
103              API</a> for more information.
104            </p></td></tr>
105</table></div>
106<h6>
107<a name="spirit.qi.reference.parser_concepts.nonterminal.h5"></a>
108            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___r1__phrase___code_______code__phrase_role__identifier__r10__phrase___code_"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___r1__phrase___code_______code__phrase_role__identifier__r10__phrase___code_"><code class="computeroutput"><span class="identifier">_r1</span></code> ... <code class="computeroutput"><span class="identifier">r10</span></code></a>
109          </h6>
110<p>
111            The <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_r1</span></code> ... <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">r10</span></code>
112            placeholders can be used in <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
113            semantic actions anywhere in the Nonterminal's definition. These <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a> placeholders
114            refer to the Nonterminal's inherited attributes.
115          </p>
116<h6>
117<a name="spirit.qi.reference.parser_concepts.nonterminal.h6"></a>
118            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.locals"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.locals">Locals</a>
119          </h6>
120<p>
121            Nonterminals can have local variables that will be created on the stack
122            at parse time. A locals descriptor added to the Nonterminal declaration
123            will give the Nonterminal local variables:
124          </p>
125<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...,</span> <span class="keyword">typename</span> <span class="identifier">TN</span><span class="special">&gt;</span>
126<span class="keyword">struct</span> <span class="identifier">locals</span><span class="special">;</span>
127</pre>
128<p>
129            where <code class="computeroutput"><span class="identifier">T0</span></code> ... <code class="computeroutput"><span class="identifier">TN</span></code> are the types of local variables
130            accessible in your <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
131            semantic actions using the placeholders:
132          </p>
133<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
134<li class="listitem">
135                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_a</span></code>
136              </li>
137<li class="listitem">
138                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_b</span></code>
139              </li>
140<li class="listitem">
141                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_c</span></code>
142              </li>
143<li class="listitem">
144                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_d</span></code>
145              </li>
146<li class="listitem">
147                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_e</span></code>
148              </li>
149<li class="listitem">
150                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_f</span></code>
151              </li>
152<li class="listitem">
153                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_g</span></code>
154              </li>
155<li class="listitem">
156                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_h</span></code>
157              </li>
158<li class="listitem">
159                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_i</span></code>
160              </li>
161<li class="listitem">
162                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_j</span></code>
163              </li>
164</ul></div>
165<p>
166            which correspond to the Nonterminal's local variables <code class="computeroutput"><span class="identifier">T0</span></code>
167            ... <code class="computeroutput"><span class="identifier">T9</span></code>.
168          </p>
169<div class="variablelist">
170<p class="title"><b>Notation</b></p>
171<dl class="variablelist">
172<dt><span class="term"><code class="computeroutput"><span class="identifier">x</span></code></span></dt>
173<dd><p>
174                  A Nonterminal
175                </p></dd>
176<dt><span class="term"><code class="computeroutput"><span class="identifier">X</span></code></span></dt>
177<dd><p>
178                  A Nonterminal type
179                </p></dd>
180<dt><span class="term"><code class="computeroutput"><span class="identifier">arg1</span></code>, <code class="computeroutput"><span class="identifier">arg2</span></code>, ..., <code class="computeroutput"><span class="identifier">argN</span></code></span></dt>
181<dd><p>
182                  <a class="link" href="../basics.html#spirit.qi.reference.basics.lazy_argument">Lazy Arguments</a>
183                  that evaluate to each of the Nonterminal's inherited attributes.
184                </p></dd>
185</dl>
186</div>
187<h6>
188<a name="spirit.qi.reference.parser_concepts.nonterminal.h7"></a>
189            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.valid_expressions"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.valid_expressions">Valid
190            Expressions</a>
191          </h6>
192<p>
193            In addition to the requirements defined in <a class="link" href="parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>, for any Nonterminal
194            the following must be met:
195          </p>
196<div class="informaltable"><table class="table">
197<colgroup>
198<col>
199<col>
200<col>
201</colgroup>
202<thead><tr>
203<th>
204                    <p>
205                      Expression
206                    </p>
207                  </th>
208<th>
209                    <p>
210                      Semantics
211                    </p>
212                  </th>
213<th>
214                    <p>
215                      Return type
216                    </p>
217                  </th>
218</tr></thead>
219<tbody>
220<tr>
221<td>
222                    <p>
223                      <code class="computeroutput"><span class="identifier">x</span></code>
224                    </p>
225                  </td>
226<td>
227                    <p>
228                      In a parser expression, invoke Nonterminal <code class="computeroutput"><span class="identifier">x</span></code>
229                    </p>
230                  </td>
231<td>
232                    <p>
233                      <code class="computeroutput"><span class="identifier">X</span></code>
234                    </p>
235                  </td>
236</tr>
237<tr>
238<td>
239                    <p>
240                      <code class="computeroutput"><span class="identifier">x</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span>
241                      <span class="identifier">arg2</span><span class="special">,</span>
242                      <span class="special">...,</span> <span class="identifier">argN</span><span class="special">)</span></code>
243                    </p>
244                  </td>
245<td>
246                    <p>
247                      In a parser expression, invoke Nonterminal <code class="computeroutput"><span class="identifier">r</span></code>
248                      passing in inherited attributes <code class="computeroutput"><span class="identifier">arg1</span></code>
249                      ... <code class="computeroutput"><span class="identifier">argN</span></code>
250                    </p>
251                  </td>
252<td>
253                    <p>
254                      <code class="computeroutput"><span class="identifier">X</span></code>
255                    </p>
256                  </td>
257</tr>
258<tr>
259<td>
260                    <p>
261                      <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span></code>
262                    </p>
263                  </td>
264<td>
265                    <p>
266                      Naming a Nonterminal.
267                    </p>
268                  </td>
269<td>
270                    <p>
271                      <code class="computeroutput"><span class="keyword">void</span></code>
272                    </p>
273                  </td>
274</tr>
275<tr>
276<td>
277                    <p>
278                      <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span><span class="special">()</span></code>
279                    </p>
280                  </td>
281<td>
282                    <p>
283                      Getting the name of a Nonterminal.
284                    </p>
285                  </td>
286<td>
287                    <p>
288                      <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
289                    </p>
290                  </td>
291</tr>
292<tr>
293<td>
294                    <p>
295                      debug(x)
296                    </p>
297                  </td>
298<td>
299                    <p>
300                      Debug Nonterminal <code class="computeroutput"><span class="identifier">x</span></code>.
301                    </p>
302                  </td>
303<td>
304                    <p>
305                      <code class="computeroutput"><span class="keyword">void</span></code>
306                    </p>
307                  </td>
308</tr>
309</tbody>
310</table></div>
311<h6>
312<a name="spirit.qi.reference.parser_concepts.nonterminal.h8"></a>
313            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.type_expressions"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.type_expressions">Type
314            Expressions</a>
315          </h6>
316<div class="informaltable"><table class="table">
317<colgroup>
318<col>
319<col>
320</colgroup>
321<thead><tr>
322<th>
323                    <p>
324                      Expression
325                    </p>
326                  </th>
327<th>
328                    <p>
329                      Description
330                    </p>
331                  </th>
332</tr></thead>
333<tbody>
334<tr>
335<td>
336                    <p>
337                      <code class="computeroutput"><span class="identifier">X</span><span class="special">::</span><span class="identifier">sig_type</span></code>
338                    </p>
339                  </td>
340<td>
341                    <p>
342                      The Signature of <code class="computeroutput"><span class="identifier">X</span></code>:
343                      In a function signature form as described above in the Signature
344                      paragraph.
345                    </p>
346                  </td>
347</tr>
348<tr>
349<td>
350                    <p>
351                      <code class="computeroutput"><span class="identifier">X</span><span class="special">::</span><span class="identifier">locals_type</span></code>
352                    </p>
353                  </td>
354<td>
355                    <p>
356                      The local variables of <code class="computeroutput"><span class="identifier">X</span></code>:
357                      An <a href="../../../../../../../../libs/mpl/doc/refmanual/forward-sequence.html" target="_top">MPL
358                      Forward Sequence</a>.
359                    </p>
360                  </td>
361</tr>
362</tbody>
363</table></div>
364<h6>
365<a name="spirit.qi.reference.parser_concepts.nonterminal.h9"></a>
366            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.models"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.models">Models</a>
367          </h6>
368<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
369<li class="listitem">
370                <a class="link" href="../nonterminal/rule.html" title="Parser Rule">rule</a>
371              </li>
372<li class="listitem">
373                <a class="link" href="../nonterminal/grammar.html" title="Parser Grammar">grammar</a>
374              </li>
375</ul></div>
376</div>
377<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
378<td align="left"></td>
379<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
380        Distributed under the Boost Software License, Version 1.0. (See accompanying
381        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>)
382      </p>
383</div></td>
384</tr></table>
385<hr>
386<div class="spirit-nav">
387<a accesskey="p" href="naryparser.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../parser_concepts.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="../basics.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
388</div>
389</body>
390</html>
391