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 // Same as calc6, but this version also shows off grammar modularization.
10 // Here you will see how expressions is built as a modular grammars.
12 // [ JDG Sometime 2000 ] pre-boost
13 // [ JDG September 18, 2002 ] spirit1
14 // [ JDG April 8, 2007 ] spirit2
15 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
16 // [ JDG April 9, 2014 ] Spirit X3 (from qi calc6)
17 // [ JDG May 2, 2014 ] Modular grammar using BOOST_SPIRIT_DEFINE.
19 ///////////////////////////////////////////////////////////////////////////////
23 #include "compiler.hpp"
24 #include "expression.hpp"
25 #include "error_handler.hpp"
27 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
33 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
34 std::cout
<< "Expression parser...\n\n";
35 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
36 std::cout
<< "Type an expression...or [q or Q] to quit\n\n";
38 typedef std::string::const_iterator iterator_type
;
39 typedef client::ast::expression ast_expression
;
40 typedef client::compiler compiler
;
43 while (std::getline(std::cin
, str
))
45 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
48 using boost::spirit::x3::ascii::space
;
50 client::vmachine mach
; // Our virtual machine
51 std::vector
<int> code
; // Our VM code
52 auto calc
= client::expression(); // grammar
54 ast_expression ast
; // Our program (AST)
55 compiler
compile(code
); // Compiles the program
57 iterator_type iter
= str
.begin();
58 iterator_type
const end
= str
.end();
59 bool r
= phrase_parse(iter
, end
, calc
, space
, ast
);
63 std::cout
<< "-------------------------\n";
64 std::cout
<< "Parsing succeeded\n";
67 std::cout
<< "\nResult: " << mach
.top() << std::endl
;
68 std::cout
<< "-------------------------\n";
72 std::string
rest(iter
, end
);
73 std::cout
<< "-------------------------\n";
74 std::cout
<< "Parsing failed\n";
75 std::cout
<< "-------------------------\n";
79 std::cout
<< "Bye... :-) \n\n";