| 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 | /////////////////////////////////////////////////////////////////////////////// |
| 8 | // |
| 9 | // A Calculator example demonstrating generation of AST. The AST, |
| 10 | // once created, is traversed, 1) To print its contents and |
| 11 | // 2) To evaluate the result. |
| 12 | // |
| 13 | // [ JDG April 28, 2008 ] For BoostCon 2008 |
| 14 | // [ JDG February 18, 2011 ] Pure attributes. No semantic actions. |
| 15 | // [ JDG January 9, 2013 ] Spirit X3 |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| 19 | #include <boost/spirit/home/x3.hpp> |
| 20 | #include <boost/spirit/home/x3/support/ast/variant.hpp> |
| 21 | #include <boost/fusion/include/adapt_struct.hpp> |
| 22 | |
| 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | #include <list> |
| 26 | #include <numeric> |
| 27 | |
| 28 | namespace x3 = boost::spirit::x3; |
| 29 | |
| 30 | namespace client { namespace ast |
| 31 | { |
| 32 | /////////////////////////////////////////////////////////////////////////// |
| 33 | // The AST |
| 34 | /////////////////////////////////////////////////////////////////////////// |
| 35 | struct nil {}; |
| 36 | struct signed_; |
| 37 | struct program; |
| 38 | |
| 39 | struct operand : x3::variant< |
| 40 | nil |
| 41 | , unsigned int |
| 42 | , x3::forward_ast<signed_> |
| 43 | , x3::forward_ast<program> |
| 44 | > |
| 45 | { |
| 46 | using base_type::base_type; |
| 47 | using base_type::operator=; |
| 48 | }; |
| 49 | |
| 50 | struct signed_ |
| 51 | { |
| 52 | char sign; |
| 53 | operand operand_; |
| 54 | }; |
| 55 | |
| 56 | struct operation |
| 57 | { |
| 58 | char operator_; |
| 59 | operand operand_; |
| 60 | }; |
| 61 | |
| 62 | struct program |
| 63 | { |
| 64 | operand first; |
| 65 | std::list<operation> rest; |
| 66 | }; |
| 67 | }} |
| 68 | |
| 69 | BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, |
| 70 | sign, operand_ |
| 71 | ) |
| 72 | |
| 73 | BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, |
| 74 | operator_, operand_ |
| 75 | ) |
| 76 | |
| 77 | BOOST_FUSION_ADAPT_STRUCT(client::ast::program, |
| 78 | first, rest |
| 79 | ) |
| 80 | |
| 81 | namespace client { namespace ast |
| 82 | { |
| 83 | /////////////////////////////////////////////////////////////////////////// |
| 84 | // The AST Printer |
| 85 | /////////////////////////////////////////////////////////////////////////// |
| 86 | struct printer |
| 87 | { |
| 88 | typedef void result_type; |
| 89 | |
| 90 | void operator()(nil) const {} |
| 91 | void operator()(unsigned int n) const { std::cout << n; } |
| 92 | |
| 93 | void operator()(operation const& x) const |
| 94 | { |
| 95 | boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 96 | switch (x.operator_) |
| 97 | { |
| 98 | case '+': std::cout << " add" ; break; |
| 99 | case '-': std::cout << " subt" ; break; |
| 100 | case '*': std::cout << " mult" ; break; |
| 101 | case '/': std::cout << " div" ; break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void operator()(signed_ const& x) const |
| 106 | { |
| 107 | boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 108 | switch (x.sign) |
| 109 | { |
| 110 | case '-': std::cout << " neg" ; break; |
| 111 | case '+': std::cout << " pos" ; break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void operator()(program const& x) const |
| 116 | { |
| 117 | boost::apply_visitor(visitor: *this, visitable: x.first); |
| 118 | for (operation const& oper: x.rest) |
| 119 | { |
| 120 | std::cout << ' '; |
| 121 | (*this)(oper); |
| 122 | } |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////// |
| 127 | // The AST evaluator |
| 128 | /////////////////////////////////////////////////////////////////////////// |
| 129 | struct eval |
| 130 | { |
| 131 | typedef int result_type; |
| 132 | |
| 133 | int operator()(nil) const { BOOST_ASSERT(0); return 0; } |
| 134 | int operator()(unsigned int n) const { return n; } |
| 135 | |
| 136 | int operator()(int lhs, operation const& x) const |
| 137 | { |
| 138 | int rhs = boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 139 | switch (x.operator_) |
| 140 | { |
| 141 | case '+': return lhs + rhs; |
| 142 | case '-': return lhs - rhs; |
| 143 | case '*': return lhs * rhs; |
| 144 | case '/': return lhs / rhs; |
| 145 | } |
| 146 | BOOST_ASSERT(0); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int operator()(signed_ const& x) const |
| 151 | { |
| 152 | int rhs = boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 153 | switch (x.sign) |
| 154 | { |
| 155 | case '-': return -rhs; |
| 156 | case '+': return +rhs; |
| 157 | } |
| 158 | BOOST_ASSERT(0); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int operator()(program const& x) const |
| 163 | { |
| 164 | return std::accumulate( |
| 165 | first: x.rest.begin(), last: x.rest.end() |
| 166 | , init: boost::apply_visitor(visitor: *this, visitable: x.first) |
| 167 | , binary_op: *this); |
| 168 | } |
| 169 | }; |
| 170 | }} |
| 171 | |
| 172 | namespace client |
| 173 | { |
| 174 | /////////////////////////////////////////////////////////////////////////////// |
| 175 | // The calculator grammar |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | namespace calculator_grammar |
| 178 | { |
| 179 | using x3::uint_; |
| 180 | using x3::char_; |
| 181 | |
| 182 | x3::rule<class expression, ast::program> const expression("expression" ); |
| 183 | x3::rule<class term, ast::program> const term("term" ); |
| 184 | x3::rule<class factor, ast::operand> const factor("factor" ); |
| 185 | |
| 186 | auto const expression_def = |
| 187 | term |
| 188 | >> *( (char_('+') >> term) |
| 189 | | (char_('-') >> term) |
| 190 | ) |
| 191 | ; |
| 192 | |
| 193 | auto const term_def = |
| 194 | factor |
| 195 | >> *( (char_('*') >> factor) |
| 196 | | (char_('/') >> factor) |
| 197 | ) |
| 198 | ; |
| 199 | |
| 200 | auto const factor_def = |
| 201 | uint_ |
| 202 | | '(' >> expression >> ')' |
| 203 | | (char_('-') >> factor) |
| 204 | | (char_('+') >> factor) |
| 205 | ; |
| 206 | |
| 207 | BOOST_SPIRIT_DEFINE( |
| 208 | expression |
| 209 | , term |
| 210 | , factor |
| 211 | ); |
| 212 | |
| 213 | auto calculator = expression; |
| 214 | } |
| 215 | |
| 216 | using calculator_grammar::calculator; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | /////////////////////////////////////////////////////////////////////////////// |
| 221 | // Main program |
| 222 | /////////////////////////////////////////////////////////////////////////////// |
| 223 | int |
| 224 | main() |
| 225 | { |
| 226 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 227 | std::cout << "Expression parser...\n\n" ; |
| 228 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 229 | std::cout << "Type an expression...or [q or Q] to quit\n\n" ; |
| 230 | |
| 231 | typedef std::string::const_iterator iterator_type; |
| 232 | typedef client::ast::program ast_program; |
| 233 | typedef client::ast::printer ast_print; |
| 234 | typedef client::ast::eval ast_eval; |
| 235 | |
| 236 | std::string str; |
| 237 | while (std::getline(is&: std::cin, str&: str)) |
| 238 | { |
| 239 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 240 | break; |
| 241 | |
| 242 | auto& calc = client::calculator; // Our grammar |
| 243 | ast_program program; // Our program (AST) |
| 244 | ast_print print; // Prints the program |
| 245 | ast_eval eval; // Evaluates the program |
| 246 | |
| 247 | iterator_type iter = str.begin(); |
| 248 | iterator_type end = str.end(); |
| 249 | boost::spirit::x3::ascii::space_type space; |
| 250 | bool r = phrase_parse(first&: iter, last: end, p: calc, s: space, attr&: program); |
| 251 | |
| 252 | if (r && iter == end) |
| 253 | { |
| 254 | std::cout << "-------------------------\n" ; |
| 255 | std::cout << "Parsing succeeded\n" ; |
| 256 | print(program); |
| 257 | std::cout << "\nResult: " << eval(program) << std::endl; |
| 258 | std::cout << "-------------------------\n" ; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | std::string rest(iter, end); |
| 263 | std::cout << "-------------------------\n" ; |
| 264 | std::cout << "Parsing failed\n" ; |
| 265 | std::cout << "stopped at: \"" << rest << "\"\n" ; |
| 266 | std::cout << "-------------------------\n" ; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | std::cout << "Bye... :-) \n\n" ; |
| 271 | return 0; |
| 272 | } |
| 273 | |