1
/*=============================================================================
2
Copyright (c) 2001-2003 Daniel Nuffer
3
http://spirit.sourceforge.net/
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
///////////////////////////////////////////////////////////////////////////////
11
// Demonstrates the ASTs. This is discussed in the
12
// "Trees" chapter in the Spirit User's Guide.
14
///////////////////////////////////////////////////////////////////////////////
15
#define BOOST_SPIRIT_DUMP_PARSETREE_AS_XML
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"
28
#if defined(BOOST_SPIRIT_DUMP_PARSETREE_AS_XML)
32
// This example shows how to use an AST.
33
////////////////////////////////////////////////////////////////////////////
35
using namespace BOOST_SPIRIT_CLASSIC_NS;
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;
41
////////////////////////////////////////////////////////////////////////////
42
long evaluate(parse_tree_match_t hit);
43
long eval_expression(iter_t const& i);
45
long evaluate(tree_parse_info<> info)
47
return eval_expression(info.trees.begin());
50
long eval_expression(iter_t const& i)
52
cout << "In eval_expression. i->value = " <<
53
string(i->value.begin(), i->value.end()) <<
54
" i->children.size() = " << i->children.size() << endl;
56
if (i->value.id() == calculator::integerID)
58
BOOST_ASSERT(i->children.size() == 0);
60
// extract integer (not always delimited by '\0')
61
string integer(i->value.begin(), i->value.end());
63
return strtol(integer.c_str(), 0, 10);
65
else if (i->value.id() == calculator::factorID)
67
// factor can only be unary minus
68
BOOST_ASSERT(*i->value.begin() == '-');
69
return - eval_expression(i->children.begin());
71
else if (i->value.id() == calculator::termID)
73
if (*i->value.begin() == '*')
75
BOOST_ASSERT(i->children.size() == 2);
76
return eval_expression(i->children.begin()) *
77
eval_expression(i->children.begin()+1);
79
else if (*i->value.begin() == '/')
81
BOOST_ASSERT(i->children.size() == 2);
82
return eval_expression(i->children.begin()) /
83
eval_expression(i->children.begin()+1);
88
else if (i->value.id() == calculator::expressionID)
90
if (*i->value.begin() == '+')
92
BOOST_ASSERT(i->children.size() == 2);
93
return eval_expression(i->children.begin()) +
94
eval_expression(i->children.begin()+1);
96
else if (*i->value.begin() == '-')
98
BOOST_ASSERT(i->children.size() == 2);
99
return eval_expression(i->children.begin()) -
100
eval_expression(i->children.begin()+1);
107
BOOST_ASSERT(0); // error
113
////////////////////////////////////////////////////////////////////////////
117
// look in tree_calc_grammar for the definition of calculator
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";
126
while (getline(cin, str))
128
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
131
tree_parse_info<> info = ast_parse(str.c_str(), calc);
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);
146
cout << "parsing succeeded\n";
147
cout << "result = " << evaluate(info) << "\n\n";
151
cout << "parsing failed\n";
155
cout << "Bye... :-) \n\n";