Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/doc/dynamic_parsers.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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