【问题标题】:how to make error handling work for boost::spirit如何为 boost::spirit 进行错误处理
【发布时间】:2013-10-15 07:43:37
【问题描述】:

在 boost::spirit 中,我添加了基于 example roman 的错误处理代码。

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/foreach.hpp>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

template <typename Iterator>
struct roman : qi::grammar<Iterator>
{
  roman() : roman::base_type(start)
  {
    using qi::eps;
    using qi::lit;
    using qi::lexeme;
    using qi::_val;
    using qi::_1;
    using ascii::char_;

    // for on_error
    using qi::on_error;
    using qi::fail;
    using phoenix::construct;
    using phoenix::val;

    start = +(lit('M') )  >> "</>";

    on_error<fail>
    (
        start
      , std::cout
            << val("Error! Expecting ")
            // << _4                            // what failed?
            << val(" here: \"")
            // << construct<std::string>(_3, _2)   // iterators to error-pos, end
            << val("\"")
            << std::endl
    );
  }
  qi::rule<Iterator> start;
};

int
main()
{
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "\t\tRoman Numerals Parser\n\n";
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n";

    typedef std::string::const_iterator iterator_type;
    typedef roman<iterator_type> roman;

    roman roman_parser; // Our grammar

    std::string str;
    unsigned result;
    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        //[tutorial_roman_grammar_parse
        bool r = parse(iter, end, roman_parser, result);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded\n";
            std::cout << "result = " << result << std::endl;
            std::cout << "-------------------------\n";
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \": " << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
        //]
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

我的问题是:

  1. “on_error”没有触发,为什么?
  2. 我注释了“

【问题讨论】:

    标签: c++ boost-spirit


    【解决方案1】:

    三个步骤:

    1. 限定占位符:

      on_error<fail>(start, 
              std::cout
                 << val("Error! Expecting ")
                 << qi::_4
                 << val(" here: \"")
                 << construct<std::string>(qi::_3, qi::_2)
                 << val("\"")
                 << std::endl
          );
      
    2. 您还需要确保有触发错误处理程序的预期点。

      start = eps > +(lit('M') ) >> "</>";
      

      参见例如Boost.Spirit.Qi - Errors at the beginning of a rule解释

    3. 可选)命名你的规则

      start.name("start");
      

      使用 BOOST_SPIRIT_DEBUG_NODE(S) 是另一种隐式命名规则的方法。

    查看Live on Coliru(在某些地方进行了清理和简化)

    现在打印(输入 iv):

    Error! Expecting <sequence>"M""</>" here: 'iv'
    Parsing failed
    stopped at: 'iv'
    

    完整代码

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    
    #include <iostream>
    #include <fstream>
    
    namespace qi  = boost::spirit::qi;
    namespace phx = boost::phoenix;
    
    template <typename Iterator>
    struct roman : qi::grammar<Iterator>
    {
        roman() : roman::base_type(start)
        {
            using namespace qi;
    
            start = eps > +lit('M') >> "</>";
            start.name("start");
    
            on_error<fail>(start, 
                    phx::ref(std::cout)
                       << "Error! Expecting "
                       << qi::_4
                       << " here: '"
                       << phx::construct<std::string>(qi::_3, qi::_2)
                       << "'\n"
                );
        }
        qi::rule<Iterator> start;
    };
    
    int main()
    {
        typedef std::string::const_iterator iterator_type;
        roman<iterator_type> roman_parser; // Our grammar
    
        std::string str;
        while (std::getline(std::cin, str))
        {
            if (str.empty() || str[0] == 'q' || str[0] == 'Q')
                break;
    
            iterator_type iter = str.begin(), end = str.end();
            unsigned result;
            bool r = parse(iter, end, roman_parser, result);
    
            if (r && iter == end)
            {
                std::cout << "Parsing succeeded\n";
                std::cout << "result = " << result << std::endl;
            }
            else
            {
                std::string rest(iter, end);
                std::cout << "Parsing failed\n";
                std::cout << "stopped at: '" << rest << "'\n";
            }
        }
    }
    

    除了评论之外:这是我一直在测试的东西 - 还没有完全让它工作,但是错误处理程序正在被调用并按照它应该的方式吃输入。或许有帮助?

    static auto const at_eol = (*_1 == '\r') || (*_1 == '\n');
    static auto const at_eoi = (_1 == _2);
    
    on_error<retry>(start, 
        (
            (phx::ref(std::cout) << "rule start: expecting " << _4 << " here: '" << escape_(_3, _2) << "'\n"),
            phx::while_ (!at_eoi && !at_eol) [ ++_1, phx::ref(std::cout) << "\nadvance to newline\n" ],
            phx::while_ (!at_eoi && at_eol)  [ ++_1, phx::ref(std::cout) << "\neat newline\n" ],
            phx::if_ (at_eoi)                [ _pass = fail ]
        )
    );
    

    另请参阅 multi_pass 文档中Important 下的注释

    【讨论】:

    • 你好,开始 = eps > +(lit('M') ) >> ">";你的意思是我们需要“eps”来触发on_error?但我在 mini_xml3.cpp 中没有看到“eps”?
    • 您需要operator&gt; 来表示预期点。由于该运算符需要 2 个操作数,因此您需要一个 left-hand-side 运算符 (qi::eps) 担任该角色。
    • mini_xml 样本中缺少领先期望点这一事实可能是一个疏忽,并且在被问及for the second time on SO 后一直是reported on the spirit-general list
    • 我可以在 on_error 中做一些恢复工作吗,例如将指针移动到下一行(在 \r\n 之后),以便解析器忽略当前行并继续解析?
    • 你好,还有一个问题,你能解释一下为什么 _4 表示期望的标记,而 _3 到 _2 是当前输入吗?为什么是这个顺序?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    相关资源
    最近更新 更多