在以下测试案例中,一个alpha和一个序列炸弹的交替具有很长的错误转储,基本上说static assertion failed: The parser expects tuple-like attribute type
。凭直觉,我希望整个规则都能产生一个字符串,但事实并非如此。我要么必须将替换的左侧更改为+alpha
(使两个向量都成为矢量),要么要走语义动作的路径,至少对于替换中的单个char(附加到_val)。或者,将单独的左侧char_
更改为string
。无论如何,我想不出什么简单的方法来解析这样的字符串,任何提示都是值得的。 TIA。
#include <iostream>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
namespace grammar {
using x3::char_;
using x3::alpha;
using x3::xdigit;
const auto x =
x3::rule< struct x_class, std::string > { "x" } =
char_('/') > alpha >> *(alpha | (char_('#') > xdigit));
} // namespace grammar
int main () {
std::string input{ "/Foobar#F" }, attr;
auto iter = input.begin ();
if (phrase_parse (iter, input.end (), grammar::x, x3::space, attr)) {
std::cout << attr << std::endl;
}
return 0;
}
我也讨厌这种行为。齐在这方面更自然。
老实说,我并不总是知道如何“修复”它,但>
raw[]
-简化语法operator>
和operator>>
这是我为您的语法所做的:
#include <iostream> #include <boost/spirit/home/x3.hpp> namespace x3 = boost::spirit::x3; namespace grammar { const auto x = x3::rule<struct x_class, std::string> { "x" } = x3::raw [ '/' > x3::alpha >> *(x3::alpha | ('#' > x3::xdigit)) ]; } int main () { std::string input{ "/Foobar#F" }, attr; auto iter = input.begin (); if (phrase_parse (iter, input.end (), grammar::x, x3::space, attr)) { std::cout << attr << std::endl; } }
打印
/Foobar#F