使用 boost::spirit 解析为向量<vector<double>>

Parse into a vector<vector<double>> with boost::spirit

我的目的是将逗号分隔的值列表解析为嵌套向量。这个列表是二维的。基本问题是:

是否可以用 boost::spirit 解析成向量的向量?

类似于 "Traction" 下的 table:

' 
' RPM
0,5000,10000,15000,20000,25000
'
' Temp
'
-40.,0.,20.,40.
'
' Traction
200.,175.,170.,165.,160.,150.
200.,175.,170.,165.,160.,150.
165.,165.,160.,155.,145.,145.
160.,155.,150.,145.,145.,140.
'

下一步,我想读入 4 维数据,但现在我正在努力处理第二维数据。数据结构是这样的:

struct table {
    std::vector<double>  index;
    std::vector<double>  index2;
    std::vector<std::vector<double> > base;
};

恕我直言,语法非常简单,如下所示:

comment %= qi::lexeme[ '\'' >> *(qi::standard::char_ - qi::eol)] >> qi::eol;
commentblock = comment >> *(comment);
doublevector = qi::double_ % ',' >> qi::eol ;
vectorblock = *doublevector;
start = commentblock            >>
        doublevector            >>
        commentblock            >>
        doublevector            >>
        commentblock            >>
        vectorblock             >>
        commentblock            >>
        qi::eoi
        ;

到目前为止,我在解析 indexindex2 这两个向量时没有问题。但问题始于 base。我认为关键部分是我定义 vectorblock 的地方:

vectorblock = *doublevector;

我已经尝试了该声明的几种变体。 this 问题中的 %= 运算符也没有任何改变。尽管属性传播可能是正确的方向。

如果我按照 boost 文档示例 "with style",结果是完全一样的:

vectorblock = doublevector % qi::eps;

使用 push_back() 的 List Redux 示例:

vectorblock = doublevector[phoenix::push_back(qi::_val, qi::_1)] % qi::eps;

引发大量编译错误,开始于:

error C2039: 'push_back' : is not a member of 'boost::spirit::unused_type'

更新: 问题出在 vectorblock 的声明处。我忘记了属性类型后面的()。所以,定义应该是这样的:

qi::rule<Iterator, std::vector<std::vector<double> >(), Skipper> vectorblock;

(更新的)工作示例在这里:

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

#define BOOST_SPIRIT_DEBUG

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted.hpp>

struct table {
    std::vector<double>  index;
    std::vector<double>  index2;
    std::vector<std::vector<double> > base;
};

BOOST_FUSION_ADAPT_STRUCT(
        table,
        (std::vector<double> , index)
        (std::vector<double> , index2)
        (std::vector<std::vector<double> >, base)
)

const std::string contents =
        "'\n"
        "' RPM\n"
        "'\n"
        "0,5010,10000,15000,20000,25000\n"
        "'\n"
        "' Temp\n"
        "'\n"
        "-40.,0.,20.,40.\n"
        "'\n"
        "' Traction\n"
        "200.,175.,170.,165.,160.,150.\n"
        "200.,175.,170.,165.,160.,150.\n"
        "165.,165.,160.,155.,145.,145.\n"
        "160.,155.,150.,145.,145.,140.\n"
        "'\n"
;


int main()
{
    namespace qi = boost::spirit::qi;
    namespace phoenix = boost::phoenix;

    typedef std::string::const_iterator Iterator;
    typedef boost::spirit::ascii::blank_type Skipper;

    qi::rule<Iterator, std::string(), Skipper> comment;
    qi::rule<Iterator, Skipper> commentblock;
    qi::rule<Iterator, std::vector<double>(), Skipper> doublevector;
    qi::rule<Iterator, std::vector<std::vector<double> >, Skipper> vectorblock;
    qi::rule<Iterator, table(), Skipper> start;

    comment %= qi::lexeme[ '\'' >> *(qi::standard::char_ - qi::eol)] >> qi::eol;
    commentblock = comment >> *(comment);

    doublevector = qi::double_ % ',' >> qi::eol ;
    vectorblock = *doublevector;

    start = commentblock            >>
            doublevector            >>
            commentblock            >>
            doublevector            >>
            commentblock            >>
            vectorblock             >>
            commentblock            >>
            qi::eoi
            ;

    BOOST_SPIRIT_DEBUG_NODES((start)(doublevector)(vectorblock));

    table tref;

    bool rv =  qi::phrase_parse(
                     std::begin(contents), std::end(contents),
                     start,
                     boost::spirit::ascii::blank,
                     tref
                 );

    std::cout << "parse " << ((char *)rv?"success":"failure") << ".\n";

    for (auto i : tref.index)
        std::cout << i << ", ";
    std::cout << "\n";
    for (auto i : tref.index2)
        std::cout << i << ", ";
    std::cout << "\nBase:\n";
    for (auto & i : tref.base)
    {
        for(auto & j : i)
            std::cout << j << ", ";
        std::cout << "\n";
    }
    std::cout << std::endl;

}

答案是肯定的。 解析成 vector<vector<double> >

实际上很简单

规则定义需要一个函数类型,而不是直接的类型。这个简单解释一下here. A more thorough explanation is probably found in the documentation of boost::phoenix

上面程序的输出现在很好地显示了解析后的值:

parse success.
0, 5011, 10000, 15000, 20000, 25000, 
-40, 0, 20, 40, 
Base:
200, 175, 170, 165, 160, 150, 
200, 175, 170, 165, 160, 150, 
165, 165, 160, 155, 145, 145, 
160, 155, 150, 145, 145, 140,