【问题标题】:Segmentation fault for nested boost::variant嵌套 boost::variant 的分段错误
【发布时间】:2017-11-02 22:17:39
【问题描述】:

以下程序已从原始程序中减少。它运行时出现分段错误。如果我用 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;
}

【问题讨论】:

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


    【解决方案1】:

    你的变种

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多