Actually the problem is that your parsers are right recursive, not left (which is more traditional for these operators). Your example expression is being parsed as if you had written
1-(2-(3-4))
which has value -2.
Try rephrasing your parsers as, for example,
expr = expr + term
| expr - term
| term
and you should get the result you expect. (Yes, Scala's parser combinators can handle left recursive productions, at least simple ones.)
cheers,
Tony
Hi,
Or you could have used something like:
lazy val expr = term ~ rep(
"+" ~> term ^^ (...) |
"-" ~> term ^^ (...)
) ^^ {
... foldleft ...
}
And you would not have had to move to a packrat parser to support your
left recursive grammar (packrat parsers are good, just telling you
another solution).
Regards,
Tommaso
I found the solution:
Thanks,
Michael
The information included in this email and any files transmitted with it may contain information that is confidential and it must not be used by, or its contents or attachments copied or disclosed, to persons other than the intended addressee. If you have received this email in error, please notify BJSS. In the absence of written agreement to the contrary BJSS' relevant standard terms of contract for any work to be undertaken will apply. Please carry out virus or such other checks as you consider appropriate in respect of this email. BJSS do not accept responsibility for any adverse effect upon your system or data in relation to this email or any files transmitted with it. BJSS Limited, a company registered in England and Wales (Company Number 2777575), VAT Registration Number 613295452, Registered Office Address, First Floor, Coronet House, Queen Street, Leeds, LS1 2TW
I'm glad you got there. Sorry, for forgetting to mention the lazy val thing
in my first response...
cheers,
Tony