refactoring.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Refactoring Parsers</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <link href="theme/style.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  10. <tr>
  11. <td width="10"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>&nbsp;</b></font></td>
  12. <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Refactoring Parsers</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="functor_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="regular_expression_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  23. </tr>
  24. </table>
  25. <p><a name="refactoring_parsers"></a>There are three types of Refactoring Parsers
  26. implemented right now, which help to abstract common parser refactoring tasks.
  27. Parser refactoring means, that a concrete parser construct is replaced (refactored)
  28. by another very similar parser construct. Two of the Refactoring Parsers described
  29. here (<tt>refactor_unary_parser</tt> and <tt>refactor_action_parser</tt>) are
  30. introduced to allow a simple and more expressive notation while using <a href="confix.html">Confix
  31. Parsers</a> and <a href="list_parsers.html">List Parsers</a>. The third Refactoring
  32. Parser (<tt>attach_action_parser</tt>) is implemented to abstract some functionality
  33. required for the Grouping Parser. Nevertheless
  34. these Refactoring Parsers may help in solving other complex parsing tasks too.</p>
  35. <h3>Refactoring unary parsers</h3>
  36. <p>The <tt>refactor_unary_d</tt> parser generator, which should be used to generate
  37. a unary refactoring parser, transforms a construct of the following type</p>
  38. <pre><code> <span class=identifier>refactor_unary_d</span><span class=special>[*</span><span class=identifier>some_parser </span><span class=special>- </span><span class=identifier>another_parser</span><span class=special>]</span></code></pre>
  39. <p>to </p>
  40. <pre><code> <span class=special>*(</span><span class=identifier>some_parser</span> <span class=special>- </span><span class=identifier>another_parser</span><span class=special>)</span></code></pre>
  41. <blockquote>
  42. <p>where <tt>refactor_unary_d</tt> is a predefined object of the parser generator
  43. struct <tt>refactor_unary_gen&lt;&gt;</tt></p>
  44. </blockquote>
  45. <p>The <tt>refactor_unary_d</tt> parser generator generates a new parser as shown
  46. above, only if the original construct is an auxilliary binary parser (here the
  47. difference parser) and the left operand of this binary parser is an auxilliary
  48. unary parser (here the kleene star operator). If the original parser isn't a
  49. binary parser the compilation will fail. If the left operand isn't an unary
  50. parser, no refactoring will take place.</p>
  51. <h3>Refactoring action parsers</h3>
  52. <p>The <tt>refactor_action_d</tt> parser generator, which should be used to generate
  53. an action refactoring parser, transforms a construct of the following type</p>
  54. <pre><code> <span class=identifier>refactor_action_d</span><span class=special>[</span><span class=identifier>some_parser</span><span class=special>[</span><span class=identifier>some_actor</span><span class=special>] </span><span class=special>- </span><span class=identifier>another_parser</span><span class=special>]</span></code></pre>
  55. <p>to </p>
  56. <pre><code> <span class=special>(</span><span class=identifier>some_parser </span><span class=special>- </span><span class=identifier>another_parser</span><span class=special>)[</span><span class=identifier>some_actor</span><span class=special>]</span></code></pre>
  57. <blockquote>
  58. <p>where <tt>refactor_action_d</tt> is a predefined object of the parser generator
  59. struct <tt>refactor_action_gen&lt;&gt;</tt></p>
  60. </blockquote>
  61. <p>The <tt>refactor_action_d</tt> parser generator generates a new parser as shown
  62. above, only if the original construct is an auxilliary binary parser (here the
  63. difference parser) and the left operand of this binary parser is an auxilliary
  64. parser generated by an attached semantic action. If the original parser isn't
  65. a binary parser the compilation will fail. If the left operand isn't an action
  66. parser, no refactoring will take place.</p>
  67. <h3>Attach action refactoring</h3>
  68. <p>The <tt>attach_action_d</tt> parser generator, which should be used to generate
  69. an attach action refactoring parser, transforms a construct of the following
  70. type</p>
  71. <pre><code> <span class=identifier>attach_action_d</span><span class=special>[</span><span class=identifier>(some_parser</span> <span class=special>&gt;&gt; </span><span class=identifier>another_parser</span>)<span class=special>[</span><span class=identifier>some_actor</span><span class=special>]</span><span class=special>]</span></code></pre>
  72. <p>to </p>
  73. <pre><code> <span class=identifier>some_parser</span><span class=special>[</span><span class=identifier>some_actor</span><span class=special>]</span><span class=identifier> </span><span class=special>&gt;&gt; </span><span class=identifier>another_parser</span><span class=special>[</span><span class=identifier>some_actor</span><span class=special>]</span></code></pre>
  74. <blockquote>
  75. <p>where <tt>attach_action_d</tt> is a predefined object of the parser generator
  76. struct <tt>attach_action_gen&lt;&gt;</tt></p>
  77. </blockquote>
  78. <p>The <tt>attach_action_d</tt> parser generator generates a new parser as shown
  79. above, only if the original construct is an auxilliary action parser and the
  80. parser to it this action is attached is an auxilliary binary parser (here the
  81. sequence parser). If the original parser isn't a action parser the compilation
  82. will fail. If the parser to which the action is attached isn't an binary parser,
  83. no refactoring will take place.</p>
  84. <h3>Nested refactoring</h3>
  85. <p>Sometimes it is required to nest different types of refactoring, i.e. to transform
  86. constructs like</p>
  87. <pre><code> <span class=special>(*</span><span class=identifier>some_parser</span><span class=special>)[</span><span class=identifier>some_actor</span><span class=special>] </span><span class=special>- </span><span class=identifier>another_parser</span></code></pre>
  88. <p>to </p>
  89. <pre><code> <span class=special>(*(</span><span class=identifier>some_parser </span><span class=special>- </span><span class=identifier>another_parser</span><span class=special>))[</span><span class=identifier>some_actor</span><span class=special>]</span></code></pre>
  90. <p>To simplify the construction of such nested refactoring parsers the <tt>refactor_unary_gen&lt;&gt;</tt>
  91. and <tt>refactor_action_gen&lt;&gt;</tt> both can take another refactoring parser
  92. generator type as their respective template parameter. For instance, to construct
  93. a refactoring parser generator for the mentioned nested transformation we should
  94. write:</p>
  95. <pre><span class=special> </span><span class=keyword>typedef </span><span class=identifier>refactor_action_gen</span><span class=special>&lt;</span><span class=identifier>refactor_unary_gen</span><span class=special>&lt;&gt; </span><span class=special>&gt; </span><span class=identifier>refactor_t</span><span class=special>;
  96. </span><span class=keyword>const </span><span class=identifier>refactor_t </span><span class=identifier>refactor_nested_d </span><span class=special>= </span><span class=identifier>refactor_t</span><span class=special>(</span><span class=identifier>refactor_unary_d</span><span class=special>);</span></pre>
  97. <p>Now we could use it as follows to get the required result:</p>
  98. <pre><code><font color="#0000FF"> </font><span class=identifier>refactor_nested_d</span><span class=special>[(*</span><span class=identifier>some_parser</span><span class=special>)[</span><span class=identifier>some_actor</span><span class=special>] </span><span class=special>- </span><span class=identifier>another_parser</span><span class=special>]</span></code></pre>
  99. <p>An empty template parameter means not to nest this particular refactoring parser.
  100. The default template parameter is <tt>non_nesting_refactoring</tt>, a predefined
  101. helper structure for inhibiting nesting. Sometimes it is required to nest a
  102. particular refactoring parser with itself. This is achieved by providing the
  103. predefined helper structure <tt>self_nested_refactoring</tt> as the template
  104. parameter to the corresponding refactoring parser generator template.</p>
  105. <p><img src="theme/lens.gif" width="15" height="16"> See <a href="../example/fundamental/refactoring.cpp">refactoring.cpp</a> for a compilable example. This is part of the Spirit distribution. </p>
  106. <table border="0">
  107. <tr>
  108. <td width="10"></td>
  109. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  110. <td width="30"><a href="functor_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  111. <td width="30"><a href="regular_expression_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  112. </tr>
  113. </table>
  114. <br>
  115. <hr size="1">
  116. <p class="copyright">Copyright &copy; 2001-2003 Hartmut Kaiser<br>
  117. <br>
  118. <font size="2">Use, modification and distribution is subject to the Boost Software
  119. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  120. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  121. <p>&nbsp;</p>
  122. </body>
  123. </html>