你能帮助我理解之间的区别a % b
解析器和其扩展a >> *(b >> a)
在Boost.Spirit形式? 即使参考手册指出,它们是等价的,
该列表操作, a % b
,是一个二进制运算符相匹配的一个或多个重复的列表a
通过的出现分隔b
。 这相当于a >> *(b >> a)
。
下面的程序产生取决于其中使用不同的结果:
#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
struct Record {
int id;
std::vector<int> values;
};
BOOST_FUSION_ADAPT_STRUCT(Record,
(int, id)
(std::vector<int>, values)
)
int main() {
namespace qi = boost::spirit::qi;
const auto str = std::string{"1: 2, 3, 4"};
const auto rule1 = qi::int_ >> ':' >> (qi::int_ % ',') >> qi::eoi;
const auto rule2 = qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_)) >> qi::eoi;
Record record1;
if (qi::phrase_parse(str.begin(), str.end(), rule1, qi::space, record1)) {
std::cout << record1.id << ": ";
for (const auto& value : record1.values) { std::cout << value << ", "; }
std::cout << '\n';
} else {
std::cerr << "syntax error\n";
}
Record record2;
if (qi::phrase_parse(str.begin(), str.end(), rule2, qi::space, record2)) {
std::cout << record2.id << ": ";
for (const auto& value : record2.values) { std::cout << value << ", "; }
std::cout << '\n';
} else {
std::cerr << "syntax error\n";
}
}
住在Coliru
1: 2, 3, 4,
1: 2,
rule1
和rule2
只在不同的rule1
使用列表操作符( (qi::int_ % ',')
和rule2
使用它的扩展形式( (qi::int_ >> *(',' >> qi::int_))
然而, rule1
产生1: 2, 3, 4,
如预期)和rule2
产生1: 2,
我不明白的结果, rule2
:1)为什么是从不同的rule1
和2)为什么是3
和4
不包括在record2.values
即使phrase_parse
莫名其妙地回到正确的?
更新 X3版本添加
首先,你掉进这里深深的陷阱:
齐规则不工作, auto
。 使用qi::copy
或只用qi::rule<>
你的程序有不确定的行为,实际上坠毁我(的valgrind指出,在悬空的引用来源)。
所以,第一关:
const auto rule = qi::copy(qi::int_ >> ':' >> (qi::int_ % ',') >> qi::eoi);
现在,当你在程序中删除冗余,您可以:
重现问题
住在Coliru
int main() {
test(qi::copy(qi::int_ >> ':' >> (qi::int_ % ',')));
test(qi::copy(qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_))));
}
印花
1: 2, 3, 4,
1: 2,
原因和修复
发生了什么事3, 4
这是成功解析 ?
那么,属性传播规则指示qi::int_ >> *(',' >> qi::int_)
公开了一tuple<int, vector<int> >
。 力图神奇DoTheRightThing(TM)精神偶然失火和“assigngs”的int
到属性参考,忽略剩余的vector<int>
。
如果你想使容器属性解析为“原子团”,用qi::as<>
test(qi::copy(qi::int_ >> ':' >> qi::as<Record::values_t>() [ qi::int_ >> *(',' >> qi::int_)]));
这里as<>
充当属性兼容性启发式的障碍和语法知道你的意思:
住在Coliru
#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
struct Record {
int id;
using values_t = std::vector<int>;
values_t values;
};
BOOST_FUSION_ADAPT_STRUCT(Record, id, values)
namespace qi = boost::spirit::qi;
template <typename T>
void test(T const& rule) {
const std::string str = "1: 2, 3, 4";
Record record;
if (qi::phrase_parse(str.begin(), str.end(), rule >> qi::eoi, qi::space, record)) {
std::cout << record.id << ": ";
for (const auto& value : record.values) { std::cout << value << ", "; }
std::cout << '\n';
} else {
std::cerr << "syntax error\n";
}
}
int main() {
test(qi::copy(qi::int_ >> ':' >> (qi::int_ % ',')));
test(qi::copy(qi::int_ >> ':' >> (qi::int_ >> *(',' >> qi::int_))));
test(qi::copy(qi::int_ >> ':' >> qi::as<Record::values_t>() [ qi::int_ >> *(',' >> qi::int_)]));
}
打印
1: 2, 3, 4,
1: 2,
1: 2, 3, 4,
因为它的时间让人们开始与X3(精神的新版本),因为我喜欢挑战msyelf做相应的任务,精神X3,这里是灵X3版本。
有没有问题, auto
在X3。
“破”的情况下也表现要好得多,这触发静态断言:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
static_assert(
fusion::result_of::size<Attribute>::value == (l_size + r_size)
, "Attribute does not have the expected size."
);
这很好 ,对不对?
解决方法似乎有点不太可读。
test(int_ >> ':' >> (rule<struct _, Record::values_t>{} = (int_ >> *(',' >> int_))));
但是,这将是微不足道的编写自己as<>
“指令”(或只是一个函数),如果你想要的东西:
namespace {
template <typename T>
struct as_type {
template <typename Expr>
auto operator[](Expr&& expr) const {
return x3::rule<struct _, T>{"as"} = x3::as_parser(std::forward<Expr>(expr));
}
};
template <typename T> static const as_type<T> as = {};
}
DEMO
住在Coliru
#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/spirit/home/x3.hpp>
struct Record {
int id;
using values_t = std::vector<int>;
values_t values;
};
namespace x3 = boost::spirit::x3;
template <typename T>
void test(T const& rule) {
const std::string str = "1: 2, 3, 4";
Record record;
auto attr = std::tie(record.id, record.values);
if (x3::phrase_parse(str.begin(), str.end(), rule >> x3::eoi, x3::space, attr)) {
std::cout << record.id << ": ";
for (const auto& value : record.values) { std::cout << value << ", "; }
std::cout << '\n';
} else {
std::cerr << "syntax error\n";
}
}
namespace {
template <typename T>
struct as_type {
template <typename Expr>
auto operator[](Expr&& expr) const {
return x3::rule<struct _, T>{"as"} = x3::as_parser(std::forward<Expr>(expr));
}
};
template <typename T> static const as_type<T> as = {};
}
int main() {
using namespace x3;
test(int_ >> ':' >> (int_ % ','));
//test(int_ >> ':' >> (int_ >> *(',' >> int_))); // COMPILER asserts "Attribute does not have the expected size."
// "clumsy" x3 style workaround
test(int_ >> ':' >> (rule<struct _, Record::values_t>{} = (int_ >> *(',' >> int_))));
// using an ad-hoc `as<>` implementation:
test(int_ >> ':' >> as<Record::values_t>[int_ >> *(',' >> int_)]);
}
打印
1: 2, 3, 4,
1: 2, 3, 4,
1: 2, 3, 4,