[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 ""
(["banana","chocolate","frito","fromage"],[])
*Main> t "b"
(["banana"],[])
*Main> t "fr"
(["frito","fromage"],[])
*Main> t "x"
(["banana","chocolate","frito","fromage"],[
The token 'x'was not consumed by the parsing process.])
*Main> t "frox"
(["fromage"],[
The token 'x'was not consumed by the parsing process.])
*Main> t "frx"
(["frito","fromage"],[
The token 'x'was not consumed by the parsing process.])
*Main>

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  
ambiguous

  Doaitse



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