I have another problem with my boost::spirit parser.
template<typename Iterator>
struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> {
expression() :
expression::base_type(expr) {
number %= lexeme[double_];
varname %= lexeme[alpha >> *(alnum | '_')];
binop = (expr >> '+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1,_2)]
| (expr >> '-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1,_2)]
| (expr >> '*' >> expr)[_val = construct<ast::binary_op<ast::mul>>(_1,_2)]
| (expr >> '/' >> expr)[_val = construct<ast::binary_op<ast::div>>(_1,_2)] ;
expr %= number | varname | binop;
}
qi::rule<Iterator, ast::expression(), ascii::space_type> expr;
qi::rule<Iterator, ast::expression(), ascii::space_type> binop;
qi::rule<Iterator, std::string(), ascii::space_type> varname;
qi::rule<Iterator, double(), ascii::space_type> number;
};
This was my parser. It parsed "3.1415" and "var" just fine, but when I tried to parse "1+2" it tells me parse failed. I’ve then tried to change the binop rule to
binop = expr >>
(('+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1, _2)]
| ('-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1, _2)]
| ('*' >> expr)[_val = construct<ast::binary_op<ast::mul>>(_1, _2)]
| ('/' >> expr)[_val = construct<ast::binary_op<ast::div>>(_1, _2)]);
But now it’s of course not able to build the AST, because _1 and _2 are set differently. I have only seen something like _r1 mentioned, but as a boost-Newbie I am not quite able to understand how boost::phoenix and boost::spirit interact.
How to solve this?
It isn’t entirely clear to me what you are trying to achieve. Most importantly, are you not worried about operator associativity? I’ll just show simple answers based on using right-recursion – this leads to left-associative operators being parsed.
The straight answer to your visible question would be to juggle a
fusion::vector2<char, ast::expression>– which isn’t really any fun, especially in Phoenix lambda semantic actions. (I’ll show below, what that looks like).Meanwhile I think you should read up on the Spirit docs
calculatorsamples, which should give you a hint on why operator associativity matters, and how you would express a grammar that captures the associativity of binary operators. Obviously, it also shows how to support parenthesized expressions to override the default evaluation order.Code:
I have three version of code that works, parsing input like:
into an
ast::expressiongrouped like (using BOOST_SPIRIT_DEBUG):Step 1: Reduce semantic actions
First thing, I’d get rid of the alternative parse expressions per operator; this leads to excessive backtracking1. Also, as you’ve found out, it makes the grammar hard to maintain. So, here is a simpler variation that uses a function for the semantic action:
1check that using BOOST_SPIRIT_DEBUG!
Step 2: Remove redundant rules, use
_valAs you can see, this has the potential to reduce complexity. It is only a small step now, to remove the binop intermediate (which has become quite redundant):
As you can see,
exprrule, the_vallazy placeholder is used as a pseudo-local variable that accumulates the binops. Across rules, you’d have to useqi::locals<ast::expression>for such an approach. (This was your question regarding_r1).exprrule no longer needs to be an auto-rule (expr =instead ofexpr %=)Step 0: Wrestle fusion types directly
Finally, for fun and gory, let me show how you could have handled your suggested code, along with the shifting bindings of _1, _2 etc.:
As you can see, not nearly as much fun writing the
make_binopfunction that way!