1 /*============================================================================= 2 Copyright (c) 2001-2014 Joel de Guzman 3 4 Distributed under the Boost Software License, Version 1.0. (See accompanying 5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 =============================================================================*/ 7 #if !defined(BOOST_SPIRIT_X3_CALC7_EXPRESSION_DEF_HPP) 8 #define BOOST_SPIRIT_X3_CALC7_EXPRESSION_DEF_HPP 9 10 #include <boost/spirit/home/x3.hpp> 11 #include "ast.hpp" 12 #include "ast_adapted.hpp" 13 #include "expression.hpp" 14 #include "error_handler.hpp" 15 16 namespace client { namespace calculator_grammar 17 { 18 using x3::uint_; 19 using x3::char_; 20 21 struct expression_class; 22 struct term_class; 23 struct factor_class; 24 25 typedef x3::rule<expression_class, ast::expression> expression_type; 26 typedef x3::rule<term_class, ast::expression> term_type; 27 typedef x3::rule<factor_class, ast::operand> factor_type; 28 29 expression_type const expression = "expression"; 30 term_type const term = "term"; 31 factor_type const factor = "factor"; 32 33 auto const expression_def = 34 term 35 >> *( (char_('+') > term) 36 | (char_('-') > term) 37 ) 38 ; 39 40 auto const term_def = 41 factor 42 >> *( (char_('*') > factor) 43 | (char_('/') > factor) 44 ) 45 ; 46 47 auto const factor_def = 48 uint_ 49 | '(' > expression > ')' 50 | (char_('-') > factor) 51 | (char_('+') > factor) 52 ; 53 54 BOOST_SPIRIT_DEFINE( 55 expression 56 , term 57 , factor 58 ); 59 60 struct expression_class : error_handler {}; 61 }} 62 63 namespace client 64 { expression()65 calculator_grammar::expression_type expression() 66 { 67 return calculator_grammar::expression; 68 } 69 } 70 71 #endif 72