loops.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <html>
  2. <head>
  3. <title> Loops</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%">
  13. <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b> Loops</b></font>
  14. </td>
  15. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  16. </tr>
  17. </table>
  18. <br>
  19. <table border="0">
  20. <tr>
  21. <td width="10"></td>
  22. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="escape_char_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  24. <td width="30"><a href="character_sets.html"><img src="theme/r_arr.gif" border="0"></a></td>
  25. </tr>
  26. </table>
  27. <p>So far we have introduced a couple of EBNF operators that deal with looping.
  28. We have the <tt>+</tt> positive operator, which matches the preceding symbol
  29. one (1) or more times, as well as the Kleene star <tt>*</tt> which matches the
  30. preceding symbol zero (0) or more times.</p>
  31. <p>Taking this further, we may want to have a generalized loop operator. To some
  32. this may seem to be a case of overkill. Yet there are grammars that are impractical
  33. and cumbersome, if not impossible, for the basic EBNF iteration syntax to specify.
  34. Examples:</p>
  35. <blockquote>
  36. <p><img src="theme/bullet.gif" width="12" height="12"> A file name may have
  37. a maximum of 255 characters only.<br>
  38. <img src="theme/bullet.gif" width="12" height="12"> A specific bitmap file
  39. format has exactly 4096 RGB color information. <br>
  40. <img src="theme/bullet.gif" width="12" height="12"> A 32 bit binary string
  41. (1..32 1s or 0s).</p>
  42. </blockquote>
  43. <p>Other than the Kleene star <tt>*</tt>, the Positive closure <tt>+</tt>, and
  44. the optional <tt>!</tt>, a more flexible mechanism for looping is provided for
  45. by the framework. <br>
  46. </p>
  47. <table width="80%" border="0" align="center">
  48. <tr>
  49. <td colspan="2" class="table_title">Loop Constructs</td>
  50. </tr>
  51. <tr>
  52. <td class="table_cells" width="26%"><b>repeat_p (n) [p]</b></td>
  53. <td class="table_cells" width="74%">Repeat <b>p</b> exactly <b>n</b> times</td>
  54. </tr>
  55. <tr>
  56. <td class="table_cells" width="26%"><b>repeat_p (n1, n2) [p]</b></td>
  57. <td class="table_cells" width="74%">Repeat <b>p</b> at least <b>n1</b> times
  58. and at most <b>n2</b> times</td>
  59. </tr>
  60. <tr>
  61. <td class="table_cells" width="26%"><b>repeat_p (n, more) [p] </b></td>
  62. <td class="table_cells" width="74%">Repeat <b>p</b> at least <b>n</b> times,
  63. continuing until <b>p</b> fails or the input is consumed</td>
  64. </tr>
  65. </table>
  66. <p>Using the <tt>repeat_p</tt> parser, we can now write our examples above:</p>
  67. <p>A file name with a maximum of 255 characters:<br>
  68. </p>
  69. <pre> <span class=identifier>valid_fname_chars </span><span class=special>= </span><span class=comment>/*..*/</span><span class=special>;
  70. </span><span class=identifier>filename </span><span class=special>= </span><span class=identifier>repeat_p</span><span class=special>(</span><span class=number>1</span><span class=special>, </span><span class=number>255</span><span class=special>)[</span><span class=identifier>valid_fname_chars</span><span class=special>];</span></pre>
  71. <p>A specific bitmap file format which has exactly 4096 RGB color information:<span class=special><br>
  72. </span></p>
  73. <pre> <span class=identifier>uint_parser</span><span class=special>&lt;</span><span class=keyword>unsigned</span><span class=special>, </span><span class=number>16</span><span class=special>, </span><span class=number>6</span><span class=special>, </span><span class=number>6</span><span class=special>&gt; </span><span class=identifier>rgb_p</span><span class=special>;
  74. </span><span class=identifier>bitmap </span><span class=special>= </span><span class=identifier>repeat_p</span><span class=special>(</span><span class=number>4096</span><span class=special>)[</span><span class=identifier>rgb_p</span><span class=special>];</span></pre>
  75. <p>As for the 32 bit binary string (1..32 1s or 0s), of course we could have easily
  76. used the <tt>bin_p</tt> numeric parser instead. For the sake of demonstration
  77. however:<span class=special><br>
  78. </span></p>
  79. <pre> <span class=identifier>bin</span><span class=number>32</span> <span class=special>= </span><span class=identifier>lexeme_d</span><span class=special>[</span><span class=identifier>repeat_p</span><span class=special>(</span>1, <span class=number>32</span><span class=special>)[</span><span class=identifier>ch_p</span><span class=special>(</span><span class=literal>'1'</span><span class=special>) </span><span class=special>| </span><span class=literal>'0'</span><span class=special>]];</span></pre>
  80. <table width="80%" border="0" align="center">
  81. <tr>
  82. <td class="note_box"><img src="theme/note.gif" width="16" height="16"> Loop
  83. parsers are run-time <a href="parametric_parsers.html">parametric</a>.</td>
  84. </tr>
  85. </table>
  86. <p>The Loop parsers can be dynamic. Consider the parsing of a binary file of Pascal-style
  87. length prefixed string, where the first byte determines the length of the incoming
  88. string. Here's a sample input:
  89. <blockquote>
  90. <table width="363" border="0" cellspacing="0" cellpadding="0">
  91. <tr>
  92. <td class="dk_grey_bkd">
  93. <table width="100%" border="0" cellspacing="2" cellpadding="2">
  94. <tr>
  95. <td class="white_bkd" width=8%">
  96. <div align="center">11</div>
  97. </td>
  98. <td class="white_bkd" width="8%">
  99. <div align="center">h</div>
  100. </td>
  101. <td class="white_bkd" width="8%">
  102. <div align="center">e</div>
  103. </td>
  104. <td class="white_bkd" width="8%">
  105. <div align="center">l</div>
  106. </td>
  107. <td class="white_bkd" width="8%">
  108. <div align="center">l</div>
  109. </td>
  110. <td class="white_bkd" width="8%">
  111. <div align="center">o</div>
  112. </td>
  113. <td class="white_bkd" width="8%">
  114. <div align="center"> _</div>
  115. </td>
  116. <td class="white_bkd" width="8%">
  117. <div align="center">w</div>
  118. </td>
  119. <td class="white_bkd" width="8%">
  120. <div align="center">o</div>
  121. </td>
  122. <td class="white_bkd" width="8%">
  123. <div align="center">r</div>
  124. </td>
  125. <td class="white_bkd" width="8%">
  126. <div align="center">l</div>
  127. </td>
  128. <td class="white_bkd" width="8%">
  129. <div align="center">d</div>
  130. </td>
  131. </tr>
  132. </table>
  133. </td>
  134. </tr>
  135. </table>
  136. </blockquote>
  137. <p>This trivial example cannot be practically defined in traditional EBNF. Although
  138. some EBNF syntax allow more powerful repetition constructs other than the Kleene
  139. star, we are still limited to parsing fixed strings. The nature of EBNF forces
  140. the repetition factor to be a constant. On the other hand, Spirit allows the
  141. repetition factor to be variable at run time. We could write a grammar that
  142. accepts the input string above:</p>
  143. <pre><span class=identifier> </span><span class=keyword>int </span><span class=identifier>c</span><span class=special>;
  144. </span><span class=identifier>r </span><span class=special>= </span><span class=identifier>anychar_p</span><span class=special>[</span><span class=identifier>assign_a</span><span class=special>(</span><span class=identifier>c</span><span class=special>)] </span><span class=special>&gt;&gt; </span><span class=identifier>repeat_p</span><span class=special>(</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>ref</span><span class=special>(</span><span class=identifier>c</span><span class=special>))[</span><span class=identifier>anychar_p</span><span class=special>];</span></pre>
  145. <p>The expression</p>
  146. <pre> <span class=identifier>anychar_p</span><span class=special>[</span><span class=identifier>assign_a</span><span class=special>(</span><span class=identifier>c</span><span class=special>)]</span></pre>
  147. <p>extracts the first character from the input and puts it in <tt>c</tt>. What
  148. is interesting is that in addition to constants, we can also use variables as
  149. parameters to <tt>repeat_p</tt>, as demonstrated in </p>
  150. <pre> <span class=identifier>repeat_p</span><span class=special>(</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>ref</span><span class=special>(</span><span class=identifier>c</span><span class=special>)</span><span class=special>)</span><span class=special>[</span><span class=identifier>anychar_p</span><span class=special>]</span></pre>
  151. <p>Notice that <tt>boost::ref</tt> is used to reference the integer <tt>c</tt>.
  152. This usage of <tt>repeat_p</tt> makes the parser defer the evaluation of the
  153. repetition factor until it is actually needed. Continuing our example, since
  154. the value 11 is already extracted from the input, <tt>repeat_p</tt> is is now
  155. expected to loop exactly 11 times.</p>
  156. <table border="0">
  157. <tr>
  158. <td width="10"></td>
  159. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  160. <td width="30"><a href="escape_char_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  161. <td width="30"><a href="character_sets.html"><img src="theme/r_arr.gif" border="0"></a></td>
  162. </tr>
  163. </table>
  164. <br>
  165. <hr size="1">
  166. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  167. <br>
  168. <font size="2">Use, modification and distribution is subject to the Boost Software
  169. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  170. http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  171. </body>
  172. </html>