gpt4 book ai didi

c++ - Boost-Spirit:将列表解析为结构的不同字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:05 26 4
gpt4 key购买 nike

我正准备开始使用 boost-spirit,但是我卡住了,而且它的错误消息也不是很容易理解。

首先,我有以下文本格式:

(111, 222, 333, ...) {
X 231
Y 227
X 54
Z 41156
Y 1112
...
}

包含整数列表的 header 和包含 (X|Y|Z) 无序序列的正文,每个序列后跟一个数字。我想将其解析为以下结构:

struct my_struct {
std::vector<int> head;
std::vector<int> X;
std::vector<int> Y;
std::vector<int> Z;
};

到目前为止,我已经有了这个解析器:

BOOST_FUSION_ADAPT_STRUCT(
my_struct,
(std::vector<int>, head)
(std::vector<int>, X)
(std::vector<int>, Y)
(std::vector<int>, Z)
)

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

template<typename Iterator, typename Skipper = ascii::space_type>
struct my_parser : qi::grammar<Iterator, my_struct(), Skipper> {
my_parser() : my_parser::base_type(start) {
start %= '(' >> head >> ')' >> '{' >> body >> '}';
head = int_ % ',';
body = +(X | Y | Z);
X = 'X' >> int_;
Y = 'Y' >> int_;
Z = 'Z' >> int_;
}

qi::rule<Iterator, my_struct(), Skipper> start;
qi::rule<Iterator, std::vector<int>(), Skipper> head;
qi::rule<Iterator, std::vector<int>(), Skipper> body;
qi::rule<Iterator, std::vector<int>(), Skipper> X;
qi::rule<Iterator, std::vector<int>(), Skipper> Y;
qi::rule<Iterator, std::vector<int>(), Skipper> Z;
};

到目前为止,这个编译很好,但结果显然是错误的。使用上面的例子,结果是:

my_struct {
header = [ 111, 222, 333 ]
X = [ 231, 227, 54, 41156, 1112 ]
Y = []
Z = []
}

我很确定我需要的是以下内容,但我无法编译它,我也不明白为什么。

template<typename Iterator, typename Skipper = ascii::space_type>
struct my_parser : qi::grammar<Iterator, my_struct(), Skipper> {
my_parser() : my_parser::base_type(start) {
start %= '(' >> head >> ')' >> '{' >> body(_val) >> '}';
head = int_ % ',';
body = +( X(bind(&my_struct::X, _r1))
| Y(bind(&my_struct::Y, _r1))
| Z(bind(&my_struct::Z, _r1))
);
X = 'X' >> int_[push_back(_r1, _1)];
Y = 'Y' >> int_[push_back(_r1, _1)];
Z = 'Z' >> int_[push_back(_r1, _1)];
}

qi::rule<Iterator, my_struct(), Skipper> start;
qi::rule<Iterator, std::vector<int>(), Skipper> head;
qi::rule<Iterator, void(my_struct), Skipper> body;
qi::rule<Iterator, void(std::vector<int>), Skipper> X;
qi::rule<Iterator, void(std::vector<int>), Skipper> Y;
qi::rule<Iterator, void(std::vector<int>), Skipper> Z;
};

最佳答案

我不太喜欢使用语义 Action ,但考虑到 AST 和输入的选择,我认为没有太多选择。

我会简化语法:

    rule  = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
>> '{' >> +(
'X' >> int_[X_(_val, _1)]
| 'Y' >> int_[Y_(_val, _1)]
| 'Z' >> int_[Z_(_val, _1)]
)
>> '}';

然后我会创建 phoenix 函数来推送元素:

template <std::vector<int> my_struct::*m> struct push {
void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
};

现在,它很简单:

px::function<push<&my_struct::head> > head_;
px::function<push<&my_struct::X> > X_;
px::function<push<&my_struct::Y> > Y_;
px::function<push<&my_struct::Z> > Z_;

演示

Live On Coliru

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

namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;

struct my_struct {
std::vector<int> head;
std::vector<int> X;
std::vector<int> Y;
std::vector<int> Z;
};

template<typename Iterator>
struct my_parser : qi::grammar<Iterator, my_struct()> {
my_parser() : my_parser::base_type(start) {
using namespace qi;
using px::push_back;

rule = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
>> '{' >> +(
'X' >> int_[X_(_val, _1)]
| 'Y' >> int_[Y_(_val, _1)]
| 'Z' >> int_[Z_(_val, _1)]
)
>> '}';

start = skip(space) [rule];
}
private:
template <std::vector<int> my_struct::*m> struct push {
void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
};
px::function<push<&my_struct::head> > head_;
px::function<push<&my_struct::X> > X_;
px::function<push<&my_struct::Y> > Y_;
px::function<push<&my_struct::Z> > Z_;

qi::rule<Iterator, my_struct()> start;
qi::rule<Iterator, my_struct(), qi::space_type> rule;
};

int main() {
using It = boost::spirit::istream_iterator;
It f(std::cin >> std::noskipws), l;

my_struct data;
if (parse(f, l, my_parser<It>{}, data)) {
std::cout << "Parsed:";
std::copy(data.head.begin(), data.head.end(), std::ostream_iterator<int>(std::cout << "\nhead: ", " " ));
std::copy(data.X.begin(), data.X.end(), std::ostream_iterator<int>(std::cout << "\nX: ", " " ));
std::copy(data.Y.begin(), data.Y.end(), std::ostream_iterator<int>(std::cout << "\nY: ", " " ));
std::copy(data.Z.begin(), data.Z.end(), std::ostream_iterator<int>(std::cout << "\nZ: ", " " ));
std::cout << "\n";
} else {
std::cout << "Parse failed\n";
}


if (f != l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}

对于输入 (111, 222, 333) { X 231 Y 227 X 54 Z 41156 Y 1112 } 打印:

Parsed:
head: 111 222 333
X: 231 54
Y: 227 1112
Z: 41156
Remaining unparsed input: '
'

关于c++ - Boost-Spirit:将列表解析为结构的不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46512672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com