]>
git.proxmox.com Git - ceph.git/blob - 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/
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 //------------------------------------------------------------------------------
18 #include <boost/typeof/typeof.hpp>
20 #include <boost/spirit/include/classic_core.hpp>
21 #include <boost/spirit/include/classic_typeof.hpp>
23 #include <boost/spirit/include/classic_rule_parser.hpp>
26 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
28 using namespace BOOST_SPIRIT_CLASSIC_NS
;
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
; }
41 // Operating at root namespace...
42 #define BOOST_SPIRIT__NAMESPACE -
45 // Our calculator grammar using subrules in a rule parser.
46 BOOST_SPIRIT_RULE_PARSER( calc
,
49 (3,( ((subrule
<0>),expression
,()),
50 ((subrule
<1>),term
,()),
51 ((subrule
<2>),factor
,() )) ),
55 >> *( ('+' >> term
)[&do_add
]
56 | ('-' >> term
)[&do_sub
]
62 >> *( ('*' >> factor
)[&do_mul
]
63 | ('/' >> factor
)[&do_div
]
69 | ('(' >> expression
>> ')')
70 | ('-' >> factor
)[&do_neg
]
75 #undef BOOST_SPIRIT__NAMESPACE
82 << "/////////////////////////////////////////////////////////\n"
83 << "\t\tA ruleless calculator using subrules...\n"
84 << "/////////////////////////////////////////////////////////\n"
85 << "Type an expression...or an empty line to quit\n"
89 while (std::getline(std::cin
, str
))
91 if (str
.empty()) break;
93 parse_info
<> info
= parse(str
.c_str(), calc
, space_p
);
102 << "Stopped at: \": " << info
.stop
<< "\".\n"