I would like to evaluate boolean expression such as a=b & s<9 or simply a=b with only comparison operator (wihtout logical operator such as |, & and !). We can have the following AST:
=
/ \
/ \
a b
or
&
/ \
/ \
= <
/ \ /\
/ \ / \
a b s 9
The leaves nodes are values. The parent of leave nodes are always comparison operator such as =, !=, <,>, >=, <=. The parent of comparison nodes are logical operator |, &, and !. I would like to access to value nodes (leaves) from their parent node and then pass these values to another function (which will be implemented later). The parsing step is ok.
How to access values nodes (leaves) from their parent node. I am using examples at : How to calculate boolean expression in Spirit
and Boolean expression (grammar) parser in c++ This is an evaluation code taken from these links:
struct eval : boost::static_visitor<bool> { eval() {} // bool operator()(const var& v) const { std::cout<<"feuille:\n"<<v<<std::endl; return true; } bool operator()(const binop<op_and>& b) const { recurse(b.oper1) && recurse(b.oper2); } bool operator()(const binop<op_or>& b) const { recurse(b.oper1) || recurse(b.oper2); } bool operator()(const unop<op_not>& u) const { return !recurse(u.oper1); } //------------adding others operators---------------------------- bool operator()(const binop<op_equal>& u) const { // will be implemented later return true; } bool operator()(const binop<op_not_equal>& u) const { // will be implemented later return true; } bool operator()(const binop<op_less>& u) const { // will be implemented later return true; } bool operator()(const binop<op_less_equal>& u) const { // will be implemented later return true; } bool operator()(const binop<op_greater>& u) const { // will be implemented later return true; } bool operator()(const binop<op_greater_equal>& u) const { // will be implemented later return true; }
Thank you. Any suggestion is welcome.