Boost logo

Boost Users :

Subject: [Boost-users] error boost spirit
From: khaled hamidouche (khaledhamidouche_at_[hidden])
Date: 2010-05-18 11:40:07


Hello

with Joel Falcou we are wondring to write a parser to parse mathematical
expression like (N1*3+N2/N3) ...
with N1, N2, N3 are variables where their value are in an std vector

so I take the calc example and I change it.

my code looks like this

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/home/phoenix/container.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

     void do_add() { std::cout << "add\n"; }

///////////////////////////////////////////////////////////////////////////
    // Our calculator grammar

///////////////////////////////////////////////////////////////////////////
    template <typename Iterator>
    struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
    {
        calculator(std::vector<int> const & vec) :
calculator::base_type(expression), vect(vec)
        {
            using qi::_val;
            using qi::_1;
            using qi::uint_;
            using ascii::char_;

            int index;

// vect.resize(3);
//
//
// vect[0]=10;
// vect[1]=11;
// vect[2]=12;
               //vect=vec;

            expression =
                term [_val = _1]
>> *( ('+' >> term [_val += _1])
                    | ('-' >> term [_val -= _1])
                    )
                ;

            term =
                factor [_val = _1]
>> *( ('*' >> factor [_val *= _1])
                    | ('/' >> factor [_val /= _1])
                    )
                ;

            factor =
                uint_ [_val = _1]
                | char_ >>uint_ [_val = at(vect,_1)]
                | '(' >> expression [_val = _1] >> ')'
                | ('-' >> factor [_val = -_1])
                | ('+' >> factor [_val = _1])
                ;
        }

        qi::rule<Iterator, int(), ascii::space_type> expression, term,
factor;
        std::vector<int> vect;

    };
}

///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
    std::cout <<
"/////////////////////////////////////////////////////////\n\n";
    std::cout << "Expression parser...\n\n";
    std::cout <<
"/////////////////////////////////////////////////////////\n\n";
    std::cout << "Type an expression...or [q or Q] to quit\n\n";

    using boost::spirit::ascii::space;
    typedef std::string::const_iterator iterator_type;
    typedef client::calculator<iterator_type> calculator;

    std::vector<int> vec(3);
    vec[0]=10;
    vec[1]=11;
    vec[2]=12;

    calculator calc(vec); // Our grammar

    std::string str;
    int result;

    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        bool r = phrase_parse(iter, end, calc, space, result);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded\n";
            std::cout << "result = " << result << std::endl;
            std::cout << "-------------------------\n";
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \": " << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

when I compile I get this error

/home/hamidou/LLVM/parser /main.cpp:296: instantiated from here
/usr/local/include/boost-1_42/boost/spirit/home/phoenix/core/detail/function_eval.hpp:135:
error: invalid initialization of reference of type ‘int&’ from expression of
type ‘const int’
Process terminated with status 1 (0 minutes, 11 seconds)
1 errors, 1 warnings

when I remove the use of the vector vect inside my grammar it works fine !!

So, please what I did wrong ??

Thank you so much for helping me



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net