-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathch11-strings.lisp
302 lines (277 loc) · 11.1 KB
/
ch11-strings.lisp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(in-package :progalgs)
(defstruct (mb-string (:conc-name mbs-))
bytes
bitmap)
(defparameter *mb-threshold* 10)
(defun mb-char-index (string i)
(let ((off 0))
(loop
(rtl:with ((cnt (count 1 (mbs-bitmap string)
:start off :end (+ off i))))
(diff (- i cnt)))
(cond
((= cnt i)
(return (+ off i)))
((< diff *mb-threshold*)
(return (mb-linear-char-index string diff off)))
((< cnt (floor i 2))
(incf off i)
(decf i cnt))
(t
(incf off (floor i 2))
(decf i cnt))))))
(defun mb-length (string)
(count 1 (mbs-bitmap string)))
(defun naive-match (pat str)
(dotimes (i (- (1+ (length str)) (length pat)))
(let ((mis (mismatch pat (rtl:slice str i))))
(when (or (null mis)
(= mis (length pat)))
(return-from naive-match i)))))
(defun kmp-table (pat)
(let ((rez (make-array (length pat)))
(i 0)) ; prefix length
(setf (aref rez 0) -1)
(loop :for j :from 1 :below (length pat) :do
(if (char= (char pat i) (char pat j))
(setf (aref rez j) (aref rez i))
(progn ;; we have to use parallel version of setf here
(psetf (aref rez j) i
i (aref rez i))
(loop :while (and (>= i 0)
(not (char= (char pat i)
(char pat j))))
:do (setf i (aref rez i)))))
(incf i))
rez))
(defun kmp-match (pat str)
(let ((s 0)
(p 0)
(ff (kmp-table pat)))
(loop :while (< s (length str)) :do
(if (char= (char pat p) (char str s))
;; if the current chars match
(if (= (1+ p) (length pat))
;; if we reached the end of the pattern - success
(return (- s p))
;; otherwise, match the subsequent chars
(setf p (1+ p)
s (1+ s)))
;; if the characters don't match
(if (= -1 (aref ff p))
;; shift the pattern for the whole length
(setf p 0
;; and skip to the next char in the string
s (1+ s))
;; try matching the current char again,
;; shifting the pattern to align the prefix
;; with the already matched part
(setf p (aref ff p)))))))
(defun rk-match (pat str)
(let ((len (length pat))
(phash (rk-hash pat)))
(loop :for i :from len :to (length str)
:for beg := (- i len)
:for shash := (rk-hash (rtl:slice str 0 len))
:then (rk-rehash shash len
(char str (1- beg)) (char str (1- i)))
:when (and (= phash shash)
(string= pat (rtl:slice str beg (+ beg len))))
:collect beg)))
(defun rk-hash-naive (str)
(loop :for ch :across str :sum (char-code ch)))
(defun rk-hash (str)
(assert (> (length str) 0))
(let ((rez (char-code (char str 0))))
(loop :for ch :across (rtl:slice str 1) :do
(setf rez (+ (rem (* rez 256) 101)
(char-code ch))))
(rem rez 101)))
(defun rk-rehash (hash len ch1 ch2)
(rem (+ (* 256
(+ hash 101
(- (rem (* (char-code ch1)
(loop :repeat (max 0 (- len 2))
:with val := 256
:do (setf val (rem (* val 256) 101))
:finally (return val)))
101))))
(char-code ch2))
101))
(deftest match ()
(should be = 0 (naive-match "foo" "foobar"))
(should be = 3 (naive-match "bar" "foobar"))
(should be null (naive-match "baz" "foobar"))
(should be = 0 (kmp-match "foo" "foobar"))
(should be = 3 (kmp-match "bar" "foobar"))
(should be null (kmp-match "baz" "foobar"))
(should be equal '(0) (rk-match "foo" "foobar"))
(should be equal '(3) (rk-match "bar" "foobar"))
(should be equal '(0 6) (rk-match "bar" "barfoobar"))
(should be null (rk-match "baz" "foobar")))
(defun re-match (regex text)
"Search for REGEX anywhere in TEXT."
(if (rtl:starts-with "^" regex)
(when (> (length regex) 1)
(match-here (rtl:slice regex 1) text))
(dotimes (i (length text))
(when (match-here regex (rtl:slice text i))
(return t)))))
(defun match-here (regex text)
"Search for REGEX at beginning of TEXT."
(cond ((= 0 (length regex))
t)
((and (> (length regex) 1)
(char= #\* (char regex 1)))
(match-star (char regex 1) (rtl:slice regex 2) text))
((string= "$" regex)
(= 0 (length text)))
((and (> (length text) 0)
(member (char regex 0) (list #\. (char text 0)))
(match-here (rtl:slice regex 1) (rtl:slice text 1))))))
(defun match-star (c regex text)
"Search for C*REGEX at beginning of TEXT."
(loop
(when (match-here regex text) (return t))
(setf text (rtl:slice text 1))
(unless (and (> (length text) 0)
(member c (list #\. (char text 0))))
(return))))
(deftest re-match ()
(should be null (re-match "foo" "bar"))
(should be rtl:true (re-match "foo" "foo"))
(should be rtl:true (re-match "bar" "foobar"))
(should be rtl:true (re-match "f.o" "foo"))
(should be rtl:true (re-match "^foo" "foobar"))
(should be null (re-match "^bar" "foobar"))
(should be null (re-match "foo$" "foobar"))
(should be rtl:true (re-match "bar$" "foobar"))
(should be rtl:true (re-match "fo*" "foobar")))
(define-condition check-start-anchor () ())
(defgeneric th-part (next-state kind &rest args)
(:documentation
"Emit the TH-STATE structure of a certain KIND
(which may be a keyword or a raw string)
using the other ARGS and pointing to NEXT-STATE struct.")
(:method (next-state (kind (eql :sequence)) &rest args)
(apply 'th-part (if (rest args)
(apply 'th-part :sequence (rest args))
next-state)
(first args)))
(:method (next-state (kind (eql :greedy-repetition)) &rest args)
;; this method can handle *, +, {n}, and {n,m} regex modifiers
;; in any case, there's a prefix sequence of fixed nonnegative length
;; of identical elements that should unconditionally match,
;; followed by a bounded or unbounded sequence that,
;; in case of a failed match, transitions to the next state
(apply 'th-part
(let ((*initial-state* next-state))
(apply 'th-part next-state :sequence
(loop :repeat (or (second args) 1)
:collect (rtl:mklist (third args)))))
:sequence (loop :repeat (first args)
:collect (rtl:mklist (third args)))))
(:method (next-state (kind character) &rest args)
(th-state kind next-state
;; Usually, *initial-state* will be null,
;; i.e. further computations along this path will be aborted,
;; but, for some variants (? or *), they will just continue
;; normally to the next state.
;; The special variable controls this setting,
;; as you can see in the method for :greedy-repetition
t *initial-state*))
(:method (next-state (kind (eql :end-anchor)) &rest args)
(th-state nil *matched-state*
t *initial-state*))
(:method (next-state (kind (eql :start-anchor)) &rest args)
;; This part is unique as all the other parts consume the next character
;; (we're not implementing lookahead here), but this one shouldn't.
;; To implement such behavior without the additional complexity
;; of passing the search string to this function (which we'll still
;; probably need to do later on, but were able to avoid so far),
;; we can resort to a cool Lisp technique of signaling a condition
;; that can be handled specially in the top-level code
(signal 'check-start-anchor)
next-state))
(defun run-nfa (nfa str)
(let ((i 0)
(start 0)
(matches (list))
(states (list nfa)))
;; this is the counterpart for the start-anchor signal
(handler-bind ((check-start-anchor
;; there's no sense to proceed matching
;; a ^... regex if the string is not
;;at its start
(lambda (c)
(when (> i 0) (return-from run-nfa)))))
(dovec (char (concatenate 'vector str
#(nil))) ; end-anchor
(let ((new-states (list)))
(dolist (state states)
(dolist (tr (th-state-transitions state))
(when (th-match tr char)
(case (rtl:rt tr)
(*matched-state* (push start matches))
((nil) ) ; ignore it
(t (pushnew (rtl:rt tr) new-states)))
(return))))
(if new-states
(setf states new-states)
(setf states (list nfa)
start nil)))
(incf i)
(unless start (setf start i))))
matches))
;; TODO (deftest nfa ()
(defstruct grammar
rules
max-length)
(defmacro grammar (&rest rules)
`(make-grammar
:rules (rtl:pairs->ht (mapcar (lambda (rule)
(rtl:pair (nthcdr 2 rule) (first rule)))
',rules)
:test 'equal)
:max-length
(let ((max 0))
(dolist (rule ',rules)
;; Here, #1= and #1# are reader-macros for capturing
;; a form and re-evaluating it again
(when (> #1=(length (nthcdr 2 rule)) max)
(setf max #1#)))
max)))
(defun parse (grammar queue)
(let ((stack (list)))
(loop :while queue :do
(print stack) ; diagnostic output
(rtl:if-it (find-rule stack grammar)
;; reduce
(dotimes (i (length (cdr rtl:it))
(push rtl:it stack))
(pop stack))
;; shift
(push (pop queue) stack))
:finally (return (find-rule stack grammar)))))
(defun find-rule (stack grammar)
(let (prefix)
(loop :for item in stack
:repeat (grammar-max-length grammar) :do
(push (first (rtl:mklist item)) prefix)
(rtl:when-it (rtl:? grammar 'rules prefix)
;; otherwise parsing will fail with a stack
;; containing a number of partial subtrees
(return (cons rtl:it (reverse (subseq stack 0 (length prefix)))))))))
(deftest parse ()
(let ((*standard-output* (make-broadcast-stream)))
(should be equal '(S (NP DET ADJ NOUN)
(VP VERB
(VP VERB
(NP PRP$ NOUN)))
|.|)
(parse (grammar (S -> NP VP |.|)
(NP -> DET ADJ NOUN)
(NP -> PRP$ NOUN)
(VP -> VERB VP)
(VP -> VERB NP))
'(DET ADJ NOUN VERB VERB PRP$ NOUN |.|)))))