1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Attributes of Rules and Grammars</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="../attributes.html" title="Attributes"> 9<link rel="prev" href="more_compound_attributes.html" title="More About Attributes of Compound Components"> 10<link rel="next" href="../../qi.html" title="Qi - Writing 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="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../attributes.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="../../qi.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.abstracts.attributes.nonterminal_attributes"></a><a class="link" href="nonterminal_attributes.html" title="Attributes of Rules and Grammars">Attributes 28 of Rules and Grammars</a> 29</h4></div></div></div> 30<p> 31 Nonterminals are well known from parsers where they are used as the main 32 means of constructing more complex parsers out of simpler ones. The nonterminals 33 in the parser world are very similar to functions in an imperative programming 34 language. They can be used to encapsulate parser expressions for a particular 35 input sequence. After being defined, the nonterminals can be used as 'normal' 36 parsers in more complex expressions whenever the encapsulated input needs 37 to be recognized. Parser nonterminals in <span class="emphasis"><em>Spirit.Qi</em></span> 38 may accept <span class="emphasis"><em>parameters</em></span> (inherited attributes) and usually 39 return a value (the synthesized attribute). 40 </p> 41<p> 42 Both, the types of the inherited and the synthesized attributes have to 43 be explicitly specified while defining the particular <code class="computeroutput"><span class="identifier">grammar</span></code> 44 or the <code class="computeroutput"><span class="identifier">rule</span></code> (the Spirit 45 <a href="../../../../../repository/doc/html/index.html" target="_top">Repository</a> additionally 46 has <code class="computeroutput"><span class="identifier">subrules</span></code> which conform 47 to a similar interface). As an example, the following code declares a 48 <span class="emphasis"><em>Spirit.Qi</em></span> <code class="computeroutput"><span class="identifier">rule</span></code> 49 exposing an <code class="computeroutput"><span class="keyword">int</span></code> as its synthesized 50 attribute, while expecting a single <code class="computeroutput"><span class="keyword">double</span></code> 51 as its inherited attribute (see the section about the <span class="emphasis"><em>Spirit.Qi</em></span> 52 Rule for more information): 53 </p> 54<pre class="programlisting"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">rule</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="keyword">int</span><span class="special">(</span><span class="keyword">double</span><span class="special">)></span> <span class="identifier">r</span><span class="special">;</span> 55</pre> 56<p> 57 In the world of generators, nonterminals are just as useful as in the parser 58 world. Generator nonterminals encapsulate a format description for a particular 59 data type, and, whenever we need to emit output for this data type, the 60 corresponding nonterminal is invoked in a similar way as the predefined 61 <span class="emphasis"><em>Spirit.Karma</em></span> generator primitives. The <span class="emphasis"><em>Spirit.Karma</em></span> 62 nonterminals are very similar to the <span class="emphasis"><em>Spirit.Qi</em></span> nonterminals. 63 Generator nonterminals may accept <span class="emphasis"><em>parameters</em></span> as well, 64 and we call those inherited attributes too. The main difference is that 65 they do not expose a synthesized attribute (as parsers do), but they require 66 a special <span class="emphasis"><em>consumed attribute</em></span>. Usually the consumed 67 attribute is the value the generator creates its output from. Even if the 68 consumed attribute is not 'returned' from the generator we chose to use 69 the same function style declaration syntax as used in <span class="emphasis"><em>Spirit.Qi</em></span>. 70 The example below declares a <span class="emphasis"><em>Spirit.Karma</em></span> <code class="computeroutput"><span class="identifier">rule</span></code> consuming a <code class="computeroutput"><span class="keyword">double</span></code> 71 while not expecting any additional inherited attributes. 72 </p> 73<pre class="programlisting"><span class="identifier">karma</span><span class="special">::</span><span class="identifier">rule</span><span class="special"><</span><span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">double</span><span class="special">()></span> <span class="identifier">r</span><span class="special">;</span> 74</pre> 75<p> 76 The inherited attributes of nonterminal parsers and generators are normally 77 passed to the component during its invocation. These are the <span class="emphasis"><em>parameters</em></span> 78 the parser or generator may accept and they can be used to parameterize 79 the component depending on the context they are invoked from. 80 </p> 81</div> 82<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 83<td align="left"></td> 84<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 85 Distributed under the Boost Software License, Version 1.0. (See accompanying 86 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>) 87 </p> 88</div></td> 89</tr></table> 90<hr> 91<div class="spirit-nav"> 92<a accesskey="p" href="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../attributes.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="../../qi.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 93</div> 94</body> 95</html> 96