开发者

stopping parser when error found in semantic action

I wish to stop a token parser when the semantic action code finds a problem.

IF x > 10

is syntactically correct, but if x does not exist the the parser should stop

The grammar rule and semantic action look like this

    condition
        =   ( tok.identifier >> tok.oper_ >> tok.value )
            [
                boost::phoenix::bind( &c开发者_如何转开发RuleKit::AddCondition, &myRulekit, 
                        boost::spirit::_1, boost::spirit::_2, boost::spirit::_3 )
            ]
        ;

So now I add a check for the existence of the identifier

    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!

I am not thrilled by the elegance. The grammar syntax is now hard to read and mixing use of boost::bind and boost::phoenix::bind is terribly confusing.

How can I improve it? I would like to get at the 'hit' parameter from phoenix::bind so that I can do the check inside cRuleKit::AddCondition() and so keep the grammar and actions seperate and avoid using boost::bind.


The answer is to use the placeholder _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 )
            ]
        ;


Spirit has a special value you can use in a semantic action to make the parse fail. It's called _pass and you should set it to false.

From some of my code:

variable_reference_impl_[_pass = lookup_symbol_(_1, false)][_val = _1]

in this case, lookup_symbol is a Phoenix functor that returns true if the symbol is found, false if not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜