繁体   English   中英

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

[英]Simple string parser in boost::spirit::x3 not working

出于学习目的,我试图编写一个简单的解析器,该解析器接受字符串文字并将其使用来自boost的x3库放入自定义结构中。 但是,下面的最小示例(根据此处的示例改编而成)无法编译。

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

即使现在解析器应具有variant类型的属性,它也可以编译和运行。 也许有人可以弄清楚为什么会发生这种情况,因为我似乎缺少有关库工作方式的信息。

我认为这是已解决的问题:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM