faq.html 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <html>
  2. <head>
  3. <title>FAQ</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <link rel="stylesheet" href="theme/style.css" type="text/css">
  6. </head>
  7. <body>
  8. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  9. <tr>
  10. <td width="10">
  11. </td>
  12. <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>FAQ</b></font></td>
  13. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  14. </tr>
  15. </table>
  16. <br>
  17. <table border="0">
  18. <tr>
  19. <td width="10"></td>
  20. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  21. <td width="30"><a href="techniques.html"><img src="theme/l_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="rationale.html"><img src="theme/r_arr.gif" border="0"></a></td>
  23. </tr>
  24. </table>
  25. <ul>
  26. <li><a href="#scanner_business">The Scanner Business</a></li>
  27. <li><a href="#left_recursion">Eliminating Left Recursion</a> </li>
  28. <li><a href="#right_associativity">Implementing Right Associativity</a></li>
  29. <li><a href="#lexeme_and_rules">The lexeme_d directive and rules</a></li>
  30. <li><a href="#kleene_star">Kleene Star infinite loop</a></li>
  31. <li><a href="#CVS">Boost CVS and Spirit CVS</a></li>
  32. <li><a href="#compilation_times">How to reduce compilation times with complex
  33. Spirit grammars</a></li>
  34. <li><strong><a href="#frame_assertion">Closure frame assertion</a></strong></li>
  35. <li><strong><a href="#greedy_rd">Greedy RD</a></strong></li>
  36. <li><strong><a href="#referencing_a_rule_at_construction">Referencing a rule
  37. at construction time</a></strong></li>
  38. <li><strong><a href="#storing_rules">Storing Rules</a></strong></li>
  39. <li><strong><a href="#parsing_ints_and_reals">Parsing ints and reals</a> </strong></li>
  40. <li><strong><a href="#output_operator">BOOST_SPIRIT_DEBUG and missing <tt>operator&lt;&lt;</tt></a></strong></li>
  41. <li><strong><a href="#repository">Applications that used to be part of spirit</a></strong></li>
  42. </ul>
  43. <p><b> <a name="scanner_business" id="scanner_business"></a> The Scanner Business</b></p>
  44. <p><font color="#FF0000">Question:</font> Why doesn't this compile?</p>
  45. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>rule</span><span class=special>&lt;&gt; </span><span class=identifier>r </span><span class=special>= /*...*/;
  46. </span> <span class=identifier>parse</span><span class=special>(</span><span class=string>"hello world"</span><span class=special>, </span><span class=identifier>r</span><span class=special>, </span><span class=identifier>space_p</span><span class=special>); </span><span class=comment>// BAD [attempts phrase level parsing]</span></font></code></pre>
  47. <p>But if I <font color="#000000">remove the skip-parser, everything goes back
  48. to normal again:<code></code></font></p>
  49. <pre><code><font color="#000000"> <span class=identifier>rule</span><span class=special>&lt;&gt; </span><span class=identifier>r </span><span class=special>= *</span><span class=identifier>anychar_p</span><span class=special>;
  50. </span><span class=identifier>parse</span><span class=special>(</span><span class=string>"hello world"</span><span class=special>, </span><span class=identifier>r</span><span class=special>); </span><span class=comment>// OK [character level parsing]</span></font></code></pre>
  51. <p>Sometimes you'll want to pass in a rule to one of the functions parse functions
  52. that Spirit provides. The problem is that the rule is a template class that
  53. is parameterized by the scanner type. This is rather awkward but unavoidable:
  54. <strong>the rule is tied to a scanner</strong>. What's not obvious is that this
  55. scanner must be compatible with the scanner that is ultimately passed to the
  56. rule's parse member function. Otherwise, the compiler will complain. </p>
  57. <p>Why does the first call to parse not compile? Because of scanner incompatibility.
  58. Behind the scenes, the free parse function creates a scanner from the iterators
  59. passed in. In the first call to parse, the scanner created is a plain vanilla
  60. <tt>scanner&lt;&gt;</tt>. This is compatible with the default scanner type of
  61. <tt>rule&lt;&gt;</tt> [see default template parameters of <a href="rule.html">the
  62. rule</a>]. The second call creates a scanner of type <tt><a href="scanner.html#phrase_scanner_t">phrase_scanner_t</a></tt>.
  63. Thus, in order for the second call to succeed, the rule must be parameterized
  64. as <tt>rule&lt;phrase_scanner_t&gt;</tt>:</p>
  65. <pre><code><font color="#000000"><span class=comment> </span><span class=identifier>rule</span><span class=special>&lt;</span><span class=identifier>phrase_scanner_t</span><span class=special>&gt; </span><span class=identifier>r </span><span class=special>= </span><span class=special>*</span><span class=identifier>anychar_p</span><span class=special>;
  66. </span><span class=identifier>parse</span><span class=special>(</span><span class=string>"hello world"</span><span class=special>, </span><span class=identifier>r</span><span class=special>, </span><span class=identifier>space_p</span><span class=special>); </span><span class=comment>// OK [phrase level parsing]</span></font></code></pre>
  67. <p>Take note however that <tt>phrase_scanner_t</tt> is compatible only when you
  68. are using <tt>char const*</tt> iterators and <tt>space_p</tt> as the skip parser.
  69. Other than that, you'll have to find the right type of scanner. This is tedious
  70. to do correctly. In light of this issue, <strong>it is best to avoid rules as
  71. arguments to the parse functions</strong>. Keep in mind that this happens only
  72. with rules. The rule is the only parser that has to be tied to a particular
  73. scanner type. For instance:</p>
  74. <pre><span class=comment> </span><span class=identifier>parse</span><span class=special>(</span><span class=string>"hello world"</span><span class=special>, *</span><span class=identifier>anychar_p</span><span class=special>); </span><span class=comment><code><font color="#000000"><span class=comment>// OK [character level parsing]</span></font></code>
  75. </span><span class=identifier>parse</span><span class=special>(</span><span class=string>"hello world"</span><span class=special>, *</span><span class=identifier>anychar_p</span><span class=special>, </span><span class=identifier>space_p</span><span class=special>); </span><span class="comment">// OK [phrase level parsing]</span></pre>
  76. <table width="80%" border="0" align="center">
  77. <tr>
  78. <td class="note_box"> <strong><img src="theme/note.gif" width="16" height="16">
  79. Multiple Scanner Support</strong><br>
  80. <br>
  81. As of v1.8.0, rules can use one or more scanner types. There are cases,
  82. for instance, where we need a rule that can work on the phrase and character
  83. levels. Rule/scanner mismatch has been a source of confusion and is the
  84. no. 1 <a href="faq.html#scanner_business">FAQ</a>. To address this issue,
  85. we now have <a href="rule.html#multiple_scanner_support">multiple scanner
  86. support</a>. <br>
  87. <br>
  88. <img src="theme/bulb.gif" width="13" height="18"> See the techniques section
  89. for an <a href="techniques.html#multiple_scanner_support">example</a> of
  90. a <a href="grammar.html">grammar</a> using a multiple scanner enabled rule,
  91. <a href="scanner.html#lexeme_scanner">lexeme_scanner</a> and <a href="scanner.html#as_lower_scanner">as_lower_scanner.</a></td>
  92. </tr>
  93. </table>
  94. <p><b> <a name="left_recursion"></a> Eliminating Left Recursion </b></p>
  95. <p><font color="#FF0000">Question:</font> I ported a grammar from YACC. It's &quot;kinda&quot;
  96. working - the parser itself compiles with no errors. But when I try to parse,
  97. it gives me an &quot;invalid page fault&quot;. I tracked down the problem to
  98. this grammar snippet:</p>
  99. <pre> <span class=identifier>or_expr </span><span class=special>= </span><span class=identifier>xor_expr </span><span class=special>| (</span><span class=identifier>or_expr </span><span class=special>&gt;&gt; </span><span class=identifier>VBAR </span><span class=special>&gt;&gt; </span><span class=identifier>xor_expr</span><span class=special>);</span></pre>
  100. <p>What you should do is to eliminate direct and indirect left-recursion. This
  101. causes the invalid page fault because the program enters an infinite loop. The
  102. code above is good for bottom up parsers such as YACC but not for LL parsers
  103. such as Spirit.</p>
  104. <p>This is similar to a rule in Hartmut Kaiser's C
  105. parser (this should be available for download from <a href="http://spirit.sf.net">Spirit's site</a> as soon as you read this).</p>
  106. <pre>
  107. <span class=identifier>inclusive_or_expression
  108. </span><span class=special>= </span><span class=identifier>exclusive_or_expression
  109. </span><span class=special>| </span><span class=identifier>inclusive_or_expression </span><span class=special>&gt;&gt; </span><span class=identifier>OR </span><span class=special>&gt;&gt; </span><span class=identifier>exclusive_or_expression
  110. </span><span class=special>;</span></pre>
  111. <p><span class=special></span>Transforming left recursion to right recursion,
  112. we have:</p>
  113. <pre> <span class=identifier>inclusive_or_expression
  114. </span><span class=special>= </span><span class=identifier>exclusive_or_expression </span><span class=special>&gt;&gt; </span><span class=identifier>inclusive_or_expression_helper
  115. </span><span class=special>;
  116. </span><span class=identifier>inclusive_or_expression_helper
  117. </span><span class=special>= </span><span class=identifier>OR </span><span class=special>&gt;&gt; </span><span class=identifier>exclusive_or_expression </span><span class=special>&gt;&gt; </span><span class=identifier>inclusive_or_expression_helper
  118. </span><span class=special>| </span><span class=identifier>epsilon_p
  119. </span><span class=special>;</span></pre>
  120. <p><span class=special></span>I'd go further. Since:</p>
  121. <pre> <span class=identifier>r </span><span class=special>= </span><span class=identifier>a </span><span class=special>| </span><span class=identifier>epsilon_p</span><span class=special>;</span></pre>
  122. <p><span class=special></span>is equivalent to:<span class=special><br>
  123. </span></p>
  124. <pre> <span class=identifier>r </span><span class=special>= !</span><span class=identifier>a</span><span class=special>;</span></pre>
  125. <p>we can simplify <tt>inclusive_or_expression_helper</tt> thus:</p>
  126. <pre> <span class=identifier>inclusive_or_expression_helper
  127. </span><span class=special>= !(</span><span class=identifier>OR </span><span class=special>&gt;&gt; </span><span class=identifier>exclusive_or_expression </span><span class=special>&gt;&gt; </span><span class=identifier>inclusive_or_expression_helper</span><span class=special>)
  128. ;</span></pre>
  129. <p><span class=special></span>Now, since:</p>
  130. <pre> <span class=identifier>r </span><span class=special>= !(</span><span class=identifier>a </span><span class=special>&gt;&gt; </span><span class=identifier>r</span><span class=special>);</span></pre>
  131. <p><span class=special></span>is equivalent to:</p>
  132. <pre> <span class=identifier>r </span><span class=special>= *</span><span class=identifier>a</span><span class=special>;</span></pre>
  133. <p><span class=special></span>we have:</p>
  134. <pre> <span class=identifier>inclusive_or_expression_helper
  135. </span><span class=special>= *(</span><span class=identifier>OR </span><span class=special>&gt;&gt; </span><span class=identifier>exclusive_or_expression</span><span class=special>)
  136. ;</span></pre>
  137. <p><span class=special></span>Now simplifying <tt>inclusive_or_expression</tt>
  138. fully, we have:</p>
  139. <pre> <span class=identifier>inclusive_or_expression
  140. </span><span class=special>= </span><span class=identifier>exclusive_or_expression </span><span class=special>&gt;&gt; *(</span><span class=identifier>OR </span><span class=special>&gt;&gt; </span><span class=identifier>exclusive_or_expression</span><span class=special>)
  141. ;</span></pre>
  142. <p><span class=special></span>Reminds me of the calculators. So in short:</p>
  143. <pre> <span class=identifier>a </span><span class=special>= </span><span class=identifier>b </span><span class=special>| </span><span class=identifier>a </span><span class=special>&gt;&gt; </span><span class=identifier>op </span><span class=special>&gt;&gt; </span><span class=identifier>b</span><span class=special>;</span></pre>
  144. <p><span class=special></span><span class=identifier>in </span><span class=identifier>pseudo-YACC
  145. </span><span class=identifier>is</span><span class=special>:</span></p>
  146. <pre> <span class=identifier>a </span><span class=special>= </span><span class=identifier>b </span><span class=special>&gt;&gt; *(</span><span class=identifier>op </span><span class=special>&gt;&gt; </span><span class=identifier>b</span><span class=special>);</span></pre>
  147. <p><span class=special></span>in Spirit. What could be simpler? Look Ma, no recursion,
  148. just iteration.</p>
  149. <p><b> <a name="right_associativity" id="right_associativity"></a> Implementing Right Associativity </b></p>
  150. <p> <font color="#FF0000">Question:</font> I tried adding <tt>'^'</tt> as an operator to compute the power to a calculator grammer. The following code
  151. </p>
  152. <pre> <span class=identifier>pow_expression
  153. </span><span class=special>= </span><span class=identifier>pow_operand </span><span class=special>&gt;&gt; </span><span class=special>*( </span><span class=literal>'^' </span><span class=special>&gt;&gt; </span><span class=identifier>pow_operand </span><span class=special>[ </span><span class=special>&amp; </span><span class=identifier>do_pow </span><span class=special>]
  154. </span><span class=special>)
  155. </span><span class=special>;</span>
  156. </pre>
  157. <p>parses the input correctly, but I want the operator to be evalutated from right to left. In other words, the expression <tt>2^3^4</tt> is supposed to have the same semantics as <tt>2^(3^4)</tt> instead of <tt>(2^3)^4</tt>. How do I do it?
  158. </p>
  159. <p> The "textbook recipe" for Right Associativity is Right Recursion. In BNF that means:
  160. <pre> &lt;pow_expression&gt; ::= &lt;pow_operand&gt; '^' &lt;pow_expression&gt; | &lt;pow_operand&gt;
  161. </pre>
  162. <p>But we better don't take the theory too literally here, because if the first alternative fails, the semantic actions within <tt>pow_operand</tt> might have been executed already and will then be executed again when trying the second alternative. So let's apply Left Factorization to factor out <tt>pow_operand</tt>:
  163. <pre> &lt;pow_expression&gt; ::= &lt;pow_operand&gt; &lt;pow_expression_helper&gt;
  164. &lt;pow_expression_helper&gt; ::= '^' &lt;pow_expression&gt; | <i>&#949;</i>
  165. </pre>
  166. <p>The production <tt>pow_expression_helper</tt> matches the empty string <i>&#949;</i>, so we can replace the alternative with the optional operator in Spirit code.
  167. </p>
  168. <pre> <span class=identifier>pow_expression
  169. </span><span class=special>= </span><span class=identifier>pow_operand </span><span class=special>&gt;&gt; </span><span class=special>!( </span><span class=literal>'^' </span><span class=special>&gt;&gt; </span><span class=identifier>pow_expression </span><span class=special>[ </span><span class=special>&amp; </span><span class=identifier>do_pow </span><span class=special>]
  170. </span><span class=special>)
  171. </span><span class=special>;</span>
  172. </pre>
  173. <p>Now any semantic actions within <tt>pow_operand</tt> can safely be executed. For stack-based evaluation that means that each match of <tt>pow_operand</tt> will leave one value on the stack and the recursion makes sure there are (at least) two values on the stack when <tt>do_pow</tt> is fired to reduce these two values to their power.
  174. </p>
  175. <p>In cases where this technique isn't applicable, such as C-style assignment
  176. <pre> <span class=identifier>assignment
  177. </span><span class=special>= </span><span class=identifier>lvalue </span><span class=special>&gt;&gt; </span><span class=literal>'=' </span><span class=special>&gt;&gt; </span><span class=identifier>assignment
  178. </span><span class=special>| </span><span class=identifier>ternary_conditional
  179. </span><span class=special>;</span>
  180. </pre>
  181. <p>you can append <tt>| epsilon_p [ <i>action</i> ] &gt;&gt; nothing_p</tt> to a parser to correct the semantic context when backtracking occurs (in the example case that would be dropping the address pushed by <tt>lvalue</tt> off the evaluation stack):
  182. </p>
  183. <pre> <span class=identifier>assignment
  184. </span><span class=special>= </span><span class=identifier>lvalue </span><span class=special>&gt;&gt; </span><span class=special>( </span><span class=literal>'=' </span><span class=special>&gt;&gt; </span><span class=identifier>assignment </span></span><span class=special>[ </span><span class=special>&amp; </span><span class=identifier>do_store </span><span class=special>]
  185. </span><span class=special>| </span><span class=identifier>epsilon_p </span><span class=special>[ </span><span class=special>&amp; </span><span class=identifier>do_drop </span><span class=special>]
  186. </span><span class=special>&gt;&gt; </span><span class=identifier>nothing_p
  187. </span><span class=special>)
  188. </span><span class=special>| </span><span class=identifier>ternary_conditional
  189. </span><span class=special>;</span>
  190. </pre>
  191. <p>However, this trick compromises the clear separation of syntax and semantics, so you also might want to consider using an <a href="trees.html">AST</a> instead of semantic actions so you can just go with the first definition of <tt>assignment</tt>.
  192. </p>
  193. <p><b> <a name="lexeme_and_rules" id="lexeme_and_rules"></a> The lexeme_d directive
  194. and rules</b></p>
  195. <p> <font color="#FF0000">Question:</font> Does lexeme_d not support expressions
  196. which include rules? In the example below, the definition of atomicRule compiles,
  197. </p>
  198. <pre> <span class=identifier></span><span class=identifier>rule</span><span class=special>&lt;</span><span class=identifier>phrase_scanner_t</span><span class=special>&gt; </span><span class=identifier>atomicRule</span>
  199. <span class=special>= </span><span class=identifier>lexeme_d</span><span class=special>[(</span><span class=identifier>alpha_p </span><span class=special>| </span><span class=literal>'_'</span><span class=special>) &gt;&gt; *(</span><span class=identifier>alnum_p </span><span class=special>| </span><span class=literal>'.' </span><span class=special>| </span><span class=literal>'-' </span><span class=special>| </span><span class=literal>'_'</span><span class=special>)];</span></pre>
  200. <p>but if I move <tt>alnum_p | '.' | '-' | '_'</tt> into its own rule, the compiler
  201. complains about conversion from <tt>const scanner&lt;...&gt;</tt> to <tt>const
  202. phrase_scaner_t&amp;</tt>. </p>
  203. <pre> <span class=identifier>rule</span><span class=special>&lt;</span><span class=identifier>phrase_scanner_t</span><span class=special>&gt; </span><span class=identifier>ch </span><span class=special>
  204. = </span><span class=identifier>alnum_p </span><span class=special>| </span><span class=literal>'.' </span><span class=special>| </span><span class=literal>'-' </span><span class=special>| </span><span class=literal>'_'</span><span class=special>;</span>
  205. <span class=identifier> rule</span><span class=special>&lt;</span><span class=identifier>phrase_scanner_t</span><span class=special>&gt; </span><span class=identifier>compositeRule</span>
  206. <span class=special>= </span><span class=identifier>lexeme_d</span><span class=special>[(</span><span class=identifier>alpha_p </span><span class=special>| </span><span class=literal>'_'</span><span class=special>) &gt;&gt; *(</span><span class=identifier>ch</span><span class=special>)]; </span><span class="comment">// &lt;- error source</span></pre>
  207. <p>You might get the impression that the <tt>lexeme_d</tt> directive and rules
  208. do not mix. Actually, this problem is related to the first FAQ entry: The Scanner
  209. Business. More precisely, the <tt>lexeme_d</tt> directive and rules with incompatible
  210. scanner types do not mix. This problem is more subtle. What's causing the scanner
  211. incompatibility is the directive itself. The <tt>lexeme_d</tt> directive transforms
  212. the scanner it receives into something that disables the skip parser. This non-skipping
  213. scanner, unfortunately, is incompatible with the original scanner before transformation
  214. took place.</p>
  215. <p>The simplest solution is not to use rules in the <tt>lexeme_d</tt>. Instead,
  216. you can definitely apply <tt>lexeme_d</tt> to subrules and grammars if you really
  217. need more complex parsers inside the <tt>lexeme_d</tt>. If you really must use
  218. a rule, you need to know the exact scanner used by the directive. The <tt>lexeme_scanner</tt>
  219. metafunction is your friend here. The example above will work as expected once
  220. we give the <tt>ch</tt> rule a correct scanner type:</p>
  221. <pre> <span class=identifier>rule</span><span class=special>&lt;</span><span class=identifier>lexeme_scanner</span><span class="special">&lt;</span><span class=identifier>phrase_scanner_t</span><span class=special>&gt;::</span><span class="identifier">type</span><span class=special>&gt; </span><span class=identifier>ch </span><span class=special>
  222. = </span><span class=identifier>alnum_p </span><span class=special>| </span><span class=literal>'.' </span><span class=special>| </span><span class=literal>'-' </span><span class=special>| </span><span class=literal>'_'</span><span class=special>;</span></pre>
  223. <p>Note: make sure to add &quot;<tt>typename</tt>&quot; before <tt>lexeme_scanner</tt>
  224. when this is used inside a template class or function.</p>
  225. <p>The same thing happens when rules are used inside the <tt>as_lower_d</tt> directive.
  226. In such cases, you can use the <tt>as_lower_scanner</tt>. See the <span class=identifier><tt><a href="scanner.html#lexeme_scanner">lexeme_scanner</a></tt></span>
  227. and <tt><a href="scanner.html#as_lower_scanner">as_lower_scanner</a></tt>.</p>
  228. <table width="80%" border="0" align="center">
  229. <tr>
  230. <td class="note_box"><img src="theme/bulb.gif" width="13" height="18"> See
  231. the techniques section for an <a href="techniques.html#multiple_scanner_support">example</a>
  232. of a <a href="grammar.html">grammar</a> using a <a href="rule.html#multiple_scanner_support">multiple
  233. scanner enabled rule,</a> <a href="scanner.html#lexeme_scanner">lexeme_scanner</a>
  234. and <a href="scanner.html#as_lower_scanner">as_lower_scanner.</a></td>
  235. </tr>
  236. </table>
  237. <p><strong><a name="kleene_star"></a>Kleene Star infinite loop</strong></p>
  238. <p><font color="#FF0000">Question</font>: Why Does This Loop Forever?</p>
  239. <pre> <span class=identifier>rule</span><span class=special>&lt;&gt; </span><span class=identifier>optional </span><span class=special>= !(</span>str_p<span class="special">(</span><span class="string">&quot;optional&quot;</span><span class="special">));
  240. </span><span class=identifier>rule</span><span class=special>&lt;&gt; </span><span class="identifier">list_of_optional </span><span class=special>= *</span><span class=identifier>optional</span><span class="special">;</span></pre>
  241. <p>The problem with this is that the kleene star will continue looping until it
  242. gets a no-match from it's enclosed parser. Because the <tt>optional</tt> rule
  243. is optional, it will always return a match. Even if the input doesn't match
  244. &quot;optional&quot; it will return a zero length match. <tt>list_of_optional</tt>
  245. will keep calling optional forever since optional will never return a no-match.
  246. So in general, any rule that can be &quot;nullable&quot; (meaning it can return
  247. a zero length match) must not be put inside a kleene star.</p>
  248. <p><strong><a name="CVS"></a>Boost CVS and Spirit CVS</strong></p>
  249. <p><font color="#FF0000">Question:</font> There is Boost CVS and Spirit CVS. Which
  250. is used for further development of Spirit?</p>
  251. <p> Generally, development takes place in Spirit's CVS. However, from time to
  252. time a new version of Spirit will be integrated in Boost. When this happens
  253. development takes place in the Boost CVS. There will be announcements on the
  254. Spirit mailing lists whenever the status of the Spirit CVS changes.<br>
  255. </p>
  256. <table width="80%" border="0" align="center">
  257. <tr>
  258. <td class="note_box"><img src="theme/alert.gif" width="16" height="16">
  259. During development of Spirit v1.8.1 (released as part of boost-1.32.0) and
  260. v1.6.2, Spirit's developers decided to stop maintaining Spirit CVS for
  261. BRANCH_1_8 and BRANCH_1_6. This was necessary to reduce the added work of
  262. maintaining and synch'ing two repositories. The maintenance of these branches
  263. will take place on Boost CVS. At this time, new developments towards Spirit
  264. v2 and other experimental developments are expected to happen in Spirit
  265. CVS.</td>
  266. </tr>
  267. </table>
  268. <p><strong><a name="compilation_times"></a>How to reduce compilation times with
  269. complex Spirit grammars </strong></p>
  270. <p><font color="#FF0000">Question:</font> Are there any techniques to minimize
  271. compile times using spirit? For simple parsers compile time doesn't seem to
  272. be a big issue, but recently I created a parser with about 78 rules
  273. and it took about 2 hours to compile. I would like to break the grammar up into
  274. smaller chunks, but it is not as easy as I thought it would be because rules
  275. in two grammar capsules are defined in terms of each other. Any thoughts?</p>
  276. <p> The only way to reduce compile times is </p>
  277. <ul>
  278. <li> to split up your grammars into smaller chunks</li>
  279. <li> prevent the compiler from seeing all grammar definitions at the same time
  280. (in the same compilation unit)</li>
  281. </ul>
  282. <p>The first task is merely logistical, the second is rather a technical one. </p>
  283. <p>A good example of solving the first task is given in the Spirit cpp_lexer example
  284. written by JCAB (you may find it on the <a href="http://spirit.sourceforge.net/repository/applications/show_contents.php">applications' repository</a>).
  285. </p>
  286. <p>The cross referencing problems may be solved by some kind of forward declaration,
  287. or, if this does not work, by introducing some dummy template argument to the
  288. non-templated grammars. Thus allows the instantiation time to be defered until the
  289. compiler has seen all the defintions:</p>
  290. <pre> <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T = <span class="keyword">int</span>&gt;<br> grammar2;</p>
  291. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T = <span class="keyword">int</span>&gt;<br> <span class="keyword">struct</span> grammar1 : <span class="keyword">public</span> grammar&lt;grammar1&gt;<br> {
  292. <span class="comment">// refers to grammar2&lt;&gt;</span>
  293. };
  294. <span class="keyword">template</span> &lt;typename T&gt;
  295. <span class="keyword">struct</span> grammar2 : <span class="keyword">public</span> grammar&lt;grammar2&gt;
  296. {
  297. <span class="comment">// refers to grammar1&lt;&gt;</span>
  298. };
  299. //...
  300. grammar1&lt;&gt; g; <span class="comment">// both grammars instantiated here</span>
  301. </pre>
  302. <p>The second task is slightly more complex. You must ensure that in the first
  303. compilation unit the compiler sees only some function/template <strong>declaration</strong>
  304. and in the second compilation unit the function/template <strong>definition</strong>.
  305. Still no problem, if no templates are involved. If templates are involved,
  306. you need to manually (explicitly) instantiate these templates with the correct
  307. template parameters inside a separate compilation unit. This way the compilation
  308. time is split between several compilation units, reducing the overall
  309. required time drastically too. </p>
  310. <p>For a sample, showing how to achieve this, you may want to look at the <tt>Wave</tt>
  311. preprocessor library, where this technique is used extensively. (this should be available for download from <a href="http://spirit.sf.net">Spirit's site</a> as soon as you read this).</p>
  312. <p><strong><a name="frame_assertion" id="frame_assertion"></a>Closure frame assertion</strong></p>
  313. <p><font color="#FF0000">Question:</font> When I run the parser I get an assertion
  314. <span class="string">&quot;frame.get() != 0 in file closures.hpp&quot;</span>.
  315. What am I doing wrong?</p>
  316. <p>Basically, the assertion fires when you are accessing a closure variable that
  317. is not constructed yet. Here's an example. We have three rules <tt>a</tt>, <tt>b</tt>
  318. and <tt>c</tt>. Consider that the rule <tt>a</tt> has a closure member <tt>m</tt>.
  319. Now:</p>
  320. <pre> <span class="identifier">a</span> <span class="special">=</span> <span class="identifier">b</span><span class="special">;</span>
  321. <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">int_p</span><span class="special">[</span><span class="identifier">a</span><span class="special">.</span><span class="identifier">m</span> <span class="special">=</span> 123<span class="special">];</span>
  322. <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">b</span><span class="special">;</span></pre>
  323. <p>When the rule <tt>a</tt> is invoked, its frame is set, along with its member
  324. <tt>m</tt>. So, when <tt>b</tt> is called from <tt>a</tt>, the semantic action
  325. <tt>[a.m = 123]</tt>will store <tt>123</tt> into <tt>a</tt>'s closure member
  326. <tt>m</tt>. On the other hand, when <tt>c</tt> is invoked, and <tt>c</tt> attempts
  327. to call <tt>b</tt>, no frame for <tt>a</tt> is set. Thus, when <tt>b</tt> is
  328. called from <tt>c</tt>, the semantic action <tt>[a.m = 123]</tt>will fire the
  329. <span class="string">&quot;frame.get() != 0 in file closures.hpp&quot;</span>
  330. assertion.</p>
  331. <p><strong><a name="greedy_rd" id="greedy_rd"></a>Greedy RD</strong></p>
  332. <p><font color="#FF0000">Question:</font> I'm wondering why the this won't work
  333. when parsed:</p>
  334. <pre>
  335. <span class="identifier"> a</span> <span class="special">= +</span><span class="identifier">anychar_p</span><span class="special">;</span>
  336. <span class="identifier">b</span> = <span class="string">'('</span> <span class="special">&gt;&gt;</span> <span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="string">')'</span><span class="special">;</span></pre>
  337. <p>Try this:</p>
  338. <pre>
  339. <span class="identifier"> a</span> <span class="special">= +(</span><span class="identifier">anychar_p - </span><span class="string">')'</span><span class="special">);</span>
  340. <span class="identifier">b</span> <span class="special">=</span> <span class="string">'('</span> <span class="special">&gt;&gt;</span> <span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="string">')'</span><span class="special">;</span></pre>
  341. <p>David Held writes: That's because it's like the langoliers--it eats everything
  342. up. You usually want to say what it shouldn't eat up by subtracting the terminating
  343. character from the parser. The moral being: Using <tt>*anychar_p</tt> or <tt>+anychar_p</tt>
  344. all by itself is usually a <em>Bad Thing</em>&#8482;.</p>
  345. <p>In other words: Recursive Descent is inherently greedy (however, see <a href="rationale.html#exhaustive_rd">Exhaustive
  346. backtracking and greedy RD</a>).</p>
  347. <p><span class="special"></span><strong><a name="referencing_a_rule_at_construction" id="referencing_a_rule_at_construction"></a>Referencing
  348. a rule at construction time</strong></p>
  349. <p><font color="#FF0000">Question:</font> The code below terminates with a segmentation
  350. fault, but I'm (obviously) confused about what I'm doing wrong.</p>
  351. <pre> rule<span class="special">&lt;</span>ScannerT<span class="special">,</span> clos<span class="special">::</span>context_t<span class="special">&gt;</span> id <span class="special">=</span> int_p<span class="special">[</span>id<span class="special">.</span>i <span class="special">=</span> arg1<span class="special">];</span></pre>
  352. <p>You have a rule <tt>id</tt> being constructed. Before it is constructed, you
  353. reference <tt>id.i</tt> in the RHS of the constructor. It's a chicken and egg
  354. thing. The closure member <tt>id.i</tt> is not yet constructed at that point.
  355. Using assignment will solve the problem. Try this instead:</p>
  356. <pre> rule<span class="special">&lt;</span>ScannerT<span class="special">,</span> clos<span class="special">::</span>context_t<span class="special">&gt;</span> id<span class="special">;</span>
  357. id <span class="special">=</span> int_p<span class="special">[</span>id<span class="special">.</span>i <span class="special">=</span> arg1<span class="special">];</span></pre>
  358. <p><span class="special"></span><strong><a name="storing_rules" id="storing_rules"></a>Storing
  359. Rules </strong></p>
  360. <p><font color="#FF0000">Question:</font> Why can't I store rules in STL containers
  361. for later use and why can't I pass and return rules to and from functions by
  362. value? </p>
  363. <p>EBNF is primarily declarative. Like in functional programming, It's a static
  364. recipe and there's no notion of do this then that. However, in Spirit, we managed
  365. to coax imperative C++ to take in declarative EBNF. Hah! Fun!... We did that
  366. by masquerading the C++ assignment operator to mimic EBNF's <tt>::=</tt>, among
  367. other things (e.g. <tt>&gt;&gt;</tt>, <tt>|</tt>, <tt>&amp;</tt> etc.). We used
  368. the rule class to let us do that by giving its assignment operator (and copy
  369. constructor) a different meaning and semantics. Doing so made the rule unlike
  370. any other C++ object. You can't copy it. You can't assign it. You can't place
  371. it in a container (vector, stack, etc).Heck, you can't even return it from a
  372. function *by value*.</p>
  373. <table width="80%" border="0" align="center">
  374. <tr>
  375. <td class="note_box"><img src="theme/alert.gif" width="16" height="16"> The
  376. rule is a weird object, unlike any other C++ object. It does not have the
  377. proper copy and assignment semantics and cannot be stored and passed around
  378. by value.</td>
  379. </tr>
  380. </table>
  381. <p>However nice declarative EBNF is, the dynamic nature of C++ can be an advantage.
  382. We've seen this in action here and there. There are indeed some interesting
  383. applications of dynamic parsers using Spirit. Yet, we haven't fully utilized
  384. the power of dynamic parsing, unless(!), we have a rule that's not so alien
  385. to C++ (i.e. behaves as a good C++ object). With such a beast, we can write
  386. parsers that's defined at run time, as opposed to at compile time.</p>
  387. <p>Now that I started focusing on rules (hey, check out the hunky new rule features),
  388. it might be a good time to implement the rule-holder. It is basically just a
  389. rule, but with C++ object semantics. Yet it's not as simple. Without true garbage
  390. collection, the implementation will be a bit tricky. We can't simply use reference
  391. counting because a rule-holder (hey, anyone here has a better name?) *is-a*
  392. rule, and rules are typically recursive and thus cyclic. The problem is which
  393. will own which.</p>
  394. <p>Ok... this will do for now. You'll definitely see more of the rule-holder in
  395. the coming days.</p>
  396. <p><strong><a name="parsing_ints_and_reals"></a>Parsing Ints and Reals</strong></p>
  397. <p> <font color="#FF0000">Question:</font> I was trying to parse an int or float value with the <tt>longest_d</tt> directive and put some actors on the alternatives to visualize the results. When I parse &quot;123.456&quot;, the output reports:</p>
  398. <ol>
  399. <li>(int) has been matched: full match = false</li>
  400. <li> (double) has been matched: full match = true</li>
  401. </ol>
  402. <p>That is not what I expected. What am I missing? </p>
  403. <p> Actually, the problem is that both semantic actions of the int and real branch will be triggered because both branches will be tried. This doesn't buy us much. What actually wins in the end is what you expected. But there's no easy way to know which one wins. The problem stems from the ambiguity. </p>
  404. <blockquote>
  405. <p>Case1: Consider this input: &quot;2&quot;. Is it an int or a real? They are both (strictly following the grammar of a real). </p>
  406. <p>Case2 : Now how about &quot;1.0&quot;? Is it an int or a real? They are both, albeit the int part gets a partial match: &quot;1&quot;. That is why you are getting a (partial) match for your <em>int</em> rule (full match = false). </p>
  407. </blockquote>
  408. <p> Instead of using the <tt>longest_d</tt> to parse ints and reals, what I suggest is to remove the ambiguity and use the plain short-circuiting alternatives. The first step is to use <tt><a href="numerics.html#strict_reals">strict_real_p</a> </tt>to make the first case unambiguous. Unlike
  409. <tt>real_p</tt>, <tt>strict_real_p</tt> requires a dot to be present for a number to be considered a successful match.
  410. Your grammar can be written unambiguously as:</p>
  411. <pre> strict_real_p<span class="special"> | </span>int_p</pre>
  412. <p> Note that because ambiguity is resolved, attaching actions to both branches is safe. Only one will be triggered:</p>
  413. <pre> strict_real_p<span class="special">[</span>R<span class="special">] | </span>int_p<span class="special">[</span>I<span class="special">]</span></pre>
  414. <blockquote>
  415. <p> &quot;1.0&quot; ---&gt; triggers R<br>
  416. &quot;2&quot; ---&gt; triggers I</p>
  417. </blockquote>
  418. <p> Again, as a rule of thumb, it is always best to resolve as much ambiguity as possible. The best grammars are those which involve no backtracking at all: an LL(1) grammar. Backtracking and semantic actions do not mix well.</p>
  419. <p><b><a name="output_operator" id="output_operator"></a>BOOST_SPIRIT_DEBUG and missing <tt>operator&lt;&lt;</tt></b></p>
  420. <p><font color="#FF0000">Question:</font> My code compiles fine in release mode but when I try to define <tt>BOOST_SPIRIT_DEBUG</tt> the compiler complains about a missing <tt><span class="keyword">operator</span><span class="special">&lt;&lt;</span></tt>.</p>
  421. <p>When <tt>BOOST_SPIRIT_DEBUG</tt> is defined debug output is generated for
  422. spirit parsers. To this end it is expected that each closure member has the
  423. default output operator defined.</p>
  424. <p>You may provide the operator overload either in the namespace where the
  425. class is declared (will be found through Argument Dependent Lookup) or make it visible where it is
  426. used, that is <tt><span class="keyword">namespace</span> <span
  427. class="identifier">boost</span><span class="special">::</span><span
  428. class="identifier">spirit</span></tt>. Here's an example for <tt><span
  429. class="identifier">std</span><span class="special">::</span><span
  430. class="identifier">pair</span></tt>:</p>
  431. <pre><code>
  432. <span class="preprocessor">#include</span> <span class="string">&lt;iosfwd&gt;</span>
  433. <span class="preprocessor">#include</span> <span class="string">&lt;utility&gt;</span>
  434. <span class="keyword">namespace</span> <span class="identifier">std</span> <span class="special">{</span>
  435. <span class="keyword">template</span> <span class="special">&lt;</span>
  436. <span class="keyword">typename</span> <span class="identifier">C</span><span class="special">,</span>
  437. <span class="keyword">typename</span> <span class="identifier">E</span><span class="special">,</span>
  438. <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">,</span>
  439. <span class="keyword">typename</span> <span class="identifier">T2</span>
  440. <span class="special">&gt;</span>
  441. <span class="identifier">basic_ostream</span><span class="special">&lt;</span><span class="identifier">C</span><span class="special">,</span> <span class="identifier">E</span><span class="special">&gt;</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span>
  442. <span class="identifier">basic_ostream</span><span class="special">&lt;</span><span class="identifier">C</span><span class="special">,</span> <span class="identifier">E</span><span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">out</span><span class="special">,</span>
  443. <span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">what</span><span class="special">)</span>
  444. <span class="special">{</span>
  445. <span class="keyword">return</span> <span class="identifier">out</span> <span class="special">&lt;&lt;</span> <span class="string">'('</span> <span class="special">&lt;&lt;</span> <span class="identifier">what</span><span class="special">.</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="string">", "</span>
  446. <span class="special">&lt;&lt;</span> <span class="identifier">what</span><span class="special">.</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="string">')'</span><span class="special">;</span>
  447. <span class="special">}</span>
  448. <span class="special">}</span>
  449. </code></pre>
  450. <p><b><a name="repository" id="repository"></a>Applications that used to be part of spirit</b></p>
  451. <p><font color="#FF0000">Question:</font> Where can I find <i>&lt;insert great application&gt;</i>, that used to be part of the Spirit distribution?</p>
  452. <p>Old versions of Spirit used to include applications built with it.
  453. In order to streamline the distribution they were moved to a separate
  454. <a href="http://spirit.sourceforge.net/repository/applications/show_contents.php">applications repository</a>.
  455. In that page you'll find links to full applications that use the Spirit
  456. parser framework. We encourage you to send in your own applications for
  457. inclusion (see the page for instructions).</p>
  458. <p>You may also check out the <a href="http://spirit.sourceforge.net/repository/grammars/show_contents.php">grammars' repository</a>.</p>
  459. <table width="80%" border="0" align="center">
  460. <tr>
  461. <td class="note_box">
  462. <img src="theme/note.gif" width="16" height="16"> You'll still find the
  463. example applications that complement (actually are part of) the
  464. documentation in the usual place: <code>libs/spirit/example</code>.<br>
  465. <br>
  466. <img src="theme/alert.gif" width="16" height="16"> The applications and
  467. grammars listed in the repositories are works of the respective authors.
  468. It is the author's responsibility to provide support and maintenance.
  469. Should you have any questions, please send the author an email.
  470. </td>
  471. </tr>
  472. </table>
  473. <br>
  474. <table border="0">
  475. <tr>
  476. <td width="10"></td>
  477. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  478. <td width="30"><a href="techniques.html"><img src="theme/l_arr.gif" border="0"></a></td>
  479. <td width="30"><a href="rationale.html"><img src="theme/r_arr.gif" border="0"></a></td>
  480. </tr>
  481. </table>
  482. <br>
  483. <hr size="1">
  484. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  485. <span class="copyright">Copyright &copy; 2002-2003 Hartmut Kaiser </span><br>
  486. <span class="copyright">Copyright &copy; 2006-2007 Tobias Schwinger </span><br>
  487. <br>
  488. <font size="2">Use, modification and distribution is subject to the Boost Software
  489. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  490. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  491. <p class="copyright">&nbsp;</p>
  492. </body>
  493. </html>