[Haskell-beginners] parser for expressions

John Moore john.moore54 at gmail.com
Sun Dec 27 13:10:54 EST 2009


Hi,
    What I'm trying to do is create a parser so as when I enter say 1+1 it
will return Add(Val 1)(Val 1). A couple of questions do I have to state
three basic parser. As in item, fail, synbol and then combine them with
below. And by the way the one below seems to be all wrong.



parse  :: String -> expr
parse expr = [(expr,string)]
expr :: Parser value
expr =  do t <- term
  do char '+'
  e <- expr
    return Add (Val t)(Val e)
 +++ return t

(+++) :: Parser a -> Parser a -> Parser a
t +++ w  inp  = case t inp of
     []  ->  w inp
     [(v,out)] -> [(v,out)]


This is what I mean by item fail and symbol

type Parser s a = [s] -> [(a,[s])]
item [] = []
item (c:cs) = [(c,cs)]
pFail :: Parser s a
pFail = \cs -> []
pSymbol :: Eq s => s -> Parser s s
pSymbol a (b:bs) |a == b = [(b,bs)]
  |otherwise = []

I know this is a bit of a mess, but could someone explain where I should
start and if I should use any of the code above. Also to point out I want to
write the complete parser without using prelude and other built in functions
as I will be changing as I go.

John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091227/66c806cd/attachment.html


More information about the Beginners mailing list