stored_rule.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <html>
  2. <head>
  3. <title>Storable Rules</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>Storable
  13. Rules</b></font></td>
  14. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  15. </tr>
  16. </table>
  17. <br>
  18. <table border="0">
  19. <tr>
  20. <td width="10"></td>
  21. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="stored_rule.html"><img src="theme/l_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="the_lazy_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  24. </tr>
  25. </table>
  26. <p>The rule is a weird C++ citizen, unlike any other C++ object. It does not have
  27. the proper copy and assignment semantics and cannot be stored and passed around
  28. by value. You cannot store rules in STL containers (vector, stack, etc) for
  29. later use and you cannot pass and return rules to and from functions by value.</p>
  30. <p>EBNF is primarily declarative. Like in functional programming, an EBNF grammar
  31. is a static recipe and there's no notion of do this then that. However, in Spirit,
  32. we managed to coax imperative C++ to take in declarative EBNF. Hah! Fun!...
  33. We did that by masquerading the C++ assignment operator to mimic EBNF's <tt>::=</tt>.
  34. To do that, we gave the rule class' assignment operator and copy constructor
  35. a different meaning and semantics. The downside is that doing so made the rule
  36. unlike any other C++ object. You can't copy it. You can't assign it. </p>
  37. <p>We want to have the dynamic nature of C++ to our advantage. We've seen dynamic
  38. Spirit in action here and there. There are indeed some interesting applications
  39. of dynamic parsers using Spirit. Yet, we will not fully utilize the power of
  40. dynamic parsing, unless we have a rule that behaves like any other good C++
  41. object. With such a beast, we can write full parsers that's defined at run time,
  42. as opposed to compile time.</p>
  43. <p>We now have dynamic rules: <tt>stored_rules</tt>. Basically they are rules
  44. with perfect C++ assignment/copy-constructor semantics. This means that <tt>stored_rules</tt>
  45. can be stored in containers and/or dynamically created at run-time.</p>
  46. <pre><code><font color="#000000"><span class=identifier> </span><span class=keyword>template</span><span class=special>&lt;
  47. </span><span class=keyword>typename </span><span class=identifier>ScannerT </span><span class=special>= </span><span class=identifier>scanner</span><span class=special>&lt;&gt;,
  48. </span><span class=keyword>typename </span><span class=identifier>ContextT </span><span class=special>= </span><span class=identifier>parser_context</span><span class=special>&lt;&gt;</span><span class=identifier>,
  49. </span><span class="keyword">typename</span><span class=identifier> TagT </span><span class="special">=</span><span class=identifier> parser_address_tag</span><span class=special>&gt;
  50. </span><span class=keyword>class </span><span class=identifier>stored_rule</span><span class=special>;</span></font></code></pre>
  51. <p>The interface is exactly the same as with the rule class (see the <a href="rule.html">section
  52. on rules</a> for more information regarding the API). The only difference is
  53. with the copy and assignment semantics. Now, with <tt>stored_rule</tt>s, we
  54. can dynamically and algorithmically define our rules. Here are some samples...
  55. </p>
  56. <p>Say I want to dynamically create a rule for:</p>
  57. <pre>
  58. <span class=identifier> start </span><span class=special>= </span><span class=special>*(</span><span class=identifier>a </span><span class=special>| </span><span class=identifier>b </span><span class=special>| </span><span class=identifier>c</span><span class=special>);</span></pre>
  59. <p> I can write it dynamically step-by-step:</p>
  60. <pre> <span class=identifier> stored_rule</span><span class=special>&lt;&gt; </span><span class=identifier>start</span><span class=special>;
  61. </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>a</span><span class=special>;
  62. </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>() </span><span class=special>| </span><span class=identifier>b</span><span class=special>;
  63. </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>() </span><span class=special>| </span><span class=identifier>c</span><span class=special>;
  64. </span><span class=identifier>start </span><span class=special>= </span><span class=special>*(</span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>());</span></pre>
  65. <p>Later, I changed my mind and want to redefine it (again dynamically) as:</p>
  66. <pre><span class=identifier> start </span><span class=special>= </span><span class=special>(</span><span class=identifier>a </span><span class=special>| </span><span class=identifier>b</span><span class=special>) </span><span class=special>&gt;&gt; </span><span class=special>(</span><span class=identifier>start </span><span class=special>| </span><span class=identifier>b</span><span class=special>);</span>
  67. </pre>
  68. <p>I write:</p>
  69. <pre> <span class=special> </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>b</span><span class=special>;
  70. </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>a </span><span class=special>| </span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>();
  71. </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>() </span><span class=special>&gt;&gt; </span><span class=special>(</span><span class=identifier>start </span><span class=special>| </span><span class=identifier>b</span><span class=special>);</span></pre>
  72. <p>Notice the statement:</p>
  73. <pre> <span class=special> </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>start</span><span class=special>.</span><span class=identifier>copy</span><span class=special>() </span><span class=special>| </span><span class=identifier>b</span><span class=special>;</span></pre>
  74. <p>Why is start.copy() required? Well, because like rules, stored rules are still
  75. embedded by reference when found in the RHS (one reason is to avoid cyclic-shared-pointers).
  76. If we write:</p>
  77. <pre> <span class=special> </span><span class=identifier>start </span><span class=special>= </span><span class=identifier>start </span><span class=special>| </span><span class=identifier>b</span><span class=special>;</span></pre>
  78. <p>We have <strong>left-recursion</strong>! Copying copy of start avoids self
  79. referencing. What we are doing is making a copy of start, ORing it with b, then
  80. destructively assigning the result back to start.</p>
  81. <table border="0">
  82. <tr>
  83. <td width="10"></td>
  84. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  85. <td width="30"><a href="stored_rule.html"><img src="theme/l_arr.gif" border="0"></a></td>
  86. <td width="30"><a href="the_lazy_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  87. </tr>
  88. </table>
  89. <br>
  90. <hr size="1">
  91. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  92. <br>
  93. <font size="2">Use, modification and distribution is subject to the Boost Software
  94. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  95. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  96. </body>
  97. </html>