data Expr = Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr
deriving(Eq, Show)
parseExpr :: String -> (Expr, String)
parseExpr ('*':'(':s) = (Mult x y, s'')
where (x,',':s') = parseExpr s
(y,')':s'') = parseExpr s'
parseExpr ('+':'(':s) = (Sum x y, s'')
where (x,',':s') = parseExpr s
(y,')':s'') = parseExpr s'
parseExpr (x : xs) | isDigit x = (Tall (digitToInt x), xs)
parseExpr (x:s) = (Var x,s)
where x >= 'A' = True
x <= 'Z' = True
My parser is lacking two things before it is complete. As from the datatype above its lacking "Neg Expr" and Let "Expr Expr Expr". The first part would be something like this:
parseExpr('-'
parseExpr('l':'e':'t':x:'b':'e
As in the datatype, the Let expressions starts with let and takes in three Expr. I don't know how to write out these last functions. Any help at all will be very much appreciated.
I did ask another question here on SO about this, and here is the link for that question.
Here is an example:
parseProg "let X be 4 in let Y be *(2 , X) in let Z be +(Y , X) in
+(+(X , Y) , Z)"
Let (Var 'X') (Tall 4) (Let (Var 'Y') (Mult (Tall 2) (Var 'X')) (Let
(Var 'Z') (Sum (Var 'Y') (Var 'X')) (Sum (Sum (Var 'X') (Var 'Y')) (Var
'Z'))))