考虑到这种语法:
const auto grammar_def = x3::lit("start")
> x3::lit("{")
> (*(char_("a-zA-Z0-9\".{}=_~")))
> x3::lit("}") > x3::lit(";");(*(char_("a-zA-Z0-9\".{}=_~")))正在使用最后一个x3::lit("}"),因为它包含"}"
有办法阻止这种情况吗?比如在解析过程中给予x3::lit("}")更高的优先级?
发布于 2020-04-14 14:34:37
您可以编写一个自定义解析器,它可以像kleene那样工作,但可以回溯,也可以使用必须匹配的第二个解析器。像这样的东西应该管用。
template <class Left, class Right>
struct backtracking : binary_parser<Left, Right, backtracking<Left, Right>>
{
using base = binary_parser<Left, Right, backtracking<Left, Right>>;
backtracking(Left const& left, Right const& right)
: base(left, right)
{
}
template<typename It, typename Ctx, typename Other>
bool parse(It& f, It l, Ctx const& ctx, Other const& other, unused_type) const
{
auto end_it = l;
while (end_it != f) {
auto save = f;
if (this->left.parse(f, end_it, ctx, other, unused)) {
if (this->right.parse(f, l, ctx, other, unused))
return true;
}
f = save;
--end_it;
}
return false;
}
};
const auto grammar_def = x3::lit("start")
> x3::lit("{")
> backtracking(
*(char_("a-zA-Z0-9\".{}=_~")), lit("}") >> lit(";"));发布于 2020-04-09 09:05:19
我发现的唯一方法是,如果'}'之后出现;,就不允许kleene星与之匹配。
const auto grammar_def = x3::lit("start")
> x3::lit("{")
> *(char_("a-zA-Z0-9\".{}=_~") >> !lit(';'))
> x3::lit("}") > x3::lit(";");https://stackoverflow.com/questions/61058763
复制相似问题