嵌套boost :: variant的分段错误

时间:2017-11-02 14:11:00

标签: c++ boost boost-spirit boost-spirit-x3

以下程序已从原始程序中删除。它运行时出现分段错误。如果我使用ArithmeticUnaryExpression删除第24行,则程序不再崩溃。如何摆脱分段错误?

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/spirit/include/qi_expect.hpp>
#include <boost/spirit/home/x3/directive/expect.hpp>

#include <iostream>
#include <string>

namespace wctl_parser {

namespace x3 = boost::spirit::x3;
namespace ascii = x3::ascii;
namespace qi = boost::spirit::qi;

using x3::ulong_;
using x3::lexeme;

//--- Ast structures
struct ArithmeticUnaryExpression;
using AtomicProp = std::string;

using ArithmeticExpression = x3::variant<
    x3::forward_ast<ArithmeticUnaryExpression>,
    unsigned long
>;

struct ArithmeticUnaryExpression {
    std::string op;
    ArithmeticExpression operand;
};

using Expression = x3::variant<
    ArithmeticExpression
>;

template <typename T> auto rule = [](const char* name = typeid(T).name()) {
    struct _{};
    return x3::rule<_, T> {name};
};

template <typename T> auto as = [](auto p) { return rule<T>() = p; };

//--- Rules

x3::rule<struct aTrivRule, ArithmeticExpression> aTriv("aTriv");
x3::rule<struct exprRule, Expression> expr("expression");

auto const aTriv_def = rule<ArithmeticExpression>("aTriv")
    = ulong_
//  | '(' > expr > ')'
    ;

auto const primitive = rule<Expression>("primitive")
    = aTriv
    ;

auto const expr_def
    = primitive
    ;

BOOST_SPIRIT_DEFINE(aTriv)
BOOST_SPIRIT_DEFINE(expr)

auto const entry = x3::skip(ascii::space) [expr];

} //End namespace

int main() {

    std::string str("prop");
    namespace x3 = boost::spirit::x3;   
    wctl_parser::Expression root;
    auto iter = str.begin();
    auto end = str.end();
    bool r = false;
    r = parse(iter, end, wctl_parser::entry, root);
    if (r) {
        std::cout << "Parses OK:" << std::endl << str << std::endl;
        if (iter != end) std::cout << "Partial match" << std::endl;
        std::cout << std::endl << "----------------------------\n";
    }
    else {
        std::cout << "!! Parsing failed:" << std::endl << str << std::endl << std::endl << "----------------------------\n";
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

你的变种

using ArithmeticExpression = x3::variant<
    x3::forward_ast<ArithmeticUnaryExpression>,
    unsigned long
>;

将默认构造为第一个元素类型。第一个元素类型包含ArithmeticExpression,它也是默认构造的。你能看到问题吗?

确保默认构造状态不会导致无限递归:

using ArithmeticExpression = x3::variant<
    unsigned long,
    x3::forward_ast<ArithmeticUnaryExpression>
>;
相关问题