使用Boost.Spirit.Qi解析为vector

时间:2012-09-05 15:31:27

标签: c++ parsing boost boost-spirit-qi

我对Spirit很新。 我试图使用Qi在我的嵌入式Tcl解释器中解析CMD命令的参数。 由于某些参数可能会多次使用,因此我需要一个向量来存储相同类型的所有参数。

这是我的问题的简化示例,我尝试将整数存储到向量中。

#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)]);

    }
  };
}

使用g ++ 4.5.1进行编译boost 1.51会产生错误

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>’

基本上我很困惑。不知道出了什么问题。

1 个答案:

答案 0 :(得分:2)

您在这里混合了两种类型的矢量模板类:

std::vector

boost::fusion::vector

如果你只是省略(注释掉)

using std::vector;
事情可能会变得非常清楚