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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:41:27+00:00 2026-05-30T21:41:27+00:00

I have a strange problem with a calculator made using boost::spirit. This calculator is

  • 0

I have a strange problem with a calculator made using boost::spirit. This calculator is supposed to take a string as argument representing a series of arithmetical expression separated by commas, like “a+4*5,77,(b-c)*4”. It also allows the string “?” and returns the array containing a -1 in this case. The calculator is initialized with a SymTable, which is a template class argument to describe any class offering the [string] -> int operator (example: a map), to resolve the value of variables.

The following code works on my Ubuntu 10.4 with both gcc 4.6.2 and gcc 4.4, and both boost 1.47 and 1.48. It also worked in the past on a Cray Linux machine with gcc 4.5.3 and boost 1.47.

#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

namespace sp = boost::spirit;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

namespace Damaris {

template <typename Iterator, typename SymTable>
struct Calc : qi::grammar<Iterator, std::vector<int>(), ascii::space_type>
{
        qi::rule<Iterator, std::vector<int>(), ascii::space_type> start;
        qi::rule<Iterator, int(), ascii::space_type> expr;
        qi::rule<Iterator, int(), ascii::space_type> qmark;
        qi::rule<Iterator, int(), ascii::space_type> factor;
        qi::rule<Iterator, int(), ascii::space_type> simple;
        qi::rule<Iterator, std::string(), ascii::space_type> identifier;
        qi::rule<Iterator, int(SymTable), ascii::space_type> value;

        /**
         * \brief Constructor.
         * \param[in] sym : table of symboles.
         */
        Calc(SymTable &sym) : Calc::base_type(start)
        {
                identifier = qi::lexeme[( qi::alpha | '_') >> *( qi::alnum | '_')];

                value   = identifier[qi::_val = qi::labels::_r1[qi::_1]];

                simple  = ('(' >> expr >> ')')
                        | qi::int_
                        | value(boost::phoenix::ref(sym));

                factor  %= (simple >> '*' >> factor)[qi::_val = qi::_1 * qi::_2]
                        |  (simple >> '/' >> factor)[qi::_val = qi::_1 / qi::_2]
                        |  (simple >> '%' >> factor)[qi::_val = qi::_1 % qi::_2]
                        |   simple;

                expr    %= (factor >> '+' >> expr)[qi::_val = qi::_1 + qi::_2]
                        |  (factor >> '-' >> expr)[qi::_val = qi::_1 - qi::_2]
                        |   factor;

                qmark   = qi::char_('?')[qi::_val = -1];

                start   = qmark
                        | (expr % ',');
        }
};

}

Today I tried again compiling the same code on the Cray machine (which has been upgraded since then, I think), I tried with gcc 4.6.2 and gcc 4.5.2, and both with boost 1.48 and 1.49, and I always get the same compilation error that I don’t understand :

/nics/b/home/mdorier/damaris-0.4/common/Calc.hpp:74:3:   instantiated from 'Damaris::Calc<Iterator, SymTable>::Calc(SymTable&) [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, SymTable = Damaris::ParameterSet]'
/nics/b/home/mdorier/damaris-0.4/common/MetadataManager.cpp:45:79:   instantiated from here
/nics/b/home/mdorier/deploy/include/boost/spirit/home/qi/detail/assign_to.hpp:123:13: error: invalid static_cast from type 'const boost::fusion::vector2<int, int>' to type 'int'

The line 74 in Calc.hpp corresponds to the line “factor = …”.
The instantiation line indicated (MetadataManager.cpp:45) is the following:

layoutInterp = new Calc<std::string::const_iterator,ParameterSet>(*parameters);

with layoutInterp being of type Calc* and parameters being of type ParameterSet*.

Any idea where this error comes from? 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-05-30T21:41:28+00:00Added an answer on May 30, 2026 at 9:41 pm

    I’m pretty sure you might have been rearranging stuff in your rules. In fact, the %= auto-rule expression assignments won’t work because the synthesized type of the parser expression doesn’t resemble an int.

    Basically, you’d change

    factor %= (simple >> '*' >> factor)[ _val = _1 * _2 ]
        |  (simple >> '/' >> factor)[ _val = _1 / _2 ]
        |  (simple >> '%' >> factor)[ _val = _1 % _2 ]
        |   simple;
    
    expr   %= (factor >> '+' >> expr)[ _val = _1 + _2 ]
        |  (factor >> '-' >> expr)[ _val = _1 - _2 ]
        |   factor;
    

    into

    factor  = (simple >> '*' >> factor)[ _val = _1 * _2 ]
            | (simple >> '/' >> factor)[ _val = _1 / _2 ]
            | (simple >> '%' >> factor)[ _val = _1 % _2 ]
            | (simple) [_val = _1 ];
    
    expr    = (factor >> '+' >> expr)[ _val = _1 + _2 ]
            | (factor >> '-' >> expr)[ _val = _1 - _2 ]
            | (factor) [_val = _1 ];
    

    I have fixed up some small issues and created a SSCCE of your post that works, as far as I can tell 1:

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/karma.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    
    namespace sp = boost::spirit;
    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    namespace karma = boost::spirit::karma;
    namespace phx   = boost::phoenix;
    
    namespace Damaris {
    
        template <typename Iterator, typename SymTable>
        struct Calc : qi::grammar<Iterator, std::vector<int>(), ascii::space_type>
        {
            qi::rule<Iterator, std::vector<int>(), ascii::space_type> start;
            qi::rule<Iterator, int(), ascii::space_type> expr;
            qi::rule<Iterator, int(), ascii::space_type> qmark;
            qi::rule<Iterator, int(), ascii::space_type> factor;
            qi::rule<Iterator, int(), ascii::space_type> simple;
            qi::rule<Iterator, std::string(), ascii::space_type> identifier;
            qi::rule<Iterator, int(SymTable), ascii::space_type> value;
    
            Calc(SymTable &sym) : Calc::base_type(start)
            {
                using namespace qi;
    
                identifier = lexeme[( alpha | '_') >> *( alnum | '_')];
    
                value   = identifier[ _val = _r1[_1] ];
    
                simple  = ('(' >> expr >> ')')
                        | int_
                        | value(boost::phoenix::ref(sym));
    
                factor  = (simple >> '*' >> factor)[ _val = _1 * _2 ]
                        | (simple >> '/' >> factor)[ _val = _1 / _2 ]
                        | (simple >> '%' >> factor)[ _val = _1 % _2 ]
                        | (simple) [_val = _1 ];
    
                expr    = (factor >> '+' >> expr)[ _val = _1 + _2 ]
                        | (factor >> '-' >> expr)[ _val = _1 - _2 ]
                        | (factor) [_val = _1 ];
    
                qmark   = char_('?')[ _val = -1 ];
    
                start   = qmark 
                    | (expr % ',');
    
                BOOST_SPIRIT_DEBUG_NODE(start);
                BOOST_SPIRIT_DEBUG_NODE(qmark);
                BOOST_SPIRIT_DEBUG_NODE(expr);
                BOOST_SPIRIT_DEBUG_NODE(factor);
                BOOST_SPIRIT_DEBUG_NODE(simple);
                BOOST_SPIRIT_DEBUG_NODE(value);
                BOOST_SPIRIT_DEBUG_NODE(identifier);
            }
        };
    
    }
    
    int main(int argc, const char *argv[])
    {
        typedef std::map<std::string, int> SymTable;
        SymTable symbols;
        Damaris::Calc<std::string::const_iterator, SymTable> calc(symbols);
    
        symbols["TheAnswerToLifeUniverse"] = 100;
        symbols["Everything"] = -58;
    
        std::string input = "3*4+5/4, TheAnswerToLifeUniverse + Everything";
        std::string::const_iterator f(input.begin()), l(input.end());
        std::vector<int> data;
    
        if (qi::phrase_parse(f,l,calc,ascii::space,data))
            std::cout << "output: " << karma::format(karma::int_ % ", " << karma::eol, data);
        else
            std::cout << "problem: '" << std::string(f,l) << "'\n";
    
        return 0;
    }
    

    Output:

    output: 13, 42
    

    1 gcc 4.6.1, boost 1_48

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

Sidebar

Related Questions

I have strange problem when using at() method of std::string. I'd like to calculate
I have strange problem with receiving data from socket. On client im using air
I have a strange problem with lxml when using the deployed version of my
I have a rather strange problem: when certain sales are made (completed) a record
I have a strange problem when using Entity Framework code first. When I return
I have strange problem... My file strings.xml contains: <?xml version=1.0 encoding=utf-8?> <resources> <string name=building_name>My
I have strange problem. For a project we are using following cloud hosting. http://www.rackspace.com
I have strange problem with the delay function here using the HTML function with
I have strange problem with sharepoint and ajax functionality. We have an UpdatePanel placed
i have strange problem doing reporting: i have numerous clients with different issued invoices.

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.