I found a really good example about boolean translator, * Boolean expression (grammar) parser in c++
What I am thinking now is to do a further step, translate (!T|F)&T
into F
or 0
, so it is very convenient for calculating a very long boolean expression.
Is there some examples about this using spirit? What I have done is making a calculator first, and then let it calculate '(T+!F*T)', which equal to (T||!F&&T)
but when I type ()
, there is an error. How to modify it? Thanks a lot!
#include <iostream>
#include <stack>
#include <boost/lexical_cast.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
using namespace std;
namespace phoenix = boost::phoenix;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct calculator
{
bool interpret(const string& s);
void do_neg();
void do_add();
void do_sub();
void do_mul();
void do_div();
void do_number(const char* first, const char* last);
int val() const;
private:
stack<int> values_;
int *pn1_, n2_;
void pop_1();
void pop_2();
};
template <typename Iterator>
struct calc_grammar : qi::grammar<Iterator, ascii::space_type>
{
calc_grammar(calculator& calc)
: calc_grammar::base_type(add_sub_expr)
, calc_(calc)
{
using namespace qi;
using boost::iterator_range;
#define LAZY_FUN0(f) phoenix::bind(&calculator::f, calc_)
#define LAZY_FUN2(f) phoenix::bind(&calculator::f, calc_, phoenix::bind(&iterator_range<Iterator>::begin, qi::_1), phoenix::bind(&iterator_range<Iterator>::end, qi::_1))
add_sub_expr =
(
-lit('+') >> mul_div_expr |
(lit('-') >> mul_div_expr)[LAZY_FUN0(do_neg)]
) >>
*(
lit('+') >> mul_div_expr[LAZY_FUN0(do_add)] |
lit('-') >> mul_div_expr[LAZY_FUN0(do_sub)]
) >> eoi;
mul_div_expr =
basic_expr >>
*(
lit('*') >> basic_expr[LAZY_FUN0(do_mul)] |
lit('/') >> basic_expr[LAZY_FUN0(do_div)]
);
basic_expr =
raw[number][LAZY_FUN2(do_number)] |
lit('(') >> add_sub_expr >> lit(')');
number = lexeme[+digit];
}
qi::rule<Iterator, ascii::space_type> add_sub_expr, mul_div_expr, basic_expr, number;
calculator& calc_;
};
bool calculator::interpret(const string& s)
{
calc_grammar<const char*> g(*this);
const char* p = s.c_str();
return qi::phrase_parse(p, p + s.length(), g, ascii::space);
}
void calculator::pop_1()
{
pn1_ = &values_.top();
}
void calculator::pop_2()
{
n2_ = values_.top();
values_.pop();
pop_1();
}
void calculator::do_number(const char* first, const char* last)
{
string str(first, last);
int n = boost::lexical_cast<int>(str);
values_.push(n);
}
void calculator::do_neg()
{
pop_1();
*pn1_ = -*pn1_;
}
void calculator::do_add()
{
pop_2();
*pn1_ += n2_;
}
void calculator::do_sub()
{
pop_2();
*pn1_ -= n2_;
}
void calculator::do_mul()
{
pop_2();
*pn1_ *= n2_;
}
void calculator::do_div()
{
pop_2();
*pn1_ /= n2_;
}
int calculator::val() const
{
assert(values_.size() == 1);
return values_.top();
}
int main()
{
for(;;){
cout << ">>> ";
string s;
getline(cin, s);
if(s.empty()) break;
calculator calc;
if(calc.interpret(s))
cout << calc.val() << endl;
else
cout << "syntax error" << endl;
}
return 0;
}
printer
visitor. Instead ofvoid
it needs to returnbool
and instead of printing the nodes it needs to calculate the results. That’s literally all there is to it. – Stalker