On Thu, Apr 05, 2007 at 03:19:15PM +0100, Joel Reymont wrote:
> numExpr :: GenParser Char a NumExpr
> numExpr =
>     choice [ integer >>= return . Int
>            , float >>= return . Num
>            ]

Parsec's choice operator works by parsing the first, and only parsing
the second if the first fails immediately.  So, given the input
"123.456":

- Parsec parses 'integer >>= return . Int'
- this is successful - numExpr returns (Int 123, ".456")
- we try to match . against ) and fail.

The fix is to left-factor the grammar, or just use the existing
factored choice operator:

> numExpr :: GenParser Char a NumExpr
> numExpr = do sg <- lexeme sign
>              nf <- natOrFloat
>              return $ either (Int . sg) (Nat . sg) nf

It seems silly that there is no signed version of natOrFloat
predefined, any Parsec experts? 

Stefan
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to