You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
340 lines
15 KiB
Racket
340 lines
15 KiB
Racket
21 years ago
|
(module deriv mzscheme
|
||
|
|
||
17 years ago
|
(require mzlib/list
|
||
|
(prefix is: mzlib/integer-set)
|
||
14 years ago
|
"re.rkt"
|
||
|
"util.rkt")
|
||
21 years ago
|
|
||
|
(provide build-dfa print-dfa (struct dfa (num-states start-state final-states/actions transitions)))
|
||
|
|
||
|
(define e (build-epsilon))
|
||
|
(define z (build-zero))
|
||
|
|
||
21 years ago
|
|
||
|
;; Don't do anything with this one but extract the chars
|
||
21 years ago
|
(define all-chars (->re `(char-complement (union)) (make-cache)))
|
||
21 years ago
|
|
||
|
;; get-char-groups : re bool -> (list-of char-setR?)
|
||
21 years ago
|
;; Collects the char-setRs in r that could be used in
|
||
|
;; taking the derivative of r.
|
||
21 years ago
|
(define (get-char-groups r found-negation)
|
||
21 years ago
|
(cond
|
||
|
((or (eq? r e) (eq? r z)) null)
|
||
|
((char-setR? r) (list r))
|
||
|
((concatR? r)
|
||
|
(if (re-nullable? (concatR-re1 r))
|
||
21 years ago
|
(append (get-char-groups (concatR-re1 r) found-negation)
|
||
|
(get-char-groups (concatR-re2 r) found-negation))
|
||
|
(get-char-groups (concatR-re1 r) found-negation)))
|
||
21 years ago
|
((repeatR? r)
|
||
21 years ago
|
(get-char-groups (repeatR-re r) found-negation))
|
||
21 years ago
|
((orR? r)
|
||
21 years ago
|
(apply append (map (lambda (x) (get-char-groups x found-negation)) (orR-res r))))
|
||
|
((andR? r)
|
||
|
(apply append (map (lambda (x) (get-char-groups x found-negation)) (andR-res r))))
|
||
|
((negR? r)
|
||
|
(if found-negation
|
||
|
(get-char-groups (negR-re r) #t)
|
||
|
(cons all-chars (get-char-groups (negR-re r) #t))))))
|
||
21 years ago
|
|
||
|
(test-block ((c (make-cache))
|
||
|
(r1 (->re #\1 c))
|
||
|
(r2 (->re #\2 c)))
|
||
21 years ago
|
((get-char-groups e #f) null)
|
||
|
((get-char-groups z #f) null)
|
||
|
((get-char-groups r1 #f) (list r1))
|
||
21 years ago
|
((get-char-groups (->re `(concatenation ,r1 ,r2) c) #f)
|
||
21 years ago
|
(list r1))
|
||
21 years ago
|
((get-char-groups (->re `(concatenation ,e ,r2) c) #f)
|
||
21 years ago
|
(list r2))
|
||
19 years ago
|
((get-char-groups (->re `(concatenation (repetition 0 +inf.0 ,r1) ,r2) c) #f)
|
||
21 years ago
|
(list r1 r2))
|
||
19 years ago
|
((get-char-groups (->re `(repetition 0 +inf.0 ,r1) c) #f)
|
||
21 years ago
|
(list r1))
|
||
21 years ago
|
((get-char-groups
|
||
19 years ago
|
(->re `(union (repetition 0 +inf.0 ,r1)
|
||
|
(concatenation (repetition 0 +inf.0 ,r2) "3") "4") c) #f)
|
||
21 years ago
|
(list r1 r2 (->re "3" c) (->re "4" c)))
|
||
21 years ago
|
((get-char-groups (->re `(complement ,r1) c) #f)
|
||
21 years ago
|
(list all-chars r1))
|
||
21 years ago
|
((get-char-groups
|
||
19 years ago
|
(->re `(intersection (repetition 0 +inf.0 ,r1)
|
||
|
(concatenation (repetition 0 +inf.0 ,r2) "3") "4") c) #f)
|
||
21 years ago
|
(list r1 r2 (->re "3" c) (->re "4" c)))
|
||
|
)
|
||
21 years ago
|
(define loc:member? is:member?)
|
||
21 years ago
|
|
||
21 years ago
|
;; deriveR : re char cache -> re
|
||
21 years ago
|
(define (deriveR r c cache)
|
||
|
(cond
|
||
|
((or (eq? r e) (eq? r z)) z)
|
||
|
((char-setR? r)
|
||
21 years ago
|
(if (loc:member? c (char-setR-chars r)) e z))
|
||
21 years ago
|
((concatR? r)
|
||
|
(let* ((r1 (concatR-re1 r))
|
||
|
(r2 (concatR-re2 r))
|
||
|
(d (build-concat (deriveR r1 c cache) r2 cache)))
|
||
|
(if (re-nullable? r1)
|
||
|
(build-or (list d (deriveR r2 c cache)) cache)
|
||
|
d)))
|
||
|
((repeatR? r)
|
||
21 years ago
|
(build-concat (deriveR (repeatR-re r) c cache)
|
||
|
(build-repeat (sub1 (repeatR-low r))
|
||
|
(sub1 (repeatR-high r))
|
||
|
(repeatR-re r) cache)
|
||
|
cache))
|
||
21 years ago
|
((orR? r)
|
||
|
(build-or (map (lambda (x) (deriveR x c cache))
|
||
|
(orR-res r))
|
||
|
cache))
|
||
|
((andR? r)
|
||
|
(build-and (map (lambda (x) (deriveR x c cache))
|
||
|
(andR-res r))
|
||
|
cache))
|
||
|
((negR? r)
|
||
|
(build-neg (deriveR (negR-re r) c cache) cache))))
|
||
|
|
||
|
(test-block ((c (make-cache))
|
||
21 years ago
|
(a (char->integer #\a))
|
||
|
(b (char->integer #\b))
|
||
21 years ago
|
(r1 (->re #\a c))
|
||
19 years ago
|
(r2 (->re `(repetition 0 +inf.0 #\a) c))
|
||
|
(r3 (->re `(repetition 0 +inf.0 ,r2) c))
|
||
21 years ago
|
(r4 (->re `(concatenation #\a ,r2) c))
|
||
19 years ago
|
(r5 (->re `(repetition 0 +inf.0 ,r4) c))
|
||
21 years ago
|
(r6 (->re `(union ,r5 #\a) c))
|
||
|
(r7 (->re `(concatenation ,r2 ,r2) c))
|
||
|
(r8 (->re `(complement ,r4) c))
|
||
|
(r9 (->re `(intersection ,r2 ,r4) c)))
|
||
21 years ago
|
((deriveR e a c) z)
|
||
|
((deriveR z a c) z)
|
||
|
((deriveR r1 b c) z)
|
||
|
((deriveR r1 a c) e)
|
||
|
((deriveR r2 a c) r2)
|
||
|
((deriveR r2 b c) z)
|
||
|
((deriveR r3 a c) r2)
|
||
|
((deriveR r3 b c) z)
|
||
|
((deriveR r4 a c) r2)
|
||
|
((deriveR r4 b c) z)
|
||
21 years ago
|
((deriveR r5 a c) (->re `(concatenation ,r2 ,r5) c))
|
||
21 years ago
|
((deriveR r5 b c) z)
|
||
21 years ago
|
((deriveR r6 a c) (->re `(union (concatenation ,r2 ,r5) "") c))
|
||
21 years ago
|
((deriveR r6 b c) z)
|
||
21 years ago
|
((deriveR r7 a c) (->re `(union (concatenation ,r2 ,r2) ,r2) c))
|
||
21 years ago
|
((deriveR r7 b c) z)
|
||
21 years ago
|
((deriveR r8 a c) (->re `(complement, r2) c))
|
||
|
((deriveR r8 b c) (->re `(complement ,z) c))
|
||
21 years ago
|
((deriveR r9 a c) r2)
|
||
21 years ago
|
((deriveR r9 b c) z)
|
||
19 years ago
|
((deriveR (->re `(repetition 1 2 "ab") c) a c)
|
||
|
(->re `(concatenation "b" (repetition 0 1 "ab")) c)))
|
||
21 years ago
|
|
||
|
;; An re-action is (cons re action)
|
||
|
|
||
|
;; derive : (list-of re-action) char cache -> (union (list-of re-action) #f)
|
||
|
;; applies deriveR to all the re-actions's re parts.
|
||
|
;; Returns #f if the derived state is equivalent to z.
|
||
|
(define (derive r c cache)
|
||
|
(let ((new-r (map (lambda (ra)
|
||
|
(cons (deriveR (car ra) c cache) (cdr ra)))
|
||
|
r)))
|
||
|
(if (andmap (lambda (x) (eq? z (car x)))
|
||
|
new-r)
|
||
|
#f
|
||
|
new-r)))
|
||
|
|
||
|
(test-block ((c (make-cache))
|
||
|
(r1 (->re #\1 c))
|
||
|
(r2 (->re #\2 c)))
|
||
21 years ago
|
((derive null (char->integer #\1) c) #f)
|
||
|
((derive (list (cons r1 1) (cons r2 2)) (char->integer #\1) c)
|
||
21 years ago
|
(list (cons e 1) (cons z 2)))
|
||
21 years ago
|
((derive (list (cons r1 1) (cons r2 2)) (char->integer #\3) c) #f))
|
||
21 years ago
|
|
||
|
|
||
|
;; get-final : (list-of re-action) -> (union #f syntax-object)
|
||
|
;; An re that accepts e represents a final state. Return the
|
||
|
;; action from the first final state or #f if there is none.
|
||
|
(define (get-final res)
|
||
|
(cond
|
||
|
((null? res) #f)
|
||
|
((re-nullable? (caar res)) (cdar res))
|
||
|
(else (get-final (cdr res)))))
|
||
20 years ago
|
|
||
21 years ago
|
(test-block ((c->i char->integer)
|
||
|
(c (make-cache))
|
||
21 years ago
|
(r1 (->re #\a c))
|
||
|
(r2 (->re #\b c))
|
||
21 years ago
|
(b (list (cons z 1) (cons z 2) (cons z 3) (cons e 4) (cons z 5)))
|
||
21 years ago
|
(a (list (cons r1 1) (cons r2 2))))
|
||
21 years ago
|
((derive null (c->i #\a) c) #f)
|
||
|
((derive a (c->i #\a) c) (list (cons e 1) (cons z 2)))
|
||
|
((derive a (c->i #\b) c) (list (cons z 1) (cons e 2)))
|
||
|
((derive a (c->i #\c) c) #f)
|
||
21 years ago
|
((derive (list (cons (->re `(union " " "\n" ",") c) 1)
|
||
19 years ago
|
(cons (->re `(concatenation (repetition 0 1 "-")
|
||
|
(repetition 1 +inf.0 (char-range "0" "9"))) c) 2)
|
||
|
(cons (->re `(concatenation "-" (repetition 1 +inf.0 "-")) c) 3)
|
||
21 years ago
|
(cons (->re "[" c) 4)
|
||
21 years ago
|
(cons (->re "]" c) 5)) (c->i #\[) c)
|
||
21 years ago
|
b)
|
||
21 years ago
|
((get-final a) #f)
|
||
21 years ago
|
((get-final (list (cons e 1) (cons e 2))) 1)
|
||
|
((get-final b) 4))
|
||
21 years ago
|
|
||
|
|
||
|
;; A state is (make-state (list-of re-action) nat)
|
||
|
(define-struct state (spec index))
|
||
|
|
||
|
;; get->key : re-action -> (list-of nat)
|
||
|
;; states are indexed by the list of indexes of their res
|
||
|
(define (get-key s)
|
||
|
(map (lambda (x) (re-index (car x))) s))
|
||
|
|
||
21 years ago
|
(define loc:partition is:partition)
|
||
|
|
||
21 years ago
|
;; compute-chars : (list-of state) -> (list-of char-set)
|
||
|
;; Computed the sets of equivalent characters for taking the
|
||
|
;; derivative of the car of st. Only one derivative per set need to be taken.
|
||
|
(define (compute-chars st)
|
||
|
(cond
|
||
|
((null? st) null)
|
||
|
(else
|
||
21 years ago
|
(loc:partition (map char-setR-chars
|
||
21 years ago
|
(apply append (map (lambda (x) (get-char-groups (car x) #f))
|
||
|
(state-spec (car st)))))))))
|
||
21 years ago
|
|
||
|
(test-block ((c (make-cache))
|
||
21 years ago
|
(c->i char->integer)
|
||
21 years ago
|
(r1 (->re `(char-range #\1 #\4) c))
|
||
|
(r2 (->re `(char-range #\2 #\3) c)))
|
||
21 years ago
|
((compute-chars null) null)
|
||
|
((compute-chars (list (make-state null 1))) null)
|
||
21 years ago
|
((map is:integer-set-contents
|
||
|
(compute-chars (list (make-state (list (cons r1 1) (cons r2 2)) 2))))
|
||
21 years ago
|
(list (is:integer-set-contents (is:make-range (c->i #\2) (c->i #\3)))
|
||
|
(is:integer-set-contents (is:union (is:make-range (c->i #\1))
|
||
|
(is:make-range (c->i #\4)))))))
|
||
21 years ago
|
|
||
|
|
||
|
;; A dfa is (make-dfa int int
|
||
|
;; (list-of (cons int syntax-object))
|
||
21 years ago
|
;; (list-of (cons int (list-of (cons char-set int)))))
|
||
21 years ago
|
;; Each transitions is a state and a list of chars with the state to transition to.
|
||
|
;; The finals and transitions are sorted by state number, and duplicate free.
|
||
21 years ago
|
(define-struct dfa (num-states start-state final-states/actions transitions) (make-inspector))
|
||
21 years ago
|
|
||
21 years ago
|
(define loc:get-integer is:get-integer)
|
||
|
|
||
21 years ago
|
;; build-dfa : (list-of re-action) cache -> dfa
|
||
|
(define (build-dfa rs cache)
|
||
|
(let* ((transitions (make-hash-table))
|
||
|
(get-state-number (make-counter))
|
||
|
(start (make-state rs (get-state-number))))
|
||
|
(cache (cons 'state (get-key rs)) (lambda () start))
|
||
|
(let loop ((old-states (list start))
|
||
|
(new-states null)
|
||
21 years ago
|
(all-states (list start))
|
||
21 years ago
|
(cs (compute-chars (list start))))
|
||
|
(cond
|
||
|
((and (null? old-states) (null? new-states))
|
||
|
(make-dfa (get-state-number) (state-index start)
|
||
19 years ago
|
(sort (filter (lambda (x) (cdr x))
|
||
|
(map (lambda (state)
|
||
|
(cons (state-index state) (get-final (state-spec state))))
|
||
|
all-states))
|
||
|
(lambda (a b) (< (car a) (car b))))
|
||
|
(sort (hash-table-map transitions
|
||
|
(lambda (state trans)
|
||
|
(cons (state-index state)
|
||
|
(map (lambda (t)
|
||
|
(cons (car t)
|
||
|
(state-index (cdr t))))
|
||
|
trans))))
|
||
|
(lambda (a b) (< (car a) (car b))))))
|
||
21 years ago
|
((null? old-states)
|
||
21 years ago
|
(loop new-states null all-states (compute-chars new-states)))
|
||
21 years ago
|
((null? cs)
|
||
21 years ago
|
(loop (cdr old-states) new-states all-states (compute-chars (cdr old-states))))
|
||
21 years ago
|
(else
|
||
|
(let* ((state (car old-states))
|
||
|
(c (car cs))
|
||
21 years ago
|
(new-re (derive (state-spec state) (loc:get-integer c) cache)))
|
||
21 years ago
|
(cond
|
||
|
(new-re
|
||
|
(let* ((new-state? #f)
|
||
|
(new-state (cache (cons 'state (get-key new-re))
|
||
|
(lambda ()
|
||
|
(set! new-state? #t)
|
||
21 years ago
|
(make-state new-re (get-state-number)))))
|
||
|
(new-all-states (if new-state? (cons new-state all-states) all-states)))
|
||
21 years ago
|
(hash-table-put! transitions
|
||
|
state
|
||
|
(cons (cons c new-state)
|
||
|
(hash-table-get transitions state
|
||
|
(lambda () null))))
|
||
|
(cond
|
||
|
(new-state?
|
||
21 years ago
|
(loop old-states (cons new-state new-states) new-all-states (cdr cs)))
|
||
21 years ago
|
(else
|
||
21 years ago
|
(loop old-states new-states new-all-states (cdr cs))))))
|
||
|
(else (loop old-states new-states all-states (cdr cs))))))))))
|
||
21 years ago
|
|
||
|
(define (print-dfa x)
|
||
15 years ago
|
(printf "number of states: ~a\n" (dfa-num-states x))
|
||
|
(printf "start state: ~a\n" (dfa-start-state x))
|
||
|
(printf "final states: ~a\n" (map car (dfa-final-states/actions x)))
|
||
21 years ago
|
(for-each (lambda (trans)
|
||
15 years ago
|
(printf "state: ~a\n" (car trans))
|
||
21 years ago
|
(for-each (lambda (rule)
|
||
15 years ago
|
(printf " -~a-> ~a\n"
|
||
21 years ago
|
(is:integer-set-contents (car rule))
|
||
21 years ago
|
(cdr rule)))
|
||
|
(cdr trans)))
|
||
|
(dfa-transitions x)))
|
||
|
|
||
|
(define (build-test-dfa rs)
|
||
|
(let ((c (make-cache)))
|
||
|
(build-dfa (map (lambda (x) (cons (->re x c) 'action))
|
||
|
rs)
|
||
|
c)))
|
||
21 years ago
|
|
||
|
|
||
|
#|
|
||
21 years ago
|
(define t1 (build-test-dfa null))
|
||
|
(define t2 (build-test-dfa `(#\a)))
|
||
|
(define t3 (build-test-dfa `(#\a #\b)))
|
||
19 years ago
|
(define t4 (build-test-dfa `((repetition 0 +inf.0 #\a)
|
||
|
(repetition 0 +inf.0 (concatenation #\a #\b)))))
|
||
|
(define t5 (build-test-dfa `((concatenation (repetition 0 +inf.0 (union #\0 #\1)) #\1))))
|
||
|
(define t6 (build-test-dfa `((repetition 0 +inf.0 (repetition 0 +inf.0 #\a))
|
||
|
(repetition 0 +inf.0 (concatenation #\b (repetition 1 +inf.0 #\b))))))
|
||
|
(define t7 (build-test-dfa `((concatenation (repetition 0 +inf.0 #\a) (repetition 0 +inf.0 #\b)
|
||
|
(repetition 0 +inf.0 #\c) (repetition 0 +inf.0 #\d)
|
||
|
(repetition 0 +inf.0 #\e)))))
|
||
21 years ago
|
(define t8
|
||
19 years ago
|
(build-test-dfa `((concatenation (repetition 0 +inf.0 (union #\a #\b)) #\a (union #\a #\b)
|
||
21 years ago
|
(union #\a #\b) (union #\a #\b) (union #\a #\b)))))
|
||
|
(define t9 (build-test-dfa `((concatenation "/*"
|
||
|
(complement (concatenation (intersection) "*/" (intersection)))
|
||
21 years ago
|
"*/"))))
|
||
21 years ago
|
(define t11 (build-test-dfa `((complement "1"))))
|
||
19 years ago
|
(define t12 (build-test-dfa `((concatenation (intersection (concatenation (repetition 0 +inf.0 "a") "b")
|
||
|
(concatenation "a" (repetition 0 +inf.0 "b")))
|
||
21 years ago
|
"ab"))))
|
||
|
(define x (build-test-dfa `((union " " "\n" ",")
|
||
19 years ago
|
(concatenation (repetition 0 1 "-") (repetition 1 +inf.0 (char-range "0" "9")))
|
||
|
(concatenation "-" (repetition 1 +inf.0 "-"))
|
||
21 years ago
|
"["
|
||
|
"]")))
|
||
21 years ago
|
(define y (build-test-dfa
|
||
19 years ago
|
`((repetition 1 +inf.0
|
||
|
(union (concatenation "|" (repetition 0 +inf.0 (char-complement "|")) "|")
|
||
|
(concatenation "|" (repetition 0 +inf.0 (char-complement "|"))))))))
|
||
21 years ago
|
(define t13 (build-test-dfa `((intersection (concatenation (intersection) "111" (intersection))
|
||
|
(complement (union (concatenation (intersection) "01")
|
||
19 years ago
|
(repetition 1 +inf.0 "1")))))))
|
||
20 years ago
|
(define t14 (build-test-dfa `((complement "1"))))
|
||
21 years ago
|
|#
|
||
19 years ago
|
)
|