| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Hartmut Kaiser |
| 3 | Copyright (c) 2001-2011 Joel de Guzman |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | =============================================================================*/ |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | // |
| 10 | // Plain calculator example demonstrating the grammar. The parser is a |
| 11 | // syntax checker only and does not do any semantic evaluation. |
| 12 | // |
| 13 | // [ JDG May 10, 2002 ] spirit1 |
| 14 | // [ JDG March 4, 2007 ] spirit2 |
| 15 | // [ HK November 30, 2010 ] spirit2/utree |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| 19 | // This rather naive example demonstrates that you can pass an instance of a |
| 20 | // utree as the attribute for almost any grammar. As the result the utree will |
| 21 | // be filled with the parse tree as generated during the parsing. This is most |
| 22 | // of the time not what's desired, but is usually a good first step in order to |
| 23 | // prepare your grammar to generate a customized AST. See the calc_utree_ast |
| 24 | // example for a modified version of this grammar filling the attribute with a |
| 25 | // AST (abstract syntax tree) representing the math expression as matched from |
| 26 | // the input. |
| 27 | |
| 28 | // #define BOOST_SPIRIT_DEBUG |
| 29 | |
| 30 | #include <boost/spirit/include/qi.hpp> |
| 31 | #include <boost/spirit/include/support_utree.hpp> |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <string> |
| 35 | |
| 36 | namespace client |
| 37 | { |
| 38 | namespace qi = boost::spirit::qi; |
| 39 | namespace ascii = boost::spirit::ascii; |
| 40 | namespace spirit = boost::spirit; |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | // Our calculator grammar |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | template <typename Iterator> |
| 46 | struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()> |
| 47 | { |
| 48 | calculator() : calculator::base_type(expression) |
| 49 | { |
| 50 | using qi::uint_; |
| 51 | using qi::char_; |
| 52 | |
| 53 | expression = |
| 54 | term |
| 55 | >> *( (char_('+') >> term) |
| 56 | | (char_('-') >> term) |
| 57 | ) |
| 58 | ; |
| 59 | |
| 60 | term = |
| 61 | factor |
| 62 | >> *( (char_('*') >> factor) |
| 63 | | (char_('/') >> factor) |
| 64 | ) |
| 65 | ; |
| 66 | |
| 67 | factor = |
| 68 | uint_ |
| 69 | | '(' >> expression >> ')' |
| 70 | | (char_('-') >> factor) |
| 71 | | (char_('+') >> factor) |
| 72 | ; |
| 73 | |
| 74 | BOOST_SPIRIT_DEBUG_NODE(expression); |
| 75 | BOOST_SPIRIT_DEBUG_NODE(term); |
| 76 | BOOST_SPIRIT_DEBUG_NODE(factor); |
| 77 | } |
| 78 | |
| 79 | qi::rule<Iterator, ascii::space_type, spirit::utree()> expression; |
| 80 | qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> term; |
| 81 | qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> factor; |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | // Main program |
| 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | int main() |
| 89 | { |
| 90 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 91 | std::cout << "Expression parser...\n\n" ; |
| 92 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 93 | std::cout << "Type an expression...or [q or Q] to quit\n\n" ; |
| 94 | |
| 95 | using boost::spirit::ascii::space; |
| 96 | using boost::spirit::utree; |
| 97 | typedef std::string::const_iterator iterator_type; |
| 98 | typedef client::calculator<iterator_type> calculator; |
| 99 | |
| 100 | calculator calc; // Our grammar |
| 101 | |
| 102 | std::string str; |
| 103 | while (std::getline(is&: std::cin, str&: str)) |
| 104 | { |
| 105 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 106 | break; |
| 107 | |
| 108 | std::string::const_iterator iter = str.begin(); |
| 109 | std::string::const_iterator end = str.end(); |
| 110 | utree ut; |
| 111 | bool r = phrase_parse(first&: iter, last: end, expr: calc, skipper: space, attr&: ut); |
| 112 | |
| 113 | if (r && iter == end) |
| 114 | { |
| 115 | std::cout << "-------------------------\n" ; |
| 116 | std::cout << "Parsing succeeded: " << ut << "\n" ; |
| 117 | std::cout << "-------------------------\n" ; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | std::string rest(iter, end); |
| 122 | std::cout << "-------------------------\n" ; |
| 123 | std::cout << "Parsing failed\n" ; |
| 124 | std::cout << "stopped at: \": " << rest << "\"\n" ; |
| 125 | std::cout << "-------------------------\n" ; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | std::cout << "Bye... :-) \n\n" ; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |