A sample grammar for polynomials:
> LINEARSUM ==
> LINEARSUM + LINEARSUM
> | LINEARSUM – LINEARSUM
> | NUMBER * LINEARSUM
> | - LINEARSUM
> | ( LINEARSUM )
> | VALUE
I have implemented this grammar using the parser combinators library (specifically PackratParsers) and it is insensitive to whitespace.
However, I have a problem in that the parsing does as much as possible. So when I do this MyParser.parse("-x + y")
, firstly it says x + y
is a linearsum, and then it sees it is preceded by a -
. My code then negates the “x + y” in the ^^ section to give -x -y
.
My question is should I be thinking about i) a new grammar ii) changing ordering (although I have tried this and the same problem reoccurs) iii) using different parsing methods in order to solve this?