[Haskell-cafe] Parsec bug, or...?

S. Doaitse Swierstra doaitse at swierstra.net
Wed Oct 14 03:34:11 EDT 2009

I could not resist this. The code

import Text.ParserCombinators.UU.Parsing

pCommand [] = pure []
pCommand xxs@(x:xs) = ((:) <$> pSym x <*> pCommand xs) `opt` xxs

pCommands = amb . foldr (<|>) pFail . map pCommand $ ["banana",  
"chocolate", "frito", "fromage"]

t :: String -> ([String], [Error Char Char Int])
t input = parse ( (,) <$> pCommands <*> pEnd)  (listToStr input)

gives the following results:

*Main> t ""
*Main> t "b"
*Main> t "fr"
*Main> t "x"
The token 'x'was not consumed by the parsing process.])
*Main> t "frox"
The token 'x'was not consumed by the parsing process.])
*Main> t "frx"
The token 'x'was not consumed by the parsing process.])

I think it is less clumsy and far less confusing than the Parsec code.  
Note that the function amb tells that its parameter parser can be  


On 13 okt 2009, at 17:10, Uwe Hollerbach wrote:

> On 10/12/09, Martijn van Steenbergen <martijn at van.steenbergen.nl>  
> wrote:
>> Brandon S. Allbery KF8NH wrote:
>>> My fix would be to have myPrefixOf require the prefix be  
>>> terminated in
>>> whatever way is appropriate (end of input, white space, operator?)
>>> instead of simply accepting as soon as it gets a prefix match  
>>> regardless
>>> of what follows.
>> Maybe you can use notFollowedBy for this.
>> HTH,
>> Martijn.
> Yes, I've looked at that and am thinking about it. I'm not quite
> certain it's needed in my real program... I seem to have convinced
> myself that if I actually specify a proper set of unique prefixes, ie,
> set the required lengths for both "frito" and "fromage" to 3 in the
> test program, I won't get into this situation. Assuming I haven't
> committed another brain-fart there, that would be sufficient;
> presumably, in a real program one would want to actually specify the
> unique prefix, rather than a non-unique pre-prefix. It seems to work
> fine in my real program, anyway.
> Uwe
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list