Search code examples
c++parsingboostboost-spirit-qi

use Boost.Spirit.Qi to parse into vector


I am quite new to Spirit. I am trying to use Qi to parse the argument for a CMD command in my embedded Tcl interpreter. As some arguments may used multiple times, I will need a vector to store all arguments of the same sort.

This is a simplified example of my problem, where I try to store integers into a vector.

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/support.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>

using std::string;
using std::vector;

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

  struct Argument {
    vector<int> svDefine;    // macro definitions
  };
}

BOOST_FUSION_ADAPT_STRUCT
(
 Argument,
 (vector<int>, svDefine)
 )

namespace {
  typedef string::const_iterator SIter;

  struct ArgParser : qi::grammar<SIter, Argument(), ascii::space_type> {
    qi::rule<SIter, Argument(), ascii::space_type> start;

    ArgParser() : ArgParser::base_type(start) {
      using phoenix::at_c;
      using qi::int_;
      using phoenix::push_back;
      using namespace qi::labels;


      start = +("define" >> int_     [push_back(at_c<0>(_val), _1)]);

    }
  };
}

Compiling it with g++ 4.5.1 boost 1.51 generates lost of errors

In file included from /usr/include/boost/spirit/home/phoenix/container.hpp:10:0,
                 from /usr/include/boost/spirit/home/phoenix.hpp:12,
                 from /usr/include/boost/spirit/include/phoenix.hpp:13,
                 from qi_test.cpp:2:
.....
qi_test.cpp:43:64:   instantiated from here
/usr/include/boost/spirit/home/phoenix/stl/container/container.hpp:492:40: error: ‘struct boost::fusion::vector<int>’ has no member named ‘push_back’
/usr/include/boost/spirit/home/phoenix/stl/container/container.hpp:492:40: error: return-statement with a value, in function returning 'void'
qi_test.cpp: In static member function ‘static 
.....
qi_test.cpp:43:64:   instantiated from here
qi_test.cpp:28:509: error: invalid initialization of reference of type boost::fusion::vector<int>&’ from expression of type ‘std::vector<int>’

Basically I am confused. No idea what is wrong.


Solution

  • You are mixing up two types of vector template classes here:

    std::vector
    

    and

    boost::fusion::vector
    

    if you just omit (comment out)

    using std::vector;
    

    things will probably become quite clear