-
Notifications
You must be signed in to change notification settings - Fork 16
/
ex8.hs
198 lines (166 loc) · 3.46 KB
/
ex8.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
import Control.Applicative
import Data.Char
newtype Parser a =
P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) inp = p inp
item :: Parser Char
item =
P (\inp ->
case inp of
[] -> []
(x:xs) -> [(x, xs)])
instance Functor Parser
-- fmap :: (a -> b) -> Parser a -> Parser b
where
fmap g p =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
instance Applicative Parser
-- pure :: a -> Parser a
where
pure v = P (\inp -> [(v, inp)])
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
pg <*> px =
P
(\inp ->
case parse pg inp of
[] -> []
[(g, out)] -> parse (fmap g px) out)
instance Monad Parser
-- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
where
p >>= f =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> parse (f v) out)
instance Alternative Parser
-- empty :: Parser a
where
empty = P (\inp -> [])
-- (<|>) :: Parser a -> Parser a -> Parser a
p <|> q =
P
(\inp ->
case parse p inp of
[] -> parse q inp
[(v, out)] -> [(v, out)])
sat :: (Char -> Bool) -> Parser Char
sat p = do
x <- item
if p x
then return x
else empty
digit :: Parser Char
digit = sat isDigit
lower :: Parser Char
lower = sat isLower
alphanum :: Parser Char
alphanum = sat isAlphaNum
char :: Char -> Parser Char
char x = sat (== x)
string :: String -> Parser String
string [] = return []
string (x:xs) = do
char x
string xs
return (x : xs)
ident :: Parser String
ident = do
x <- lower
xs <- many alphanum
return (x : xs)
nat :: Parser Int
nat = do
xs <- some digit
return (read xs)
space :: Parser ()
space = do
many (sat isSpace)
return ()
int :: Parser Int
int =
do char '-'
n <- nat
return (-n)
<|> nat
token :: Parser a -> Parser a
token p = do
space
v <- p
space
return v
identifier :: Parser String
identifier = token ident
natural :: Parser Int
natural = token nat
integer :: Parser Int
integer = token int
symbol :: String -> Parser String
symbol xs = token (string xs)
-- a. grammar
-- expr ::= (expr + | expr - | e) term
-- b. implementation
-- c. problem: cannot determine where expr ends
-- thus stuck in an infinite loop
expr' :: Parser Int
expr' = pre <*> term
where
pre = do
e <- expr'
symbol "+"
return (e+)
<|> do
e <- expr'
symbol "-"
return (e-)
<|> empty
-- d. fixed expr
exprop :: Parser (Int, Char)
exprop = do
t <- term
do
symbol "+"
return (t, '+')
<|> do
symbol "-"
return (t, '-')
expr :: Parser Int
expr = do
es <- many exprop
t <- term
return (fst (foldl1 op (es ++ [(t, ' ')])))
where
op (vl, sl) (vr, sr) =
case sl of
'+' -> (vl + vr, sr)
'-' -> (vl - vr, sr)
term :: Parser Int
term = do
f <- factor
do symbol "*"
t <- term
return (f * t)
<|> do
symbol "/"
t <- term
return (f `div ` t)
<|> return f
factor :: Parser Int
factor = do
symbol "("
e <- expr
symbol ")"
return e
<|> integer
eval :: String -> Int
eval xs =
case parse expr xs of
[(n, [])] -> n
[(_, out)] -> error ("Unused input " ++ out)
[] -> error "Invalid input"