1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2013-2014 Agustin Berge
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 ///////////////////////////////////////////////////////////////////////////////
10 // A Calculator example demonstrating generation of AST. The AST,
11 // once created, is traversed, 1) To print its contents and
12 // 2) To evaluate the result.
14 // [ JDG April 28, 2008 ] For BoostCon 2008
15 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
16 // [ JDG January 9, 2013 ] Spirit X3
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "grammar.hpp"
25 ///////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////
31 std::cout << "/////////////////////////////////////////////////////////\n\n";
32 std::cout << "Expression parser...\n\n";
33 std::cout << "/////////////////////////////////////////////////////////\n\n";
34 std::cout << "Type an expression...or [q or Q] to quit\n\n";
36 typedef std::string::const_iterator iterator_type;
37 typedef client::ast::program ast_program;
38 typedef client::ast::printer ast_print;
39 typedef client::ast::eval ast_eval;
42 while (std::getline(std::cin, str))
44 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
47 auto& calc = client::calculator; // Our grammar
48 ast_program program; // Our program (AST)
49 ast_print print; // Prints the program
50 ast_eval eval; // Evaluates the program
52 iterator_type iter = str.begin();
53 iterator_type end = str.end();
54 boost::spirit::x3::ascii::space_type space;
55 bool r = phrase_parse(iter, end, calc, space, program);
59 std::cout << "-------------------------\n";
60 std::cout << "Parsing succeeded\n";
62 std::cout << "\nResult: " << eval(program) << std::endl;
63 std::cout << "-------------------------\n";
67 std::string rest(iter, end);
68 std::cout << "-------------------------\n";
69 std::cout << "Parsing failed\n";
70 std::cout << "stopped at: \"" << rest << "\"\n";
71 std::cout << "-------------------------\n";
75 std::cout << "Bye... :-) \n\n";