[Haskell-beginners] expression parser

John Moore john.moore54 at gmail.com
Tue Dec 22 15:48:11 EST 2009


Hi,
   Could some one point out what I'm doing wrong below. This is a parser
which takes an arithmetic expression built up from single digits. Turns
3*4+2 into (3*4)+2 etc.It giving me the last expression in a do statement or
parse error on ->

(+++) :: Parser a -> Parser a -> Parser a
expr :: term( '+' expr |"")
term -> factor('*' term |"")

expr :: Parser Int
expr  = do t <- term
   do char '+'
      e <- expr
      return (t + e)
    +++ return t

term :: Parser Int
term = do f <- factor
      do char '*'
         t <- term
      return (f * t)
   +++ return f

factor :: Parser Int
factor = do d <- digit
             return (digitToInt d)
  +++ do char '('
         e <- expr
      char ')'
      return e

eval :: String -> Int
eval xs = fst(head(parse expr xs))


John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091222/14484e65/attachment.html


More information about the Beginners mailing list