Boost :: Spirit - on_error不打印

Tho*_*ews 6 c++ error-handling parsing boost-spirit boost-spirit-qi

我正在尝试使用on_errorBoost :: Spirit :: qi 的机制来找出解析失败的原因.

我在on_error函数中设置了一个断点,并且正在调用函数,但没有输出(nada,nothing,void,...).

简单on_error:

on_error<fail>(level1,
    boost::phoenix::ref(std::cout) << "I've failed.\n"
    );
Run Code Online (Sandbox Code Playgroud)

复杂on_error(来自不同的网站):

on_error<fail>
(
    start,
    boost::phoenix::ref(std::cout)
        << val("Error! Expecting ")
        << _4
        << val(" here: \"")
        << construct<std::string>(qi::_3, qi::_2)
        << val("\"")
        << std::endl
    );
Run Code Online (Sandbox Code Playgroud)

这是我的类包含简单on_error:

template <typename Iterator, typename Skipper>
struct Event_Compound
    : qi::grammar<Iterator, Skipper>
{
    Event_Compound () 
        : Event_Compound::base_type(start, "Compound-Event")
        {
            using qi::lexeme;
            using qi::lit;
            using namespace qi;
            using boost::spirit::ascii::char_;

            relational_operator =
                lit("&&")[Matched_Relational_AND]
                || lit("||")[Matched_Relational_OR]
                ;

            compound =
                level1[Matched_Nested_Level1_Begin] >> relational_operator[Matched_Relational_Operator] >> level1[Matched_Nested_Level1_End]
                ;

            compare_or_compound =
                compound[Matched_Compound] | grammar_comparison_event[Matched_Comparison_Event]
                ;

            level1 =
                grammar_boolean_event[Matched_Boolean_Event]
                | ( char_('(')[Matched_Open_Paren] >> compare_or_compound[Matched_Compare_Or] >> char_(')')[Matched_Close_Paren]  )
                ;

            start =
                level1[Matched_Level1_Begin] >> relational_operator[Matched_Relational_Operator] >> level1[Matched_Level1_End]
                ;
            on_error<fail>(level1,
                boost::phoenix::ref(std::cout) << "I've failed.\n"
                );
        }

    Event_Boolean<Iterator, Skipper>        grammar_boolean_event;
    Event_Comparison<Iterator, Skipper>     grammar_comparison_event;
    qi::rule<Iterator, Skipper>             level1;
    qi::rule<Iterator, Skipper>             compound;
    qi::rule<Iterator, Skipper>             compare_or_compound;
    qi::rule<Iterator, Skipper>             relational_operator;
    qi::rule<Iterator, Skipper>             start;
};
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法来跟踪解析器的行为或思维模式?(例如设置预处理器宏或某些标志变量)

为什么没有任何输出on_error

另外,做什么_1, _2, _3_4参考?

我正在尝试调试语法,并且我已经输出了匹配的规则,但是当规则不匹配时,我想知道哪个规则和原因.

我正在使用:

  • 提升1.57.0
  • Visual Studio 2010
  • Windows 7的

研究:

seh*_*ehe 4

错误处理仅与期望点相关。这些你好像都没有。

调试语法使用

  • #define BOOST_SPIRIT_DEBUG 在任何提升之前包括
  • BOOST_SPIRIT_DEBUG_NODE(node)BOOST_SPIRIT_DEBUG_NODES((node1)(node2)...)选择节点进行调试

这将向您展示正在执行的回溯(如果有)和属性传播。如果您使用它们,还将显示本地属性和继承属性。

请注意,您的规则的属性需要融合适应/可流化才能进行调试。