2

因此,我试图在输入该文件时获得精神来解析该文件中的字符。如果可能的话,我宁愿不将完整的字符串读入内存。

这是我当前的相关代码, 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 butter_scotch

应该好好阅读并接受。像这样的字符串

《45苹果苹果苹果》

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

感谢任何人都可以给我的任何帮助!

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

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

4

1 回答 1

2

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

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

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

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

于 2013-12-11T19:54:02.157 回答