c++ - boost::spirit::x3 中的简单字符串解析器不起作用

标签 c++ boost boost-spirit

出于学习目的,我尝试编写一个简单的解析器,它接受字符串文字并使用 boost 中的 x3 库将其放入自定义结构中。但是,以下最小示例改编自示例 here无法编译。

#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>

namespace x3 = boost::spirit::x3;

namespace ast {
struct Symbol {
    std::string val;
  };
}
BOOST_FUSION_ADAPT_STRUCT(
ast::Symbol,
val
)

namespace parser {
  x3::rule<class Symbol, ast::Symbol> const
  symbol = "symbol";

  auto const symbol_def = x3::string("asd");// | x3::string("qwe");

  BOOST_SPIRIT_DEFINE(
  symbol
  );
}

int main(int argc, char** argv) {
  std::string input = "asd";
  if (argc > 1) {
    input = argv[1];
  }
  std::string::const_iterator begin = input.begin();
  std::string::const_iterator end = input.end();

  ast::Symbol sym;
  bool success = x3::phrase_parse(begin, end, parser::symbol, 
                                  x3::ascii::space, sym);
  std::cout << success << std::endl;
  std::cout << sym.val << std::endl;
  return 0;
 }

这会产生一个很长的模板编译器错误,可归结为

cannot convert ‘dest’ (type ‘ast::Symbol’) to type 
‘boost::spirit::x3::traits::variant_attribute’

这对我来说没有意义,因为解析器 x3::string 应该有一个字符串属性,而 ast::Symbol 结构有一个字符串字段,x3 应该能够自动填充该字段,因为我已经使用 Fusion 调整了该结构宏。更令人困惑的是,如果我将解析器的定义更改为 read

auto const symbol_def = x3::string("asd") | x3::string("qwe");

它编译并工作,即使现在解析器应该有一个类型变体的属性。也许有人可以澄清为什么会发生这种情况,因为我似乎缺少有关库如何工作的一些信息。

最佳答案

我认为这个问题已得到解决:

关于c++ - boost::spirit::x3 中的简单字符串解析器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359086/

相关文章:

c++ - 使用 boost::spirit::qi 解析带分隔符的数字

c++ - 重复的属性传播规则 (%)

c++ - 从单链表中删除最后一个节点

c++ - Visual Studio 2012 - 嵌入式输出控制台而不是 cmd?

c++ - regex_search.hpp:56: 未定义对`boost::re_detail_106100::perl_matcher 的引用

c++ - Boost如何直接使用流插入到C字符串中?

c++ - 如何仅使用 boost/standard 库在 C++ 中读/写图像?

c++ - 类成员的typedef

c++ - 如何使用 boost::spirit 验证代数语句?

c++ - pthread_cond_timedwait 与 Solaris 10 上的 clock_gettime 链接错误