[Haskell-cafe] Parsec : Problems with operator precedence (solution)

Erik de Castro Lopo mle+cl at mega-nerd.com
Mon Dec 29 22:50:52 EST 2008


Erik de Castro Lopo wrote:

> Replacing reservedOp above with:
> 
>     reservedOpNf :: String -> CharParser st ()
>     reservedOpNf name = try (reservedOp name >> notFollowedBy (oneOf opChars))
> 
> fixed the problem.

Just for the sake of completeness and the google archive, the above
fixed the main problem but left another problem in parsing something
like:

   if (whatever == -1)

which  resulted in a error at uninary minus sign.

The problem was that the reservedOp combinator as used above chews
up any trailing whitespace so that the notFollowedBy was triggered
by the minus.

The solution was to use the string combinator instead of reservedOp,
then use the notFollowedBy and finally chew up trailing whitespace
as follows:

    reservedOpNf :: String -> CharParser st ()
    reservedOpNf name =
        try (string name >> notFollowedBy (oneOf "|&=") >> whiteSpace)

Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
Is God willing to prevent evil, but not able?
Then he is not omnipotent.
Is he able, but not willing?
Then he is malevolent.
Is he both able and willing?
Then whence cometh evil?
Is he neither able nor willing?
Then why call him God?
-- Epicurus, Greek philosopher, BC 341-270


More information about the Haskell-Cafe mailing list