I would like to add the exponentiation operator to the expression grammar provided in the Boost spirit samples.
The BNF grammar is the following: (see this answer for example: "Unambiguous grammar for exponentiation operation" )
E -> E + T | E - T | T
T -> T * F | T / F | X
X -> X ^ Y | Y
Y -> i | (E)
which I translated to Boost spirit like this:
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::uint_type uint_;
expression =
term
>> *( ('+' >> term [&do_add])
| ('-' >> term [&do_subt])
)
;
term =
factor
>> *( ( '*' >> factor [&do_mult])
| ('x' >> factor [&do_mult])
| ('/' >> factor [&do_div])
);
factor= expo >> *( '^' >> expo [&do_power]);
expo =
uint_ [&do_int]
| '(' >> expression >> ')'
| ('-' >> expo[&do_neg])
| ('+' >> expo)
;
}
qi::rule<Iterator, ascii::space_type> expression, term, factor, expo;
};
The problem is that the ^ operator in this case is left associative, ie 2 ^ 3 ^ 4
is parsed incorrectly as (2 ^ 3) ^ 4
instead of 2^ (3 ^ 4)
.
How may I rewrite the grammar so that ^
becomes right associative? Obviously the Kleene star I used in the definition of factor
is incorrect. What is the method to translate a grammar to Spirit code ? There seems to be a way to go from the left-factored grammar to the Spirit implementation but I can't see it immediately.
In a more formal way, the Spirit code looks like this (before I tried to add the exponent):
E = T ( +T | -T ) *
T = F ( xF | /F ) *
F = int | ( E ) | +F | -F
and the left-factored grammmar is
E = T E'
E' = +T E' | -T E' | epsilon
T = F T'
T' = *F T' | /F T' | epsilon
F = ( E ) | int | +F | -F