Search code examples
c++boostboost-spiritboost-spirit-x3

Segmentation fault for nested boost::variant


The following program has been reduced from the original. I get a segmentation fault when it runs. If I remove line 24 with ArithmeticUnaryExpression then the program no longer crashes. How do I get rid of the segmentation fault?

#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;
}

Solution

  • Your variant

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

    will default-construct to the first element type. The first element type contains ArithmeticExpression which is also default constructed. Can you see the problem already?

    Just make sure the default constructed state doesn't lead to infinite recursion:

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