[Haskell-cafe] Parsing R5RS Scheme with Parsec

Alex Queiroz asandroq at gmail.com
Tue Oct 2 09:52:53 EDT 2007


Hallo,

     For fun and learning I'm trying to parse R5RS Scheme with Parsec.
The code to parse lists follows:

--
-- Lists
--

parseLeftList :: Parser [SchDatum]
parseLeftList = do
  char '('
  many parseDatum >>= return . filter (/=  SchAtmosphere)

parseDottedList :: [SchDatum] -> Parser SchDatum
parseDottedList ls = do
  char '.'
  many1 parseAtmosphere
  d <- parseDatum
  many parseAtmosphere
  char ')'
  return $ SchDottedList ls d

parseProperList :: [SchDatum] -> Parser SchDatum
parseProperList ls = do
  char ')'
  return $ SchList ls

parseList :: Parser SchDatum
parseList = do
  ls <- parseLeftList
  (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?

Cheers,
-- 
-alex
http://www.ventonegro.org/


More information about the Haskell-Cafe mailing list