[Haskell-cafe] Parsing R5RS Scheme with Parsec

Stefan O'Rear stefanor at cox.net
Tue Oct 2 16:58:52 EDT 2007


On Tue, Oct 02, 2007 at 11:36:52AM -0300, Alex Queiroz wrote:
> Hallo,
> 
> On 10/2/07, Brandon S. Allbery KF8NH <allbery at ece.cmu.edu> wrote:
> >
> > On Oct 2, 2007, at 9:52 , Alex Queiroz wrote:
> >
> > >   (parseDottedList ls) <|> (parseProperList ls)
> > >
> > >      I've factored out the common left sub-expression in
> > > parseLeftList. The problem is that "..." is a valid identifier so when
> > > inside the left of the list the parser sees a single dot, it tries to
> > > match it with "...", which fails. Can anybody give advice on how to
> > > rewrite these list parsing functions?
> >
> >    try (parseDottedList ls) <|> parseProperList ls
> >
> > Overuse of try is a bad idea because it's slow, but sometimes it's
> > the only way to go; it insures backtracking in cases like this.
> >
> 
>      This does not work. The parser chokes in parseLeftList, because
> it finds a single dot which is not the beginning of "...".

I suggest left-factoring.

parseThingyOrEOL =
     (space >> parseThingyOrEOL)
 <|> (fmap Left parseAtom)
 <|> (char '.' >> parseThingyOrEOL >>= \(Left x) -> Right x)
 <|> (char ')' >> return (Right nil))
 <|> (char '(' >> fmap Left (fix (\ plist -> do obj <- parseThingyOrEOL
                                                case obj of Left x  -> fmap (Cons x) plist
                                                            Right x -> return x)))

etc.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071002/87f4db55/attachment.bin


More information about the Haskell-Cafe mailing list