]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/calc/calc4c/calc4c.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / calc / calc4c / calc4c.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2013-2014 Agustin Berge
4
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 ///////////////////////////////////////////////////////////////////////////////
9 //
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.
13 //
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
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "grammar.hpp"
21
22 #include <iostream>
23 #include <string>
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // Main program
27 ///////////////////////////////////////////////////////////////////////////////
28 int
29 main()
30 {
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";
35
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;
40
41 std::string str;
42 while (std::getline(std::cin, str))
43 {
44 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
45 break;
46
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
51
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);
56
57 if (r && iter == end)
58 {
59 std::cout << "-------------------------\n";
60 std::cout << "Parsing succeeded\n";
61 print(program);
62 std::cout << "\nResult: " << eval(program) << std::endl;
63 std::cout << "-------------------------\n";
64 }
65 else
66 {
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";
72 }
73 }
74
75 std::cout << "Bye... :-) \n\n";
76 return 0;
77 }