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 missing two things before it is complete. Both of the data type above its absence are "Neg Expr" and Let "Expr Expr Expr". The first part will be something like this:
parseExpr('-'
parseExpr('l':'e':'t':x:'b':'e
As with the data type, Let expressions begin with let and take three Expr. I do not know how to write these last functions. Any help whatsoever would be greatly appreciated.
I asked here another question about this, and here is a link to this 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'))))
source
share