c++ - boost Spirit istream 迭代器给出误报

标签 c++ parsing boost boost-spirit istream-iterator

所以我试图让 spirit 在输入时解析该文件中的字符。如果可能的话,我宁愿不将完整字符串读入内存。

这是我当前的相关代码,Rosters_Grammar 是一个语法文件,我用它来指定我想要的语法。

#include "StdAfx.h"
#include "Interpreter.h"
#include "Rosters_Grammar.h"
#include <boost\spirit\include\qi.hpp>
#include <fstream>

bool Interpreter::invoke(std::string path)
{
  //Define our parser type and iterator types.
  typedef boost::spirit::istream_iterator iter_type;
  typedef Rosters_Grammar<iter_type> parser_type;

  //Create an instance of our grammar parser and pass it an appropriate project.
  parser_type grammar_parser(project);

  //Open the target file and wrap ifstream into the iterator.
  std::ifstream in = std::ifstream(path);
  if(in.is_open()){

    //Disable Whitespace Skipping
    in.unsetf(std::ios::skipws);

    iter_type begin(in);
    iter_type end;

    //Phrase parse the grammar
    return boost::spirit::qi::phrase_parse(begin,
                                             end, 
                                       qi::int_ , 
                                       boost::spirit::qi::space);
  }
  else{
    return false;
  }
}

出现的问题是我的解析由于某种原因总是成功。考虑到花名册语法,我可以知道它正在读取部分输入,因为它正在相应地执行操作,并且完全按照正确输入的预期工作。然而,解析器不会因输入错误而失败,它只是在文件中途停止并返回 true。

我当前的文件内容是整数和字符串的重复,这样

45 布里干酪 23 黄油苏格兰威士忌

应该仔细阅读并接受。字符串如

“45苹果苹果苹果”

不应该。然而考虑到这个问题,解析器应该会失败。相反,它对“45 Apple”执行操作,然后返回 true 进行解析。我认为这是我的迭代器的问题,但我不能确定。 在上面发布的代码中,我使用 qi::int_ 作为我的解析器,并且无论我的输入数据如何,它总是成功。所以我不认为我的语法文件应该与这里的问题无关。 到目前为止,我让数据失败的唯一方法是使用 !qi::eps 作为我的解析器输入。

感谢任何人能给我的帮助!

编辑: 经过进一步研究后,我实际上认为我的 skipper 出于某种原因是问题所在。 按照我的理解,phrase_parse 传递了 2 个迭代器、某种语法和一个跳过解析器。它根据跳过解析器对输入进行标记,并在语法中使用这些标记。

在不禁用迭代器类型的空白跳过的情况下,我的结果解析出“45 appleappleapple”,并且仅以“45 apple”成功。

最佳答案

我们看不到语法,因为您没有发布它。

可以看到您没有检查输入是否已完全消耗:

    return boost::spirit::qi::phrase_parse(
           begin, end,
           grammar_parser ,
           qi::space);

您可以通过要求 qi::eoi:

来解决这个问题
    return boost::spirit::qi::phrase_parse(
           begin, end,
           grammar_parser >> qi::eoi,
           qi::space);

或者您可以检查迭代器:

    bool ok = boost::spirit::qi::phrase_parse(
           begin, end,
           grammar_parser ,
           qi::space);

    if (begin != end)
        std::cerr << "Remaining unparsed: '" << std::string(begin, end) << "'\n";

    return ok && (begin == end);

最后,请注意,在回溯的情况下,语义操作的副作用永远无法撤销。另请参阅:

关于c++ - boost Spirit istream 迭代器给出误报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20527516/

相关文章:

c++ - 在 C++ 结构中,构造函数中的默认值和默认参数有什么区别?

Javascript 正则表达式解析表情符号并忽略 url

c++ - Boost Intrusive unordered_set 在 C++11 模式下使用 GCC 在 1.48 中被破坏

C++/Boost 文件系统 - 检测到 '_MSC_VER' 不匹配 : value '1700' doesn't match value '1600'

C++ UBSAN 使用派生对象产生误报

c++ - 如何将艺术效果应用于图像和实现它的方法?

c++ - 获取二进制文件的数据和代码区域的大小

javascript - 使用 JSON.NET 解析 JSON

python - 使用 Python 从 HTML 中提取字符串不适用于正则表达式或 BeautifulSoup

c++ - 使用 boost::future with continuations 和 boost::when_all