簡體   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