【问题标题】:Internal compiler error, while using boost spirit x3内部编译器错误,同时使用 boost spirit x3
【发布时间】:2016-10-22 18:59:50
【问题描述】:

我目前正在使用 boost spirit X3 为我的 DSL 实现表达式和运算符层次结构。

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

我试图最小化表达式解析器周围的代码,但它在某种程度上并不是那么微不足道

Here 是一个演示。

谁能告诉我我在这里做错了什么,或者这是一个错误?

编辑: 我认为问题出在哪里:

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),它也会永远编译并使用多达 16GB 的内存,但 gcc 能够编译它,并且解析器可以正常工作。

编辑二:请查看上面的链接,这是导致错误的示例。

【问题讨论】:

  • 始终在问题中包含代码,这样如果链接断开,问题不会中断。
  • 内部编译器错误有点严重,所以我认为这是您的编译器中的一个错误,因为它未能在您的代码中找到错误并在正确时编译代码。
  • @Rakete1111 如果生成的示例太大,我应该如何处理?
  • @Exagon 您的演示链接显示的代码不长,所以我不明白您的问题,您可以添加该代码。是的,您不应该添加整个应用程序,但这就是您创建 minimal reproducible example 的原因,对吧?
  • @Rakete1111 你看到我的链接中的 mcve 包含多个文件?

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


【解决方案1】:

我对您的源文件做了很多更改。请查看http://melpon.org/wandbox/permlink/sY2CQkXiMiLoS1BM

变化如下:

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

注意:如果我的更改不符合您的要求,我建议您启用调试“BOOST_SPIRIT_X3_DEBUG”并跟踪它。在这种情况下,BOOST_SPIRIT_X3_DEBUG 的用处再怎么强调也不为过。

解析器.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_)

【讨论】:

  • 通过我的最新编辑,在之前对 parser.hpp 所做的更改之后,我也可以通过第二个测试用例。
  • 感谢您的帮助,添加一元作为操作数有什么问题?一元表达式是一个操作数。我设法获得了一个解析器,它使用带有非模板parse() 方法的自定义解析器来解析所有内容。我会在将来添加这个作为答案,因为它是一种完全不同的方法。
  • TBH,我是精神的新用户,也在学习它:)。我从 AST 中删除它的原因是,它与“struct Operation”相同。从那里我继续理解语法应该如何工作。
  • 我只是使用了一个单独的结构,因为这简化了我以后对 ast 的操作。
【解决方案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 对象。 这减少了 ram 的使用、编译时间以及生成的二进制文件的大小(在没有模板防火墙的情况下约为 1.6 Gb) 看看我的例子如何解决这个问题have a look here.

说实话,我自己永远不会解决这个问题,这个想法和大部分代码都来自here,我只是稍微改变了一下以解决我给定的例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多