c++ - 如何通过 boost spirit 提取 std::string 对象

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

我有以下代码:

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/qi.hpp>

#include <iostream>
#include <string>

struct function
{
  std::string ret_type;
  std::string name;
};

BOOST_FUSION_ADAPT_STRUCT(
  ::function,
  (std::string, ret_type)
  (std::string, name)
)

template <typename Iterator>
struct function_parser : boost::spirit::qi::grammar<Iterator, function(), boost::spirit::qi::ascii::space_type>
{
  function_parser() : function_parser::base_type(start)
  {
    using boost::spirit::qi::ascii::char_;
    using boost::spirit::qi::int_;

    start %= +char_ >> +char_;
  }

  boost::spirit::qi::rule<Iterator, function(), boost::spirit::qi::ascii::space_type> start;
};

int main()
{
  std::string input_data("void foo");

  function fn;
  auto itr = input_data.begin();
  auto end = input_data.end();
  function_parser<decltype(itr)> g;
  bool res = boost::spirit::qi::phrase_parse(itr, end, g, boost::spirit::ascii::space, fn);
  if (res && itr == end)
  {
    std::cout << boost::fusion::tuple_open('[');
    std::cout << boost::fusion::tuple_close(']');
    std::cout << boost::fusion::tuple_delimiter(", ");

    std::cout << "Parsing succeeded\n";
    std::cout << "got: " << boost::fusion::as_vector(fn) << std::endl;
  }
  else
  {
    std::cout << "Parsing failed \n";
  }
}

输出

Parsing failed

我做错了什么?我该如何解决?

最佳答案

+char_

吃掉所有输入!现在,下一个

+char_

需要至少一个不存在的字符(第一个 kleen 加了它)所以解析失败。

我建议:

    using namespace boost::spirit::qi;

    start = lexeme[+graph] >> lexeme[+graph];

文档应该能够告诉你它做了什么(我希望。没时间详细说明)

关于c++ - 如何通过 boost spirit 提取 std::string 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21622936/

相关文章:

c++ - boost::spirit ,如何获取占位符的 "value"

python - 将 Python 函数转换为 C++ 函数

c++ - 我正在尝试使用 glTexSubImage2D 更新纹理

c++ - boost::asio::io_service 优先级如何工作?

c++ - 将 Boost.Filesystem 静态链接到共享库时出现问题

c++ - 将规则生成绑定(bind)到我的结构成员时出现编译错误

c++ - 用列号 boost spirit 动态词法分析器?

c++ - 播放器结构生成 C2440 错误

c++ - 我可以绑定(bind)到新的 char[] 吗?

c++ - boost::lambda::var 嵌套在 boost::bind 中不等同于 boost::lambda::var 本身