【问题标题】:boost spirit istream iterator giving false positives提升精神 istream 迭代器给出误报
【发布时间】:2013-12-29 22:05:39
【问题描述】:

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

这是我当前的相关代码,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 作为我的解析器输入。

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

        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);
    

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

    【讨论】:

    • 哇。我在这里根本错了。我曾认为只有在消耗了整个输入时才认为解析是成功的。这段时间我一直在想。感谢您的现实检查。这应该会有很大帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2013-06-09
    相关资源
    最近更新 更多