2016-10-22 86 views
3

我目前正在使用boost spirit X3实现表达式和我的DSL的运算符层次结构。内部编译器错误,同时使用boost spirit x3

我认为我的语法分析器在语义上是正确的,但是当我编译它时,编译时,gcc和clang具有巨大的内存占用,编译了无限的时间,然后退出“g ++:internal compiler error:杀死(程序cc1plus)“。

我试图减少约于表达式解析器的代码,但它在某种程度上不是小事

Here是一个演示。

有人可以告诉我我在做什么错在这里,或者这是一个错误?

编辑: 我认为这个问题是somwhere有:

auto const idx = as<ast::Operation>(helper::idxaccess_op > expression > ']'); 
auto const func = as<ast::Operation>(helper::func_call_op > expression%',' > ')'); 
auto const data = as<ast::Operation>(helper::access_op > expression); 

auto const func_call_expr_def = 
    primary_expr >> *(idx|func|data); 

如果我改变(idx|func|data)(idx|func),还编译永远和使用最多的RAM 16GB,但GCC能够编译它,解析器的工作原理如何。

编辑二:请看看上面的链接是我的例子导致错误。

+0

始终在您的问题中包含代码,以便如果链接发生故障,问题不会中断。 – Rakete1111

+0

内部编译器错误有点严重,所以我认为这是编译器中的一个错误,因为它无法在代码中找到错误,并且在代码正确的情况下编译代码。 – ForceBru

+0

@ Rakete1111我应该如何处理这个,如果产生的例子是对大的方式? – Exagon

回答

6

我已经对源文件进行了一系列更改。请检查那些http://melpon.org/wandbox/permlink/sY2CQkXiMiLoS1BM

的变化是:

  1. 改变了AST。这似乎不正确。主要是向变体添加“一元”的部分。
  2. 更正了语法。这是基于我真正可以从你的测试中得到的语法。我可能是错的,我只尝试过让第一个测试案例工作。更好地运行一个diff工具比较我对你的变化,尤其是在“parser.hpp”

注:如果我的变化不是按您的要求正确的,我建议你启用调试“BOOST_SPIRIT_X3_DEBUG”和跟踪它。在这种情况下,无法强调足够多的BOOST_SPIRIT_X3_DEBUG。

parser.hpp

#include <boost/spirit/home/x3.hpp> 
#include "ast.hpp" 
#include "operators.hpp" 
namespace parser{ 
    namespace x3 = boost::spirit::x3; 


    template<typename T> 
    auto as = [](auto p) { return x3::rule<struct _, T>{} = as_parser(p); }; 


    typedef x3::rule<struct multiplicative_expr_class, ast::Expression> multiplicative_expr_type; 
    typedef x3::rule<struct expression_class, ast::Expression> expression_type; 
    typedef x3::rule<struct primary_expr_class, ast::Operand> primary_expr_type; 
    typedef x3::rule<struct func_call_call_class, ast::Expression> func_call_expr_type; 
    typedef x3::rule<struct unary_expr_class, ast::Operand> unary_expr_type; 



    const primary_expr_type primary_expr = "primary_expr";  
    const func_call_expr_type func_call_expr = "func_call_expr"; 
    const expression_type expression = "expression"; 
    const multiplicative_expr_type multiplicative_expr = "multiplicative_expr"; 
    const unary_expr_type unary_expr = "unary_expr"; 


    auto const primary_expr_def = 
     +(x3::alpha | x3::char_('.')) 
     | ("(" > expression > ")"); 

    auto const idx = as<ast::Operation>(helper::idxaccess_op > primary_expr > ']'); 
    auto const func = as<ast::Operation>(helper::func_call_op > primary_expr%',' > ')'); 
    auto const data = as<ast::Operation>(helper::access_op > expression); 

    auto const func_call_expr_def = 
     primary_expr >> *(idx | func | data); 

    auto const unary_expr_def = 
       func_call_expr 
         | as<ast::Operation>(helper::unary_op > func_call_expr); 

    auto const multiplicative_expr_def = 
     primary_expr >> *(idx | func); 

    auto const expression_def = multiplicative_expr_def; 


BOOST_SPIRIT_DEFINE(primary_expr, 
        func_call_expr, 
        multiplicative_expr, 
        unary_expr, 
        expression); 

} 

ast.hpp

#include <boost/spirit/home/x3.hpp> 
#include <boost/spirit/home/x3/support/ast/variant.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

#include <vector> 

namespace ast{ 
    namespace x3 = boost::spirit::x3; 

    enum class operator_t 
    { 
     _eq_,  // == 
     _ne_,  // != 
     _lt_,  // < 
     _gt_,  // > 
     _le_,  // <= 
     _ge_,  // >= 
     _add_,  // + 
     _sub_,  // - 
     _mul_,  // * 
     _div_,  ///
     _mod_,  // % 
     _pos_,  // unary + 
     _neg_,  // unary - 
     _not_,  // unary ! 
     _size_,  // unary # 
     _bitnot_, // unary ~ 
     _bitand_, // & 
     _bitor_, // | 
     _bitxor_, //^
     _shl_,  // << 
     _shr_,  // >> 
     _and_,  // && 
     _or_,  // || 
     _idx_,  // [] 
     _apply_, //() 
     _access_ // . 
    }; 

    struct nil{}; 
    struct Expression; 

    typedef x3::variant< 
      nil, 
      std::string, 
      x3::forward_ast<Expression> 
      //std::vector<Expression> //adding this as an operand for function parameter 
    > Operand; 

    struct Operation { 
     operator_t operator_; 
     Operand operand_; 
    }; 

    struct Expression { 
     Operand first_; 
     std::vector<Operation> rest_; 
    }; 
} 
BOOST_FUSION_ADAPT_STRUCT(ast::Operation, operator_, operand_) 
BOOST_FUSION_ADAPT_STRUCT(ast::Expression, first_, rest_) 
+0

随着我最新的编辑,继parser.hpp中完成的早期更改,我可以让第二个测试用例通过。 – Arunmu

+0

谢谢你的帮助,添加一元操作数最新怎么了?一元表达式是一个操作数。 我设法得到一个解析器,解析一切,使用一个非模板'parse()'方法的自定义解析器。 我将在未来添加这个答案,因为它有一个完全不同的方法。 – Exagon

+0

TBH,我是精神的新用户,也学习它:)。我从AST中删除它的原因是因为它与“结构操作”相同。从那里我继续了解语法应该如何工作。 – Arunmu

2

问题有事情做与模板实例化深度。 为了避免internal compiler error,并获得可接受的编译时,对于我的解析器,我不得不实现类似模板防火墙,它是作为自定义解析器实现的,它在解析表达式中调用非模板函数。

struct OptimizedExpressionParser : x3::parser<OptimizedExpressionParser> { 
    using attribute_type = ast::Expression; 
    static bool const has_attribute = true; 

    //parse fnction, which calls "non-templated"-firewall function 
    template <typename Iter, typename Ctx, typename Attribute> 
    bool parse(Iter& iFirst, const Iter& iLast, const Ctx& iCtx, x3::unused_type, Attribute& oAttr) const { 
     ast::Expression a; 
     return parse(iFirst, iLast, iCtx, x3::unused, a); 
    } 

    //parse fnction, which calls "non-templated"-firewall function 
    template <typename Iter, typename Ctx> 
    bool parse(Iter& iFirst, const Iter& iLast, const Ctx& iCtx, x3::unused_type, ast::Expression& oAttr) const { 
     if (iFirst != iLast) { 
     return parse_expression(iFirst, iLast, oAttr); 
     } 
     return false; 
    } 
    private: 

    //"non-template"- parse function 
    //of cause this is a template function, but the parser isnt a template argument 
    template <typename Iter> 
    bool parse_expression(Iter& iFirst, const Iter& iLast, ast::Expression& oAst) const { 
     return x3::parse(iFirst, iLast, expression_impl, oAst); 

    } 
    }; 
在此代码 expression_impl

是旧的“重”表达式解析器, 新的“防火墙”表达式解析器是这样的:

auto const expression = OptimizedExpressionParser{};

无论我想在另一个使用表达式解析器或用于解析我现在使用我的OptimizedExpressionParser解析器中的expression对象。 这减少内存的使用,compiletimes,也是生成的二进制文件的大小(大约1.6 GB无模板防火墙) 要看到这是如何工作与我的例子have a look here.

说实话,我从来没有得到解决这个问题在我自己的想法和大部分代码来自here,我只是改变了一点,以解决我给出的例子。