Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestions about Spirit.Qi sequence operator and semantic actions
    primarykey
    data
    text
    <p>I have some questions about the sequence operator and semantic actions in Spirit Qi.</p> <p>I'm trying to define a grammar rule for a floating point number that accepts metric prefixes (u, m, k, M, etc.) as well as the normal exponent form.</p> <pre><code> rule&lt;Iterator, std::string()&gt; sign = char_("+-") [ _val = _1 ]; rule&lt;Iterator, std::string()&gt; exp = char_("eE") &gt;&gt; -sign &gt;&gt; +digit; rule&lt;Iterator, std::string()&gt; suffix = char_("yzafpnumkKMGTPEZY") [ _val = _1 ]; rule&lt;Iterator, std::string()&gt; mantissa = ((*digit &gt;&gt; char_('.') &gt;&gt; +digit) | (+digit &gt;&gt; char_('.') &gt;&gt; *digit)); rule&lt;Iterator, std::string()&gt; unsigned_floating = (mantissa &gt;&gt; -(exp | suffix) | +digit &gt;&gt; (exp | suffix)); rule&lt;Iterator, std::string()&gt; floating = -sign &gt;&gt; unsigned_floating; </code></pre> <p><strong>Question 1:</strong> Why do I have to add a semantic action to the rule <code>sign</code> above? Isn't <code>char</code> convertible to <code>std::string</code>?</p> <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> <p><strong>Question 3:</strong> Let's say I want to let the attribute of <code>floating</code> be <code>double</code> 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> <p><strong>Question 4:</strong> In the rule <code>floating</code> of Question 2, what does the placeholder <code>_2</code> refer to and what is its type?</p> <p><strong>Edit:</strong></p> <p>I guess the last question needs some clarification:</p> <p>What does the placeholder _2 refer to in the semantic action of the following rule, and what's its type?</p> <pre><code> rule&lt;Iterator, std::string()&gt; floating = (-sign &gt;&gt; (mantissa &gt;&gt; -(exp | suffix) | +digit &gt;&gt; (exp | suffix))) [ _2 ]; </code></pre> <p>Thanks!</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.
 

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