1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
9 // Plain calculator example demonstrating the grammar. The parser is a
10 // syntax checker only and does not do any semantic evaluation.
12 // [ JDG May 10, 2002 ] spirit 1
13 // [ JDG March 4, 2007 ] spirit 2
14 // [ JDG February 21, 2011 ] spirit 2.5
15 // [ JDG June 6, 2014 ] spirit x3
17 ///////////////////////////////////////////////////////////////////////////////
19 #include <boost/config/warning_disable.hpp>
20 #include <boost/spirit/home/x3.hpp>
21 #include <boost/spirit/home/x3/support/ast/variant.hpp>
22 #include <boost/fusion/include/adapt_struct.hpp>
29 namespace x3 = boost::spirit::x3;
33 ///////////////////////////////////////////////////////////////////////////////
34 // The calculator grammar
35 ///////////////////////////////////////////////////////////////////////////////
36 namespace calculator_grammar
41 x3::rule<class expression> const expression("expression");
42 x3::rule<class term> const term("term");
43 x3::rule<class factor> const factor("factor");
45 auto const expression_def =
59 auto const factor_def =
61 | '(' >> expression >> ')'
67 expression = expression_def
72 auto calculator = expression;
75 using calculator_grammar::calculator;
79 ///////////////////////////////////////////////////////////////////////////////
81 ///////////////////////////////////////////////////////////////////////////////
85 std::cout << "/////////////////////////////////////////////////////////\n\n";
86 std::cout << "Expression parser...\n\n";
87 std::cout << "/////////////////////////////////////////////////////////\n\n";
88 std::cout << "Type an expression...or [q or Q] to quit\n\n";
90 typedef std::string::const_iterator iterator_type;
93 while (std::getline(std::cin, str))
95 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
98 auto& calc = client::calculator; // Our grammar
100 iterator_type iter = str.begin();
101 iterator_type end = str.end();
102 boost::spirit::x3::ascii::space_type space;
103 bool r = phrase_parse(iter, end, calc, space);
105 if (r && iter == end)
107 std::cout << "-------------------------\n";
108 std::cout << "Parsing succeeded\n";
109 std::cout << "-------------------------\n";
113 std::string rest(iter, end);
114 std::cout << "-------------------------\n";
115 std::cout << "Parsing failed\n";
116 std::cout << "stopped at: \"" << rest << "\"\n";
117 std::cout << "-------------------------\n";
121 std::cout << "Bye... :-) \n\n";