rationale.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <html>
  2. <head>
  3. <title>Rationale</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>Rationale</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="faq.html"><img src="theme/l_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="acknowledgments.html"><img src="theme/r_arr.gif" border="0"></a></td>
  23. </tr>
  24. </table>
  25. <p><img src="theme/lens.gif" width="15" height="16"> <strong>Virtual functions:
  26. From static to dynamic C++</strong></p>
  27. <p>Rules straddle the border between static and dynamic C++. In effect, a rule
  28. transforms compile-time polymorphism (using templates) into run-time polymorphism
  29. (using virtual functions). This is necessary due to C++'s inability to automatically
  30. declare a variable of a type deduced from an arbitrarily complex expression
  31. in the right-hand side (rhs) of an assignment. Basically, we want to do something
  32. like:</p>
  33. <pre><code><font color="#000000"> <span class=identifier>T </span><span class=identifier>rule </span><span class=special>= </span><span class=identifier>an_arbitrarily_complex_expression</span><span class=special>;</span></font></code></pre>
  34. <p>without having to know or care about the resulting type of the right-hand side
  35. (rhs) of the assignment expression. Apart from this, we also need a facility
  36. to forward declare an unknown type:</p>
  37. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>T </span><span class=identifier>rule</span><span class=special>;
  38. </span><span class=special>...
  39. </span><span class=identifier>rule </span><span class=special>= </span><span class=identifier>a </span><span class=special>| </span><span class=identifier>b</span><span class=special>;</span></font></code></pre>
  40. <p>These limitations lead us to this implementation of rules. This comes at the
  41. expense of the overhead of a virtual function call, once through each invocation
  42. of a rule.</p>
  43. <p><img src="theme/lens.gif" width="15" height="16"> <strong>Multiple declaration
  44. </strong> </p>
  45. <p>Some BNF variants allow multiple declarations of a <tt>rule</tt>. The declarations
  46. are taken as alternatives. Example:</p>
  47. <pre>
  48. <span class=identifier><code>r </code></span><code><span class=special>= </span><span class=identifier>a</span><span class=special>; </span><span class=identifier>
  49. r </span><span class=special>= </span><span class=identifier>b</span><span class=special>;</span></code></pre>
  50. <p> is equivalent to: </p>
  51. <pre>
  52. <span class=identifier><code>r </code></span><code><span class=special>= </span><span class=identifier>a </span><span class=special>| </span><span class=identifier>b</span><span class=special>;</span></code></pre>
  53. <p>Spirit v1.3 allowed this behavior. However, the current version of Spirit <b>no
  54. longer</b> allows this because experience shows that this behavior leads to
  55. unwanted gotchas (for instance, it does not allow rules to be held in containers).
  56. In the current release of Spirit, a second assignment to a rule will simply
  57. redefine it. The old definition is destructed. This follows more closely C++
  58. semantics and is more in line with what the user expects the rule to behave.</p>
  59. <p><img src="theme/lens.gif" width="15" height="16"> <b>Sequencing Syntax</b>
  60. <br>
  61. <br>
  62. The comma operator as in a, b seems to be a better candidate, syntax-wise. But
  63. then the problem is with its precedence. It has the lowest precedence in C/C++,
  64. which makes it virtually useless. <br>
  65. <br>
  66. Bjarne Stroustrup, in his article <a href="references.html#generalized_overloading">&quot;Generalizing
  67. Overloading for C++2000&quot;</a> talks about overloading whitespace. Such a
  68. feature would allow juxtapositioning of parser objects exactly as we do in (E)BNF
  69. (e.g. a b | c instead of a &gt;&gt; b | c). Unfortunately, the article was dated
  70. April 1, 1998. Oh well.</p>
  71. <p><img src="theme/lens.gif" width="15" height="16"> <b>Forward iterators</b><br>
  72. <br>
  73. In general, the scanner expects at least a standard conforming forward iterator.
  74. Forward iterators are needed for backtracking where the iterator needs to be
  75. saved and restored later. Generally speaking, Spirit is a backtracking parser.
  76. The implication of this is that at some point, the iterator position will have
  77. to be saved to allow the parser to backtrack to a previous point. Thus, for
  78. backtracking to work, the framework requires at least a forward iterator.<br>
  79. <br>
  80. Some parsers might require more specialized iterators (bi-directional or even
  81. random access). Perhaps in the future, deterministic parsers when added to the
  82. framework, will perform no backtracking and will need just a single token lookahead,
  83. hence will require input iterators only.</p>
  84. <p><img src="theme/lens.gif" width="15" height="16"><b> Why are subrules important?</b><br>
  85. <br>
  86. Subrules open up the oportunity to do aggressive meta programming as well because
  87. they do not rely on virtual functions. The virtual function is the meta-programmer's
  88. hell. Not only does it slow down the program due to the virtual function indirect
  89. call, it is also an opaque wall where no metaprogram can get past. It kills
  90. all meta-information beyond the virtual function call. Worse, the virtual function
  91. cannot be templated. Which means that its arguments have to be tied to a actual
  92. types. Many problems stem from this limitation. <br>
  93. <br>
  94. While Spirit is a currently classified as a non-deterministic recursive-descent
  95. parser, Doug Gregor first noted that other parsing techniques apart from top-down
  96. recursive descent may be applied. For instance, apart from non-deterministic
  97. recursive descent, deterministic LL(1) and LR(1) can theoretically be implemented
  98. using the same expression template front end. Spirit rules use virtual functions
  99. to encode the RHS parser expression in an opaque abstract parser type. While
  100. it serves its purpose well, the rule's virtual functions are the stumbling blocks
  101. to more advanced metaprogramming. Subrules are free from virtual functions.</p>
  102. <p><img src="theme/lens.gif" width="15" height="16"><b> <a name="exhaustive_rd"></a>Exhaustive
  103. backtracking and greedy RD</b></p>
  104. <p>Spirit doesn't do exhaustive backtracking like regular expressions are expected
  105. to. For example:</p>
  106. <pre> <span class="special">*</span>chlit_p<span class="special">(</span><span class="quotes">'a'</span><span class="special">) &gt;&gt;</span> chlit_p<span class="special">(</span><span class="quotes">'a'</span><span class="special">);</span></pre>
  107. <p>will always fail to match because Spirit's Kleene star does not back off when
  108. the rest of the rule fails to match. </p>
  109. <p>Actually, there's a solution to this greedy RD problem. Such a scheme is discussed
  110. in section 6.6.2 of <a
  111. href="http://www.cs.vu.nl/%7Edick/PTAPG.html">Parsing Techniques: A Practical
  112. Guide</a>. The trick involves passing a <em>tail</em> parser (in addition to
  113. the scanner) to each parser. The start parser will then simply be: <tt>start
  114. &gt;&gt; end_p;</tt> (end_p is the start's tail). </p>
  115. <p>Spirit is greedy --using straight forward, naive RD. It is certainly possible
  116. to implement the fully backtracking scheme presented above, but there will be
  117. also certainly be a performance hit. The scheme will always try to match all
  118. possible parser paths (full parser hierarchy traversal) until it reaches a point
  119. of certainty, that the whole thing matches or fails to match. </p>
  120. <table border="0" width="80%" align="center">
  121. <tr>
  122. <td class="note_box"><p><img src="theme/note.gif" width="16" height="16"><b>Backtracking
  123. and Greedy RD </b><br>
  124. <br>
  125. Spirit is quite consistent and intuitive about when it backtracks and
  126. to where, although it may not be obvious to those coming from different
  127. backgrounds. In general, any (sub)parser will, given the same input, always
  128. match the same portion of the input (or fail to match the input at all).
  129. This means that Spirit is inherently greedy. Spirit will only backtrack
  130. when a (sub)parser fails to match the input, and it will always backtrack
  131. to the next choice point upward (not backward) in the parser structure.
  132. In other words abb|ab will match &quot;ab&quot;, as will a(bb|b), but
  133. (ab|a)b won't because the (ab|a) subparser will always match the 'b' after
  134. the 'a' if it is available.</p>
  135. <p>--Rainer Deyke</p>
  136. </td>
  137. </tr>
  138. </table>
  139. <p>There's a strong preference on &quot;simplicity with all the knobs when you
  140. need them&quot; approach, right now. On the other hand, the flexibility of Spirit
  141. makes it possible to have different optional schemes available. It might be
  142. possible to implement an exhaustive backtracking RD scheme as an optional feature
  143. in the future. </p>
  144. <table border="0">
  145. <tr>
  146. <td width="10"></td>
  147. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  148. <td width="30"><a href="faq.html"><img src="theme/l_arr.gif" border="0"></a></td>
  149. <td width="30"><a href="acknowledgments.html"><img src="theme/r_arr.gif" border="0"></a></td>
  150. </tr>
  151. </table>
  152. <br>
  153. <hr size="1">
  154. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  155. <br>
  156. <font size="2">Use, modification and distribution is subject to the Boost Software
  157. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  158. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  159. <p class="copyright">&nbsp;</p>
  160. </body>
  161. </html>