[英]Simple string parser in boost::spirit::x3 not working
For learning purposes I'm trying to write a simple parser that accepts a string literal and puts it in a custom struct using the x3 library from boost. 出于学习目的,我试图编写一个简单的解析器,该解析器接受字符串文字并将其使用来自boost的x3库放入自定义结构中。 However, the following minimal example which is adapted from the example here does not compile.
但是,下面的最小示例(根据此处的示例改编而成)无法编译。
#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;
}
This gives a very long, template compiler error that boils down to 这给出了一个很长的模板编译器错误,最终归结为
cannot convert ‘dest’ (type ‘ast::Symbol’) to type
‘boost::spirit::x3::traits::variant_attribute’
This does not make sense to me because the parser x3::string should have a string attribute and the ast::Symbol struct has a string field which x3 should be able to populate automatically because I have adapted the struct with the Fusion macro. 这对我来说没有意义,因为解析器x3 :: string应该具有字符串属性,而ast :: Symbol结构具有一个字符串字段,x3应该能够自动填充该字符串字段,因为我已经使用Fusion宏对结构进行了调整。 What's more confusing is that if I change the definition of the parser to read
更令人困惑的是,如果我将解析器的定义更改为read
auto const symbol_def = x3::string("asd") | x3::string("qwe");
it compiles and works, even though now the parser should have an attribute of type variant. 即使现在解析器应具有variant类型的属性,它也可以编译和运行。 Maybe someone can clarify why this happens because I seem to be missing something about how the library works.
也许有人可以弄清楚为什么会发生这种情况,因为我似乎缺少有关库工作方式的信息。
I think that's an issue that has been fixed: 我认为这是已解决的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.