Name: Anonymous 2010-06-09 12:13
So I got this arithmetic expression parser. I want to add subtraction and division support but I can't get it to work properly (aka make them left associative). Anyone got any ideas?:(
I import this:
http://www.cs.nott.ac.uk/~gmh/Parsing.lhs
then:
I import this:
http://www.cs.nott.ac.uk/~gmh/Parsing.lhs
then:
expr :: Parser Int
expr = do t <- term
do symbol "+"
e <- expr
return (t+e)
+++ return t
term :: Parser Int
term = do f <- factor
do symbol "*"
t <- term
return (f * t)
+++ return f
factor :: Parser Int
factor = do symbol "("
e <- expr
symbol ")"
return e
+++ natural
eval :: String -> Int
eval xs = case (parse expr xs) of
[(n,[])] -> n
[(_,out)] -> error ("unused input " ++ out)
[] -> error "invalid input"