dynamic_parsers.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head>
  3. <title>Dynamic Parsers</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <link rel="stylesheet" href="theme/style.css" type="text/css"></head>
  5. <body>
  6. <table background="theme/bkd2.gif" border="0" cellspacing="2" width="100%">
  7. <tbody><tr>
  8. <td width="10">
  9. </td>
  10. <td width="85%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="6"><b>Dynamic
  11. Parsers </b></font></td>
  12. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0" height="48" width="112"></a></td>
  13. </tr>
  14. </tbody></table>
  15. <br>
  16. <table border="0">
  17. <tbody><tr>
  18. <td width="10"></td>
  19. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  20. <td width="30"><a href="closures.html"><img src="theme/l_arr.gif" border="0"></a></td>
  21. <td width="30"><a href="stored_rule.html"><img src="theme/r_arr.gif" border="0"></a></td>
  22. </tr>
  23. </tbody></table>
  24. <p>We see dynamic parsing everywhere in Spirit. A special group of
  25. parsers, aptly named dynamic parsers, form the most basic building
  26. blocks to dynamic parsing. This chapter focuses on these critters.
  27. You'll notice the similarity of these parsers with C++'s control
  28. structures. The similarity is not a coincidence. These parsers give an
  29. imperative flavor to parsing, and, since imperative constructs are not
  30. native to declarative EBNF, mimicking the host language, C++, should
  31. make their use immediately familiar. </p>
  32. <p>Dynamic parsers modify the parsing behavior according to conditions. Constructing
  33. dynamic parsers requires a condition argument and a body parser argument. Additional
  34. arguments are required by some parsers.</p>
  35. <h2>Conditions</h2>
  36. <p>Functions or functors returning values convertable to bool can be used as conditions.
  37. When the evaluation of the function/functor yields true it will be considered
  38. as meeting the condition.</p>
  39. <p>Parsers can be used as conditions, as well. When the parser matches the condition
  40. is met. Parsers used as conditions work in an all-or-nothing manner: the scanner
  41. will not be advanced when they don't match.</p>
  42. <p>A failure to meet the condition will not result in a parse error.</p>
  43. <h2>if_p</h2>
  44. <p><tt>if_p</tt> can be used with or without an else-part. The syntax is:</p>
  45. <pre> <span class="identifier">if_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">then</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre>
  46. <p><span class="special"></span>or</p>
  47. <pre><span class="identifier"> if_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">then</span><span class="special">-</span><span class="identifier">parser</span><span class="special">].</span><span class="identifier">else_p</span><span class="special">[</span><span class="identifier">else</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre>
  48. <p>When the condition is met the then-parser is used next in the parsing process.
  49. When the condition is not met and an else-parser is available the else-parser
  50. is used next. When the condition isn't met and no else-parser is available then
  51. the whole parser matches the empty sequence. (<img src="theme/alert.gif" height="16" width="16">
  52. Note: older versions of <tt>if_p</tt> report a failure when the condition isn't
  53. met and no else-parser is available.)</p>
  54. <p>Example:</p>
  55. <pre> <span class="special"></span><span class="identifier">if_p</span><span class="special">(</span><span class="string">"0x"</span><span class="special">)[</span><span class="identifier">hex_p</span><span class="special">].</span><span class="identifier">else_p</span><span class="special">[</span><span class="identifier">uint_p</span><span class="special">]</span></pre>
  56. <h2>while_p, do_p</h2>
  57. <p><tt>while_p</tt>/<tt>do_p</tt> syntax is:</p>
  58. <pre> <span class="identifier">while_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]<br> </span><span class="identifier">do_p</span><span class="special">[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">].</span><span class="identifier">while_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)</span></pre>
  59. <p>As long as the condition is met the dynamic parser constructed by <tt>while_p</tt>
  60. will try to match the body-parser. <tt>do_p</tt> returns a parser that tries
  61. to match the body-parser and then behaves just like the parser returned by <tt>while_p</tt>.
  62. A failure to match the body-parser will cause a failure to be reported by the
  63. while/do-parser.</p>
  64. <p>Example:</p>
  65. <pre><span class="special"> </span><span class="identifier">uint_p</span><span class="special">[</span><span class="identifier">assign_a</span><span class="special">(</span><span class="identifier">sum</span><span class="special">)] &gt;&gt; </span><span class="identifier">while_p</span><span class="special">(</span><span class="literal">'+'</span><span class="special">)[</span><span class="identifier">uint_p</span><span class="special">[</span><span class="identifier">add</span><span class="special">(</span><span class="identifier">sum</span><span class="special">)]]<br> </span><span class="literal">'"' </span><span class="special">&gt;&gt; </span><span class="identifier">while_p</span><span class="special">(~</span><span class="identifier">eps_p</span><span class="special">(</span><span class="literal">'"'</span><span class="special">))[</span><span class="identifier">c_escape_ch_p</span><span class="special">[</span><span class="identifier">push_back_a</span><span class="special">(</span><span class="identifier">result</span><span class="special">)]] &gt;&gt; </span><span class="literal">'"'</span>
  66. </pre>
  67. <p>Assuming <span style="font-family: monospace;">add</span> is a user defined function object.<br></p><h2>for_p</h2>
  68. <p><tt>for_p</tt> requires four arguments. The syntax is:</p>
  69. <pre> <span class="literal"></span><span class="identifier">for_p</span><span class="special">(</span><span class="identifier">init</span><span class="special">, </span><span class="identifier">condition</span><span class="special">, </span><span class="identifier">step</span><span class="special">)[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre>
  70. <p>init and step have to be 0-ary functions/functors. for_p returns a parser that
  71. will:</p>
  72. <ol>
  73. <li> call init</li>
  74. <li>check the condition, if the
  75. condition isn't met then a match is returned. The match will cover
  76. everything that has been matched successfully up to this point.</li>
  77. <li> tries to match the body-parser. A failure to match the body-parser will cause a failure to be reported by the for-parser</li>
  78. <li> calls step</li>
  79. <li> goes to 2.</li>
  80. </ol>
  81. <table border="0">
  82. <tbody><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="closures.html"><img src="theme/l_arr.gif" border="0"></a></td>
  86. <td width="30"><a href="stored_rule.html"><img src="theme/r_arr.gif" border="0"></a></td>
  87. </tr>
  88. </tbody></table>
  89. <br>
  90. <hr size="1">
  91. <p class="copyright">Copyright © 2002-2003 Joel de Guzman<br>
  92. Copyright © 2002-2003 Martin Wille<br>
  93. <br>
  94. <font size="2">Use, modification and distribution is subject to the Boost Software
  95. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  96. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  97. <p class="copyright">&nbsp;</p>
  98. </body></html>