c++ - boost::spirit::qi 规则减少解析错误

标签 c++ parsing boost boost-spirit boost-spirit-qi

我尝试使用 boost::spirit::qi 解析 integerdouble 对的列表。 integer 在此列表之前和之后。此列表的一个示例是:

20 1 1.3 2 2.3 30

我要创建的结构是:

typedef std::vector< std::pair<int, double> > vec_t;
struct list_s{
  int first;
  std::vector< std::pair<int, double> > list;
  int last;
};

我还使用 qi::on_error 向我的解析器添加了错误处理,并将所有序列 >>> 转换为预期的 >

我的实现是:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

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

typedef std::vector< std::pair<int, double> > vec_t;
struct list_s{
  int first;
  vec_t list;
  int last;
};

BOOST_FUSION_ADAPT_STRUCT(
    list_s,
    (int, first)
    (vec_t, list)
    (int, last)
)

template <typename Iterator>
struct list_parser : qi::grammar<Iterator, list_s(), ascii::space_type >
{
  public:
    list_parser() : list_parser::base_type(r) {
      r = qi::eps >>
        (
          qi::int_
          > *( qi::int_ > qi::double_ )
          > qi::int_
        );

      r.name("r");
      qi::on_error<qi::fail>
        (
         r
         , std::cout
         << phoenix::val("\nError! Expecting ")
         << qi::labels::_4 
         << phoenix::val(" here: \"")
         << phoenix::construct<std::string>(qi::labels::_3, qi::labels::_2)
         << phoenix::val("\"\n\n")
        );
    }
  private:
    qi::rule<Iterator, list_s(), ascii::space_type > r;
};

int main() {
  std::string str("20 1 1.3 2 2.3 30");

  list_s r;
  list_parser<std::string::const_iterator> p;
  std::string::const_iterator iter = str.begin();
  std::string::const_iterator end = str.end();
  if( qi::phrase_parse(iter, end, p, ascii::space, r) ){
    std::cout << r.first << std::endl;
    for(auto& i : r.list) std::cout << i.first << " , " << i.second << std::endl;
    std::cout << r.last << std::endl;
  }
  return 0;
}

当运行这个例子时我得到

Error! Expecting <real> here: ""

我知道我可以将列表解析规则更改为sequence

*( qi::int_ >> qi::double_ )

克服上述错误。但是随后20 1 1.3 2 error 30的解析也是成功的。结果将是:

20
1 , 1.3
2

有什么解决这个问题的建议吗?

最佳答案

期望点创建非回溯期望。

所以你不能接受失败后的替代分支。

这意味着您的最后一个整数将与 (int_ > double_) 中的整数匹配。然而,该表达式需要 (>) double_ 遵循,因为它不需要,它会抛出。

> 代替 >>

事实上,看起来您只是想确保整个输入/行都被消耗掉,所以让您的期望显式:

    r = (
            qi::int_
            > *( qi::int_ >> qi::double_ )
            > qi::int_
        )
        > qi::eoi
        ;

演示

Live On Coliru

Note: also enabled rule debug (implicitly names the rule):

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapted.hpp>

#include <iostream>

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

typedef std::vector<std::pair<int, double>> vec_t;

struct list_s{
  int first;
  vec_t list;
  int last;
};

BOOST_FUSION_ADAPT_STRUCT(list_s, first, list, last)

template <typename Iterator>
struct list_parser : qi::grammar<Iterator, list_s(), ascii::space_type>
{
  public:
    list_parser() : list_parser::base_type(r) {
        r = (
                qi::int_
                > *( qi::int_ >> qi::double_ )
                > qi::int_
            )
            > qi::eoi
            ;

      BOOST_SPIRIT_DEBUG_NODES((r))

      qi::on_error<qi::fail>
        (
            r
            , std::cout
            << phoenix::val("\nError! Expecting ")
            << qi::labels::_4 
            << phoenix::val(" here: \"")
            << phoenix::construct<std::string>(qi::labels::_3, qi::labels::_2)
            << phoenix::val("\"\n\n")
        );
    }
  private:
    qi::rule<Iterator, list_s(), ascii::space_type > r;
};

int main() {
    std::string str("20 1 1.3 2 2.3 30");

    using It = std::string::const_iterator;

    list_parser<It> p;
    It iter = str.begin(), end = str.end();

    list_s data;
    if (qi::phrase_parse(iter, end, p, ascii::space, data)){
        std::cout << data.first << "\n";

        for(auto& i : data.list) 
            std::cout << i.first << ", " << i.second << "\n";

        std::cout << data.last << "\n";
    }
}

打印:

20
1, 1.3
2, 2.3
30

调试输出:

<r>
    <try>20 1 1.3 2 2.3 30</try>
    <success></success>
    <attributes>[[20, [[1, 1.3], [2, 2.3]], 30]]</attributes>
</r>

对于错误输入:

Live On Coliru

Error! Expecting <eoi> here: "error 30"

带有调试输出:

<r>
  <try>20 1 1.3 2 error 30</try>
  <fail/>
</r>

关于c++ - boost::spirit::qi 规则减少解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34896701/

相关文章:

c++ - async_send 数据未发送

c# - .net/C# 的 Html 解析器和对象模型

python - 如何解析 Python 代码,同时保持字符串文字完全原样?

c++ - boost bcp --namespace 到底做了什么?

c++ - 如何在 Visual Studio 2010 中安装 Crypto++?

c++ - 从子类中调用类函数

c++ - 如何用 C++ 从 MySQL 获取 Blob 数据

bash - 如何在 Bash 源代码中查找变量名

c++ - 如何使用 boost::asio::serial_port 支持硬件流控制?

c++ - 如何使用 Boost 在 C++ 中制作代理服务器