I'm working on a Bison file for a mathematical expression parser. Up to now it's mostly fine, but I'm facing a problem with implicit multiplications.
You see, I'd like to support expressions like 2x sin(4x) cos(4x)
. It should parse like 2 * x * sin(4 * x) * cos(4 * x)
. Nothing too bad here, but consider the following set of rules:
expr
: /* snip */
| '-' expr { /* negate expression */ }
| expr '-' expr { /* subtract expressions */ }
| expr expr { /* multiply expressions */ }
Having that implicit multiplication rule creates an ambiguity with the subtraction rule: is x - log(x)
the subtraction of log(x)
to x
or the multiplication of x
by -log(x)
?
I'd be ready to settle for an easy solution, like "it's a multiplication unless it's subtracting", but I don't know how to tell that to Bison.