1 /*=============================================================================
2 Copyright (c) 2001-2003 Daniel Nuffer
3 http://spirit.sourceforge.net/
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
10 //
11 // Demonstrates the ASTs. This is discussed in the
12 // "Trees" chapter in the Spirit User's Guide.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15 #define BOOST_SPIRIT_DUMP_PARSETREE_AS_XML
16
17 #include <boost/spirit/include/classic_core.hpp>
18 #include <boost/spirit/include/classic_ast.hpp>
19 #include <boost/spirit/include/classic_tree_to_xml.hpp>
20 #include <boost/assert.hpp>
21 #include "tree_calc_grammar.hpp"
22
23 #include <iostream>
24 #include <stack>
25 #include <functional>
26 #include <string>
27
28 #if defined(BOOST_SPIRIT_DUMP_PARSETREE_AS_XML)
29 #include <map>
30 #endif
31
32 // This example shows how to use an AST.
33 ////////////////////////////////////////////////////////////////////////////
34 using namespace std;
35 using namespace BOOST_SPIRIT_CLASSIC_NS;
36
37 typedef char const* iterator_t;
38 typedef tree_match<iterator_t> parse_tree_match_t;
39 typedef parse_tree_match_t::tree_iterator iter_t;
40
41 ////////////////////////////////////////////////////////////////////////////
42 long evaluate(parse_tree_match_t hit);
43 long eval_expression(iter_t const& i);
44
evaluate(tree_parse_info<> info)45 long evaluate(tree_parse_info<> info)
46 {
47 return eval_expression(info.trees.begin());
48 }
49
eval_expression(iter_t const & i)50 long eval_expression(iter_t const& i)
51 {
52 cout << "In eval_expression. i->value = " <<
53 string(i->value.begin(), i->value.end()) <<
54 " i->children.size() = " << i->children.size() << endl;
55
56 if (i->value.id() == calculator::integerID)
57 {
58 BOOST_ASSERT(i->children.size() == 0);
59
60 // extract integer (not always delimited by '\0')
61 string integer(i->value.begin(), i->value.end());
62
63 return strtol(integer.c_str(), 0, 10);
64 }
65 else if (i->value.id() == calculator::factorID)
66 {
67 // factor can only be unary minus
68 BOOST_ASSERT(*i->value.begin() == '-');
69 return - eval_expression(i->children.begin());
70 }
71 else if (i->value.id() == calculator::termID)
72 {
73 if (*i->value.begin() == '*')
74 {
75 BOOST_ASSERT(i->children.size() == 2);
76 return eval_expression(i->children.begin()) *
77 eval_expression(i->children.begin()+1);
78 }
79 else if (*i->value.begin() == '/')
80 {
81 BOOST_ASSERT(i->children.size() == 2);
82 return eval_expression(i->children.begin()) /
83 eval_expression(i->children.begin()+1);
84 }
85 else
86 BOOST_ASSERT(0);
87 }
88 else if (i->value.id() == calculator::expressionID)
89 {
90 if (*i->value.begin() == '+')
91 {
92 BOOST_ASSERT(i->children.size() == 2);
93 return eval_expression(i->children.begin()) +
94 eval_expression(i->children.begin()+1);
95 }
96 else if (*i->value.begin() == '-')
97 {
98 BOOST_ASSERT(i->children.size() == 2);
99 return eval_expression(i->children.begin()) -
100 eval_expression(i->children.begin()+1);
101 }
102 else
103 BOOST_ASSERT(0);
104 }
105 else
106 {
107 BOOST_ASSERT(0); // error
108 }
109
110 return 0;
111 }
112
113 ////////////////////////////////////////////////////////////////////////////
114 int
main()115 main()
116 {
117 // look in tree_calc_grammar for the definition of calculator
118 calculator calc;
119
120 cout << "/////////////////////////////////////////////////////////\n\n";
121 cout << "\t\tThe simplest working calculator...\n\n";
122 cout << "/////////////////////////////////////////////////////////\n\n";
123 cout << "Type an expression...or [q or Q] to quit\n\n";
124
125 string str;
126 while (getline(cin, str))
127 {
128 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
129 break;
130
131 tree_parse_info<> info = ast_parse(str.c_str(), calc);
132
133 if (info.full)
134 {
135 #if defined(BOOST_SPIRIT_DUMP_PARSETREE_AS_XML)
136 // dump parse tree as XML
137 std::map<parser_id, std::string> rule_names;
138 rule_names[calculator::integerID] = "integer";
139 rule_names[calculator::factorID] = "factor";
140 rule_names[calculator::termID] = "term";
141 rule_names[calculator::expressionID] = "expression";
142 tree_to_xml(cout, info.trees, str.c_str(), rule_names);
143 #endif
144
145 // print the result
146 cout << "parsing succeeded\n";
147 cout << "result = " << evaluate(info) << "\n\n";
148 }
149 else
150 {
151 cout << "parsing failed\n";
152 }
153 }
154
155 cout << "Bye... :-) \n\n";
156 return 0;
157 }
158
159
160