2016-10-22 52 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,我只是改變了一點,以解決我給出的例子。

相關問題