[Haskell-cafe] help

Daniel Fischer daniel.is.fischer at web.de
Mon Dec 10 09:23:14 EST 2007

Am Montag, 10. Dezember 2007 14:45 schrieb Ryan Bloor:
> hi I am writing a basic Parser from scratch. So far I have functions;#
> removeSpaces# match - which checks if a string is a substring of another#
> orParser which combines two parser's abilities# Basic pasrers like...
> parseInt, parseTrue, parseFalse, parseBoolusing the orParser on True and
> False.What I want to do now is have a parseBinaryOp that
> recognises:parseBinaryOp "+" "(5 + 2) if"                      >>>gives>>  
>           [(EInt 5, EInt 2, "if")]So I think that I have to split the
> initial string into four parts."+" becomes op'(' becomes tokenF')' becomes
> tokenB"5" becomes e1"2" becomes e2parseBinaryOp :: String -> String ->
> [(Expr, Expr, String)]parseBinaryOp op str = let (tokenF,e1,op,e2,tokenB)
> =I am not sure how to go about separating the string for how I need itusing
> my other functiuons. Ryan

Keep it simpler. Write combinators

parseFirstThen  :: Parser a -> Parser b -> Parser (a,b)

parseFirstThenWith :: (a -> b -> c) -> Parser a -> Parser b -> Parser c

applyToParse :: (a -> b) -> Parser a -> Parser b

(then you would find
parseFirstThenWith f p1 p2 === applyToParse (uncurry f) (parseFirstThen p1 p2)

applyToParse f p === parseFirstThenWith (const . f) p (succeed ())

where succeed x input = [(x,input)]
and compose your parser using these.
Better still, read
and the chapter on parsing in any tutorial/textbook, docs and sources for 
parsing libraries, such as parsec.

More information about the Haskell-Cafe mailing list