Boost Spirit X3带变量代数表达式解析器编译报错求助
基于Boost Spirit X3的代数表达式解析器开发求助
项目背景与遇到的问题
我正在基于Boost Spirit X3开发代数表达式解析器,计划在calc4c示例解析器的基础上扩展三类语法能力:
- 支持带变量的表达式解析
- 支持指数运算语法
- 支持以括号作为隐式乘法运算符的语法
当前开发卡在编译环节,无法定位问题根因:编译器抛出的大量报错来自boost/move.hpp、boost/move_to.hpp、boost/transform_attribute.hpp等Boost核心头文件,错误提示的参考价值很低,推测这类问题和自身对Boost相关组件的掌握程度有限有关。现将各部分实现代码整理如下,寻求解决思路。
实现代码
AST结构定义
namespace client { namespace ast { //AST struct nil {}; struct exponential; struct letNum; struct signed_; struct paren; struct program; typedef x3::variant< nil , float , char , std::list<char> , x3::forward_ast<exponential> , x3::forward_ast<letNum> , x3::forward_ast<signed_> , x3::forward_ast<paren> , x3::forward_ast<program> > operand; struct exponential { operand first; operand second; }; struct letNum { float num; std::list<char> let; }; struct signed_ { char sign_; operand operand_; }; struct paren { std::list<operand> parenList; }; struct operation { char operator_; operand operand_; }; struct program { operand first; std::list<operation> rest; }; }} BOOST_FUSION_ADAPT_STRUCT(client::ast::exponential, first, second) BOOST_FUSION_ADAPT_STRUCT(client::ast::letNum, num, let) BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, sign_, operand_) BOOST_FUSION_ADAPT_STRUCT(client::ast::paren, parenList) BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, operator_, operand_) BOOST_FUSION_ADAPT_STRUCT(client::ast::program, first, rest) }
语法规则定义
namespace client { //Simplify Grammar namespace simplify_grammar { using x3::float_; using x3::char_; using x3::alpha; x3::real_parser<float, x3::ureal_policies<float> > const ureal_float_ = {}; x3::rule<class expression, ast::program> const expression("expression"); x3::rule<class term, ast::program> const term("term"); x3::rule<class exponent, ast::exponential> const exponent("exponent"); x3::rule<class factor, ast::operand> const factor("factor"); x3::rule<class factorSigned, ast::signed_> const factorSigned("factorSigned"); x3::rule<class factorParen, ast::paren> const factorParen("factorParen"); x3::rule<class letters, std::list<char> > const letters("letters"); x3::rule<class combo, ast::letNum> const combo("combo"); auto const expression_def = term >> *( (char('+') >> term) | (char('-') >> term)) ; auto const term_def = (exponent | factor | factorSigned | factorParen) >> *( (char('*') >> (exponent | factor | factorSigned | factorParen)) | (char('/') >> (exponent | factor | factorSigned | factorParen)) ) ; auto const exponent_def = ( ( (ureal_float_ | alpha | ('(' >> expression >> ')')) >> '^' >> (factor | ('(' >> expression >> ')'))) | ( char('-') >> (ureal_float_ | alpha | ('(' >> expression >> ')')) >> '^' >> (factor | ('(' >> expression >> ')'))) ) ; auto const factor_def = combo | ureal_float_ | letters ; auto const factorSigned_def = char('-') >> factor ; auto const factorParen_def = ( (factor | factorSigned) >> +('(' >> expression >> ')')) | ( '(' >> expression >> ')' >> +('(' >> expression >> ')')) ; auto const letters_def = +alpha ; auto const combo_def = ureal_float_ >> letters ; BOOST_SPIRIT_DEFINE( expression , term , exponent , factor , factorSigned , factorParen , letters , combo ); parser_type simplify() { return expression; } } }
parser_type类型定义
namespace client { /////////////////////////////////////////////////////////////////////////////// // The calculator grammar /////////////////////////////////////////////////////////////////////////////// namespace calculator_grammar { using parser_type = x3::any_parser< std::string::const_iterator , ast::program , decltype(x3::make_context<x3::skipper_tag>(x3::ascii::space)) >; parser_type calculator(); } auto const calculator = calculator_grammar::calculator(); }
内容的提问来源于stack exchange,提问作者sheetkey
相关产品推荐
相关产品推荐

