This is the code for parsing steps:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/lexical_cast.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
struct op_or {};
struct op_and {};
struct op_not {};
struct op_equal{};
struct op_not_equal{};
struct op_less{};
struct op_less_equal{};
struct op_greater{};
struct op_greater_equal{};
typedef std::string var;
template <typename tag> struct binop;
template <typename tag> struct unop;
typedef boost::variant<var,
boost::recursive_wrapper<unop <op_not> >,
boost::recursive_wrapper<binop<op_and> >,
boost::recursive_wrapper<binop<op_or> >,
boost::recursive_wrapper<binop<op_equal> >,
boost::recursive_wrapper<binop<op_not_equal> >,
boost::recursive_wrapper<binop<op_less> >,
boost::recursive_wrapper<binop<op_less_equal> >,
boost::recursive_wrapper<binop<op_greater> >,
boost::recursive_wrapper<binop<op_greater_equal> >
> expr;
template <typename tag> struct binop
{
explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
expr oper1, oper2;
};
template <typename tag> struct unop
{
explicit unop(const expr& o) : oper1(o) {}
expr oper1;
};
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, expr(), Skipper>
{
parser() : parser::base_type(expr_)
{
using namespace qi;
expr_ = boolExpr_.alias();
boolExpr_ = *((compExpr_ >> '|' >> compExpr_ ) [ _val = phx::construct<binop<op_or > >(_1, _2) ]
|(compExpr_ >> '&' >> compExpr_ ) [ _val = phx::construct<binop<op_and > >(_1, _2) ]
|(compExpr_ >> '!' >> compExpr_ ) [ _val = phx::construct<binop<op_not > >(_1, _2) ]
);
compExpr_=((compExpr_ >> '=' >> compExpr_ ) [ _val = phx::construct<binop<op_equal > >(_1, _2) ]
|(compExpr_ >> "!=" >> compExpr_ ) [ _val = phx::construct<binop<op_not_equal > >(_1, _2) ]
|(compExpr_ >> '<' >> compExpr_ ) [ _val = phx::construct<binop<op_less > >(_1, _2) ]
| (compExpr_ >> '<=' >> compExpr_ ) [ _val = phx::construct<binop<op_less_equal> >(_1, _2) ]
| (compExpr_ >> '>' >> compExpr_ ) [ _val = phx::construct<binop<op_greater> >(_1, _2) ]
| (compExpr_ >> ">=" >> compExpr_ ) [ _val = phx::construct<binop<op_greater_equal> >(_1, _2) ]
|'(' >> boolExpr_>>')'
| varExpr_
);
varExpr_=qi::lexeme[ +(alpha|digit) ];
BOOST_SPIRIT_DEBUG_NODE(expr_);
BOOST_SPIRIT_DEBUG_NODE(boolExpr_);
BOOST_SPIRIT_DEBUG_NODE(compExpr_);
BOOST_SPIRIT_DEBUG_NODE(varExpr_);
// BOOST_SPIRIT_DEBUG_NODE(simple);
// BOOST_SPIRIT_DEBUG_NODE(var_);
}
private:
qi::rule<It, var() , Skipper> varExpr_;
qi::rule<It, expr(), Skipper> boolExpr_, compExpr_, expr_;
};
int main()
{
std::string inputs="a=b & c>9";
typedef std::string::const_iterator It;
It iter=inputs.begin(),end=inputs.end();
parser<It> p;
try
{
expr result;
bool ok = qi::phrase_parse(iter,end,p,qi::space,result);
if (!ok)
std::cerr << "invalid input\n";
else
{
std::cout << "result:\t" << result << "\n";
// std::cout << "evaluated:\t" << evaluate(result) << "\n";
}
} catch (const qi::expectation_failure<It>& e)
{
std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
}
if (iter!=end) std::cerr << "unparsed: '" << std::string(iter,end) << "'\n";
return 0;
}
Before the parser instantiation, the compilation is ok. This is error. Thank you for any suggestion: