1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Karma 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="Karma Generator Non-terminals"> 9<link rel="prev" href="../nonterminal.html" title="Karma Generator Non-terminals"> 10<link rel="next" href="../../acknowledgments.html" title="Acknowledgments"> 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="../../acknowledgments.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.karma_components.nonterminal.subrule"></a><a class="link" href="subrule.html" title="Karma subrules">Karma 28 subrules</a> 29</h4></div></div></div> 30<h6> 31<a name="spirit_repository.karma_components.nonterminal.subrule.h0"></a> 32 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.description"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.description">Description</a> 33 </h6> 34<p> 35 The <span class="emphasis"><em>Spirit.Karma</em></span> <code class="computeroutput"><span class="identifier">subrule</span></code> 36 is a component allowing to create a named generator, and to refer to it 37 by 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 generator slightly faster (see <a class="link" href="subrule.html#spirit_repository.karma_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 generator 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">ast_node</span> <span class="special">%=</span> <span class="identifier">int_</span> <span class="special">|</span> <span class="identifier">binary_node</span> <span class="special">|</span> <span class="identifier">unary_node</span> 67 68 <span class="special">,</span> <span class="identifier">binary_node</span> <span class="special">%=</span> <span class="char">'('</span> <span class="special"><<</span> <span class="identifier">ast_node</span> <span class="special"><<</span> <span class="identifier">char_</span> <span class="special"><<</span> <span class="identifier">ast_node</span> <span class="special"><<</span> <span class="char">')'</span> 69 70 <span class="special">,</span> <span class="identifier">unary_node</span> <span class="special">%=</span> <span class="char">'('</span> <span class="special"><<</span> <span class="identifier">char_</span> <span class="special"><<</span> <span class="identifier">ast_node</span> <span class="special"><<</span> <span class="char">')'</span> 71<span class="special">);</span> 72</pre> 73<p> 74 </p> 75<p> 76 The example above can be found here: <a href="../../../../../example/karma/calc2_ast_dump_sr.cpp" target="_top">../../example/karma/calc2_ast_dump_sr.cpp</a> 77 </p> 78<p> 79 As shown in this code snippet (an extract from the calc2_ast_dump_sr example), 80 subrules can be freely mixed with rules and grammars. Here, a group of 81 3 subrules (<code class="computeroutput"><span class="identifier">ast_node</span></code>, 82 <code class="computeroutput"><span class="identifier">binary_node</span></code>, <code class="computeroutput"><span class="identifier">unary_node</span></code>) is assigned to a rule (named 83 <code class="computeroutput"><span class="identifier">entry</span></code>). This means that 84 parts of a generator can use subrules (typically the innermost, most performance-critical 85 parts), whereas the rest can use rules and grammars. 86 </p> 87<h6> 88<a name="spirit_repository.karma_components.nonterminal.subrule.h1"></a> 89 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.header"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.header">Header</a> 90 </h6> 91<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/repository/home/karma/nonterminal/subrule.hpp></span> 92<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">karma_subrule</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 93</pre> 94<h6> 95<a name="spirit_repository.karma_components.nonterminal.subrule.h2"></a> 96 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.synopsis__declaration_"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.synopsis__declaration_">Synopsis 97 (declaration)</a> 98 </h6> 99<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> 100</pre> 101<h6> 102<a name="spirit_repository.karma_components.nonterminal.subrule.h3"></a> 103 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.parameters__declaration_"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.parameters__declaration_">Parameters 104 (declaration)</a> 105 </h6> 106<div class="informaltable"><table class="table"> 107<colgroup> 108<col> 109<col> 110</colgroup> 111<thead><tr> 112<th> 113 <p> 114 Parameter 115 </p> 116 </th> 117<th> 118 <p> 119 Description 120 </p> 121 </th> 122</tr></thead> 123<tbody> 124<tr> 125<td> 126 <p> 127 <code class="computeroutput"><span class="identifier">ID</span></code> 128 </p> 129 </td> 130<td> 131 <p> 132 Required numeric argument. Gives the subrule a unique 'identification 133 tag'. 134 </p> 135 </td> 136</tr> 137<tr> 138<td> 139 <p> 140 <code class="computeroutput"><span class="identifier">A1</span></code>, <code class="computeroutput"><span class="identifier">A2</span></code> 141 </p> 142 </td> 143<td> 144 <p> 145 Optional types, can be specified in any order. Can be one of 146 1. signature, 2. locals (see rules reference for more information 147 on those parameters). 148 </p> 149 <p> 150 Note that the delimiter type need not be specified in the parameters, 151 unlike with grammars and rules. Subrules will automatically use 152 the delimiter type which is in effect when they are invoked. 153 </p> 154 </td> 155</tr> 156<tr> 157<td> 158 <p> 159 <code class="computeroutput"><span class="identifier">name</span></code> 160 </p> 161 </td> 162<td> 163 <p> 164 Optional string. Gives the subrule a name, useful for debugging 165 and error handling. 166 </p> 167 </td> 168</tr> 169</tbody> 170</table></div> 171<h6> 172<a name="spirit_repository.karma_components.nonterminal.subrule.h4"></a> 173 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.synopsis__usage_"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.synopsis__usage_">Synopsis 174 (usage)</a> 175 </h6> 176<p> 177 Subrules are defined and used within groups, typically (and by convention) 178 enclosed inside parentheses. 179 </p> 180<pre class="programlisting"><span class="comment">// Group containing N subrules</span> 181<span class="special">(</span> 182 <span class="identifier">sr1</span> <span class="special">=</span> <span class="identifier">expr1</span> 183 <span class="special">,</span> <span class="identifier">sr2</span> <span class="special">=</span> <span class="identifier">expr2</span> 184 <span class="special">,</span> <span class="special">...</span> <span class="comment">// Any number of subrules</span> 185<span class="special">}</span> 186</pre> 187<p> 188 The IDs of all subrules defined within the same group must be different. 189 It is an error to define several subrules with the same ID (or to define 190 the same subrule multiple times) in the same group. 191 </p> 192<pre class="programlisting"><span class="comment">// Auto-subrules and inherited attributes</span> 193<span class="special">(</span> 194 <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> 195 <span class="special">,</span> <span class="identifier">srB</span> <span class="special">%=</span> <span class="identifier">exprB</span> 196 <span class="special">,</span> <span class="identifier">srC</span> <span class="special">=</span> <span class="identifier">exprC</span> 197 <span class="special">,</span> <span class="special">...</span> 198<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> 199</pre> 200<h6> 201<a name="spirit_repository.karma_components.nonterminal.subrule.h5"></a> 202 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.parameters__usage_"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.parameters__usage_">Parameters 203 (usage)</a> 204 </h6> 205<div class="informaltable"><table class="table"> 206<colgroup> 207<col> 208<col> 209</colgroup> 210<thead><tr> 211<th> 212 <p> 213 Parameter 214 </p> 215 </th> 216<th> 217 <p> 218 Description 219 </p> 220 </th> 221</tr></thead> 222<tbody> 223<tr> 224<td> 225 <p> 226 <code class="computeroutput"><span class="identifier">sr1</span></code>, <code class="computeroutput"><span class="identifier">sr2</span></code> 227 </p> 228 </td> 229<td> 230 <p> 231 Subrules with different IDs. 232 </p> 233 </td> 234</tr> 235<tr> 236<td> 237 <p> 238 <code class="computeroutput"><span class="identifier">expr1</span></code>, <code class="computeroutput"><span class="identifier">expr2</span></code> 239 </p> 240 </td> 241<td> 242 <p> 243 Generator expressions. Can include <code class="computeroutput"><span class="identifier">sr1</span></code> 244 and <code class="computeroutput"><span class="identifier">sr2</span></code>, as well 245 as any other valid generator expressions. 246 </p> 247 </td> 248</tr> 249<tr> 250<td> 251 <p> 252 <code class="computeroutput"><span class="identifier">srA</span></code> 253 </p> 254 </td> 255<td> 256 <p> 257 Subrule with a synthesized attribute and inherited attributes. 258 </p> 259 </td> 260</tr> 261<tr> 262<td> 263 <p> 264 <code class="computeroutput"><span class="identifier">srB</span></code> 265 </p> 266 </td> 267<td> 268 <p> 269 Subrule with a synthesized attribute. 270 </p> 271 </td> 272</tr> 273<tr> 274<td> 275 <p> 276 <code class="computeroutput"><span class="identifier">srC</span></code> 277 </p> 278 </td> 279<td> 280 <p> 281 Subrule with inherited attributes. 282 </p> 283 </td> 284</tr> 285<tr> 286<td> 287 <p> 288 <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> 289 </p> 290 </td> 291<td> 292 <p> 293 Generator expressions. 294 </p> 295 </td> 296</tr> 297<tr> 298<td> 299 <p> 300 <code class="computeroutput"><span class="identifier">a1</span></code>, <code class="computeroutput"><span class="identifier">a2</span></code> 301 </p> 302 </td> 303<td> 304 <p> 305 Arguments passed to the subrule group. They are passed as inherited 306 attributes to the group's start subrule, <code class="computeroutput"><span class="identifier">srA</span></code>. 307 </p> 308 </td> 309</tr> 310<tr> 311<td> 312 <p> 313 <code class="computeroutput"><span class="identifier">c1</span></code>, <code class="computeroutput"><span class="identifier">c2</span></code> 314 </p> 315 </td> 316<td> 317 <p> 318 Arguments passed as inherited attributes to subrule <code class="computeroutput"><span class="identifier">srC</span></code>. 319 </p> 320 </td> 321</tr> 322</tbody> 323</table></div> 324<h6> 325<a name="spirit_repository.karma_components.nonterminal.subrule.h6"></a> 326 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.groups"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.groups">Groups</a> 327 </h6> 328<p> 329 A subrule group (a set of subrule definitions) is a generator, which can 330 be used anywhere in a generator expression (in assignments to rules, as 331 well as directly in arguments to functions such as <code class="computeroutput"><span class="identifier">generate</span></code>). 332 In a group, generation proceeds from the start subrule, which is the first 333 (topmost) subrule defined in that group. In the two groups in the synopsis 334 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 335 -- for example when the first subrule group is called forth, the <code class="computeroutput"><span class="identifier">sr1</span></code> subrule is called. 336 </p> 337<p> 338 A subrule can only be used in a group which defines it. Groups can be viewed 339 as scopes: a definition of a subrule is limited to its enclosing group. 340 </p> 341<pre class="programlisting"><span class="identifier">rule</span><span class="special"><</span><span class="identifier">outiter_type</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> 342<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> 343<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> 344 345<span class="identifier">r1</span> <span class="special">=</span> 346 <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">space</span> <span class="special">)</span> <span class="comment">// First group in r1.</span> 347 <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> 348 <span class="comment">// ^^^</span> 349 <span class="comment">// DOES NOT COMPILE: sr1 is not defined in this</span> 350 <span class="comment">// second group, it cannot be used here (its</span> 351 <span class="comment">// previous definition is out of scope).</span> 352<span class="special">;</span> 353 354<span class="identifier">r2</span> <span class="special">=</span> 355 <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">space</span> <span class="special">)</span> <span class="comment">// Only group in r2.</span> 356 <span class="special"><<</span> <span class="identifier">sr1</span> 357 <span class="comment">// ^^^</span> 358 <span class="comment">// DOES NOT COMPILE: not in a subrule group,</span> 359 <span class="comment">// sr1 cannot be used here (here too, its</span> 360 <span class="comment">// previous definition is out of scope).</span> 361<span class="special">;</span> 362 363<span class="identifier">r3</span> <span class="special">=</span> 364 <span class="special">(</span> <span class="identifier">sr1</span> <span class="special">=</span> <span class="identifier">space</span> <span class="special"><<</span> <span class="char">'x'</span> <span class="special">)</span> <span class="comment">// Another group. The same subrule `sr1`</span> 365 <span class="comment">// can have another, independent</span> 366 <span class="comment">// definition in this group.</span> 367<span class="special">;</span> 368</pre> 369<h6> 370<a name="spirit_repository.karma_components.nonterminal.subrule.h7"></a> 371 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.attributes"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.attributes">Attributes</a> 372 </h6> 373<p> 374 A subrule has the same behavior as a rule with respect to attributes. In 375 particular: 376 </p> 377<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 378<li class="listitem"> 379 the type of its synthesized attribute is the one specified in the subrule's 380 signature, if any. Otherwise it is <code class="computeroutput"><span class="identifier">unused_type</span></code>. 381 </li> 382<li class="listitem"> 383 the types of its inherited attributes are the ones specified in the 384 subrule's signature, if any. Otherwise the subrule has no inherited 385 attributes. 386 </li> 387<li class="listitem"> 388 an auto-subrule can be defined by assigning it with the <code class="computeroutput"><span class="special">%=</span></code> syntax. In this case, the subrule's 389 synthesized attribute is automatically propagated to the RHS generator's 390 attribute. 391 </li> 392<li class="listitem"> 393 the Phoenix placeholders <code class="computeroutput"><span class="identifier">_val</span></code>, 394 <code class="computeroutput"><span class="identifier">_r1</span></code>, <code class="computeroutput"><span class="identifier">_r2</span></code>, ... are available to refer to 395 the subrule's synthesized and inherited attributes, if present. 396 </li> 397</ul></div> 398<h6> 399<a name="spirit_repository.karma_components.nonterminal.subrule.h8"></a> 400 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.locals"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.locals">Locals</a> 401 </h6> 402<p> 403 A subrule has the same behavior as a rule with respect to locals. In particular, 404 the Phoenix placeholders <code class="computeroutput"><span class="identifier">_a</span></code>, 405 <code class="computeroutput"><span class="identifier">_b</span></code>, ... are available to 406 refer to the subrule's locals, if present. 407 </p> 408<h6> 409<a name="spirit_repository.karma_components.nonterminal.subrule.h9"></a> 410 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.example"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.example">Example</a> 411 </h6> 412<p> 413 Some includes: 414 </p> 415<p> 416</p> 417<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">karma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 418<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">karma_subrule</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 419<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> 420<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> 421<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_fusion</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 422</pre> 423<p> 424 </p> 425<p> 426 Some using declarations: 427 </p> 428<p> 429</p> 430<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> 431<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">ascii</span><span class="special">;</span> 432<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> 433</pre> 434<p> 435 </p> 436<p> 437 A grammar containing only one rule, defined with a group of 2 subrules: 438 </p> 439<p> 440</p> 441<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">OutputIterator</span><span class="special">></span> 442<span class="keyword">struct</span> <span class="identifier">mini_xml_generator</span> 443 <span class="special">:</span> <span class="identifier">karma</span><span class="special">::</span><span class="identifier">grammar</span><span class="special"><</span><span class="identifier">OutputIterator</span><span class="special">,</span> <span class="identifier">mini_xml</span><span class="special">()></span> 444<span class="special">{</span> 445 <span class="identifier">mini_xml_generator</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">mini_xml_generator</span><span class="special">::</span><span class="identifier">base_type</span><span class="special">(</span><span class="identifier">entry</span><span class="special">)</span> 446 <span class="special">{</span> 447 <span class="identifier">entry</span> <span class="special">%=</span> <span class="special">(</span> 448 <span class="identifier">xml</span> <span class="special">=</span> 449 <span class="char">'<'</span> <span class="special"><<</span> <span class="identifier">string</span><span class="special">[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">at_c</span><span class="special"><</span><span class="number">0</span><span class="special">>(</span><span class="identifier">_val</span><span class="special">)]</span> <span class="special"><<</span> <span class="char">'>'</span> 450 <span class="special"><<</span> <span class="special">(*</span><span class="identifier">node</span><span class="special">)[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">at_c</span><span class="special"><</span><span class="number">1</span><span class="special">>(</span><span class="identifier">_val</span><span class="special">)]</span> 451 <span class="special"><<</span> <span class="string">"</"</span> <span class="special"><<</span> <span class="identifier">string</span><span class="special">[</span><span class="identifier">_1</span> <span class="special">=</span> <span class="identifier">at_c</span><span class="special"><</span><span class="number">0</span><span class="special">>(</span><span class="identifier">_val</span><span class="special">)]</span> <span class="special"><<</span> <span class="char">'>'</span> 452 453 <span class="special">,</span> <span class="identifier">node</span> <span class="special">%=</span> <span class="identifier">string</span> <span class="special">|</span> <span class="identifier">xml</span> 454 <span class="special">);</span> 455 <span class="special">}</span> 456 457 <span class="identifier">karma</span><span class="special">::</span><span class="identifier">rule</span><span class="special"><</span><span class="identifier">OutputIterator</span><span class="special">,</span> <span class="identifier">mini_xml</span><span class="special">()></span> <span class="identifier">entry</span><span class="special">;</span> 458 459 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">karma</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">xml</span><span class="special">;</span> 460 <span class="identifier">repo</span><span class="special">::</span><span class="identifier">karma</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> 461<span class="special">};</span> 462</pre> 463<p> 464 </p> 465<p> 466 The definitions of the <code class="computeroutput"><span class="identifier">mini_xml</span></code> 467 and <code class="computeroutput"><span class="identifier">mini_xml_node</span></code> data 468 structures are not shown here. The full example above can be found here: 469 <a href="../../../../../example/karma/mini_xml_karma_sr.cpp" target="_top">../../example/karma/mini_xml_karma_sr.cpp</a> 470 </p> 471<h6> 472<a name="spirit_repository.karma_components.nonterminal.subrule.h10"></a> 473 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.performance"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.performance">Performance</a> 474 </h6> 475<p> 476 For comparison of run-time and compile-time performance when using subrules, 477 please see the <a class="link" href="../../qi_components/nonterminal/subrule.html#spirit_repository.qi_components.nonterminal.subrule.performance">Performance</a> 478 section of <span class="emphasis"><em>Spirit.Qi</em></span> subrules (the implementation 479 of <span class="emphasis"><em>Spirit.Karma</em></span> and <span class="emphasis"><em>Spirit.Qi</em></span> 480 subrules is very similar, so performance is very similar too). 481 </p> 482<h6> 483<a name="spirit_repository.karma_components.nonterminal.subrule.h11"></a> 484 <span class="phrase"><a name="spirit_repository.karma_components.nonterminal.subrule.notes"></a></span><a class="link" href="subrule.html#spirit_repository.karma_components.nonterminal.subrule.notes">Notes</a> 485 </h6> 486<p> 487 Subrules push the C++ compiler hard. A group of subrules is a single C++ 488 expression. Current C++ compilers cannot handle very complex expressions 489 very well. One restricting factor is the typical compiler's limit on template 490 recursion depth. Some, but not all, compilers allow this limit to be configured. 491 </p> 492<p> 493 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 494 use relatively complex subrules. 495 </p> 496</div> 497<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 498<td align="left"></td> 499<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 500 Distributed under the Boost Software License, Version 1.0. (See accompanying 501 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>) 502 </p> 503</div></td> 504</tr></table> 505<hr> 506<div class="spirit-nav"> 507<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="../../acknowledgments.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 508</div> 509</body> 510</html> 511