epsilon.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <html>
  2. <head>
  3. <title>Epsilon</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>Epsilon</b></font>
  13. </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="rule.html"><img src="theme/l_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="directives.html"><img src="theme/r_arr.gif" border="0"></a></td>
  24. </tr>
  25. </table>
  26. <p>The <strong>Epsilon</strong> (<tt>epsilon_p</tt> and <tt>eps_p</tt>) is a multi-purpose
  27. parser that returns a zero length match. </p>
  28. <h3>Simple Form</h3>
  29. <p>In its simplest form, epsilon_p matches the null string and always returns
  30. a match of zero length:</p>
  31. <pre><code><span class=special> </span><span class="identifier">epsilon_p </span><span class="comment">// always returns a zero-length match</span></code></pre>
  32. <p>This form is usually used to trigger a <a href="semantic_actions.html">semantic
  33. action</a> unconditionally. For example, it is useful in triggering error messages
  34. when a set of alternatives fail:</p>
  35. <pre><code><span class=special> </span><span class="identifier">r</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><span class="identifier">eps_p</span><span class="special">[</span><span class="identifier">error</span><span class="special">];</span><span class="identifier"></span><span class="comment"> // error if A, B, or C fails to match</span></code></pre>
  36. <h3>Semantic Predicate</h3>
  37. <p>Semantic predicates allow you to attach a function anywhere in the grammar.
  38. In this role, the epsilon takes a 0-ary (nullary) function/functor. The run-time
  39. function/functor is typically a test that is called upon to resolve ambiguity
  40. in the grammar. A parse failure will be reported when the function/functor result
  41. evaluates to false. Otherwise an empty match will be reported. The general form
  42. is:</p>
  43. <pre> eps_p<span class="special">(</span>f<span class="special">) &gt;&gt;</span> rest<span class="special">;</span>
  44. </pre>
  45. <p>The nullary function <tt>f</tt> is called to do a semantic test (say, checking
  46. if a symbol is in the <a href="symbols.html">symbol table</a>). If test returns
  47. <tt>true</tt>, <tt>rest</tt> will be evaluated. Otherwise, the production will
  48. return early with a no-match without ever touching <tt>rest</tt>.</p>
  49. <h3>Syntactic Predicate</h3>
  50. <p>Similar to Semantic predicates, Syntactic predicates assert a certain conditional
  51. syntax to be satisfied before evaluating another production. This time, epsilon_p
  52. accepts a (conditional) parser. The general form is:</p>
  53. <pre> eps_p<span class="special">(</span>p<span class="special">) &gt;&gt;</span> rest<span class="special">;</span>
  54. </pre>
  55. <p>If <tt>p</tt> is matched on the input stream then attempt to recognize <tt>rest</tt>.
  56. The parser <tt>p </tt>is called to do a syntax check. Regardless of <tt>p</tt>'s
  57. success, <tt>eps_p(p)</tt> will always return a zero length match (i.e. the
  58. input is not consumed). If test returns <tt>true</tt>, <tt>rest</tt> will be
  59. evaluated. Otherwise, the production will return early with a no-match without
  60. ever touching <tt>rest</tt>.</p>
  61. <p>Example:</p>
  62. <pre><code><span class=special> </span><span class="identifier">eps_p</span><span class="special">(</span><span class="literal">'0'</span><span class="special">) &gt;&gt; </span><span class="identifier">oct_p </span><span class="comment">// note that '0' is actually a ch_p('0')</span><span class="identifier"> </span></code></pre>
  63. <p>Epsilon here is used as a syntactic predicate. <tt>oct_p</tt> (see <a href="numerics.html">numerics</a>)
  64. is parsed only if we see a leading <tt>'0'</tt>. Wrapping the leading <tt>'0'</tt>
  65. inside an epsilon makes the parser not consume anything from the input. If a
  66. <tt>'0'</tt> is seen, <tt>epsilon_p</tt> reports a successful match with zero
  67. length. </p>
  68. <table width="80%" border="0" align="center">
  69. <tr>
  70. <td class="note_box"><div align="justify"><img src="theme/note.gif" width="16" height="16">
  71. <b>Primitive arguments</b> <br>
  72. <br>
  73. Epsilon allows primitive type arguments such as <tt>char</tt>, <tt>int</tt>,
  74. <tt>wchar_t</tt>, <tt>char const<span class="operators">*</span></tt>,
  75. <tt>wchar_t const<span class="operators">*</span></tt> and so on. Examples:
  76. <tt><br>
  77. <br>
  78. </tt><code><span class="identifier">eps_p</span><tt><span class=special>(</span><span class=string>"hello"</span><span class=special>)</span><span class=comment>
  79. // same as eps_p(str_p("hello"))</span></tt><span class=identifier><br>
  80. eps_p</span><span class=special>(</span><span class=literal>'x'</span><span class="special">)
  81. </span><span class=comment>// same as eps_p(ch_p('x'))</span></code></div></td>
  82. </tr>
  83. </table>
  84. <h3><img src="theme/alert.gif" width="16" height="16"> Inhibiting Semantic Actions</h3>
  85. <p>In a syntactic predicate <tt>eps_p(p)</tt>, any semantic action directly or
  86. indirectly attached to the conditional parser <tt>p</tt> will not be called.
  87. However, semantic actions attached to epsilon itself will always be called.
  88. The following code snippets illustrates the behavior:</p>
  89. <pre> eps_p<span class="special">(</span>c<span class="special">[</span>f<span class="special">])</span> <span class="comment">// f not called</span><br> eps_p<span class="special">(</span>c<span class="special">)[</span>f<span class="special">]</span> <span class="comment">// f is called</span><br> eps_p<span class="special">[</span>f<span class="special">]</span> <span class="comment">// f is called</span></pre>
  90. <p>Actually, the conditional parser <tt>p</tt> is implicitly wrapped in a <tt><a href="scanner.html#no_actions_scanner">no_actions_d</a></tt>
  91. directive:</p>
  92. <pre><code><span class=special> </span>no_actions_d<span class="special">[</span>p<span class="special">]</span></code></pre>
  93. <p>The conditional parser is required to be free from side-effects (semantic actions).
  94. <code></code>The conditional parser's purpose is to resolve ambiguity by looking
  95. ahead in the input stream for a certain pattern. Ambiguity and semantic actions
  96. do not mix well. On an ambiguous grammar, backtracking happens. And when it
  97. happens, we cannot undo the effects of triggered semantic actions. </p>
  98. <h3>Negation</h3>
  99. <p>Operator <tt>~</tt> is defined for parsers constructed by <tt>epsilon_p</tt>/<tt>eps_p</tt>.
  100. It performs negation by complementing the results reported. <tt>~~eps_p(x)</tt>
  101. is identical to <tt>eps_p(x)</tt>.</p>
  102. <table border="0">
  103. <tr>
  104. <td width="10"></td>
  105. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  106. <td width="30"><a href="rule.html"><img src="theme/l_arr.gif" border="0"></a></td>
  107. <td width="30"><a href="directives.html"><img src="theme/r_arr.gif" border="0"></a></td>
  108. </tr>
  109. </table>
  110. <br>
  111. <hr size="1">
  112. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  113. Copyright &copy; 2003 Martin Wille<br>
  114. <br>
  115. <font size="2">Use, modification and distribution is subject to the Boost Software
  116. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  117. http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  118. <p>&nbsp;</p>
  119. </body>
  120. </html>