c++ - 简单的 C 风格结构化输入的 Spirit Qi 解析问题

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

我正在尝试为一些使用熟悉且非常简单的“C 风格”格式的游戏数据设置基本解析器。基本上,命名支撑的“结构”,然后将参数和嵌套的“结构”放入其中。它会解析这样的东西:

Name0
{
 Name1
 {
  Param0 *= 2
  Param2 = "lol"
 }
 Param0 = 1
 Param1 = "test"

 Name2 { }
}

Name3 {
 Param0 = "test"
}

但是,即使是简单的“Test {}”输入测试,它也失败了,更不用说像我上面的例子那样高级了。这些结构设置为使用 fusion ,这看起来很简单,我怀疑这是问题所在。我目前没有使用一些规则,而且我的大部分规则都未经测试,因为它在 root 中尝试第一个 category 规则时失败了。这是我在输入“Test {}”时遇到的错误:

Error! Expecting <sequence>"{"<node> here: ""

这是解析器类:

 template<typename Iterator>
 struct Parser : qi::grammar<Iterator, std::vector<Category>(), ascii::space_type>
 {
  qi::rule<Iterator, std::vector<Category>(), ascii::space_type> root;

  qi::rule<Iterator, Category(), ascii::space_type> category;
  qi::rule<Iterator, Param(), ascii::space_type> param;

  qi::rule<Iterator, Node(), ascii::space_type> node;

  qi::rule<Iterator, Value(), ascii::space_type> value;

  qi::rule<Iterator, char()> escape;
  qi::rule<Iterator, std::string()> quotedstring;
  qi::rule<Iterator, std::string()> normalstring;

  qi::rule<Iterator> comment;
  qi::rule<Iterator> commentblock;

  Parser() : Parser::base_type(root, "root")
  {
   using namespace qi;

   using ascii::char_;

   using phoenix::construct;
   using phoenix::val;

   escape %= '\\' > char_("\\\"");
   quotedstring %= '"' >> *((char_ - '"') | escape) > '"';
   normalstring %= *(char_ - qi::eol);

   comment = "//" >> *(char_ - qi::eol);
   commentblock = "/*" >> *(char_ - "*/") > "*/";

   node %= category | param; //comment? comment block? holding off for now

   value %= normalstring | float_;

   param %=
    lexeme[+(char_ - operators)]
    > operators
    > value
    > qi::eol;

   category %=
    lexeme[+(char_ - '{')] //won't this grab all whitespace around the tag too?
    > '{'
    >> *node
    > '}';

    root %= *category;

    root.name("root");

    category.name("category");
    param.name("param");

    node.name("node");

    value.name("value");

    escape.name("escape");
    quotedstring.name("quotedstring");
    normalstring.name("normalstring");

    comment.name("comment");
    commentblock.name("commentblock");

    debug(root);
    debug(category);
    debug(param);
    debug(node);
    debug(value);
    debug(escape);
    debug(quotedstring);
    debug(normalstring);
    debug(comment);
    debug(commentblock);

    on_error<fail>
     (
     root,
     std::cout
     << val("Error! Expecting ")
     << _4
     << val(" here: \"")
     << construct<std::string>(_3, _2)
     << val("\"")
     << std::endl
     );
  }
 };

与此无关的是,是否可以在 on_successon_error 调用中使用 C++11 lambda?我查看了 on_error 函数,它似乎将其参数模板化为规则类型,这意味着必须为每个规则类型(基本上是每个规则)定义一个 lambda。那是对的吗?太糟糕了,那些 phoenix lambda 太不透明了,我什至不知道如何提取行号并将其放入结构中。

编辑:

这是运算符表:

 struct Operators : qi::symbols<char, Operator>
 {
  Operators()
  {
   add
    ("=", Operator::equal)
    ("+=", Operator::plusequal)
    ("-=", Operator::minusequal)
    ("*=", Operator::timesequal)
    ("/=", Operator::divideequal)
    ;
  }
 } operators;

最佳答案

operators 没有给出。

我猜你的 node 规则吃掉了结束 所以 catagory 规则不能成功。

关于c++ - 简单的 C 风格结构化输入的 Spirit Qi 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21835977/

相关文章:

c++ - boost 智能指针对我有帮助吗?

c++ - concurrency::when_all 的返回 vector 的结果是 FIFO 吗?

c++ - 包装 dx9 接口(interface)和 Hook 函数

c++ - 如何获取所有使用全局变量的函数名?

html - 在 R 中的 rvest 包中的 html 函数中使用哪个选择器?

c# - 如何将 Delegate 转换为一串代码?

c - 在C中将字符串解析为 float

c++ - solaris 10 gcc 4.8.2 上的 -lpthread 和 -pthreads 有什么区别

c++ - 来自 file_descriptor_source (boost::iostreams) 或文件的 istream

c# - 模拟鼠标单击侧边按钮