1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Qi subrules</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 Repository 0.2"> 8<link rel="up" href="../nonterminal.html" title="Qi Parser Non-terminals"> 9<link rel="prev" href="../nonterminal.html" title="Qi Parser Non-terminals"> 10<link rel="next" href="../operators.html" title="Qi Parser Operators"> 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="../nonterminal.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../operators.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_repository.qi_components.nonterminal.subrule"></a><a class="link" href="subrule.html" title="Qi subrules">Qi 28 subrules</a> 29</h4></div></div></div> 30<h6> 31<a name="spirit_repository.qi_components.nonterminal.subrule.h0"></a> 32 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.description"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.description">Description</a> 33 </h6> 34<p> 35 The <span class="emphasis"><em>Spirit.Qi</em></span> <code class="computeroutput"><span class="identifier">subrule</span></code> 36 is a component allowing to create a named parser, and to refer to it by 37 name -- much like rules and grammars. It is in fact a fully static version 38 of the rule. 39 </p> 40<p> 41 The strength of subrules is performance. Replacing some rules with subrules 42 can make a parser slightly faster (see <a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.performance">Performance</a> 43 below for measurements). The reason is that subrules allow aggressive inlining 44 by the C++ compiler, whereas the implementation of rules is based on a 45 virtual function call which, depending on the compiler, can have some run-time 46 overhead and stop inlining. 47 </p> 48<p> 49 The weaknesses of subrules are: 50 </p> 51<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 52<li class="listitem"> 53 subrules can only be defined and used within the same parser expression. 54 A subrule cannot be defined at one location, and then used in another 55 location. 56 </li> 57<li class="listitem"> 58 subrules put a massive strain on the C++ compiler. They increase compile 59 times and memory usage during compilation, and also increase the risk 60 of hitting compiler limits and/or bugs. 61 </li> 62</ul></div> 63<p> 64</p> 65<pre class="programlisting"><span class="identifier">entry</span> <span class="special">=</span> <span class="special">(</span> 66 <span class="identifier">expression</span> <span class="special">=</span> 67 <span class="identifier">term</span> 68 <span class="special">>></span> <span class="special">*(</span> <span class="special">(</span><span class="char">'+'</span> <span class="special">>></span> <span class="identifier">term</span><span class="special">)</span> 69 <span class="special">|</span> <span class="special">(</span><span class="char">'-'</span> <span class="special">>></span> <span class="identifier">term</span><span class="special">)</span> 70 <span class="special">)</span> 71 72 <span class="special">,</span> <span class="identifier">term</span> <span class="special">=</span> 73 <span class="identifier">factor</span> 74 <span class="special">>></span> <span class="special">*(</span> <span class="special">(</span><span class="char">'*'</span> <span class="special">>></span> <span class="identifier">factor</span><span class="special">)</span> 75 <span class="special">|</span> <span class="special">(</span><span class="char">'/'</span> <span class="special">>></span> <span class="identifier">factor</span><span class="special">)</span> 76 <span class="special">)</span> 77 78 <span class="special">,</span> <span class="identifier">factor</span> <span class="special">=</span> 79 <span class="identifier">uint_</span> 80 <span class="special">|</span> <span class="char">'('</span> <span class="special">>></span> <span class="identifier">expression</span> <span class="special">>></span> <span class="char">')'</span> 81 <span class="special">|</span> <span class="special">(</span><span class="char">'-'</span> <span class="special">>></span> <span class="identifier">factor</span><span class="special">)</span> 82 <span class="special">|</span> <span class="special">(</span><span class="char">'+'</span> <span class="special">>></span> <span class="identifier">factor</span><span class="special">)</span> 83<span class="special">);</span> 84</pre> 85<p> 86 </p> 87<p> 88 The example above can be found here: <a href="../../../../../example/qi/calc1_sr.cpp" target="_top">../../example/qi/calc1_sr.cpp</a> 89 </p> 90<p> 91 As shown in this code snippet (an extract from the calc1_sr example), subrules 92 can be freely mixed with rules and grammars. Here, a group of 3 subrules 93 (<code class="computeroutput"><span class="identifier">expression</span></code>, <code class="computeroutput"><span class="identifier">term</span></code>, <code class="computeroutput"><span class="identifier">factor</span></code>) 94 is assigned to a rule (named <code class="computeroutput"><span class="identifier">entry</span></code>). 95 This means that parts of a parser can use subrules (typically the innermost, 96 most performance-critical parts), whereas the rest can use rules and grammars. 97 </p> 98<h6> 99<a name="spirit_repository.qi_components.nonterminal.subrule.h1"></a> 100 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.header"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.header">Header</a> 101 </h6> 102<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/repository/home/qi/nonterminal/subrule.hpp></span> 103<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">repository</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">qi_subrule</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 104</pre> 105<h6> 106<a name="spirit_repository.qi_components.nonterminal.subrule.h2"></a> 107 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.synopsis__declaration_"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.synopsis__declaration_">Synopsis 108 (declaration)</a> 109 </h6> 110<pre class="programlisting"><span class="identifier">subrule</span><span class="special"><</span><span class="identifier">ID</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">></span> <span class="identifier">sr</span><span class="special">(</span><span class="identifier">name</span><span class="special">);</span> 111</pre> 112<h6> 113<a name="spirit_repository.qi_components.nonterminal.subrule.h3"></a> 114 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.parameters__declaration_"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.parameters__declaration_">Parameters 115 (declaration)</a> 116 </h6> 117<div class="informaltable"><table class="table"> 118<colgroup> 119<col> 120<col> 121</colgroup> 122<thead><tr> 123<th> 124 <p> 125 Parameter 126 </p> 127 </th> 128<th> 129 <p> 130 Description 131 </p> 132 </th> 133</tr></thead> 134<tbody> 135<tr> 136<td> 137 <p> 138 <code class="computeroutput"><span class="identifier">ID</span></code> 139 </p> 140 </td> 141<td> 142 <p> 143 Required numeric argument. Gives the subrule a unique 'identification 144 tag'. 145 </p> 146 </td> 147</tr> 148<tr> 149<td> 150 <p> 151 <code class="computeroutput"><span class="identifier">A1</span></code>, <code class="computeroutput"><span class="identifier">A2</span></code> 152 </p> 153 </td> 154<td> 155 <p> 156 Optional types, can be specified in any order. Can be one of 157 1. signature, 2. locals (see rules reference for more information 158 on those parameters). 159 </p> 160 <p> 161 Note that the skipper type need not be specified in the parameters, 162 unlike with grammars and rules. Subrules will automatically use 163 the skipper type which is in effect when they are invoked. 164 </p> 165 </td> 166</tr> 167<tr> 168<td> 169 <p> 170 <code class="computeroutput"><span class="identifier">name</span></code> 171 </p> 172 </td> 173<td> 174 <p> 175 Optional string. Gives the subrule a name, useful for debugging 176 and error handling. 177 </p> 178 </td> 179</tr> 180</tbody> 181</table></div> 182<h6> 183<a name="spirit_repository.qi_components.nonterminal.subrule.h4"></a> 184 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.synopsis__usage_"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.synopsis__usage_">Synopsis 185 (usage)</a> 186 </h6> 187<p> 188 Subrules are defined and used within groups, typically (and by convention) 189 enclosed inside parentheses. 190 </p> 191<pre class="programlisting"><span class="comment">// Group containing N subrules</span> 192<span class="special">(</span> 193 <span class="identifier">sr1</span> <span class="special">=</span> <span class="identifier">expr1</span> 194 <span class="special">,</span> <span class="identifier">sr2</span> <span class="special">=</span> <span class="identifier">expr2</span> 195 <span class="special">,</span> <span class="special">...</span> <span class="comment">// Any number of subrules</span> 196<span class="special">}</span> 197</pre> 198<p> 199 The IDs of all subrules defined within the same group must be different. 200 It is an error to define several subrules with the same ID (or to define 201 the same subrule multiple times) in the same group. 202 </p> 203<pre class="programlisting"><span class="comment">// Auto-subrules and inherited attributes</span> 204<span class="special">(</span> 205 <span class="identifier">srA</span> <span class="special">%=</span> <span class="identifier">exprA</span> <span class="special">>></span> <span class="identifier">srB</span> <span class="special">>></span> <span class="identifier">srC</span><span class="special">(</span><span class="identifier">c1</span><span class="special">,</span> <span class="identifier">c2</span><span class="special">,</span> <span class="special">...)</span> <span class="comment">// Arguments to subrule srC</span> 206 <span class="special">,</span> <span class="identifier">srB</span> <span class="special">%=</span> <span class="identifier">exprB</span> 207 <span class="special">,</span> <span class="identifier">srC</span> <span class="special">=</span> <span class="identifier">exprC</span> 208 <span class="special">,</span> <span class="special">...</span> 209<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="comment">// Arguments to group, i.e. to start subrule srA</span> 210</pre> 211<h6> 212<a name="spirit_repository.qi_components.nonterminal.subrule.h5"></a> 213 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.parameters__usage_"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.parameters__usage_">Parameters 214 (usage)</a> 215 </h6> 216<div class="informaltable"><table class="table"> 217<colgroup> 218<col> 219<col> 220</colgroup> 221<thead><tr> 222<th> 223 <p> 224 Parameter 225 </p> 226 </th> 227<th> 228 <p> 229 Description 230 </p> 231 </th> 232</tr></thead> 233<tbody> 234<tr> 235<td> 236 <p> 237 <code class="computeroutput"><span class="identifier">sr1</span></code>, <code class="computeroutput"><span class="identifier">sr2</span></code> 238 </p> 239 </td> 240<td> 241 <p> 242 Subrules with different IDs. 243 </p> 244 </td> 245</tr> 246<tr> 247<td> 248 <p> 249 <code class="computeroutput"><span class="identifier">expr1</span></code>, <code class="computeroutput"><span class="identifier">expr2</span></code> 250 </p> 251 </td> 252<td> 253 <p> 254 Parser expressions. Can include <code class="computeroutput"><span class="identifier">sr1</span></code> 255 and <code class="computeroutput"><span class="identifier">sr2</span></code>, as well 256 as any other valid parser expressions. 257 </p> 258 </td> 259</tr> 260<tr> 261<td> 262 <p> 263 <code class="computeroutput"><span class="identifier">srA</span></code> 264 </p> 265 </td> 266<td> 267 <p> 268 Subrule with a synthesized attribute and inherited attributes. 269 </p> 270 </td> 271</tr> 272<tr> 273<td> 274 <p> 275 <code class="computeroutput"><span class="identifier">srB</span></code> 276 </p> 277 </td> 278<td> 279 <p> 280 Subrule with a synthesized attribute. 281 </p> 282 </td> 283</tr> 284<tr> 285<td> 286 <p> 287 <code class="computeroutput"><span class="identifier">srC</span></code> 288 </p> 289 </td> 290<td> 291 <p> 292 Subrule with inherited attributes. 293 </p> 294 </td> 295</tr> 296<tr> 297<td> 298 <p> 299 <code class="computeroutput"><span class="identifier">exprA</span></code>, <code class="computeroutput"><span class="identifier">exprB</span></code>, <code class="computeroutput"><span class="identifier">exprC</span></code> 300 </p> 301 </td> 302<td> 303 <p> 304 Parser expressions. 305 </p> 306 </td> 307</tr> 308<tr> 309<td> 310 <p> 311 <code class="computeroutput"><span class="identifier">a1</span></code>, <code class="computeroutput"><span class="identifier">a2</span></code> 312 </p> 313 </td> 314<td> 315 <p> 316 Arguments passed to the subrule group. They are passed as inherited 317 attributes to the group's start subrule, <code class="computeroutput"><span class="identifier">srA</span></code>. 318 </p> 319 </td> 320</tr> 321<tr> 322<td> 323 <p> 324 <code class="computeroutput"><span class="identifier">c1</span></code>, <code class="computeroutput"><span class="identifier">c2</span></code> 325 </p> 326 </td> 327<td> 328 <p> 329 Arguments passed as inherited attributes to subrule <code class="computeroutput"><span class="identifier">srC</span></code>. 330 </p> 331 </td> 332</tr> 333</tbody> 334</table></div> 335<h6> 336<a name="spirit_repository.qi_components.nonterminal.subrule.h6"></a> 337 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.groups"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.groups">Groups</a> 338 </h6> 339<p> 340 A subrule group (a set of subrule definitions) is a parser, which can be 341 used anywhere in a parser expression (in assignments to rules, as well 342 as directly in arguments to functions such as <code class="computeroutput"><span class="identifier">parse</span></code>). 343 In a group, parsing proceeds from the start subrule, which is the first 344 (topmost) subrule defined in that group. In the two groups in the synopsis 345 above, <code class="computeroutput"><span class="identifier">sr1</span></code> and <code class="computeroutput"><span class="identifier">srA</span></code> are the start subrules respectively 346 -- for example when the first subrule group is called forth, the <code class="computeroutput"><span class="identifier">sr1</span></code> subrule is called. 347 </p> 348<p> 349 A subrule can only be used in a group which defines it. Groups can be viewed 350 as scopes: a definition of a subrule is limited to its enclosing group. 351 </p> 352<pre class="programlisting"><span class="identifier">rule</span><span class="special"><</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*></span> <span class="identifier">r1</span><span class="special">,</span> <span class="identifier">r2</span><span class="special">,</span> <span class="identifier">r3</span><span class="special">;</span> 353<span class="identifier">subrule</span><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="identifier">sr1</span><span class="special">;</span> 354<span class="identifier">subrule</span><span class="special"><</span><span class="number">2</span><span class="special">></span> <span class="identifier">sr2</span><span class="special">;</span> 355 356<span class="identifier">r1</span> <span class="special">=</span> 357 <span class="special">(</span> <span class="identifier">sr1</span> <span class="special">=</span> <span class="char">'a'</span> <span class="special">>></span> <span class="identifier">int_</span> <span class="special">)</span> <span class="comment">// First group in r1.</span> 358 <span class="special">>></span> <span class="special">(</span> <span class="identifier">sr2</span> <span class="special">=</span> <span class="special">+</span><span class="identifier">sr1</span> <span class="special">)</span> <span class="comment">// Second group in r1.</span> 359 <span class="comment">// ^^^</span> 360 <span class="comment">// DOES NOT COMPILE: sr1 is not defined in this</span> 361 <span class="comment">// second group, it cannot be used here (its</span> 362 <span class="comment">// previous definition is out of scope).</span> 363<span class="special">;</span> 364 365<span class="identifier">r2</span> <span class="special">=</span> 366 <span class="special">(</span> <span class="identifier">sr1</span> <span class="special">=</span> <span class="char">'a'</span> <span class="special">>></span> <span class="identifier">int_</span> <span class="special">)</span> <span class="comment">// Only group in r2.</span> 367 <span class="special">>></span> <span class="identifier">sr1</span> 368 <span class="comment">// ^^^</span> 369 <span class="comment">// DOES NOT COMPILE: not in a subrule group,</span> 370 <span class="comment">// sr1 cannot be used here (here too, its</span> 371 <span class="comment">// previous definition is out of scope).</span> 372<span class="special">;</span> 373 374<span class="identifier">r3</span> <span class="special">=</span> 375 <span class="special">(</span> <span class="identifier">sr1</span> <span class="special">=</span> <span class="char">'x'</span> <span class="special">>></span> <span class="identifier">double_</span> <span class="special">)</span> <span class="comment">// Another group. The same subrule `sr1`</span> 376 <span class="comment">// can have another, independent</span> 377 <span class="comment">// definition in this group.</span> 378<span class="special">;</span> 379</pre> 380<h6> 381<a name="spirit_repository.qi_components.nonterminal.subrule.h7"></a> 382 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.attributes"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.attributes">Attributes</a> 383 </h6> 384<p> 385 A subrule has the same behavior as a rule with respect to attributes. In 386 particular: 387 </p> 388<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 389<li class="listitem"> 390 the type of its synthesized attribute is the one specified in the subrule's 391 signature, if any. Otherwise it is <code class="computeroutput"><span class="identifier">unused_type</span></code>. 392 </li> 393<li class="listitem"> 394 the types of its inherited attributes are the ones specified in the 395 subrule's signature, if any. Otherwise the subrule has no inherited 396 attributes. 397 </li> 398<li class="listitem"> 399 an auto-subrule can be defined by assigning it with the <code class="computeroutput"><span class="special">%=</span></code> syntax. In this case, the RHS parser's 400 attribute is automatically propagated to the subrule's synthesized 401 attribute. 402 </li> 403<li class="listitem"> 404 the Phoenix placeholders <code class="computeroutput"><span class="identifier">_val</span></code>, 405 <code class="computeroutput"><span class="identifier">_r1</span></code>, <code class="computeroutput"><span class="identifier">_r2</span></code>, ... are available to refer to 406 the subrule's synthesized and inherited attributes, if present. 407 </li> 408</ul></div> 409<h6> 410<a name="spirit_repository.qi_components.nonterminal.subrule.h8"></a> 411 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.locals"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.locals">Locals</a> 412 </h6> 413<p> 414 A subrule has the same behavior as a rule with respect to locals. In particular, 415 the Phoenix placeholders <code class="computeroutput"><span class="identifier">_a</span></code>, 416 <code class="computeroutput"><span class="identifier">_b</span></code>, ... are available to 417 refer to the subrule's locals, if present. 418 </p> 419<h6> 420<a name="spirit_repository.qi_components.nonterminal.subrule.h9"></a> 421 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.example"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.example">Example</a> 422 </h6> 423<p> 424 Some includes: 425 </p> 426<p> 427</p> 428<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> 429<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">repository</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">qi_subrule</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 430<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_core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 431<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> 432</pre> 433<p> 434 </p> 435<p> 436 Some using declarations: 437 </p> 438<p> 439</p> 440<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">qi</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">qi</span><span class="special">;</span> 441<span class="keyword">namespace</span> <span class="identifier">repo</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">repository</span><span class="special">;</span> 442<span class="keyword">namespace</span> <span class="identifier">ascii</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">ascii</span><span class="special">;</span> 443</pre> 444<p> 445 </p> 446<p> 447 A grammar containing only one rule, defined with a group of 5 subrules: 448 </p> 449<p> 450</p> 451<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> 452<span class="keyword">struct</span> <span class="identifier">mini_xml_grammar</span> 453 <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="identifier">mini_xml</span><span class="special">(),</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space_type</span><span class="special">></span> 454<span class="special">{</span> 455 <span class="identifier">mini_xml_grammar</span><span class="special">()</span> 456 <span class="special">:</span> <span class="identifier">mini_xml_grammar</span><span class="special">::</span><span class="identifier">base_type</span><span class="special">(</span><span class="identifier">entry</span><span class="special">)</span> 457 <span class="special">{</span> 458 <span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">lit</span><span class="special">;</span> 459 <span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">lexeme</span><span class="special">;</span> 460 <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">char_</span><span class="special">;</span> 461 <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">string</span><span class="special">;</span> 462 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">labels</span><span class="special">;</span> 463 464 <span class="identifier">entry</span> <span class="special">%=</span> <span class="special">(</span> 465 <span class="identifier">xml</span> <span class="special">%=</span> 466 <span class="identifier">start_tag</span><span class="special">[</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">_1</span><span class="special">]</span> 467 <span class="special">>></span> <span class="special">*</span><span class="identifier">node</span> 468 <span class="special">>></span> <span class="identifier">end_tag</span><span class="special">(</span><span class="identifier">_a</span><span class="special">)</span> 469 470 <span class="special">,</span> <span class="identifier">node</span> <span class="special">%=</span> <span class="identifier">xml</span> <span class="special">|</span> <span class="identifier">text</span> 471 472 <span class="special">,</span> <span class="identifier">text</span> <span class="special">%=</span> <span class="identifier">lexeme</span><span class="special">[+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'<'</span><span class="special">)]</span> 473 474 <span class="special">,</span> <span class="identifier">start_tag</span> <span class="special">%=</span> 475 <span class="char">'<'</span> 476 <span class="special">>></span> <span class="special">!</span><span class="identifier">lit</span><span class="special">(</span><span class="char">'/'</span><span class="special">)</span> 477 <span class="special">>></span> <span class="identifier">lexeme</span><span class="special">[+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'>'</span><span class="special">)]</span> 478 <span class="special">>></span> <span class="char">'>'</span> 479 480 <span class="special">,</span> <span class="identifier">end_tag</span> <span class="special">%=</span> 481 <span class="string">"</"</span> 482 <span class="special">>></span> <span class="identifier">lit</span><span class="special">(</span><span class="identifier">_r1</span><span class="special">)</span> 483 <span class="special">>></span> <span class="char">'>'</span> 484 <span class="special">);</span> 485 <span class="special">}</span> 486 487 <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="identifier">mini_xml</span><span class="special">(),</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space_type</span><span class="special">></span> <span class="identifier">entry</span><span class="special">;</span> 488 489 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">subrule</span><span class="special"><</span><span class="number">0</span><span class="special">,</span> <span class="identifier">mini_xml</span><span class="special">(),</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">locals</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="special">></span> <span class="identifier">xml</span><span class="special">;</span> 490 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">subrule</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="identifier">mini_xml_node</span><span class="special">()></span> <span class="identifier">node</span><span class="special">;</span> 491 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">subrule</span><span class="special"><</span><span class="number">2</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">text</span><span class="special">;</span> 492 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">subrule</span><span class="special"><</span><span class="number">3</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">start_tag</span><span class="special">;</span> 493 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">subrule</span><span class="special"><</span><span class="number">4</span><span class="special">,</span> <span class="keyword">void</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">end_tag</span><span class="special">;</span> 494<span class="special">};</span> 495</pre> 496<p> 497 </p> 498<p> 499 The definitions of the <code class="computeroutput"><span class="identifier">mini_xml</span></code> 500 and <code class="computeroutput"><span class="identifier">mini_xml_node</span></code> data 501 structures are not shown here. The full example above can be found here: 502 <a href="../../../../../example/qi/mini_xml2_sr.cpp" target="_top">../../example/qi/mini_xml2_sr.cpp</a> 503 </p> 504<h6> 505<a name="spirit_repository.qi_components.nonterminal.subrule.h10"></a> 506 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.performance"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.performance">Performance</a> 507 </h6> 508<p> 509 This table compares run-time and compile-time performance when converting 510 examples to subrules, with various compilers. 511 </p> 512<div class="table"> 513<a name="spirit_repository.qi_components.nonterminal.subrule.t0"></a><p class="title"><b>Table 2. Subrules performance</b></p> 514<div class="table-contents"><table class="table" summary="Subrules performance"> 515<colgroup> 516<col> 517<col> 518<col> 519<col> 520<col> 521</colgroup> 522<thead><tr> 523<th> 524 <p> 525 Example 526 </p> 527 </th> 528<th> 529 <p> 530 Compiler 531 </p> 532 </th> 533<th> 534 <p> 535 Speed (run-time) 536 </p> 537 </th> 538<th> 539 <p> 540 Time (compile-time) 541 </p> 542 </th> 543<th> 544 <p> 545 Memory (compile-time) 546 </p> 547 </th> 548</tr></thead> 549<tbody> 550<tr> 551<td> 552 <p> 553 calc1_sr 554 </p> 555 </td> 556<td> 557 <p> 558 gcc 4.4.1 559 </p> 560 </td> 561<td> 562 <p> 563 +6% 564 </p> 565 </td> 566<td> 567 <p> 568 n/a 569 </p> 570 </td> 571<td> 572 <p> 573 n/a 574 </p> 575 </td> 576</tr> 577<tr> 578<td> 579 <p> 580 calc1_sr 581 </p> 582 </td> 583<td> 584 <p> 585 Visual C++ 2008 (VC9) 586 </p> 587 </td> 588<td> 589 <p> 590 +5% 591 </p> 592 </td> 593<td> 594 <p> 595 n/a 596 </p> 597 </td> 598<td> 599 <p> 600 n/a 601 </p> 602 </td> 603</tr> 604<tr> 605<td> 606 <p> 607 mini_xml2_sr 608 </p> 609 </td> 610<td> 611 <p> 612 gcc 3.4.6 613 </p> 614 </td> 615<td> 616 <p> 617 -1% 618 </p> 619 </td> 620<td> 621 <p> 622 +54% 623 </p> 624 </td> 625<td> 626 <p> 627 +32% 628 </p> 629 </td> 630</tr> 631<tr> 632<td> 633 <p> 634 mini_xml2_sr 635 </p> 636 </td> 637<td> 638 <p> 639 gcc 4.1.2 640 </p> 641 </td> 642<td> 643 <p> 644 +5% 645 </p> 646 </td> 647<td> 648 <p> 649 +58% 650 </p> 651 </td> 652<td> 653 <p> 654 +25% 655 </p> 656 </td> 657</tr> 658<tr> 659<td> 660 <p> 661 mini_xml2_sr 662 </p> 663 </td> 664<td> 665 <p> 666 gcc 4.4.1 667 </p> 668 </td> 669<td> 670 <p> 671 +8% 672 </p> 673 </td> 674<td> 675 <p> 676 +20% 677 </p> 678 </td> 679<td> 680 <p> 681 +14% 682 </p> 683 </td> 684</tr> 685<tr> 686<td> 687 <p> 688 mini_xml2_sr 689 </p> 690 </td> 691<td> 692 <p> 693 Visual C++ 2005 (VC8) SP1 694 </p> 695 </td> 696<td> 697 <p> 698 +1% 699 </p> 700 </td> 701<td> 702 <p> 703 +33% 704 </p> 705 </td> 706<td> 707 <p> 708 +27% 709 </p> 710 </td> 711</tr> 712<tr> 713<td> 714 <p> 715 mini_xml2_sr 716 </p> 717 </td> 718<td> 719 <p> 720 Visual C++ 2008 (VC9) 721 </p> 722 </td> 723<td> 724 <p> 725 +9% 726 </p> 727 </td> 728<td> 729 <p> 730 +52% 731 </p> 732 </td> 733<td> 734 <p> 735 +40% 736 </p> 737 </td> 738</tr> 739</tbody> 740</table></div> 741</div> 742<br class="table-break"><p> 743 The columns are: 744 </p> 745<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 746<li class="listitem"> 747 <span class="bold"><strong>Speed (run-time)</strong></span>: speed-up of the 748 parser resulting from the use of subrules (higher is better). 749 </li> 750<li class="listitem"> 751 <span class="bold"><strong>Time (compile-time)</strong></span>: increase in compile 752 time (lower is better). 753 </li> 754<li class="listitem"> 755 <span class="bold"><strong>Memory (compile-time)</strong></span>: increase in 756 compiler memory usage (lower is better). 757 </li> 758</ul></div> 759<h6> 760<a name="spirit_repository.qi_components.nonterminal.subrule.h11"></a> 761 <span class="phrase"><a name="spirit_repository.qi_components.nonterminal.subrule.notes"></a></span><a class="link" href="subrule.html#spirit_repository.qi_components.nonterminal.subrule.notes">Notes</a> 762 </h6> 763<p> 764 Subrules push the C++ compiler hard. A group of subrules is a single C++ 765 expression. Current C++ compilers cannot handle very complex expressions 766 very well. One restricting factor is the typical compiler's limit on template 767 recursion depth. Some, but not all, compilers allow this limit to be configured. 768 </p> 769<p> 770 g++'s maximum can be set using a compiler flag: <code class="computeroutput"><span class="special">-</span><span class="identifier">ftemplate</span><span class="special">-</span><span class="identifier">depth</span></code>. Set this appropriately if you 771 use relatively complex subrules. 772 </p> 773</div> 774<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 775<td align="left"></td> 776<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 777 Distributed under the Boost Software License, Version 1.0. (See accompanying 778 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>) 779 </p> 780</div></td> 781</tr></table> 782<hr> 783<div class="spirit-nav"> 784<a accesskey="p" href="../nonterminal.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../nonterminal.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../operators.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 785</div> 786</body> 787</html> 788