-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
339 lines (284 loc) · 12 KB
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import Data.Char (isDigit, isAlpha, isAlphaNum, toUpper)
import Debug.Trace (trace)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import System.IO (hFlush, stdout)
{-
-- Base Grammar --
Expression -> Term Expression'
Expression' -> + Term Expression'
| - Term Expression'
Term -> Factor Term'
Term' -> * Factor Term'
| / Factor Term'
Factor -> - Factor | ( Expression ) | Number
Number -> [0-9] { [0-9] }
data Expr = Number Int
| Var String
| Add Expr Expr
| Sub Expr Expr
| Neg Expr
| Mult Expr Expr
| Div Expr Expr deriving Show
-}
{-
-- Grammar with function expression --
Function -> name ( name {, name } ) = Expression
FunctionExpression -> name ( { Argument {, Argument } } )
Argument -> Expression | FunctionExpression
Expression -> Term Expression'
Expression' -> + Term Expression'
| - Term Expression'
Term -> Factor Term'
Term' -> * Factor Term'
| / Factor Term'
Factor -> - Factor | ( Expression ) | number | name | FunctionExpression
name -> [a-Z] { [a-Z'] }
number -> [0-9] { [0-9] }
-}
-- Extend with functions
data Expr = Number Int
| Var String
| FuncExpr String [Expr]
| Add Expr Expr
| Sub Expr Expr
| Neg Expr
| Mult Expr Expr
| Div Expr Expr deriving Show
data FuncDef = FuncDef String [String] Expr deriving Show
parse :: String -> Expr
parseExpr :: String -> Either String (Expr, String)
parseExprExt :: (Expr, String) -> Either String (Expr, String)
parseTerm :: String -> Either String (Expr, String)
parseTermExt :: (Expr, String) -> Either String (Expr, String)
parseFactor :: String -> Either String (Expr, String)
parseVar :: String -> Either String (Expr, String)
parseNumber :: String -> Either String (Expr, String)
parseExpr [] = Left "Empty expression"
parseExpr (x:xs) = case parseTerm (x:xs) of
Right (lexpr, s@('+':ys)) -> parseExprExt (lexpr, s)
Right (lexpr, s@('-':ys)) -> parseExprExt (lexpr, s)
Right (expr, ys) -> Right (expr, ys)
Left err -> Left err
parseExprExt (lexpr, '+':xs) = case parseTerm xs of
Right (rexpr, []) -> Right (Add lexpr rexpr, [])
Right (rexpr, ys) -> parseExprExt (Add lexpr rexpr, ys)
Left err -> Left err
parseExprExt (lexpr, '-':xs) = case parseTerm xs of
Right (rexpr, []) -> Right (Sub lexpr rexpr, [])
Right (rexpr, ys) -> parseExprExt (Sub lexpr rexpr, ys)
Left err -> Left err
parseExprExt (expr, xs) = Right (expr, xs)
parseTerm xs = case parseFactor xs of
Right (lexpr, s@('*':ys)) -> parseTermExt (lexpr, s)
Right (lexpr, s@('/':ys)) -> parseTermExt (lexpr, s)
Right (expr, ys) -> Right (expr, ys)
Left err -> Left err
--parseTermExt (expr, xs) | (trace $ show xs ++ show expr) False = undefined
parseTermExt (lexpr, ['*']) = Left "Expected expression after (*)"
parseTermExt (lexpr, '*':xs) = case parseFactor xs of
Right (rexpr, []) -> Right (Mult lexpr rexpr, [])
Right (rexpr, ys) -> parseTermExt (Mult lexpr rexpr, ys)
Left err -> Left err
parseTermExt (lexpr, ['/']) = Left "Expected expression after (/)"
parseTermExt (lexpr, '/':xs) = case parseFactor xs of
Right (rexpr, []) -> Right (Div lexpr rexpr, [])
Right (rexpr, ys) -> parseTermExt (Div lexpr rexpr, ys)
Left err -> Left err
parseTermExt (expr, xs) = Right (expr, xs)
parseFactor ('(':xs) = case parseExpr xs of
Right (expr, ')':ys) -> Right (expr, ys)
Left err -> Left err
parseFactor ('-':xs) = case parseFactor xs of
Right (expr, ys) -> Right (Neg expr, ys)
Left err -> Left err
parseFactor s@(x:xs)
| isDigit x = parseNumber s
| otherwise = case parseFuncName s of
Right(name, '(':ys) -> case parseFuncExpr s of
Right (fexpr, zs) -> Right (fexpr, zs)
Left err -> Left err
Right(_, _) -> parseVar s
Left err -> Left err
parseVar [] = Left "Expected a non-empty input for a variable"
parseVar s@(x:xs)
| isAlpha x = Right (Var (fst alphaSpan), snd alphaSpan)
| otherwise = Left ("Expected a variable, instead got " ++ [x] ++ " at " ++ (x:xs))
where alphaSpan = span isAlpha s
parseNumber [] = Left "Expected a non-empty input for a number"
parseNumber s@(x:xs)
| isDigit x = Right (Number (read (fst numSpan) :: Int), snd numSpan)
| otherwise = Left ("Expected a digit, instead got " ++ [x] ++ " at " ++ (x:xs))
where numSpan = span isDigit s
parse xs = case parseExpr (filter (not . (`elem` " \n\r\t")) xs) of
Right (expr, []) -> expr
Right (expr, y:ys) -> error $ "Unparsed text left: " ++ (y:ys)
Left err -> error $ "Error in parsing" ++ err
--
-- Parse function defition, e.g. f(x) = x + 3
--
parseDef :: String -> FuncDef
parseFuncDef :: String -> Either String (FuncDef, String)
parseFuncName :: String -> Either String (String, String)
parseFuncArgs :: String -> Either String ([String], String)
parseFuncArg :: String -> Either String (String, String)
parseFuncArgsExt :: [String] -> String -> Either String ([String], String)
isName c = isAlpha c || c == '\''
parseDef (x:xs) = case parseFuncDef (filter (not . (`elem` " \n\r\t")) (x:xs)) of
Right (f, []) -> f
Right (f, ys) -> error $ "Unparsed text left after function definition: " ++ ys
Left err -> error err
parseFuncDef [] = Left "Expected function definition"
parseFuncDef (x:xs) = case parseFuncName (x:xs) of
Right (name@(y:ys), rest) -> case parseFuncArgs rest of
Right (args, '=':rest) -> case parseExpr rest of
--Right (expr, []) -> Right (FuncDef name args expr, [])
Right (expr, zs) -> Right (FuncDef name args expr, zs)
Left err -> Left err
Right (args, rest) -> Left $ "Expected an equal sign = before " ++ rest
Left err -> Left err
Left err -> Left err
parseFuncName [] = Left "Expected function name"
parseFuncName (x:xs)
| isAlpha x = Right (name, rest)
| otherwise = Left ("First non-alphabetic character " ++ [x] ++ " at " ++ x:xs)
where (name, rest) = span isName (x:xs)
parseFuncArgs ('(':')':xs) = Right ([], xs)
parseFuncArgs ('(':xs) = case parseFuncArg xs of
Right (arg, ',':ys) -> parseFuncArgsExt [arg] ys
Right (arg, ')':ys) -> Right ([arg], ys)
Right (arg, ys) -> Left $ "Expected closing paranthesis ) at " ++ ys
Left err -> Left err
parseFuncArg [] = Left "Expected an argument"
parseFuncArg (x:xs)
| isAlpha x = Right (arg, rest)
| otherwise = Left $ "Expected alphabet character for an argument at " ++ (x:xs)
where (arg, rest) = span isName (x:xs)
parseFuncArgsExt args s = case parseFuncArg s of
Right (arg, ',':ys) -> parseFuncArgsExt (args ++ [arg]) ys
Right (arg, ')':ys) -> Right (args ++ [arg], ys)
--Right (arg, []) -> Left "Expected argument termination either , or )"
Left err -> Left err
--
-- Parse function expression, e.g. f(a + 3)
--
parseFuncExpr :: String -> Either String (Expr, String)
parseFuncExpr [] = Left "Expected function expression"
parseFuncExpr (x:xs) = case parseFuncName (x:xs) of
Right (name, '(':ys) -> case parseFuncExprArgs ys of
Right (argExprs, ')':zs) -> Right (FuncExpr name argExprs, zs)
Right (argExprs, zs) -> Left $ "Expected argument termination with ) at " ++ zs
Left err -> Left err
Right (_, ys) -> Left $ "Expected argument beginning with ( at " ++ ys
Left err -> Left err
parseFuncExprArgs :: String -> Either String ([Expr], String)
parseFuncExprArgs s@(')':_) = Right ([], s)
parseFuncExprArgs (x:xs) = case parseFuncExprArg (x:xs) of
Right (argExpr, ',':ys) -> parseFuncExprArgsExt [argExpr] ys
Right (argExpr, ys) -> Right ([argExpr], ys)
Left err -> Left err
parseFuncExprArgs xs = Left $ "Expected argument list within () at " ++ xs
parseFuncExprArg :: String -> Either String (Expr, String)
parseFuncExprArg (x:xs)
| isAlpha x = case parseFuncName (x:xs) of
Right (name, '(':ys) -> case parseFuncExpr (x:xs) of
Right (fexpr, zs) -> Right (fexpr, zs)
Left err -> Left err
Right (name, ys) -> case parseExpr (x:xs) of
Right (expr, zs) -> Right (expr, zs)
Left err -> Left err
Left err -> Left err
| otherwise = case parseExpr (x:xs) of
Right (expr, ys) -> Right (expr, ys)
Left err -> Left err
parseFuncExprArgsExt :: [Expr] -> String -> Either String ([Expr], String)
parseFuncExprArgsExt args (x:xs) = case parseFuncExprArg (x:xs) of
Right (expr, ',':ys) -> parseFuncExprArgsExt (args ++ [expr]) ys
Right (expr, s@(')':ys)) -> Right (args ++ [expr], s)
Right (expr, s) -> Left $ "Expected argument termination with ) or , at " ++ s
Left err -> Left err
--
-- Evaluate the arithmetic expression
--
evalExpr :: Map.Map String FuncDef -> Map.Map String Int -> Expr -> Int
evalExpr fmap varMap (Add lexpr rexpr) = evalExpr fmap varMap lexpr + evalExpr fmap varMap rexpr
evalExpr fmap varMap (Sub lexpr rexpr) = evalExpr fmap varMap lexpr - evalExpr fmap varMap rexpr
evalExpr fmap varMap (Mult lexpr rexpr) = evalExpr fmap varMap lexpr * evalExpr fmap varMap rexpr
evalExpr fmap varMap (Div lexpr rexpr) = evalExpr fmap varMap lexpr `div` evalExpr fmap varMap rexpr
evalExpr fmap varMap (Neg expr) = negate (evalExpr fmap varMap expr)
evalExpr fmap varMap (Number x) = x
evalExpr fmap varMap (Var s) = fromMaybe (error $ "Key " ++ s ++ " not found") (Map.lookup s varMap)
evalExpr fmap varMap (FuncExpr name argExprs) = evalFuncExpr fmap varMap fdef argExprs
where fdef = fromMaybe (error $ "Function " ++ name ++ " is not defined") (Map.lookup name fmap)
evalFuncExpr :: Map.Map String FuncDef -> Map.Map String Int -> FuncDef -> [Expr] -> Int
evalFuncExpr fmap varMap (FuncDef name argNames expr) argExprs =
evalExpr fmap (addArgs varMap argNames evaledArgs) expr
where evaledArgs = map (evalExpr fmap varMap) argExprs
addArgs :: Map.Map String Int -> [String] -> [Int] -> Map.Map String Int
addArgs map [] [] = map
addArgs map (arg:argNames) (evaled:evaledArgs) = addArgs (Map.insert arg evaled map) argNames evaledArgs
addArgs _ _ _ = error "Unequal number of arguments"
--
-- Helper functions
--
parseExprList :: [String] -> [Expr]
parseExprList = map parse
parseDefList :: [String] -> [FuncDef]
parseDefList = map parseDef
mapDefinitions :: [FuncDef] -> Map.Map String FuncDef -> Map.Map String FuncDef
mapDefinitions [] fmap = fmap
mapDefinitions (f@(FuncDef name args expr): fs) fmap = mapDefinitions fs (Map.insert name f fmap)
--
-- Examples
--
p0 = map parseExpr ["f(3)", "g(f(5))", "f()", "e(0+r(20+3*m(5-3)))"]
-- Examples --
add3 = case parseFuncDef "f(x)=x+3" of
Right (f, []) -> f
Left err -> error err
callAdd3 = case parseExpr "f(10)" of
Right (FuncExpr _ args, []) -> evalFuncExpr Map.empty Map.empty add3 args
Left err -> error err
add100 = evalExpr (mapDefinitions baseFs Map.empty) Map.empty $ parse "add(add(3*5, 0), 100)"
baseFunctions = [
"add(a, b) = a + b",
"sub(a, b) = a - b",
"mult(a, b) = a * b",
"div(a, b) = a / b",
"double(a) = a * a"
]
callExprs = [
"add(add(3,5), 5)",
"double(3) - 3",
"mult(add(2, 1), double(2))"
]
--
-- Main interface start with base functions
--
baseFs = parseDefList baseFunctions
evalCallExprs = map (evalExpr (mapDefinitions baseFs Map.empty) Map.empty) parsedExprs == [13, 6, 12]
where parsedExprs = parseExprList callExprs
isFuncDef s = case parseFuncDef (filter (not . (`elem` " \n\r\t")) s) of
Right _ -> True
Left err -> False
main = do
let fmap = mapDefinitions baseFs Map.empty
let varMap = Map.empty
print "Enter expression, e.g. 3+3 or f(a)=a+2 or f(3)"
putStrLn "Press Ctrl-C to quit"
handler fmap varMap
handler fmap varMap = do
putStr "\955> "
hFlush stdout
text <- getLine
if isFuncDef text
then handleFuncDefs fmap varMap text
else handleExprs fmap varMap text
handleFuncDefs fmap varMap text = do
let parsedDefText = parseDef text
case parsedDefText of
f@(FuncDef name _ _) -> handler (Map.insert name f fmap) varMap
handleExprs fmap varMap text = do
let parsedText = parse text
print $ evalExpr fmap varMap parsedText
handler fmap varMap