treating spaces with Parsec

ben.a benedikt.ahrens at
Fri Dec 5 11:14:44 EST 2008

i am trying to write a parser in haskell, using parsec.
the task is as follows: a string shall go into a data type (String,
[(String, [Integer])]). as an example: 
" : 0 , 2 22. abs : 1 ."
shall give
("LC", [("app", [0, 2, 22]), ("abs", [1])])
the string is separated by dots, and the integers are separated by either
commas or spaces (this is why i cannot use lexer).
i have written the following parser, which is not elegant at all, and has
the flaw that it does not accept the example because of the space between
the 1 and the last dot. furthermore it won't accept newline as a separator
for SepBy.

--------   the types
type Constructor = (String, [Integer])
type Sig = (String, [Constructor])       -- ("LC", [("app", [0, 0]), ("abs",

--------  the main parser
parseString2 :: Parser Sig
parseString2 = do
        name <- word
        sig <- endBy line eol
        return (name, sig)

---------  the parser for one symbol
line :: Parser Constructor
line = do
        constru <- word
        (char ':')
        ar <- sepBy number separator
        return (constru, ar)

--------  the parser for the integer values
number  :: Parser Integer
number  = do
        ds <- many1 digit;     
        return (read ds)

--------   the end of line parser
--------  two symbols are separated by at least one point or newline
eol :: Parser ()
eol = do
        skipMany1 (char '.' <|> char '\n')

--------   a separator between two integers is a space or a comma
separator :: Parser ()
separator = skipMany1 (space <|> char ',')

i would appreciate if somebody could help me. i tried to exploit all the
manuals i found for haskell, but was not successful.

View this message in context:
Sent from the Haskell - Libraries mailing list archive at

More information about the Libraries mailing list