Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:49:54+00:00 2026-06-08T07:49:54+00:00

I have some questions about the sequence operator and semantic actions in Spirit Qi.

  • 0

I have some questions about the sequence operator and semantic actions in Spirit Qi.

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.

  rule<Iterator, std::string()> sign = char_("+-") [ _val = _1 ];
  rule<Iterator, std::string()> exp = char_("eE") >> -sign >> +digit;
  rule<Iterator, std::string()> suffix = char_("yzafpnumkKMGTPEZY") [ _val = _1 ];
  rule<Iterator, std::string()> mantissa = ((*digit >> char_('.') >> +digit) | (+digit >> char_('.') >> *digit));
  rule<Iterator, std::string()> unsigned_floating = (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));
  rule<Iterator, std::string()> floating = -sign >> unsigned_floating;

Question 1: Why do I have to add a semantic action to the rule sign above? Isn’t char convertible to std::string?

Question 2: Why does compilation fail when I try to merge the last two rules like this:

  rule<Iterator, std::string()> floating = -sign >> (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));

Question 3: 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?

Question 4: In the rule floating of Question 2, what does the placeholder _2 refer to and what is its type?

Edit:

I guess the last question needs some clarification:

What does the placeholder _2 refer to in the semantic action of the following rule, and what’s its type?

  rule<Iterator, std::string()> floating = (-sign >> (mantissa >> -(exp | suffix) | +digit >> (exp | suffix))) [ _2 ];

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:49:56+00:00Added an answer on June 8, 2026 at 7:49 am

    First, blow-by-blow. See below for a out-of-the-box answer.

    Question 1: Why do I have to add a semantic action to the rule sign above?
    Isn’t char convertible to std::string?

    Erm, no char is not convertible to string. See below for other options.

    Question 2: Why does compilation fail when I try to merge the last two rules
    like this:

    rule<Iterator, std::string()> floating = -sign >> 
                  (mantissa >> -(exp | suffix) | +digit >> (exp | suffix));
    

    This is due to the rules for atomic attribute assignment. The parser exposes something like

    vector2<optional<string>, variant<
          vector2<string, optional<string> >,
          vector2<std::vector<char>, optional<string> > >
    

    or similar (see the documentation for the parsers, I typed this in the browser from memory). This is, obviously, not assignable to string. Use qi::as<> to coerce atomic assignment. For convenience ***there is qi::as_string:

    floating = qi::as_string [ -sign >> (mantissa >> -(exp | suffix) | 
                                         +digit >> (exp | suffix)) ] 
    

    Question 3: 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?

    You could use qi::as_string again, but the most appropriate would seem to be to use qi::raw:

    floating = qi::raw [ -sign >> (mantissa >> -(exp | suffix) | 
                                   +digit >> (exp | suffix)) ] 
           [ _val = parse_float(_1, _2) ];
    

    This parser directive exposes a pair of source iterators, so you can use it to refer to the exact input sequence matched.

    Question 4: In the rule floating of Question 2, what does the placeholder _2
    refer to and what is its type?

    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:

    • Detecting the parameter types in a Spirit semantic action

    Out-of-the-box

    Have you looked at using Qi’s builtin real_parser<> template, which can be comprehensively customized. It sure looks like you’d want to use that instead of doing custom parsing in your semantic action.

    The real_parser template with policies is both fast and very flexible and robust. See also the recent answer Is it possible to read infinity or NaN values using input streams?.

    For models of RealPolicies the following expressions must be valid:

    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
    

    See the example for a compelling idea of how you’d use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions about importing data from Excel/CSV File into SQL Server. Let
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about using MySQLi queries, and related memory management. Suppose I
I have some questions about the default values in a function parameter list Is
I have some questions about vector in STL to clarify..... Where are the objects
I have some questions about the registry. We have Preferences p = Preferences.userRoot(); If
I have some questions about the performance of this simple python script: import sys,
I am using Tomcat 6 and have some questions about Apache mod_jk as follows.
I have created a wildcard App ID and have some questions about bundle ID
We are writing an inventory system and I have some questions about sqlalchemy (postgresql)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.