]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/example/techniques/no_rules_with_typeof/rule_parser_2_1.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / classic / example / techniques / no_rules_with_typeof / rule_parser_2_1.cpp
1 /*==============================================================================
2 Copyright (c) 2002-2003 Joel de Guzman
3 Copyright (c) 2006 Tobias Schwinger
4 http://spirit.sourceforge.net/
5
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 ==============================================================================*/
10 //------------------------------------------------------------------------------
11 // This example shows a recursive grammar built using subrules and typeof.
12 // See boost/spirit/include/rule_parser.hpp for details.
13 // This example is based on subrule_calc.cpp.
14 //------------------------------------------------------------------------------
15 #include <string>
16 #include <iostream>
17
18 #include <boost/typeof/typeof.hpp>
19
20 #include <boost/spirit/include/classic_core.hpp>
21 #include <boost/spirit/include/classic_typeof.hpp>
22
23 #include <boost/spirit/include/classic_rule_parser.hpp>
24
25 // Don't forget to
26 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
27
28 using namespace BOOST_SPIRIT_CLASSIC_NS;
29
30 // Semantic actions
31 namespace
32 {
33 void do_int(int v) { std::cout << "PUSH(" << v << ')' << std::endl; }
34 void do_add(char const*, char const*) { std::cout << "ADD" << std::endl; }
35 void do_sub(char const*, char const*) { std::cout << "SUB" << std::endl; }
36 void do_mul(char const*, char const*) { std::cout << "MUL" << std::endl; }
37 void do_div(char const*, char const*) { std::cout << "DIV" << std::endl; }
38 void do_neg(char const*, char const*) { std::cout << "NEG" << std::endl; }
39 }
40
41 // Operating at root namespace...
42 #define BOOST_SPIRIT__NAMESPACE -
43
44
45 // Our calculator grammar using subrules in a rule parser.
46 BOOST_SPIRIT_RULE_PARSER( calc,
47 -,
48 -,
49 (3,( ((subrule<0>),expression,()),
50 ((subrule<1>),term,()),
51 ((subrule<2>),factor,() )) ),
52 (
53 expression =
54 term
55 >> *( ('+' >> term)[&do_add]
56 | ('-' >> term)[&do_sub]
57 )
58 ,
59
60 term =
61 factor
62 >> *( ('*' >> factor)[&do_mul]
63 | ('/' >> factor)[&do_div]
64 )
65 ,
66
67 factor =
68 int_p[&do_int]
69 | ('(' >> expression >> ')')
70 | ('-' >> factor)[&do_neg]
71 | ('+' >> factor)
72 )
73 )
74
75 #undef BOOST_SPIRIT__NAMESPACE
76
77
78 // Main program
79 int main()
80 {
81 std::cout
82 << "/////////////////////////////////////////////////////////\n"
83 << "\t\tA ruleless calculator using subrules...\n"
84 << "/////////////////////////////////////////////////////////\n"
85 << "Type an expression...or an empty line to quit\n"
86 << std::endl;
87
88 std::string str;
89 while (std::getline(std::cin, str))
90 {
91 if (str.empty()) break;
92
93 parse_info<> info = parse(str.c_str(), calc, space_p);
94
95 if (info.full)
96 std::cout
97 << "OK."
98 << std::endl;
99 else
100 std::cout
101 << "ERROR.\n"
102 << "Stopped at: \": " << info.stop << "\".\n"
103 << std::endl;
104 }
105 return 0;
106 }
107