• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2011 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 the grammar and semantic actions
10 //  using phoenix to do the actual expression evaluation. The parser is
11 //  essentially an "interpreter" that evaluates expressions on the fly.
12 //
13 //  [ JDG June 29, 2002 ]   spirit1
14 //  [ JDG March 5, 2007 ]   spirit2
15 //
16 ///////////////////////////////////////////////////////////////////////////////
17 
18 // Spirit v2.5 allows you to suppress automatic generation
19 // of predefined terminals to speed up complation. With
20 // BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
21 // responsible in creating instances of the terminals that
22 // you need (e.g. see qi::uint_type uint_ below).
23 #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
24 
25 #include <boost/config/warning_disable.hpp>
26 #include <boost/spirit/include/qi.hpp>
27 #include <boost/spirit/include/phoenix_operator.hpp>
28 
29 #include <iostream>
30 #include <string>
31 
32 namespace client
33 {
34     namespace qi = boost::spirit::qi;
35     namespace ascii = boost::spirit::ascii;
36 
37     ///////////////////////////////////////////////////////////////////////////
38     //  Our calculator grammar
39     ///////////////////////////////////////////////////////////////////////////
40     template <typename Iterator>
41     struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
42     {
calculatorclient::calculator43         calculator() : calculator::base_type(expression)
44         {
45             qi::_val_type _val;
46             qi::_1_type _1;
47             qi::uint_type uint_;
48 
49             expression =
50                 term                            [_val = _1]
51                 >> *(   ('+' >> term            [_val += _1])
52                     |   ('-' >> term            [_val -= _1])
53                     )
54                 ;
55 
56             term =
57                 factor                          [_val = _1]
58                 >> *(   ('*' >> factor          [_val *= _1])
59                     |   ('/' >> factor          [_val /= _1])
60                     )
61                 ;
62 
63             factor =
64                 uint_                           [_val = _1]
65                 |   '(' >> expression           [_val = _1] >> ')'
66                 |   ('-' >> factor              [_val = -_1])
67                 |   ('+' >> factor              [_val = _1])
68                 ;
69         }
70 
71         qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
72     };
73 }
74 
75 ///////////////////////////////////////////////////////////////////////////////
76 //  Main program
77 ///////////////////////////////////////////////////////////////////////////////
78 int
main()79 main()
80 {
81     std::cout << "/////////////////////////////////////////////////////////\n\n";
82     std::cout << "Expression parser...\n\n";
83     std::cout << "/////////////////////////////////////////////////////////\n\n";
84     std::cout << "Type an expression...or [q or Q] to quit\n\n";
85 
86     typedef std::string::const_iterator iterator_type;
87     typedef client::calculator<iterator_type> calculator;
88 
89     boost::spirit::ascii::space_type space; // Our skipper
90     calculator calc; // Our grammar
91 
92     std::string str;
93     int result;
94     while (std::getline(std::cin, str))
95     {
96         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
97             break;
98 
99         std::string::const_iterator iter = str.begin();
100         std::string::const_iterator end = str.end();
101         bool r = phrase_parse(iter, end, calc, space, result);
102 
103         if (r && iter == end)
104         {
105             std::cout << "-------------------------\n";
106             std::cout << "Parsing succeeded\n";
107             std::cout << "result = " << result << std::endl;
108             std::cout << "-------------------------\n";
109         }
110         else
111         {
112             std::string rest(iter, end);
113             std::cout << "-------------------------\n";
114             std::cout << "Parsing failed\n";
115             std::cout << "stopped at: \" " << rest << "\"\n";
116             std::cout << "-------------------------\n";
117         }
118     }
119 
120     std::cout << "Bye... :-) \n\n";
121     return 0;
122 }
123 
124 
125