Permalink
Browse files

Rewrote swangee.core/match, it was totally broken. Should be slightly…

… less broken now. All tests pass.
  • Loading branch information...
1 parent 87004d9 commit 34287bda4c53d252c6e1e6ab99830b743d491f1c @davidsantiago committed Mar 18, 2011
Showing with 18 additions and 19 deletions.
  1. +15 −18 src/swangee/core.clj
  2. +3 −1 test/swangee/test_automata_defs.clj
View
33 src/swangee/core.clj
@@ -54,26 +54,23 @@
(recur next-cfg)))))) ;; Still have input, so recur.
(defn match
- "Given a FiniteAutomaton and an input sequence, return the longest string matched
- starting on the first character of the string."
+ "Given a FiniteAutomaton and an input sequence, return the longest string
+ matched starting on the first character of the string."
[^FiniteAutomaton fa input]
- (loop [curr-cfg (config (initial-state fa)
- input)
- symbols-seen [(first input)]
+ (loop [curr-cfg (config (initial-state fa) input)
+ symbols-seen []
longest-match []]
- (let [next-cfg (step fa curr-cfg)]
- (if (not (valid-state? fa (:state next-cfg))) ;; No route to acceptance...
+ (if (not (valid-state? fa (:state curr-cfg)))
+ longest-match ;; No route to acceptance, just return longest-match.
+ ;; The state is valid, so process further.
+ (if (empty? (:input curr-cfg))
longest-match
- (if (empty? (:input next-cfg)) ;; Out of input, return depends on state.
- (if (accepting-state? fa (:state next-cfg)) ;; If we are in accepting state,
- (conj longest-match (first (:input curr-cfg))) ;; Add last input we saw and return.
- ;; Otherwise, we are out of input but not in accepting, state...
- longest-match)
- ;; Not out of input, so continue...
+ ;; Not out of input, so continue...
+ (let [next-cfg (step fa curr-cfg)
+ next-symbols-seen (conj symbols-seen (first (:input curr-cfg)))]
(recur next-cfg
- (conj symbols-seen (first (:input next-cfg)))
- ;; Only set longest-match to symbols seen if we're in accepting state.
+ next-symbols-seen
+ ;; Only add to longest-match if we are in an accepting state.
(if (accepting-state? fa (:state next-cfg))
- symbols-seen
- longest-match)))))))
-
+ next-symbols-seen
+ longest-match)))))))
View
4 test/swangee/test_automata_defs.clj
@@ -23,7 +23,9 @@
(def lang1-strings ["abccbb" "ab" "abc" "abbb" "abbbccc" "abccbb"])
(def not-lang1-strings ["a" "b" "aba" "abac" "abbc" "abbbb" "abbcb"
"abbbbccc" "abddcba" "dfa"])
-(def lang1-string-matches [["abcd" (seq "abc")]
+(def lang1-string-matches [["" []]
+ ["adbc" []]
+ ["abcd" (seq "abc")]
["dbbabc" []]
["ab" (seq "ab")]
["abccbb" (seq "abccbb")]

0 comments on commit 34287bd

Please sign in to comment.