C++ Boost.Spirit.X3如何防止令牌被以前的规则解析?
鉴于这种语法:C++ Boost.Spirit.X3如何防止令牌被以前的规则解析?,c++,boost-spirit-x3,C++,Boost Spirit X3,鉴于这种语法: const auto grammar_def=x3::lit(“开始”) >x3::lit(“{”) >(*(字符(“a-zA-Z0-9\”{}=}) >x3::lit(“}”)>x3::lit(“;”; 最后一个x3::lit(“}”)被(*(char_uz0-9\“{}=”))消耗,因为它包含“}” 有没有办法防止这种情况发生?类似于在解析过程中为x3::lit(“}”)提供更高的优先级 我发现的唯一方法是禁止kleene星与'}'匹配如果a紧随其后 const auto
const auto grammar_def=x3::lit(“开始”)
>x3::lit(“{”)
>(*(字符(“a-zA-Z0-9\”{}=})
>x3::lit(“}”)>x3::lit(“;”;
最后一个x3::lit(“}”)被(*(char_uz0-9\“{}=”))消耗,因为它包含“}”
有没有办法防止这种情况发生?类似于在解析过程中为x3::lit(“}”)提供更高的优先级 我发现的唯一方法是禁止kleene星与
'}'匹配如果a代码>紧随其后
const auto grammar_def = x3::lit("start")
> x3::lit("{")
> *(char_("a-zA-Z0-9\".{}=_~") >> !lit(';'))
> x3::lit("}") > x3::lit(";");
您总是可以编写一个像kleene star一样工作的自定义解析器,但可以回溯,还可以使用第二个必须匹配的解析器。这样的办法应该行得通
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(";"));
模板
结构回溯:二进制语法分析器
{
使用base=binary_解析器;
回溯(左常量和左常量,右常量和右常量)
:底座(左、右)
{
}
模板
布尔解析(It&f、It l、Ctx常量和Ctx、其他常量和其他、未使用的类型)常量
{
自动结束\u it=l;
while(结束它!=f){
自动保存=f;
if(this->left.parse(f,end\u it,ctx,other,unused)){
if(this->right.parse(f,l,ctx,other,unused))
返回true;
}
f=保存;
--结束它;
}
返回false;
}
};
常量自动语法_def=x3::lit(“开始”)
>x3::lit(“{”)
>回溯(
*(字符(“a-zA-Z0-9\”{}={}>),亮(“}”)>>亮(“;”);
它适用于这种情况,但我正在寻找更通用的方法,例如:假设”;“也在表达式中。