I'm trying to parse math expression into a tree and evaluate it later with specific variables (char). Here is my grammar:
#include <boost/spirit/include/qi.hpp>
#include <boost/variant/recursive_variant.hpp>
struct Expression;
struct SubFunction;
using OperandVariant = boost::variant<double, char, boost::recursive_wrapper<SubFunction>, boost::recursive_wrapper<Expression>>;
struct SubFunction
{
std::string name;
std::vector<OperandVariant> arguments;
};
struct Operation
{
std::string type;
OperandVariant operand;
};
struct Expression
{
OperandVariant operand;
std::vector<Operation> operations;
};
BOOST_FUSION_ADAPT_STRUCT(
SubFunction,
(std::string, name)
(std::vector<OperandVariant>, arguments)
)
BOOST_FUSION_ADAPT_STRUCT(
Operation,
(std::string, type)
(OperandVariant, operand)
)
BOOST_FUSION_ADAPT_STRUCT(
Expression,
(OperandVariant, operand)
(std::vector<Operation>, operations)
)
template <typename Iterator>
struct FunctionGrammar : boost::spirit::qi::grammar<Iterator, Expression(), boost::spirit::ascii::space_type>
{
FunctionGrammar() : FunctionGrammar::base_type(logic)
{
using boost::spirit::qi::string;
using boost::spirit::qi::double_;
using boost::spirit::qi::lexeme;
using boost::spirit::qi::char_;
using boost::spirit::qi::lit;
using boost::spirit::qi::attr;
logic = relation >> *(string("&&") >> relation
| string("^^") >> relation
| string("||") >> relation);
relation = addition >> *(string("<=") >> addition
| string(">=") >> addition
| string("!=") >> addition
| string("==") >> addition
| char_('>') >> addition
| char_('<') >> addition);
addition = multiplication >> *(char_('+') >> multiplication
| char_('-') >> multiplication);
multiplication = value >> *(char_('*') >> value
| char_('/') >> value);
value = '(' >> logic >> ')'
| +(char_ - '(') >> '(' >> value % ',' >> ')'
| double_
| char_;
}
boost::spirit::qi::rule<Iterator, Expression(), boost::spirit::ascii::space_type> logic;
boost::spirit::qi::rule<Iterator, Expression(), boost::spirit::ascii::space_type> relation;
boost::spirit::qi::rule<Iterator, Expression(), boost::spirit::ascii::space_type> addition;
boost::spirit::qi::rule<Iterator, Expression(), boost::spirit::ascii::space_type> multiplication;
boost::spirit::qi::rule<Iterator, OperandVariant(), boost::spirit::ascii::space_type> value;
};
I did this based on an example from the official documentation and it works. But I want to add the ability to use +
, -
, *
etc. inside a Subfunction
arguments (that splitted by ,
). If I replace this part
+(char_ - '(') >> '(' >> value % ',' >> ')'
into this:
+(char_ - '(') >> '(' >> logic % ',' >> ')'
the code will not compile. What am I doing wrong?
+(char_ - '(') >> '(' >> value % ',' >> ')'
parser has fusion::vector<std::string, std::vector<OperandVariant> >
attribute type, it can be converted to SubFunction
and stored in OperandVariant
(the attribute of value
rule), while +(char_ - '(') >> '(' >> logic % ',' >> ')'
parser has fusion::vector<std::string, std::vector<Expression> >
attribute type and cannot be converted to any type that OperandVariant
can hold.
That explanation could also be deciphered from this part of a Clang output:
/opt/wandbox/boost-1.72.0/clang-head/include/boost/spirit/home/qi/detail/assign_to.hpp:153:20: error: no matching conversion for static_cast from 'const boost::fusion::vector<std::__1::vector<char, std::__1::allocator<char> >, std::__1::vector<Expression, std::__1::allocator<Expression> > >' to 'boost::variant<double, char, boost::recursive_wrapper<SubFunction>, boost::recursive_wrapper<Expression> >'
attr = static_cast<Attribute>(val);
^~~~~~~~~~~~~~~~~~~~~~~~~~~