Tem*_*Rex 5 c++ parsing boost c++14 boost-spirit-x3
使用Boost.Spirit X3,我想将逗号分隔的范围列表和单个数字(例如1-4,6,7,9-12)解析为单个std::vector<int>.这是我想出的:
namespace ast {
struct range
{
int first_, last_;
};
using expr = std::vector<int>;
}
namespace parser {
template<typename T>
auto as_rule = [](auto p) { return x3::rule<struct _, T>{} = x3::as_parser(p); };
auto const push = [](auto& ctx) {
x3::_val(ctx).push_back(x3::_attr(ctx));
};
auto const expand = [](auto& ctx) {
for (auto i = x3::_attr(ctx).first_; i <= x3::_attr(ctx).last_; ++i)
x3::_val(ctx).push_back(i);
};
auto const number = x3::uint_;
auto const range = as_rule<ast::range> (number >> '-' >> number );
auto const expr = as_rule<ast::expr> ( -(range [expand] | number [push] ) % ',' );
}
Run Code Online (Sandbox Code Playgroud)
鉴于输入
"1,2,3,4,6,7,9,10,11,12", // individually enumerated
"1-4,6-7,9-12", // short-hand: using three ranges
Run Code Online (Sandbox Code Playgroud)
这被成功解析为(Live On Coliru):
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
Run Code Online (Sandbox Code Playgroud)
问题:我认为我理解将语义动作expand应用于该range部分是必要的,但为什么我还必须将语义动作push应用于该number部分?没有它(即使用简单的( -(range [expand] | number) % ',')规则expr,单个数字不会传播到AST(Live On Coliru):
OK! Parsed:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
Run Code Online (Sandbox Code Playgroud)
奖金问题:我是否甚至需要语义行为才能做到这一点?Spirit X3文档似乎不鼓励他们.
语义操作抑制自动属性传播的常见问题解答。假设语义动作会处理它。
一般来说有两种方法:
使用operator%=而不是operator=将定义分配给规则
或者使用模板的第三个(可选)模板参数rule<>,可以将其指定为true强制自动传播语义。
在这里,我主要通过删除范围规则本身内的语义操作来简化。现在,我们可以ast::range完全放弃该类型。不再有融合适应。
相反,我们使用“自然”合成的属性,其numer>>'-'>>number是整数的融合序列(fusion::deque<int, int>在本例中)。
现在,要使其正常工作,剩下的就是确保|yield 兼容类型的分支。一个简单的方法repeat(1)[]就可以解决这个问题。
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace ast {
using expr = std::vector<int>;
struct printer {
std::ostream& out;
auto operator()(expr const& e) const {
std::copy(std::begin(e), std::end(e), std::ostream_iterator<expr::value_type>(out, ", "));;
}
};
}
namespace parser {
auto const expand = [](auto& ctx) {
using boost::fusion::at_c;
for (auto i = at_c<0>(_attr(ctx)); i <= at_c<1>(_attr(ctx)); ++i)
x3::_val(ctx).push_back(i);
};
auto const number = x3::uint_;
auto const range = x3::rule<struct _r, ast::expr> {} = (number >> '-' >> number) [expand];
auto const expr = x3::rule<struct _e, ast::expr> {} = -(range | x3::repeat(1)[number] ) % ',';
}
template<class Phrase, class Grammar, class Skipper, class AST, class Printer>
auto test(Phrase const& phrase, Grammar const& grammar, Skipper const& skipper, AST& data, Printer const& print)
{
auto first = phrase.begin();
auto last = phrase.end();
auto& out = print.out;
auto const ok = phrase_parse(first, last, grammar, skipper, data);
if (ok) {
out << "OK! Parsed: "; print(data); out << "\n";
} else {
out << "Parse failed:\n";
out << "\t on input: " << phrase << "\n";
}
if (first != last)
out << "\t Remaining unparsed: '" << std::string(first, last) << '\n';
}
int main() {
std::string numeric_tests[] =
{
"1,2,3,4,6,7,9,10,11,12", // individually enumerated
"1-4,6-7,9-12", // short-hand: using three ranges
};
for (auto const& t : numeric_tests) {
ast::expr numeric_data;
test(t, parser::expr, x3::space, numeric_data, ast::printer{std::cout});
}
}
Run Code Online (Sandbox Code Playgroud)
印刷:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
617 次 |
| 最近记录: |