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 6920197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:07:01+00:00 2026-05-27T10:07:01+00:00

I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(),

  • 0

I have another problem with my boost::spirit parser.

template<typename Iterator>
struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> {
    expression() :
        expression::base_type(expr) {
        number %= lexeme[double_];
        varname %= lexeme[alpha >> *(alnum | '_')];

        binop = (expr >> '+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1,_2)]
              | (expr >> '-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1,_2)]
              | (expr >> '*' >> expr)[_val = construct<ast::binary_op<ast::mul>>(_1,_2)]
              | (expr >> '/' >> expr)[_val = construct<ast::binary_op<ast::div>>(_1,_2)] ;

        expr %= number | varname | binop;
    }

    qi::rule<Iterator, ast::expression(), ascii::space_type> expr;
    qi::rule<Iterator, ast::expression(), ascii::space_type> binop;
    qi::rule<Iterator, std::string(), ascii::space_type> varname;
    qi::rule<Iterator, double(), ascii::space_type> number;
};

This was my parser. It parsed "3.1415" and "var" just fine, but when I tried to parse "1+2" it tells me parse failed. I’ve then tried to change the binop rule to

    binop = expr >>
           (('+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1, _2)]
          | ('-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1, _2)]
          | ('*' >> expr)[_val = construct<ast::binary_op<ast::mul>>(_1, _2)]
          | ('/' >> expr)[_val = construct<ast::binary_op<ast::div>>(_1, _2)]);

But now it’s of course not able to build the AST, because _1 and _2 are set differently. I have only seen something like _r1 mentioned, but as a boost-Newbie I am not quite able to understand how boost::phoenix and boost::spirit interact.

How to solve this?

  • 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-05-27T10:07:02+00:00Added an answer on May 27, 2026 at 10:07 am

    It isn’t entirely clear to me what you are trying to achieve. Most importantly, are you not worried about operator associativity? I’ll just show simple answers based on using right-recursion – this leads to left-associative operators being parsed.

    The straight answer to your visible question would be to juggle a fusion::vector2<char, ast::expression> – which isn’t really any fun, especially in Phoenix lambda semantic actions. (I’ll show below, what that looks like).

    Meanwhile I think you should read up on the Spirit docs

    • here in the old Spirit docs (eliminating left recursion); Though the syntax no longer applies, Spirit still generates LL recursive descent parsers, so the concept behind left-recursion still applies. The code below shows this applied to Spirit Qi
    • here: the Qi examples contain three calculator samples, which should give you a hint on why operator associativity matters, and how you would express a grammar that captures the associativity of binary operators. Obviously, it also shows how to support parenthesized expressions to override the default evaluation order.

    Code:

    I have three version of code that works, parsing input like:

    std::string input("1/2+3-4*5");
    

    into an ast::expression grouped like (using BOOST_SPIRIT_DEBUG):

    <expr>
      ....
      <success></success>
      <attributes>[[1, [2, [3, [4, 5]]]]]</attributes>
    </expr>
    

    The links to the code are here:

    • step_#1_reduce_semantic_actions.cpp
    • step_#2_drop_rule.cpp
    • step_#0_vector2.cpp

    Step 1: Reduce semantic actions

    First thing, I’d get rid of the alternative parse expressions per operator; this leads to excessive backtracking1. Also, as you’ve found out, it makes the grammar hard to maintain. So, here is a simpler variation that uses a function for the semantic action:


    1check that using BOOST_SPIRIT_DEBUG!

    static ast::expression make_binop(char discriminant, 
         const ast::expression& left, const ast::expression& right)
    {
        switch(discriminant)
        {
            case '+': return ast::binary_op<ast::add>(left, right);
            case '-': return ast::binary_op<ast::sub>(left, right);
            case '/': return ast::binary_op<ast::div>(left, right);
            case '*': return ast::binary_op<ast::mul>(left, right);
        }
        throw std::runtime_error("unreachable in make_binop");
    }
    
    // rules:
    number %= lexeme[double_];
    varname %= lexeme[alpha >> *(alnum | '_')];
    
    simple = varname | number;
    binop = (simple >> char_("-+*/") >> expr) 
        [ _val = phx::bind(make_binop, qi::_2, qi::_1, qi::_3) ]; 
    
    expr = binop | simple;
    

    Step 2: Remove redundant rules, use _val

    As you can see, this has the potential to reduce complexity. It is only a small step now, to remove the binop intermediate (which has become quite redundant):

    number %= lexeme[double_];
    varname %= lexeme[alpha >> *(alnum | '_')];
    
    simple = varname | number;
    expr = simple [ _val = _1 ] 
        > *(char_("-+*/") > expr) 
                [ _val = phx::bind(make_binop, qi::_1, _val, qi::_2) ]
        > eoi;
    

    As you can see,

    • within the expr rule, the _val lazy placeholder is used as a pseudo-local variable that accumulates the binops. Across rules, you’d have to use qi::locals<ast::expression> for such an approach. (This was your question regarding _r1).
    • there are now explicit expectation points, making the grammar more robust
    • the expr rule no longer needs to be an auto-rule (expr = instead of expr %=)

    Step 0: Wrestle fusion types directly

    Finally, for fun and gory, let me show how you could have handled your suggested code, along with the shifting bindings of _1, _2 etc.:

    static ast::expression make_binop(
            const ast::expression& left, 
            const boost::fusion::vector2<char, ast::expression>& op_right)
    {
        switch(boost::fusion::get<0>(op_right))
        {
            case '+': return ast::binary_op<ast::add>(left, boost::fusion::get<1>(op_right));
            case '-': return ast::binary_op<ast::sub>(left, boost::fusion::get<1>(op_right));
            case '/': return ast::binary_op<ast::div>(left, boost::fusion::get<1>(op_right));
            case '*': return ast::binary_op<ast::mul>(left, boost::fusion::get<1>(op_right));
        }
        throw std::runtime_error("unreachable in make_op");
    }
    
    // rules:
    expression::base_type(expr) {
    number %= lexeme[double_];
    varname %= lexeme[alpha >> *(alnum | '_')];
    
    simple = varname | number;
    binop %= (simple >> (char_("-+*/") > expr)) 
        [ _val = phx::bind(make_binop, qi::_1, qi::_2) ]; // note _2!!!
    
    expr %= binop | simple;
    

    As you can see, not nearly as much fun writing the make_binop function that way!

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

Sidebar

Related Questions

Following on from a question I posted yesterday about GUIs, I have another problem
I've been struggling with Zend_Navigation all weekend, and now I have another problem, which
Okay, so I have another question on a prolog homework problem I am struggling
After getting a helpful answer here , I have run into yet another problem:
another ordered delivery problem. We have an orchestration which is bound to a send
here is the problem. I have one PC using VS2008 (SP1 ) and another
I have ran into yet another problem I do not understand. The following does
I have a problem with the following code: #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream>
I have another problem with my scripy now I have made it more advance,
I have another problem regarding Git. This time I thoroughly searched Google and Stack

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.