出于学习目的,我尝试编写一个简单的解析器,它接受字符串文字并使用 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");
它编译并工作,即使现在解析器应该有一个类型变体的属性。也许有人可以澄清为什么会发生这种情况,因为我似乎缺少有关库如何工作的一些信息。
最佳答案
我认为这个问题已得到解决:
- Boost 1.66 失败 https://wandbox.org/permlink/H1InadjEYI8KTHto
- Boost 1.67 成功 https://wandbox.org/permlink/aZnnpzizXFOe7emp
关于c++ - boost::spirit::x3 中的简单字符串解析器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359086/