3

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

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

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

是一个演示。

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

编辑:我认为问题出在某个地方:

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 能够编译它,并且解析器可以正常工作。

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

4

2 回答 2

6

我在您的源文件之上做了很多更改。请查看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_)
于 2016-10-24T14:32:58.213 回答
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{};

无论我想在另一个解析器中使用表达式,还是为了解析,我现在都使用解析器中的expression对象OptimizedExpressionParser。这减少了 ram 使用量、编译时间以及生成的二进制文件的大小(在没有模板防火墙的情况下大约为 1.6 Gb)要查看我的示例如何解决这个问题,请查看此处。

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

于 2016-10-26T00:34:38.233 回答