簡體   English   中英

從boost :: spirit解析器中檢索AST

[英]Retrieving AST from boost::spirit parser

在我閱讀了關於boost :: spirit教程之后,由於解析器組合器語法,我非常喜歡它。 制作解析器非常簡單。

遺憾的是,在從解析器中獲取復雜數據結構的問題上,教程並不那么精確。 我正試圖進入Kaleidoscope AST

無論如何,這是我的AST代碼:

#ifndef __AST_HPP__
#define __AST_HPP__

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <string>
#include <vector>

namespace ast {

struct add;
struct sub;
struct mul;
struct div;
struct func_call;
template<typename OpTag> struct binary_op;

typedef boost::variant<double, std::string, boost::recursive_wrapper<binary_op<
        add>>, boost::recursive_wrapper<binary_op<sub>>,
        boost::recursive_wrapper<binary_op<mul>>, boost::recursive_wrapper<
                binary_op<div>>, boost::recursive_wrapper<func_call>>
        expression;

template<typename OpTag>
struct binary_op {
    expression left;
    expression right;

    binary_op(const expression & lhs, const expression & rhs) :
        left(lhs), right(rhs) {
    }
};

struct func_call {
    std::string callee;
    std::vector<expression> args;

    func_call(const std::string func, const std::vector<expression> &args) :
        callee(func), args(args) {
    }
};

struct prototype {
    std::string name;
    std::vector<std::string> args;

    prototype(const std::string &name, const std::vector<std::string> &args) :
        name(name), args(args) {
    }
};

struct function {
    prototype proto;
    expression body;

    function(const prototype &proto, const expression &body) :
        body(body), proto(proto) {
    }
};

}
    #endif

我省略了BOOST_FUSION_ADAPT_STRUCT部分,但它們在那里。

這是我的表達式解析器:

#ifndef __PARSER_HPP__
#define __PARSER_HPP__

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include "ast.hpp"

namespace parser {

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

template<typename Iterator>
struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> {
    expression() :
        expression::base_type(expr) {
        using qi::lit;
        using qi::lexeme;
        using ascii::char_;
        using ascii::string;
        using ascii::alnum;
        using ascii::alpha;
        using qi::double_;
        using namespace qi::labels;

        using phoenix::at_c;
        using phoenix::push_back;

        number %= lexeme[double_];
        varname %= lexeme[alpha >> *(alnum | '_')];

        binop
                = (expr >> '+' >> expr)[_val = ast::binary_op<ast::add>(_1, _3)]
                        | (expr >> '-' >> expr)[_val
                                = ast::binary_op<ast::sub>(_1, _3)]
                        | (expr >> '*' >> expr)[_val
                                = ast::binary_op<ast::mul>(_1, _3)]
                        | (expr >> '/' >> expr)[_val
                                = ast::binary_op<ast::div>(_1, _3)];

        expr %= number | varname | binop;
    }

    qi::rule<Iterator, ast::expression(), ascii::space_type> expr;
    qi::rule<Iterator, ast::expression(), ascii::space_type> binop;
    qi::rule<Iterator, std::string, ascii::space_type> varname;
    qi::rule<Iterator, double, ascii::space_type> number;
};

}

#endif

我遇到的問題是它似乎與生成的ast::expression有問題。 編譯時拋出超過200行復雜模板錯誤。 我懷疑這是我嘗試從binop規則中獲取信息的方式,但我不確定。

有人可以幫忙嗎?

您正在嘗試使用Boost Phoenix占位符調用ast::binary_op的構造函數。 它們混合不好。 你需要使用對ast::binary_op構造函數的惰性調用 這是在鳳凰城使用construct

binop = (expr >> '+' >> expr) [_val = construct< ast::binary_op<ast::add> >(_1, _2)]
      | (expr >> '-' >> expr) [_val = construct< ast::binary_op<ast::sub> >(_1, _2)]
      | (expr >> '*' >> expr) [_val = construct< ast::binary_op<ast::mul> >(_1, _2)]
      | (expr >> '/' >> expr) [_val = construct< ast::binary_op<ast::div> >(_1, _2)] ;

另外,我認為你只需要_1_2占位符,因為'+''-' ,...被轉換為qi::lit (litteral)因此沒有屬性。

我還注意到varnamenumber規則中有兩個缺少的括號:

qi::rule<Iterator, std::string(), ascii::space_type> varname;
//                            ^^          
qi::rule<Iterator, double(), ascii::space_type> number;
//                       ^^

Boost Spirit Qi非常強大,但也很難調試。 當我開始使用它時,我發現這些Boost Spirit應用程序非常有用。

我希望這會有所幫助,因為我不是一個提升精神專家。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM