Wil*_*urn 5 c++ boost-spirit boost-spirit-x3
我正在尝试用Spirit X3编写一个解析器,但我还没有走得太远,因为我遇到了一个我无法弄清楚的编译错误.我想我知道编译器在抱怨什么,但我不明白为什么它会关心.以下代码编译(clang 3.9,Boost 1.62.0)并且有效.(我意识到它的结构很差,并且有常量的内置解析器;我只是试图证明这个问题.)
#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
using namespace std;
namespace x3 = boost::spirit::x3;
namespace lang {
    using namespace x3;
    struct Constant {
    };
    rule<class constant_id, Constant> const constant = "constant";
    auto const integer_literal = -char_('-') >> +digit;
    auto const float_literal = -char_('-') >> +digit >> -(char_('.') >> *digit);
    auto const constant_def = (integer_literal)[([](auto &ctx) { _val(ctx) = Constant(); })];
    BOOST_SPIRIT_DEFINE(constant);
}
int main(int argc, char *argv[]) {
    string s("3.14159");
    if (x3::phrase_parse(s.begin(), s.end(), lang::constant, x3::space)) {
        cout << "Ok!";
    } else {
        cout << "Error!";
    }
    return 0;
}
但是,如果我更改constant_def以使用float_literal而不是integer_literal:
auto const constant_def = (float_literal)[([](auto &ctx) { _val(ctx) = Constant(); })];
我开始得到编译错误.
sequence.hpp:143:9: error: static_assert failed "Attribute does not have the expected size."
    static_assert(
    ^
看一下来源,我看到一条消息:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
我认为错误与float_literal定义中存在可选项有关.我相信属性类型integer_literal是类似vector<char>但是可选导致属性float_literal类似tuple<vector<char>, optional<vector<char>>>?在某个地方,我试图强迫它成为一个字符串或其他东西而且它失败了,因为它试图将2元组转换为1元组.我怀疑解决方案是将float_literal定义包装起来,raw[]以便我得到底层字符串,这是我想要的.但由于这是我的第一个X3解析器,我想了解为什么编译器会引发此错误,因此如果它出现在另一个上下文中我可以诊断它.
我真正不明白的是为什么编译器关心的类型float_literal.我附加了一个生成Constant实例的语义动作,它是常量规则的属性类型,没有任何对该属性的引用float_literal,那么编译器为什么要关心该属性float_literal呢?它在哪里尝试进行不等长度的序列分配,以及用于什么目的?
谢谢!
| 归档时间: | 
 | 
| 查看次数: | 273 次 | 
| 最近记录: |