-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest6.hs
248 lines (210 loc) · 6.18 KB
/
test6.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
import Data.Maybe
import Data.List
data RE = Null |
Term Char |
Seq RE RE |
Alt RE RE |
Rep RE |
Plus RE |
Opt RE
deriving (Eq, Show)
type State = Int
data Label = C Char | Eps
deriving (Eq, Ord, Show)
type Transition = (State, State, Label)
type Automaton = (State, [State], [Transition])
--------------------------------------------------------
-- showRE - this may be useful for testing
showRE :: RE -> String
showRE (Seq re re')
= showRE re ++ showRE re'
showRE (Alt re re')
= "(" ++ showRE re ++ "|" ++ showRE re' ++ ")"
showRE (Rep re)
= showRE' re ++ "*"
showRE (Plus re)
= showRE' re ++ "+"
showRE (Opt re)
= showRE' re ++ "?"
showRE re
= showRE' re
showRE' Null
= ""
showRE' (Term c)
= [c]
showRE' (Alt re re')
= showRE (Alt re re')
showRE' re
= "(" ++ showRE re ++ ")"
--------------------------------------------------------
-- Part I
lookUp :: Eq a => a -> [(a, b)] -> b
--Pre: There is exactly one occurrence of the item being looked up.
lookUp x xs
= fromJust (lookup x xs)
simplify :: RE -> RE
simplify (Plus re)
= Seq re (Rep re)
simplify (Opt re)
= Alt re Null
simplify Null
= Null
simplify (Seq re re')
= Seq (simplify re) (simplify re')
simplify (Alt re re')
= Alt (simplify re) (simplify re')
simplify (Rep re)
= Rep (simplify re)
simplify p = p
--------------------------------------------------------
-- Part II
startState :: Automaton -> State
startState (s, _, _)
= s
terminalStates :: Automaton -> [State]
terminalStates (_, ss, _)
= ss
transitions :: Automaton -> [Transition]
transitions (_, _, ts)
= ts
isTerminal :: State -> Automaton -> Bool
isTerminal s a
= elem s (terminalStates a)
transitionsFrom :: State -> Automaton -> [Transition]
transitionsFrom s a
= filter (\(s', _, _) -> s == s') (transitions a)
labels :: [Transition] -> [Label]
labels ts
= nub (map (extractLabel) filteredList)
where
extractLabel (_, _, C x) = C x
filteredList = filter (\(_, _, l) -> l /= Eps) ts
accepts :: Automaton -> String -> Bool
accepts a s
= accepts' (startState a) s
where
accepts' state s'
| (null s') && (isTerminal state a) = True
| otherwise = or (map try remTrans)
where
remTrans = transitionsFrom state a
try (_, to, Eps) = accepts' to s'
try (_, to, C c)
| null s' = False
| c == h = accepts' to t
| otherwise = False
where
(h : t) = s'
--------------------------------------------------------
-- Part III
makeNDA :: RE -> Automaton
makeNDA re
= (1, [2], sort transitions)
where
(transitions, k) = make (simplify re) 1 2 3
make :: RE -> Int -> Int -> Int -> ([Transition], Int)
make Null m n k
= ([(m,n, Eps)], k)
make (Term c) m n k
= ([(m, n, (C c))], k)
make (Seq r r') m n k
= ((k, k+1, Eps) : (trans ++ trans'), nextK')
where
(trans, nextK) = make r m k (k+2)
(trans', nextK')= make r' (k+1) n (nextK)
make (Alt r r') m n k
= (ts, nextK')
where
(trans, nextK)
= make r k (k+1) (k+4)
(trans', nextK')
= make r' (k+2) (k+3) nextK
ts
= (m, k, Eps) : (m, k+2, Eps) : (k+1, n, Eps) : (k+3, n, Eps) : (trans ++ trans')
make (Rep r) m n k
= (ts, nextK)
where
(trans, nextK)
= make r k (k+1) (k+2)
ts
= (m, k, Eps) : (m, n, Eps) : (k+1, n, Eps) : (k+1, k, Eps) : (trans)
--------------------------------------------------------
-- Part IV
type MetaState = [State]
type MetaTransition = (MetaState, MetaState, Label)
getFrontier :: State -> Automaton -> [Transition]
getFrontier
= undefined
groupTransitions :: [Transition] -> [(Label, [State])]
groupTransitions
= undefined
makeDA :: Automaton -> Automaton
-- Pre: Any cycle in the NDA must include at least one non-Eps transition
makeDA
= undefined
--------------------------------------------------------
-- Test cases
reFigure, re1, re2, re3, re4, re5 :: RE
reFigure
= Seq (Rep (Alt (Term 'a') (Term 'b'))) (Term 'c')
re1
= Seq (Alt (Term 'x') (Term 'y')) (Alt (Term '1') (Term '2'))
re2
= Seq (Term 'x') (Rep (Term '\''))
re3
= Rep (Alt (Seq (Term 'a') (Term 'b')) (Term 'c'))
re4
= Seq (Alt (Term 'a') Null) (Term 'a')
re5
= Seq (Opt (Seq (Term 'a') (Term 'b'))) (Plus (Term 'd'))
nd, nd' :: Automaton
nd = (1,[4],[(1,2,C 'a'),(1,3,C 'b'),(2,3,Eps),(2,4,C 'c')])
nd' = (1,[4],[(1,2,Eps),(1,3,C 'a'),(2,4,C 'a'),(2,4,C 'b'),
(3,4,C 'b'),(3,4,Eps)])
da :: Automaton
da = (0,[3],[(0,1,C 'a'),(0,2,C 'a'),(0,2,C 'b'),(1,2,C 'a'),
(1,3,C 'b'),(2,2,C 'a'),(2,1,C 'a'),(2,3,C 'b')])
re :: RE
re = Seq (Alt (Term 'a') (Term 'b')) (Seq (Rep (Term 'a')) (Term 'b'))
ndaFigure, nda1, nda2, nda3, nda4, nda5 :: Automaton
daFigure, da1, da2, da3, da4, da5 :: Automaton
ndaFigure
= (1,[2],[(1,3,Eps),(1,5,Eps),(3,4,Eps),(4,2,C 'c'),(5,7,Eps),
(5,9,Eps),(6,3,Eps),(6,5,Eps),(7,8,C 'a'),(8,6,Eps),
(9,10,C 'b'),(10,6,Eps)])
daFigure
= (1,[2],
[(1,1,C 'a'),(1,1,C 'b'),(1,2,C 'c')])
nda1
= (1,[2],[(1,5,Eps),(1,7,Eps),(3,4,Eps),(4,9,Eps),(4,11,Eps),
(5,6,C 'x'),(6,3,Eps),(7,8,C 'y'),(8,3,Eps),(9,10,C '1'),
(10,2,Eps),(11,12,C '2'),(12,2,Eps)])
da1
= (1,[3],
[(1,2,C 'x'),(1,2,C 'y'),(2,3,C '1'),(2,3,C '2')])
nda2
= (1,[2],[(1,3,C 'x'),(3,4,Eps),(4,2,Eps),(4,5,Eps),(5,6,C '\''),
(6,2,Eps),(6,5,Eps)])
da2
= (1,[2],
[(1,2,C 'x'),(2,2,C '\'')])
nda3
= (1,[2],[(1,2,Eps),(1,3,Eps),(3,5,Eps),(3,7,Eps),(4,2,Eps),
(4,3,Eps), (5,9,C 'a'),(6,4,Eps),(7,8,C 'c'),(8,4,Eps),
(9,10,Eps),(10,6,C 'b')])
da3
= (1,[1],
[(1,1,C 'c'),(1,2,C 'a'),(2,1,C 'b')])
nda4
= (1,[2],[(1,5,Eps),(1,7,Eps),(3,4,Eps),(4,2,C 'a'),(5,6,C 'a'),
(6,3,Eps),(7,8,Eps),(8,3,Eps)])
da4
= (1,[2,3],[(1,2,C 'a'),(2,3,C 'a')])
nda5
= (1,[2],[(1,5,Eps),(1,7,Eps),(3,4,Eps),(4,11,C 'd'),(5,9,C 'a'),
(6,3,Eps),(7,8,Eps),(8,3,Eps),(9,10,Eps),(10,6,C 'b'),
(11,12,Eps),(12,2,Eps),(12,13,Eps),(13,14,C 'd'),
(14,2,Eps),(14,13,Eps)])
da5
= (1,[2],[(1,2,C 'd'),(1,3,C 'a'),(2,2,C 'd'),(3,4,C 'b'),
(4,2,C 'd')])