boost spirit AST stack overflow!?



  • Hallo!

    Nachdem ich nach den Hinweisen hier im Forum angefangen habe einen AST parser zu implementieren bin ich schon direkt in ein Problem gelaufen.
    Mein Evaluationscode verursacht leider einen stack overflow da anscheinend mein variant-visitor nicht mehr zurückkehrt.
    Mitlerweile bin ich ratlos, deshalb frage ich hier.

    struct AstNode {
        typedef boost::variant<
                        Nil,  // should not happen, only for initialization
                        unsigned int,
                        double,
                        boost::recursive_wrapper<AstNode>,
                        boost::recursive_wrapper<UnaryOperator>,
                        boost::recursive_wrapper<BinaryOperator>
                    >
                type;
    
        AstNode()
            : expression(Nil()) {}
    
        template <typename ExpressionType>
        AstNode(ExpressionType const& expr)
            : expression(expr) {}
    
        type    expression;
    };
    // ------------------------------------------------
    template <typename Iterator>
    struct exec_grammar : qi::grammar<Iterator, new_grammar::AstNode(), ascii::space_type> {
        exec_grammar() 
            : exec_grammar::base_type(expression)
        {
            using qi::_val;
            using qi::_1;
            using qi::double_;
    
            float_constant  =
                    double_                     [_val = _1]
                    ;
    
            expression      = float_constant;
        }
    
        qi::rule<Iterator, new_grammar::AstNode(), ascii::space_type> float_constant, expression;
    };
    // ------------------------------
    struct AstEvaluate {
        typedef double result_type;  // TODO(neelie): change to boost::variant type in AstNode
        double operator()(qi::info::nil) const {
            return 0.0;
        }
        double operator()(double n) const {
            return n;
        }
        double operator()(unsigned int n) const {
            return static_cast<double>(n);
        }
        double operator()(new_grammar::AstNode const& node) const {
            return boost::apply_visitor(*this, node.expression);
        }
    };
    // -------------------------------
    typedef AstNode tpi_type;
    
    double CGrammar::DoEvaluateExpression(CFormula * const f, tpi_type& i, CEvalInfoFunction **logCallingFunction, int *e) {
        AstEvaluate evaluator;
    	double result = evaluator(i); 
        return result;
    }
    

    Hat jemand einen hilfreichen Tipp?

    cheerio


Anmelden zum Antworten