1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators fry kernel locals
4 math math.order regexp.nfa regexp.transition-tables sequences
5 sets sorting vectors regexp.utils sequences.deep ;
6 USING: io prettyprint threads ;
9 : find-delta ( states transition regexp -- new-states )
10 nfa-table>> transitions>>
11 rot [ swap at at ] with with gather sift ;
13 : (find-epsilon-closure) ( states regexp -- new-states )
16 : find-epsilon-closure ( states regexp -- new-states )
17 '[ dup _ (find-epsilon-closure) union ] [ length ] while-changes
20 : find-closure ( states transition regexp -- new-states )
21 [ find-delta ] 2keep nip find-epsilon-closure ;
23 : find-start-state ( regexp -- state )
24 [ nfa-table>> start-state>> 1vector ] keep find-epsilon-closure ;
26 : find-transitions ( seq1 regexp -- seq2 )
27 nfa-table>> transitions>>
28 [ at keys ] curry gather
31 : add-todo-state ( state regexp -- )
32 2dup visited-states>> key? [
35 [ visited-states>> conjoin ]
36 [ new-states>> push ] 2bi
39 : new-transitions ( regexp -- )
43 dupd pop dup pick find-transitions rot
45 [ [ find-closure ] 2keep nip dupd add-todo-state ] 3keep
46 [ swapd transition make-transition ] dip
47 dfa-table>> add-transition
52 : states ( hashtable -- array )
54 [ values [ values concat ] map concat append ] bi ;
56 : set-final-states ( regexp -- )
58 [ nfa-table>> final-states>> keys ]
59 [ dfa-table>> transitions>> states ] bi
60 [ intersects? ] with filter
62 swap dfa-table>> final-states>>
63 [ conjoin ] curry each ;
65 : set-initial-state ( regexp -- )
67 [ dfa-table>> ] [ find-start-state ] bi
68 [ >>start-state drop ] keep
69 1vector >>new-states drop ;
71 : set-traversal-flags ( regexp -- )
73 [ nfa-traversal-flags>> ]
74 [ dfa-table>> transitions>> keys ] bi
75 [ tuck [ swap at ] with map concat ] with H{ } map>assoc
76 >>dfa-traversal-flags drop ;
78 : construct-dfa ( regexp -- )
83 [ set-traversal-flags ]