Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, blow-by-blow. See below for a out-of-the-box answer.</p> <blockquote> <p><strong>Question 1</strong>: Why do I have to add a semantic action to the rule sign above? Isn't char convertible to std::string?</p> </blockquote> <p>Erm, no char is not convertible to string. See below for other options.</p> <blockquote> <p><strong>Question 2</strong>: Why does compilation fail when I try to merge the last two rules like this:</p> <pre><code>rule&lt;Iterator, std::string()&gt; floating = -sign &gt;&gt; (mantissa &gt;&gt; -(exp | suffix) | +digit &gt;&gt; (exp | suffix)); </code></pre> </blockquote> <p>This is due to the rules for atomic attribute assignment. The parser exposes something like </p> <pre><code>vector2&lt;optional&lt;string&gt;, variant&lt; vector2&lt;string, optional&lt;string&gt; &gt;, vector2&lt;std::vector&lt;char&gt;, optional&lt;string&gt; &gt; &gt; </code></pre> <p>or similar (see <a href="http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/operator.html" rel="nofollow noreferrer">the documentation for the parsers</a>, I typed this in the browser from memory). This is, obviously, not assignable to string. Use <code>qi::as&lt;&gt;</code> to coerce atomic assignment. For convenience ***there is <code>qi::as_string</code>:</p> <pre><code>floating = qi::as_string [ -sign &gt;&gt; (mantissa &gt;&gt; -(exp | suffix) | +digit &gt;&gt; (exp | suffix)) ] </code></pre> <blockquote> <p><strong>Question 3</strong>: Let's say I want to let the attribute of floating be double and write a semantic action to do the conversion from string to double. How can I refer to the entire string matched by the rule from inside the semantic action?</p> </blockquote> <p>You could use <code>qi::as_string</code> again, but the most appropriate would seem to be to use <code>qi::raw</code>:</p> <pre><code>floating = qi::raw [ -sign &gt;&gt; (mantissa &gt;&gt; -(exp | suffix) | +digit &gt;&gt; (exp | suffix)) ] [ _val = parse_float(_1, _2) ]; </code></pre> <p>This parser <em>directive</em> exposes a pair of source iterators, so you can use it to refer to the exact input sequence matched.</p> <blockquote> <p><strong>Question 4</strong>: In the rule floating of Question 2, what does the placeholder _2 refer to and what is its type?</p> </blockquote> <p>In general, to detect attribute types - that is, when the documentation has you confused or you want to double check your understanding of it - see the answers here:</p> <ul> <li><a href="https://stackoverflow.com/questions/9404189/detecting-the-parameter-types-in-a-spirit-semantic-action/9405265#9405265">Detecting the parameter types in a Spirit semantic action</a></li> </ul> <hr> <h2><strong>Out-of-the-box</strong></h2> <p>Have you looked at using Qi's <a href="http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/numeric/real.html#spirit.qi.reference.numeric.real._code__phrase_role__identifier__realpolicies__phrase___code_" rel="nofollow noreferrer">builtin <code>real_parser&lt;&gt;</code> template</a>, which can be comprehensively customized. It sure looks like you'd want to use that instead of doing custom parsing in your semantic action. </p> <p>The real_<code>parser</code> template with <em>policies</em> is both fast and very flexible and robust. See also the recent answer <a href="https://stackoverflow.com/questions/11420263/is-it-possible-to-read-infinity-or-nan-values-using-input-streams/11420901#11420901">Is it possible to read infinity or NaN values using input streams?</a>.</p> <p>For models of RealPolicies the following expressions must be valid: </p> <pre><code>Expression | Semantics ===========================+============================================================================= RP::allow_leading_dot | Allow leading dot. RP::allow_trailing_dot | Allow trailing dot. RP::expect_dot | Require a dot. RP::parse_sign(f, l) | Parse the prefix sign (e.g. '-'). Return true if successful, otherwise false. RP::parse_n(f, l, n) | Parse the integer at the left of the decimal point. Return true if successful, otherwise false. If successful, place the result into n. RP::parse_dot(f, l) | Parse the decimal point. Return true if successful, otherwise false. RP::parse_frac_n(f, l, n) | Parse the fraction after the decimal point. Return true if successful, otherwise false. If successful, place the result into n. RP::parse_exp(f, l) | Parse the exponent prefix (e.g. 'e'). Return true if successful, otherwise false. RP::parse_exp_n(f, l, n) | Parse the actual exponent. Return true if successful, otherwise false. If successful, place the result into n. RP::parse_nan(f, l, n) | Parse a NaN. Return true if successful, otherwise false. If successful, place the result into n. RP::parse_inf(f, l, n) | Parse an Inf. Return true if successful, otherwise false. If successful, place the result into n </code></pre> <p>See <strong><a href="http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/numeric/real.html#spirit.qi.reference.numeric.real.example" rel="nofollow noreferrer">the example</a></strong> for a compelling idea of how you'd use it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload