[Haskell-cafe] Associative prefix operators in Parsec
Christian Maeder
Christian.Maeder at dfki.de
Mon Mar 12 10:55:14 CET 2012
Am 08.03.2012 17:16, schrieb Troels Henriksen:
> Christian Maeder<Christian.Maeder at dfki.de> writes:
>
>> The simplest solution is to parse the prefixes yourself and do not put
>> it into the table.
>>
>> (Doing the infixes "&" and "|" by hand is no big deal, too, and
>> possibly easier then figuring out the capabilities of
>> buildExpressionParser)
>
> Is there another solution? My post was a simplified example to showcase
> the problem; in general I would prefer to use a function to build the
> expression parser. I could just write my own that does not have this
> problem, and in fact, I already have, I just wanted to know whether
> Parsec could be wrangled into shape.
Yes, it certainly could do better. The code for prefix and postfix
currently looks like:
termP = do{ pre <- prefixP
; x <- term
; post <- postfixP
; return (post (pre x))
}
This supports (only) one prefix or postfix (or both), where the prefix
binds stronger than the postfix (although, they have equal precedence).
Problem 1: "- - 5" is not supported
Another problem are prefix or postfix operators that bind weaker than
infixes, like infix "^" and prefix "-".
Problem 2: "1 ^ -2" is rejected, although no other parsing is possible.
(The same would apply to a weakly binding postfix operator following the
left argument: "4+ ^ 5". This would even need some look ahead to find
out, if + is not an infix and ^ a prefix operator)
(Haskell features these problems, too.)
Maybe the special case of repeated prefixes could be solved by putting
in the prefix entry twice into the table for the
currentbuildExpressionParser, but considering possibly equal symbols for
prefix, postfix or infix symbols seems quite difficult, although only an
ambiguity needs to be reported.
Cheers C.
More information about the Haskell-Cafe
mailing list