]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/compiler_tutorial/calc3.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / compiler_tutorial / calc3.cpp
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/spirit/include/qi.hpp>
26 #include <boost/phoenix/operator.hpp>
27
28 #include <iostream>
29 #include <string>
30
31 namespace client
32 {
33 namespace qi = boost::spirit::qi;
34 namespace ascii = boost::spirit::ascii;
35
36 ///////////////////////////////////////////////////////////////////////////
37 // Our calculator grammar
38 ///////////////////////////////////////////////////////////////////////////
39 template <typename Iterator>
40 struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
41 {
42 calculator() : calculator::base_type(expression)
43 {
44 qi::_val_type _val;
45 qi::_1_type _1;
46 qi::uint_type uint_;
47
48 expression =
49 term [_val = _1]
50 >> *( ('+' >> term [_val += _1])
51 | ('-' >> term [_val -= _1])
52 )
53 ;
54
55 term =
56 factor [_val = _1]
57 >> *( ('*' >> factor [_val *= _1])
58 | ('/' >> factor [_val /= _1])
59 )
60 ;
61
62 factor =
63 uint_ [_val = _1]
64 | '(' >> expression [_val = _1] >> ')'
65 | ('-' >> factor [_val = -_1])
66 | ('+' >> factor [_val = _1])
67 ;
68 }
69
70 qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
71 };
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // Main program
76 ///////////////////////////////////////////////////////////////////////////////
77 int
78 main()
79 {
80 std::cout << "/////////////////////////////////////////////////////////\n\n";
81 std::cout << "Expression parser...\n\n";
82 std::cout << "/////////////////////////////////////////////////////////\n\n";
83 std::cout << "Type an expression...or [q or Q] to quit\n\n";
84
85 typedef std::string::const_iterator iterator_type;
86 typedef client::calculator<iterator_type> calculator;
87
88 boost::spirit::ascii::space_type space; // Our skipper
89 calculator calc; // Our grammar
90
91 std::string str;
92 int result;
93 while (std::getline(std::cin, str))
94 {
95 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
96 break;
97
98 std::string::const_iterator iter = str.begin();
99 std::string::const_iterator end = str.end();
100 bool r = phrase_parse(iter, end, calc, space, result);
101
102 if (r && iter == end)
103 {
104 std::cout << "-------------------------\n";
105 std::cout << "Parsing succeeded\n";
106 std::cout << "result = " << result << std::endl;
107 std::cout << "-------------------------\n";
108 }
109 else
110 {
111 std::string rest(iter, end);
112 std::cout << "-------------------------\n";
113 std::cout << "Parsing failed\n";
114 std::cout << "stopped at: \" " << rest << "\"\n";
115 std::cout << "-------------------------\n";
116 }
117 }
118
119 std::cout << "Bye... :-) \n\n";
120 return 0;
121 }
122
123