diff options
Diffstat (limited to 'src/boost/libs/spirit/example/x3/calc/calc1.cpp')
-rw-r--r-- | src/boost/libs/spirit/example/x3/calc/calc1.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/example/x3/calc/calc1.cpp b/src/boost/libs/spirit/example/x3/calc/calc1.cpp new file mode 100644 index 00000000..0a8e0c96 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc1.cpp @@ -0,0 +1,123 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Plain calculator example demonstrating the grammar. The parser is a +// syntax checker only and does not do any semantic evaluation. +// +// [ JDG May 10, 2002 ] spirit 1 +// [ JDG March 4, 2007 ] spirit 2 +// [ JDG February 21, 2011 ] spirit 2.5 +// [ JDG June 6, 2014 ] spirit x3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression> const expression("expression"); + x3::rule<class term> const term("term"); + x3::rule<class factor> const factor("factor"); + + auto const expression_def = + term + >> *( ('+' >> term) + | ('-' >> term) + ) + ; + + auto const term_def = + factor + >> *( ('*' >> factor) + | ('/' >> factor) + ) + ; + + auto const factor_def = + uint_ + | '(' >> expression >> ')' + | ('-' >> factor) + | ('+' >> factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + auto calculator = expression; + } + + using calculator_grammar::calculator; + +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} |