I'm trying to write a parser for the Mathematica language in F# using FParsec.
I have written one for a MiniML that supports the syntax f x y = (f(x))(y)
with high precedence for function application. Now I need to use the same syntax to mean f*x*y
and, therefore, have the same precedence as multiply. In particular, x y + 2 = x*y + 2
whereas x y ^ 2 = x * y^2
.
How can this be accomplished?
As Stephan pointed out in a comment you can split the operator parser into two separate parsers and put your own parser in the middle for space-separated expressions. The following code demonstrates this:
The output is:
which represents
1 * 2 + 3 * 4^5 * 6
as expected.