我已經對源文件進行了一系列更改。請檢查那些http://melpon.org/wandbox/permlink/sY2CQkXiMiLoS1BM
的變化是:
- 改變了AST。這似乎不正確。主要是向變體添加「一元」的部分。
- 更正了語法。這是基於我真正可以從你的測試中得到的語法。我可能是錯的,我只嘗試過讓第一個測試案例工作。更好地運行一個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_)
始終在您的問題中包含代碼,以便如果鏈接發生故障,問題不會中斷。 – Rakete1111
內部編譯器錯誤有點嚴重,所以我認爲這是編譯器中的一個錯誤,因爲它無法在代碼中找到錯誤,並且在代碼正確的情況下編譯代碼。 – ForceBru
@ Rakete1111我應該如何處理這個,如果產生的例子是對大的方式? – Exagon