1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Porting from Spirit 1.8.x</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="../notes.html" title="Notes"> 9<link rel="prev" href="../notes.html" title="Notes"> 10<link rel="next" href="style_guide.html" title="Style Guide"> 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="../notes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../notes.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="style_guide.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="spirit.notes.porting_from_spirit_1_8_x"></a><a class="link" href="porting_from_spirit_1_8_x.html" title="Porting from Spirit 1.8.x">Porting from 28 Spirit 1.8.x</a> 29</h3></div></div></div> 30<p> 31 The current version of <a href="http://boost-spirit.com" target="_top">Spirit</a> 32 is a complete rewrite of earlier versions (we refer to earlier versions as 33 <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>). 34 The parser generators are now only one part of the whole library. The parser 35 submodule of <a href="http://boost-spirit.com" target="_top">Spirit</a> is now called 36 <span class="emphasis"><em>Spirit.Qi</em></span>. It is conceptually different and exposes 37 a completely different interface. Generally, there is no easy (or automated) 38 way of converting parsers written for <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 39 to <span class="emphasis"><em>Spirit.Qi</em></span>. Therefore this section can give only guidelines 40 on how to approach porting your older parsers to the current version of 41 <a href="http://boost-spirit.com" target="_top">Spirit</a>. 42 </p> 43<h5> 44<a name="spirit.notes.porting_from_spirit_1_8_x.h0"></a> 45 <span class="phrase"><a name="spirit.notes.porting_from_spirit_1_8_x.include_files"></a></span><a class="link" href="porting_from_spirit_1_8_x.html#spirit.notes.porting_from_spirit_1_8_x.include_files">Include Files</a> 46 </h5> 47<p> 48 The overall directory structure of the <a href="http://boost-spirit.com" target="_top">Spirit</a> 49 directories is described in the section <a class="link" href="../structure/include.html" title="Include">Include 50 Structure</a> and the FAQ entry <a class="link" href="../faq.html#spirit.faq.i_m_very_confused_about_the_header_hell_in_my_boost_spirit_directory__what_s_all_this_about_">Header 51 Hell</a>. This should give you a good overview on how to find the needed 52 header files for your new parsers. Moreover, each section in the <a class="link" href="../qi/reference.html" title="Reference">Qi 53 Reference</a> lists the required include files needed for any particular 54 component. 55 </p> 56<p> 57 It is possible to tell from the name of a header file, what version it belongs 58 to. While all main include files for <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 59 have the string 'classic_' in their name, for instance: 60 </p> 61<pre class="programlisting"><span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">classic_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 62</pre> 63<p> 64 we named all main include files for <span class="emphasis"><em>Spirit.Qi</em></span> to have 65 the string 'qi_' as part of their name, for instance: 66 </p> 67<pre class="programlisting"><span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">qi_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 68</pre> 69<p> 70 The following table gives a rough list of corresponding header file between 71 <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 72 and <span class="emphasis"><em>Spirit.Qi</em></span>, but this can be used as a starting point 73 only, as several components have either been moved to different submodules 74 or might not exist in the never version anymore. We list only include files 75 for the topmost submodules. For header files required for more lower level 76 components please refer to the corresponding reference documentation of this 77 component. 78 </p> 79<div class="informaltable"><table class="table"> 80<colgroup> 81<col> 82<col> 83</colgroup> 84<thead><tr> 85<th> 86 <p> 87 Include file in <span class="emphasis"><em>Spirit.Classic</em></span> 88 </p> 89 </th> 90<th> 91 <p> 92 Include file in <span class="emphasis"><em>Spirit.Qi</em></span> 93 </p> 94 </th> 95</tr></thead> 96<tbody> 97<tr> 98<td> 99 <p> 100 <code class="computeroutput"><span class="identifier">classic</span><span class="special">.</span><span class="identifier">hpp</span></code> 101 </p> 102 </td> 103<td> 104 <p> 105 <code class="computeroutput"><span class="identifier">qi</span><span class="special">.</span><span class="identifier">hpp</span></code> 106 </p> 107 </td> 108</tr> 109<tr> 110<td> 111 <p> 112 <code class="computeroutput"><span class="identifier">classic_actor</span><span class="special">.</span><span class="identifier">hpp</span></code> 113 </p> 114 </td> 115<td> 116 <p> 117 none, use <a href="../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a> 118 for writing semantic actions 119 </p> 120 </td> 121</tr> 122<tr> 123<td> 124 <p> 125 <code class="computeroutput"><span class="identifier">classic_attribute</span><span class="special">.</span><span class="identifier">hpp</span></code> 126 </p> 127 </td> 128<td> 129 <p> 130 none, use local variables for rules instead of closures, the primitives 131 parsers now directly support lazy parameterization 132 </p> 133 </td> 134</tr> 135<tr> 136<td> 137 <p> 138 <code class="computeroutput"><span class="identifier">classic_core</span><span class="special">.</span><span class="identifier">hpp</span></code> 139 </p> 140 </td> 141<td> 142 <p> 143 <code class="computeroutput"><span class="identifier">qi_core</span><span class="special">.</span><span class="identifier">hpp</span></code> 144 </p> 145 </td> 146</tr> 147<tr> 148<td> 149 <p> 150 <code class="computeroutput"><span class="identifier">classic_debug</span><span class="special">.</span><span class="identifier">hpp</span></code> 151 </p> 152 </td> 153<td> 154 <p> 155 <code class="computeroutput"><span class="identifier">qi_debug</span><span class="special">.</span><span class="identifier">hpp</span></code> 156 </p> 157 </td> 158</tr> 159<tr> 160<td> 161 <p> 162 <code class="computeroutput"><span class="identifier">classic_dynamic</span><span class="special">.</span><span class="identifier">hpp</span></code> 163 </p> 164 </td> 165<td> 166 <p> 167 none, use <span class="emphasis"><em>Spirit.Qi</em></span> predicates instead of 168 if_p, while_p, for_p (included by <code class="computeroutput"><span class="identifier">qi_core</span><span class="special">.</span><span class="identifier">hpp</span></code>), 169 the equivalent for lazy_p is now included by <code class="computeroutput"><span class="identifier">qi_auxiliary</span><span class="special">.</span><span class="identifier">hpp</span></code> 170 </p> 171 </td> 172</tr> 173<tr> 174<td> 175 <p> 176 <code class="computeroutput"><span class="identifier">classic_error_handling</span><span class="special">.</span><span class="identifier">hpp</span></code> 177 </p> 178 </td> 179<td> 180 <p> 181 none, included in <code class="computeroutput"><span class="identifier">qi_core</span><span class="special">.</span><span class="identifier">hpp</span></code> 182 </p> 183 </td> 184</tr> 185<tr> 186<td> 187 <p> 188 <code class="computeroutput"><span class="identifier">classic_meta</span><span class="special">.</span><span class="identifier">hpp</span></code> 189 </p> 190 </td> 191<td> 192 <p> 193 none 194 </p> 195 </td> 196</tr> 197<tr> 198<td> 199 <p> 200 <code class="computeroutput"><span class="identifier">classic_symbols</span><span class="special">.</span><span class="identifier">hpp</span></code> 201 </p> 202 </td> 203<td> 204 <p> 205 none, included in <code class="computeroutput"><span class="identifier">qi_core</span><span class="special">.</span><span class="identifier">hpp</span></code> 206 </p> 207 </td> 208</tr> 209<tr> 210<td> 211 <p> 212 <code class="computeroutput"><span class="identifier">classic_utility</span><span class="special">.</span><span class="identifier">hpp</span></code> 213 </p> 214 </td> 215<td> 216 <p> 217 none, not part of <span class="emphasis"><em>Spirit.Qi</em></span> anymore, these 218 components will be added over time to the <a href="../../../../repository/doc/html/index.html" target="_top">Repository</a> 219 </p> 220 </td> 221</tr> 222</tbody> 223</table></div> 224<h5> 225<a name="spirit.notes.porting_from_spirit_1_8_x.h1"></a> 226 <span class="phrase"><a name="spirit.notes.porting_from_spirit_1_8_x.the_free_parse_functions"></a></span><a class="link" href="porting_from_spirit_1_8_x.html#spirit.notes.porting_from_spirit_1_8_x.the_free_parse_functions">The 227 Free Parse Functions</a> 228 </h5> 229<p> 230 The free parse functions (i.e. the main parser API) has been changed. This 231 includes the names of the free functions as well as their interface. In 232 <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 233 all free functions were named <code class="computeroutput"><span class="identifier">parse</span></code>. 234 In <span class="emphasis"><em>Spirit.Qi</em></span> they are are named either <code class="computeroutput"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">parse</span></code> 235 or <code class="computeroutput"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span></code> depending on whether the parsing 236 should be done using a skipper (<code class="computeroutput"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span></code>) 237 or not (<code class="computeroutput"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">parse</span></code>). All free functions now return a 238 simple <code class="computeroutput"><span class="keyword">bool</span></code>. A returned <code class="computeroutput"><span class="keyword">true</span></code> means success (i.e. the parser has matched) 239 or <code class="computeroutput"><span class="keyword">false</span></code> (i.e. the parser didn't 240 match). This is equivalent to the former old <code class="computeroutput"><span class="identifier">parse_info</span></code> 241 member <code class="computeroutput"><span class="identifier">hit</span></code>. <span class="emphasis"><em>Spirit.Qi</em></span> 242 doesn't support tracking of the matched input length anymore. The old <code class="computeroutput"><span class="identifier">parse_info</span></code> member <code class="computeroutput"><span class="identifier">full</span></code> 243 can be emulated by comparing the iterators after <code class="computeroutput"><span class="identifier">qi</span><span class="special">::</span><span class="identifier">parse</span></code> 244 returned. 245 </p> 246<p> 247 All code examples in this section assume the following include statements 248 and using directives to be inserted. For <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>: 249 </p> 250<p> 251</p> 252<pre class="programlisting"><span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">classic</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 253<span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">phoenix1</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 254<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 255<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 256</pre> 257<p> 258 </p> 259<p> 260</p> 261<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">classic</span><span class="special">;</span> 262</pre> 263<p> 264 </p> 265<p> 266 and for <span class="emphasis"><em>Spirit.Qi</em></span>: 267 </p> 268<p> 269</p> 270<pre class="programlisting"><span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">qi</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 271<span class="preprocessor">#include</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">include</span><span class="special">/</span><span class="identifier">phoenix_operator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 272<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 273<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 274<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">algorithm</span><span class="special">></span> 275</pre> 276<p> 277 </p> 278<p> 279</p> 280<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">;</span> 281</pre> 282<p> 283 </p> 284<p> 285 The following similar examples should clarify the differences. First the 286 base example in <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>: 287 </p> 288<p> 289</p> 290<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"1,1"</span><span class="special">);</span> 291<span class="identifier">parse_info</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">></span> <span class="identifier">pi</span> <span class="special">=</span> <span class="identifier">parse</span><span class="special">(</span><span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">int_p</span><span class="special">);</span> 292 293<span class="keyword">if</span> <span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">hit</span><span class="special">)</span> 294 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successful match!\n"</span><span class="special">;</span> 295 296<span class="keyword">if</span> <span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">full</span><span class="special">)</span> 297 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"full match!\n"</span><span class="special">;</span> 298<span class="keyword">else</span> 299 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"stopped at: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">stop</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 300 301<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"matched length: "</span> <span class="special"><<</span> <span class="identifier">pi</span><span class="special">.</span><span class="identifier">length</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 302</pre> 303<p> 304 </p> 305<p> 306 And here is the equivalent piece of code using <span class="emphasis"><em>Spirit.Qi</em></span>: 307 </p> 308<p> 309</p> 310<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"1,1"</span><span class="special">);</span> 311<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 312<span class="keyword">bool</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">it</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">int_</span><span class="special">);</span> 313 314<span class="keyword">if</span> <span class="special">(</span><span class="identifier">result</span><span class="special">)</span> 315 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successful match!\n"</span><span class="special">;</span> 316 317<span class="keyword">if</span> <span class="special">(</span><span class="identifier">it</span> <span class="special">==</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> 318 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"full match!\n"</span><span class="special">;</span> 319<span class="keyword">else</span> 320 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"stopped at: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">it</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 321 322<span class="comment">// seldomly needed: use std::distance to calculate the length of the match</span> 323<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"matched length: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">it</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 324</pre> 325<p> 326 </p> 327<p> 328 The changes required for phrase parsing (i.e. parsing using a skipper) are 329 similar. Here is how phrase parsing works in <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>: 330 </p> 331<p> 332</p> 333<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">" 1, 1"</span><span class="special">);</span> 334<span class="identifier">parse_info</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">></span> <span class="identifier">pi</span> <span class="special">=</span> <span class="identifier">parse</span><span class="special">(</span><span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">int_p</span><span class="special">,</span> <span class="identifier">space_p</span><span class="special">);</span> 335 336<span class="keyword">if</span> <span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">hit</span><span class="special">)</span> 337 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successful match!\n"</span><span class="special">;</span> 338 339<span class="keyword">if</span> <span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">full</span><span class="special">)</span> 340 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"full match!\n"</span><span class="special">;</span> 341<span class="keyword">else</span> 342 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"stopped at: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">stop</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 343 344<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"matched length: "</span> <span class="special"><<</span> <span class="identifier">pi</span><span class="special">.</span><span class="identifier">length</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 345</pre> 346<p> 347 </p> 348<p> 349 And here the equivalent example in <span class="emphasis"><em>Spirit.Qi</em></span>: 350 </p> 351<p> 352</p> 353<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">" 1, 1"</span><span class="special">);</span> 354<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 355<span class="keyword">bool</span> <span class="identifier">result</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">it</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">int_</span><span class="special">,</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">);</span> 356 357<span class="keyword">if</span> <span class="special">(</span><span class="identifier">result</span><span class="special">)</span> 358 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successful match!\n"</span><span class="special">;</span> 359 360<span class="keyword">if</span> <span class="special">(</span><span class="identifier">it</span> <span class="special">==</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> 361 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"full match!\n"</span><span class="special">;</span> 362<span class="keyword">else</span> 363 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"stopped at: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">it</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 364 365<span class="comment">// seldomly needed: use std::distance to calculate the length of the match</span> 366<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"matched length: "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">it</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 367</pre> 368<p> 369 </p> 370<p> 371 Note, how character parsers are in a separate namespace (here <code class="computeroutput"><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</span></code>) as <span class="emphasis"><em>Spirit.Qi</em></span> 372 now supports working with different character sets. See the section <a class="link" href="../qi/reference/basics.html#spirit.qi.reference.basics.character_encoding_namespace">Character 373 Encoding Namespace</a> for more information. 374 </p> 375<h5> 376<a name="spirit.notes.porting_from_spirit_1_8_x.h2"></a> 377 <span class="phrase"><a name="spirit.notes.porting_from_spirit_1_8_x.naming_conventions"></a></span><a class="link" href="porting_from_spirit_1_8_x.html#spirit.notes.porting_from_spirit_1_8_x.naming_conventions">Naming 378 Conventions</a> 379 </h5> 380<p> 381 In <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 382 all parser primitives have suffixes appended to their names, encoding their 383 type: <code class="computeroutput"><span class="string">"_p"</span></code> for parsers, 384 <code class="computeroutput"><span class="string">"_a"</span></code> for lazy actions, 385 <code class="computeroutput"><span class="string">"_d"</span></code> for directives, 386 etc. In <span class="emphasis"><em>Spirit.Qi</em></span> we don't have anything similar. The 387 only suffixes are single underscore letters <code class="computeroutput"><span class="string">"_"</span></code> 388 applied where the name would otherwise conflict with a keyword or predefined 389 name (such as <code class="computeroutput"><span class="identifier">int_</span></code> for the 390 integer parser). Overall, most, if not all primitive parsers and directives 391 have been renamed. Please see the <a class="link" href="../qi/quick_reference.html" title="Quick Reference">Qi 392 Quick Reference</a> for an overview on the names for the different available 393 parser primitives, directives and operators. 394 </p> 395<h5> 396<a name="spirit.notes.porting_from_spirit_1_8_x.h3"></a> 397 <span class="phrase"><a name="spirit.notes.porting_from_spirit_1_8_x.parser_attributes"></a></span><a class="link" href="porting_from_spirit_1_8_x.html#spirit.notes.porting_from_spirit_1_8_x.parser_attributes">Parser 398 Attributes</a> 399 </h5> 400<p> 401 In <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 402 most of the parser primitives don't expose a specific attribute type. Most 403 parsers expose the pair of iterators pointing to the matched input sequence. 404 As in <span class="emphasis"><em>Spirit.Qi</em></span> all parsers expose a parser specific 405 attribute type it introduces a special directive <a class="link" href="../qi/reference/directive/raw.html" title="Directive for Transduction Parsing (raw[])"><code class="computeroutput"><span class="identifier">raw</span></code></a><code class="computeroutput"><span class="special">[]</span></code> 406 allowing to achieve a similar effect as in <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>. 407 The <a class="link" href="../qi/reference/directive/raw.html" title="Directive for Transduction Parsing (raw[])"><code class="computeroutput"><span class="identifier">raw</span></code></a><code class="computeroutput"><span class="special">[]</span></code> directive exposes the pair of iterators 408 pointing to the matching sequence of its embedded parser. Even if we very 409 much encourage you to rewrite your parsers to take advantage of the generated 410 parser specific attributes, sometimes it is helpful to get access to the 411 underlying matched input sequence. 412 </p> 413<h5> 414<a name="spirit.notes.porting_from_spirit_1_8_x.h4"></a> 415 <span class="phrase"><a name="spirit.notes.porting_from_spirit_1_8_x.grammars_and_rules"></a></span><a class="link" href="porting_from_spirit_1_8_x.html#spirit.notes.porting_from_spirit_1_8_x.grammars_and_rules">Grammars 416 and Rules</a> 417 </h5> 418<p> 419 The <code class="computeroutput"><span class="identifier">grammar</span><span class="special"><></span></code> 420 and <code class="computeroutput"><span class="identifier">rule</span><span class="special"><></span></code> 421 types are of equal importance to <span class="emphasis"><em>Spirit.Qi</em></span> as they are 422 for <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>. 423 Their main purpose is still the same: they allow to define non-terminals 424 and they are the main building blocks for more complex parsers. Nevertheless, 425 both types have been redesigned and their interfaces have changed. Let's 426 have a look at two examples first, we'll explain the differences afterwards. 427 Here is a simple grammar and its usage in <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a>: 428 </p> 429<p> 430</p> 431<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">roman</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">grammar</span><span class="special"><</span><span class="identifier">roman</span><span class="special">></span> 432<span class="special">{</span> 433 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">ScannerT</span><span class="special">></span> 434 <span class="keyword">struct</span> <span class="identifier">definition</span> 435 <span class="special">{</span> 436 <span class="identifier">definition</span><span class="special">(</span><span class="identifier">roman</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">self</span><span class="special">)</span> 437 <span class="special">{</span> 438 <span class="identifier">hundreds</span><span class="special">.</span><span class="identifier">add</span> 439 <span class="special">(</span><span class="string">"C"</span> <span class="special">,</span> <span class="number">100</span><span class="special">)(</span><span class="string">"CC"</span> <span class="special">,</span> <span class="number">200</span><span class="special">)(</span><span class="string">"CCC"</span> <span class="special">,</span> <span class="number">300</span><span class="special">)(</span><span class="string">"CD"</span> <span class="special">,</span> <span class="number">400</span><span class="special">)(</span><span class="string">"D"</span> <span class="special">,</span> <span class="number">500</span><span class="special">)</span> 440 <span class="special">(</span><span class="string">"DC"</span> <span class="special">,</span> <span class="number">600</span><span class="special">)(</span><span class="string">"DCC"</span> <span class="special">,</span> <span class="number">700</span><span class="special">)(</span><span class="string">"DCCC"</span> <span class="special">,</span> <span class="number">800</span><span class="special">)(</span><span class="string">"CM"</span> <span class="special">,</span> <span class="number">900</span><span class="special">)</span> <span class="special">;</span> 441 442 <span class="identifier">tens</span><span class="special">.</span><span class="identifier">add</span> 443 <span class="special">(</span><span class="string">"X"</span> <span class="special">,</span> <span class="number">10</span><span class="special">)(</span><span class="string">"XX"</span> <span class="special">,</span> <span class="number">20</span><span class="special">)(</span><span class="string">"XXX"</span> <span class="special">,</span> <span class="number">30</span><span class="special">)(</span><span class="string">"XL"</span> <span class="special">,</span> <span class="number">40</span><span class="special">)(</span><span class="string">"L"</span> <span class="special">,</span> <span class="number">50</span><span class="special">)</span> 444 <span class="special">(</span><span class="string">"LX"</span> <span class="special">,</span> <span class="number">60</span><span class="special">)(</span><span class="string">"LXX"</span> <span class="special">,</span> <span class="number">70</span><span class="special">)(</span><span class="string">"LXXX"</span> <span class="special">,</span> <span class="number">80</span><span class="special">)(</span><span class="string">"XC"</span> <span class="special">,</span> <span class="number">90</span><span class="special">)</span> <span class="special">;</span> 445 446 <span class="identifier">ones</span><span class="special">.</span><span class="identifier">add</span> 447 <span class="special">(</span><span class="string">"I"</span> <span class="special">,</span> <span class="number">1</span><span class="special">)(</span><span class="string">"II"</span> <span class="special">,</span> <span class="number">2</span><span class="special">)(</span><span class="string">"III"</span> <span class="special">,</span> <span class="number">3</span><span class="special">)(</span><span class="string">"IV"</span> <span class="special">,</span> <span class="number">4</span><span class="special">)(</span><span class="string">"V"</span> <span class="special">,</span> <span class="number">5</span><span class="special">)</span> 448 <span class="special">(</span><span class="string">"VI"</span> <span class="special">,</span> <span class="number">6</span><span class="special">)(</span><span class="string">"VII"</span> <span class="special">,</span> <span class="number">7</span><span class="special">)(</span><span class="string">"VIII"</span> <span class="special">,</span> <span class="number">8</span><span class="special">)(</span><span class="string">"IX"</span> <span class="special">,</span> <span class="number">9</span><span class="special">)</span> <span class="special">;</span> 449 450 <span class="identifier">first</span> <span class="special">=</span> <span class="identifier">eps_p</span> <span class="special">[</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">var</span><span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">r</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">val</span><span class="special">(</span><span class="number">0</span><span class="special">)]</span> 451 <span class="special">>></span> <span class="special">(</span> <span class="special">+</span><span class="identifier">ch_p</span><span class="special">(</span><span class="char">'M'</span><span class="special">)</span> <span class="special">[</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">var</span><span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">r</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">val</span><span class="special">(</span><span class="number">1000</span><span class="special">)]</span> 452 <span class="special">||</span> <span class="identifier">hundreds</span> <span class="special">[</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">var</span><span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">r</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 453 <span class="special">||</span> <span class="identifier">tens</span> <span class="special">[</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">var</span><span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">r</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 454 <span class="special">||</span> <span class="identifier">ones</span> <span class="special">[</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">var</span><span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">r</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 455 <span class="special">)</span> <span class="special">;</span> 456 <span class="special">}</span> 457 458 <span class="identifier">rule</span><span class="special"><</span><span class="identifier">ScannerT</span><span class="special">></span> <span class="identifier">first</span><span class="special">;</span> 459 <span class="identifier">symbols</span><span class="special"><</span><span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">hundreds</span><span class="special">;</span> 460 <span class="identifier">symbols</span><span class="special"><</span><span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">tens</span><span class="special">;</span> 461 <span class="identifier">symbols</span><span class="special"><</span><span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">ones</span><span class="special">;</span> 462 463 <span class="identifier">rule</span><span class="special"><</span><span class="identifier">ScannerT</span><span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">start</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">first</span><span class="special">;</span> <span class="special">}</span> 464 <span class="special">};</span> 465 466 <span class="identifier">roman</span><span class="special">(</span><span class="keyword">unsigned</span><span class="special">&</span> <span class="identifier">r_</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">r</span><span class="special">(</span><span class="identifier">r_</span><span class="special">)</span> <span class="special">{}</span> 467 <span class="keyword">unsigned</span><span class="special">&</span> <span class="identifier">r</span><span class="special">;</span> 468<span class="special">};</span> 469</pre> 470<p> 471 </p> 472<p> 473</p> 474<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"MMIX"</span><span class="special">);</span> <span class="comment">// MMIX == 2009</span> 475<span class="keyword">unsigned</span> <span class="identifier">value</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 476<span class="identifier">roman</span> <span class="identifier">r</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span> 477<span class="identifier">parse_info</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">></span> <span class="identifier">pi</span> <span class="special">=</span> <span class="identifier">parse</span><span class="special">(</span><span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">r</span><span class="special">);</span> 478<span class="keyword">if</span> <span class="special">(</span><span class="identifier">pi</span><span class="special">.</span><span class="identifier">hit</span><span class="special">)</span> 479 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successfully matched: "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 480</pre> 481<p> 482 </p> 483<p> 484 And here is a similar grammar and its usage in <span class="emphasis"><em>Spirit.Qi</em></span>: 485 </p> 486<p> 487</p> 488<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">></span> 489<span class="keyword">struct</span> <span class="identifier">roman</span> <span class="special">:</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">grammar</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">()></span> 490<span class="special">{</span> 491 <span class="identifier">roman</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">roman</span><span class="special">::</span><span class="identifier">base_type</span><span class="special">(</span><span class="identifier">first</span><span class="special">)</span> 492 <span class="special">{</span> 493 <span class="identifier">hundreds</span><span class="special">.</span><span class="identifier">add</span> 494 <span class="special">(</span><span class="string">"C"</span> <span class="special">,</span> <span class="number">100</span><span class="special">)(</span><span class="string">"CC"</span> <span class="special">,</span> <span class="number">200</span><span class="special">)(</span><span class="string">"CCC"</span> <span class="special">,</span> <span class="number">300</span><span class="special">)(</span><span class="string">"CD"</span> <span class="special">,</span> <span class="number">400</span><span class="special">)(</span><span class="string">"D"</span> <span class="special">,</span> <span class="number">500</span><span class="special">)</span> 495 <span class="special">(</span><span class="string">"DC"</span> <span class="special">,</span> <span class="number">600</span><span class="special">)(</span><span class="string">"DCC"</span> <span class="special">,</span> <span class="number">700</span><span class="special">)(</span><span class="string">"DCCC"</span> <span class="special">,</span> <span class="number">800</span><span class="special">)(</span><span class="string">"CM"</span> <span class="special">,</span> <span class="number">900</span><span class="special">)</span> <span class="special">;</span> 496 497 <span class="identifier">tens</span><span class="special">.</span><span class="identifier">add</span> 498 <span class="special">(</span><span class="string">"X"</span> <span class="special">,</span> <span class="number">10</span><span class="special">)(</span><span class="string">"XX"</span> <span class="special">,</span> <span class="number">20</span><span class="special">)(</span><span class="string">"XXX"</span> <span class="special">,</span> <span class="number">30</span><span class="special">)(</span><span class="string">"XL"</span> <span class="special">,</span> <span class="number">40</span><span class="special">)(</span><span class="string">"L"</span> <span class="special">,</span> <span class="number">50</span><span class="special">)</span> 499 <span class="special">(</span><span class="string">"LX"</span> <span class="special">,</span> <span class="number">60</span><span class="special">)(</span><span class="string">"LXX"</span> <span class="special">,</span> <span class="number">70</span><span class="special">)(</span><span class="string">"LXXX"</span> <span class="special">,</span> <span class="number">80</span><span class="special">)(</span><span class="string">"XC"</span> <span class="special">,</span> <span class="number">90</span><span class="special">)</span> <span class="special">;</span> 500 501 <span class="identifier">ones</span><span class="special">.</span><span class="identifier">add</span> 502 <span class="special">(</span><span class="string">"I"</span> <span class="special">,</span> <span class="number">1</span><span class="special">)(</span><span class="string">"II"</span> <span class="special">,</span> <span class="number">2</span><span class="special">)(</span><span class="string">"III"</span> <span class="special">,</span> <span class="number">3</span><span class="special">)(</span><span class="string">"IV"</span> <span class="special">,</span> <span class="number">4</span><span class="special">)(</span><span class="string">"V"</span> <span class="special">,</span> <span class="number">5</span><span class="special">)</span> 503 <span class="special">(</span><span class="string">"VI"</span> <span class="special">,</span> <span class="number">6</span><span class="special">)(</span><span class="string">"VII"</span> <span class="special">,</span> <span class="number">7</span><span class="special">)(</span><span class="string">"VIII"</span> <span class="special">,</span> <span class="number">8</span><span class="special">)(</span><span class="string">"IX"</span> <span class="special">,</span> <span class="number">9</span><span class="special">)</span> <span class="special">;</span> 504 505 <span class="comment">// qi::_val refers to the attribute of the rule on the left hand side </span> 506 <span class="identifier">first</span> <span class="special">=</span> <span class="identifier">eps</span> <span class="special">[</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span> <span class="special">=</span> <span class="number">0</span><span class="special">]</span> 507 <span class="special">>></span> <span class="special">(</span> <span class="special">+</span><span class="identifier">lit</span><span class="special">(</span><span class="char">'M'</span><span class="special">)</span> <span class="special">[</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span> <span class="special">+=</span> <span class="number">1000</span><span class="special">]</span> 508 <span class="special">||</span> <span class="identifier">hundreds</span> <span class="special">[</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span> <span class="special">+=</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 509 <span class="special">||</span> <span class="identifier">tens</span> <span class="special">[</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span> <span class="special">+=</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 510 <span class="special">||</span> <span class="identifier">ones</span> <span class="special">[</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span> <span class="special">+=</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">_1</span><span class="special">]</span> 511 <span class="special">)</span> <span class="special">;</span> 512 <span class="special">}</span> 513 514 <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">unsigned</span><span class="special">()></span> <span class="identifier">first</span><span class="special">;</span> 515 <span class="identifier">qi</span><span class="special">::</span><span class="identifier">symbols</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">hundreds</span><span class="special">;</span> 516 <span class="identifier">qi</span><span class="special">::</span><span class="identifier">symbols</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">tens</span><span class="special">;</span> 517 <span class="identifier">qi</span><span class="special">::</span><span class="identifier">symbols</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">></span> <span class="identifier">ones</span><span class="special">;</span> 518<span class="special">};</span> 519</pre> 520<p> 521 </p> 522<p> 523</p> 524<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"MMIX"</span><span class="special">);</span> <span class="comment">// MMIX == 2009</span> 525<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 526<span class="keyword">unsigned</span> <span class="identifier">value</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 527<span class="identifier">roman</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">></span> <span class="identifier">r</span><span class="special">;</span> 528<span class="keyword">if</span> <span class="special">(</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">it</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">r</span><span class="special">,</span> <span class="identifier">value</span><span class="special">))</span> 529 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"successfully matched: "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> 530</pre> 531<p> 532 </p> 533<p> 534 Both versions look similar enough, but we see several differences (we will 535 cover each of those differences in more detail below): 536 </p> 537<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 538<li class="listitem"> 539 Neither the grammars nor the rules depend on a scanner type anymore, 540 both depend only on the underlying iterator type. That means the dreaded 541 scanner business is no issue anymore! 542 </li> 543<li class="listitem"> 544 Grammars have no embedded class <code class="computeroutput"><span class="identifier">definition</span></code> 545 anymore 546 </li> 547<li class="listitem"> 548 Grammars and rules may have an explicit attribute type specified in their 549 definition 550 </li> 551<li class="listitem"> 552 Grammars do not have any explicit start rules anymore. Instead one of 553 the contained rules is used as a start rule by default. 554 </li> 555</ul></div> 556<p> 557 The first two points are tightly interrelated. The scanner business (see 558 the FAQ number one of <a href="../../../../../../libs/spirit/classic/index.html" target="_top"><span class="emphasis"><em>Spirit.Classic</em></span></a> 559 here: <a href="../../../../../../libs/spirit/classic/doc/faq.html#scanner_business" target="_top">The 560 Scanner Business</a>) has been a problem for a long time. The grammar 561 and rule types have been specifically redesigned to avoid this problem in 562 the future. This also means that we don't need any delayed instantiation 563 of the inner definition class in a grammar anymore. So the redesign not only 564 helped fixing a long standing design problem, it helped to simplify things 565 considerably. 566 </p> 567<p> 568 All <span class="emphasis"><em>Spirit.Qi</em></span> parser components have well defined attribute 569 types. Grammars and rules are no exception. But since both need to be generic 570 enough to be usable for any parser their attribute type has to be explicitly 571 specified. In the example above the <code class="computeroutput"><span class="identifier">roman</span></code> 572 grammar and the rule <code class="computeroutput"><span class="identifier">first</span></code> 573 both have an <code class="computeroutput"><span class="keyword">unsigned</span></code> attribute: 574 </p> 575<pre class="programlisting"><span class="comment">// grammar definition</span> 576<span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">></span> 577<span class="keyword">struct</span> <span class="identifier">roman</span> <span class="special">:</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">grammar</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">,</span> <span class="keyword">unsigned</span><span class="special">()></span> <span class="special">{...};</span> 578 579<span class="comment">// rule definition</span> 580<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">unsigned</span><span class="special">()></span> <span class="identifier">first</span><span class="special">;</span> 581</pre> 582<p> 583 The used notation resembles the definition of a function type. This is very 584 natural as you can think of the synthesized attribute of the grammar and 585 the rule as of its 'return value'. In fact the rule and the grammar both 586 'return' an unsigned value - the value they matched. 587 </p> 588<div class="note"><table border="0" summary="Note"> 589<tr> 590<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td> 591<th align="left">Note</th> 592</tr> 593<tr><td align="left" valign="top"><p> 594 The function type notation allows to specify parameters as well. These 595 are interpreted as the types of inherited attributes the rule or grammar 596 expect to be passed during parsing. For more information please see the 597 section about inherited and synthesized attributes for rules and grammars 598 (<a class="link" href="../abstracts/attributes.html" title="Attributes">Attributes</a>). 599 </p></td></tr> 600</table></div> 601<p> 602 If no attribute is desired none needs to be specified. The default attribute 603 type for both, grammars and rules, is <code class="computeroutput"><span class="identifier">unused_type</span></code>, 604 which is a special placeholder type. Generally, using <code class="computeroutput"><span class="identifier">unused_type</span></code> 605 as the attribute of a parser is interpreted as 'this parser has no attribute'. 606 This is mostly used for parsers applied to parts of the input not carrying 607 any significant information, rather being delimiters or structural elements 608 needed for correct interpretation of the input. 609 </p> 610<p> 611 The last difference might seem to be rather cosmetic and insignificant. But 612 it turns out that not having to specify which rule in a grammar is the start 613 rule (by returning it from the function <code class="computeroutput"><span class="identifier">start</span><span class="special">()</span></code>) also means that any rule in a grammar 614 can be directly used as the start rule. Nevertheless, the grammar base class 615 gets initialized with the rule it has to use as the start rule in case the 616 grammar instance is directly used as a parser. 617 </p> 618</div> 619<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 620<td align="left"></td> 621<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 622 Distributed under the Boost Software License, Version 1.0. (See accompanying 623 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>) 624 </p> 625</div></td> 626</tr></table> 627<hr> 628<div class="spirit-nav"> 629<a accesskey="p" href="../notes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../notes.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="style_guide.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 630</div> 631</body> 632</html> 633