I tried to write a simple expression parser with boost::spirit. I started with the calculator example (see: http://www.boost.org/doc/libs/1_41_0/libs/spirit/example/qi/calc2_ast.cpp) and tried to add a "ref" rule denoting a reference to a variable. Here is my code:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <iostream>
#include <vector>
#include <string>
namespace client{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our AST
///////////////////////////////////////////////////////////////////////////
struct binary_op;
struct unary_op;
struct ref {
std::string name;
ref(const std::string& name) : name(name){}
};
struct nil {};
struct expression_ast
{
typedef
boost::variant<
nil // can't happen!
, unsigned int
, ref
, boost::recursive_wrapper<expression_ast>
, boost::recursive_wrapper<binary_op>
, boost::recursive_wrapper<unary_op>
>
type;
expression_ast()
: expr(nil()) {}
template <typename Expr>
expression_ast(Expr const& expr)
: expr(expr) {}
expression_ast& operator+=(expression_ast const& rhs);
expression_ast& operator-=(expression_ast const& rhs);
expression_ast& operator*=(expression_ast const& rhs);
expression_ast& operator/=(expression_ast const& rhs);
type expr;
};
struct binary_op
{
binary_op(
char op
, expression_ast const& left
, expression_ast const& right)
: op(op), left(left), right(right) {}
char op;
expression_ast left;
expression_ast right;
};
struct unary_op
{
unary_op(
char op
, expression_ast const& subject)
: op(op), subject(subject) {}
char op;
expression_ast subject;
};
expression_ast& expression_ast::operator+=(expression_ast const& rhs)
{
expr = binary_op('+', expr, rhs);
return *this;
}
expression_ast& expression_ast::operator-=(expression_ast const& rhs)
{
expr = binary_op('-', expr, rhs);
return *this;
}
expression_ast& expression_ast::operator*=(expression_ast const& rhs)
{
expr = binary_op('*', expr, rhs);
return *this;
}
expression_ast& expression_ast::operator/=(expression_ast const& rhs)
{
expr = binary_op('/', expr, rhs);
return *this;
}
// We should be using expression_ast::operator-. There's a bug
// in phoenix type deduction mechanism that prevents us from
// doing so. Phoenix will be switching to BOOST_TYPEOF. In the
// meantime, we will use a phoenix::function below:
struct negate_expr
{
template <typename T>
struct result { typedef T type; };
expression_ast operator()(expression_ast const& expr) const
{
return expression_ast(unary_op('-', expr));
}
};
struct ref_maker
{
template <typename T>
struct result { typedef T type; };
ref operator()(std::string const& expr) const
{
return expr;
}
};
boost::phoenix::function<negate_expr> neg;
boost::phoenix::function<ref_maker> make_ref;
///////////////////////////////////////////////////////////////////////////
// Walk the tree
///////////////////////////////////////////////////////////////////////////
struct ast_print
{
typedef void result_type;
void operator()(qi::info::nil) const {
}
void operator()(int n) const {
std::cout << n;
}
void operator()(expression_ast const& ast) const {
boost::apply_visitor(*this, ast.expr);
}
void operator()(binary_op const& expr) const {
std::cout << "op:" << expr.op << "(";
boost::apply_visitor(*this, expr.left.expr);
std::cout << ", ";
boost::apply_visitor(*this, expr.right.expr);
std::cout << ')';
}
void operator()(unary_op const& expr) const {
std::cout << "op:" << expr.op << "(";
boost::apply_visitor(*this, expr.subject.expr);
std::cout << ')';
}
void operator()(ref const& expr) const {
std::cout << "ref:" << expr.name ;
}
};
///////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, expression_ast(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
using qi::_val;
using qi::_1;
using qi::uint_;
using qi::lit;
using ascii::alnum;
identifier = (alnum [_val += _1]);
expression =
term [_val = _1]
>> *( ('+' >> term [_val += _1])
| ('-' >> term [_val -= _1])
)
;
term =
factor [_val = _1]
>> *( ('*' >> factor [_val *= _1])
| ('/' >> factor [_val /= _1])
)
;
factor =
uint_ [_val = _1]
| identifier [_val = make_ref(_1)]
| '(' >> expression [_val = _1] >> ')'
| ('-' >> factor [_val = neg(_1)])
| ('+' >> factor [_val = _1])
;
}
qi::rule<Iterator, std::string(), ascii::space_type> identifier;
qi::rule<Iterator, expression_ast(), ascii::space_type> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using client::ascii::space;
using client::expression_ast;
using client::ast_print;
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
expression_ast ast;
ast_print printer;
bool r = phrase_parse(iter, end, calc, space, ast);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
printer(ast);
std::cout << "\n-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \": " << rest << "\"\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
The only changes I made to the example were the following:
- Add a
ref
struct and an entry in the variant. - Add an
identifier
rule which reads a single variable name asstring
- Create a functor and phoenix function
make_ref
for making aref
fromstring
- Add an
identifier
option to thefactor
rule which callsmake_ref
to transform theidentifier
string
to aref
.
The code does not work. There is always the following type error:
/usr/include/boost/spirit/home/phoenix/core/detail/function_eval.hpp:135:69: error: could not convert ‘bench::expr::ref_maker::operator()(const string&) const((*(const string*)boost::phoenix::detail::help_rvalue_deduction<std::basic_string<char> >((* &(& _0)->boost::spirit::argument<N>::eval<boost::phoenix::basic_environment<boost::fusion::vector1<std::basic_string<char>&>, boost::spirit::context<boost::fusion::cons<bench::expr::expression_ast&, boost::fusion::nil>, boost::fusion::vector0<> >, bool, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> >((* & env))))))’ from ‘bench::expr::ref’ to ‘std::basic_string<char>’
It seems that phoenix wants make_ref
to return a string
instead of a ref
, but this is not what I want. What am I doing wrong here?
The result of
ref_maker
is alwaysref
, not the same as the input argument. Make itAlso, replace
bench::expr::
in main withclient::
, or you'll get errors that namespacebench::expr
doesn't exist.