在语义操作中发现错误时停止语法分析器
问题描述:
我希望在语义操作代码发现问题时停止令牌分析器。在语义操作中发现错误时停止语法分析器
如果X> 10
在语法上是正确的,但如果x不存在的解析器应该停止
的语法规则和语义动作看起来像这样
condition
= (tok.identifier >> tok.oper_ >> tok.value)
[
boost::phoenix::bind(&cRuleKit::AddCondition, &myRulekit,
boost::spirit::_1, boost::spirit::_2, boost::spirit::_3)
]
;
所以现在我添加一个检查存在的标识符
condition
= (tok.identifier[boost::bind(&cRuleKit::CheckIdentifier, &myRulekit, ::_1, ::_3) ]
>> tok.oper_ >> tok.value)
[
boost::phoenix::bind(&cRuleKit::AddCondition, &myRulekit,
boost::spirit::_1, boost::spirit::_2, boost::spirit::_3)
]
;
This Works!
我不会为优雅而激动。语法语法现在很难阅读,混合使用boost :: bind和boost :: phoenix :: bind是非常令人困惑的。
我该如何改进?我想从phoenix :: bind得到'hit'参数,这样我就可以在cRuleKit :: AddCondition()内部进行检查,并保持语法和动作独立,并避免使用boost :: bind。
答案是使用占位符_pass
condition
= (tok.identifier >> tok.oper_ >> tok.value)
[
boost::phoenix::bind(&cRuleKit::AddCondition, &myRulekit,
boost::spirit::_pass, boost::spirit::_1, boost::spirit::_2, boost::spirit::_3)
]
;
答
精神,你可以在一个语义动作中使用一个特殊的值,使解析失败。它叫做_pass
,你应该把它设置为false
。
从我的一些代码:
variable_reference_impl_[_pass = lookup_symbol_(_1, false)][_val = _1]
在这种情况下
,lookup_symbol是凤凰函子如果符号被发现,false
如果不返回true
。
听起来不错。现在尝试... – ravenspoint 2011-03-30 15:05:24
效果不错!谢谢 – ravenspoint 2011-03-30 15:15:39