treating spaces with Parsec
ben.a
benedikt.ahrens at web.de
Fri Dec 5 11:14:44 EST 2008
hello,
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:
"LC.app : 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",
[1])])
-------- the main parser
parseString2 :: Parser Sig
parseString2 = do
spaces
name <- word
spaces
eol
sig <- endBy line eol
return (name, sig)
--------- the parser for one symbol
line :: Parser Constructor
line = do
spaces
constru <- word
spaces
(char ':')
spaces
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.
greetings
ben
--
View this message in context: http://www.nabble.com/treating-spaces-with-Parsec-tp20857293p20857293.html
Sent from the Haskell - Libraries mailing list archive at Nabble.com.
More information about the Libraries
mailing list