使用语义操作解析以逗号分隔的范围和数字列表
Parsing comma-separated list of ranges and numbers with semantic actions
使用 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] ) % ',' );
}
给定输入
"1,2,3,4,6,7,9,10,11,12", // individually enumerated
"1-4,6-7,9-12", // short-hand: using three ranges
这被成功解析为 ( 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,
问题:我想我理解将语义动作expand
应用到range
部分是必要的,但为什么我也必须应用语义动作 push
到 number
部分?没有它(即使用 expr
的普通 ( -(range [expand] | number) % ',')
规则,单个数字不会传播到 AST(Live On Coliru):
OK! Parsed:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
奖金问题:我什至需要语义动作来做到这一点吗? Spirit X3 文档似乎让他们望而却步。
语义动作抑制自动属性传播的常见问题解答。假设是语义动作会处理它。
一般有两种做法:
使用 operator%=
而不是 operator=
将定义分配给规则
或使用 rule<>
模板的第三个(可选)模板参数,可以将其指定为 true
以强制自动传播语义。
简化样本
在这里,我主要通过删除范围规则本身内部的语义操作来简化。现在,我们可以完全放弃 ast::range
类型。不再融合适应。
相反,我们使用 numer>>'-'>>number
的 "naturally" 综合属性,它是整数的融合序列(在本例中为 fusion::deque<int, int>
)。
现在,要使其正常工作,剩下的就是确保 |
的分支产生兼容的类型。一个简单的 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});
}
}
打印:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
使用 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] ) % ',' );
}
给定输入
"1,2,3,4,6,7,9,10,11,12", // individually enumerated
"1-4,6-7,9-12", // short-hand: using three ranges
这被成功解析为 ( 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,
问题:我想我理解将语义动作expand
应用到range
部分是必要的,但为什么我也必须应用语义动作 push
到 number
部分?没有它(即使用 expr
的普通 ( -(range [expand] | number) % ',')
规则,单个数字不会传播到 AST(Live On Coliru):
OK! Parsed:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
奖金问题:我什至需要语义动作来做到这一点吗? Spirit X3 文档似乎让他们望而却步。
语义动作抑制自动属性传播的常见问题解答。假设是语义动作会处理它。
一般有两种做法:
使用
operator%=
而不是operator=
将定义分配给规则或使用
rule<>
模板的第三个(可选)模板参数,可以将其指定为true
以强制自动传播语义。
简化样本
在这里,我主要通过删除范围规则本身内部的语义操作来简化。现在,我们可以完全放弃 ast::range
类型。不再融合适应。
相反,我们使用 numer>>'-'>>number
的 "naturally" 综合属性,它是整数的融合序列(在本例中为 fusion::deque<int, int>
)。
现在,要使其正常工作,剩下的就是确保 |
的分支产生兼容的类型。一个简单的 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});
}
}
打印:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,