git @ Cat's Eye Technologies The-Dipple / 4133c1b
Add way simple implementation of an NFA. Will likely refine this. Chris Pressey 13 years ago
1 changed file(s) with 40 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 module NFA where
1
2 import Char
3
4 type State = Integer
5
6 data Transition = Transition State Char State
7 deriving (Show, Ord, Eq)
8
9 type NFA = [Transition]
10
11
12 transitionOnce :: NFA -> [State] -> Char -> [State]
13
14 transitionOnce nfa states input =
15 let
16 test (Transition here x there) =
17 x == input && elem here states
18 extract (Transition here x there) =
19 there
20 in
21 map (extract) $ filter (test) nfa
22
23
24 transitionMany :: NFA -> [State] -> String -> [State]
25
26 transitionMany nfa states [] =
27 states
28 transitionMany nfa states (input:rest) =
29 transitionMany nfa (transitionOnce nfa states input) rest
30
31
32 egNFA = [
33 Transition 1 'a' 2,
34 Transition 1 'a' 3,
35 Transition 1 'b' 4,
36 Transition 2 'b' 1,
37 Transition 3 'b' 2,
38 Transition 4 'c' 1
39 ]