解析为向量<向量<double>>与 boost::spirit

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

本文关键字:gt lt 向量 boost spirit double      更新时间:2023-10-16

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

有可能用boost::spirit解析成向量的向量吗

类似于"牵引"下的表格:

' 
' 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;

我已经尝试过这种说法的几种变体。同样,这个问题中的%=运算符没有改变任何内容。尽管属性传播可能是正确的方向。

如果我遵循boost文档中的"with style"示例,结果完全相同:

vectorblock = doublevector % qi::eps;

使用push_back()的List Redux示例:

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

引发大量编译错误,从开始

错误C2039:"push_back":不是"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"
"' RPMn"
"'n"
"0,5010,10000,15000,20000,25000n"
"'n"
"' Tempn"
"'n"
"-40.,0.,20.,40.n"
"'n"
"' Tractionn"
"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> >是非常琐碎的

规则定义需要一个函数类型,而不是直接使用该类型。这里简单解释一下。更彻底的解释可能在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,