-
Notifications
You must be signed in to change notification settings - Fork 1
/
trx.rkt
515 lines (446 loc) · 18.2 KB
/
trx.rkt
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
;; ===================================================================================================
;; Implements a matcher for regular tree expressions
;; based on Ilya Bagrak and Olin Shivers (2004) 'trx: Regular-tree expressions, now in Scheme'.
#lang racket
(require racket/control)
(require racket/generator)
(require (for-syntax syntax/parse))
(module+ test
(require rackunit))
(provide trx trx-match ast->fta build-ast)
;; ---------------------------------------------------------------------------------------------------
;; automata
;; the record type that holds a compiled tree automaton
(struct fta
(states
alphabet
labeled-rules
empty-rules
final-states
special-states
submatch-states)
#:transparent)
;; a non-empty rule: f(q_in)q_out -> q
(struct labeled-rule
(sym-name
in-state
out-state
final-state)
#:transparent)
;; an empty rule: () -> q
(struct empty-rule
(final-state)
#:transparent)
;; symbol for epsilon rules
(define epsilon (string->uninterned-symbol "ε"))
;; special symbol for matching unlabeled trees
(define unlabeled-tree (string->uninterned-symbol "^"))
(module+ test
;; example fta that matches (a b c d) and no other tree
(define ex-fta
(fta '(q0 q1 q2 q3 q4 q6)
'(a b c d)
(list (labeled-rule 'a 'q1 'q2 'q0)
(labeled-rule 'b 'q2 'q4 'q1)
(labeled-rule 'c 'q2 'q6 'q4)
(labeled-rule 'd 'q2 'q2 'q6))
(list (empty-rule 'q2))
'(q0)
'()
'())))
;; does the given rule match the given node in the given state
(define (rule-match? fta rule node state)
(let* ([q (labeled-rule-final-state rule)]
[f (labeled-rule-sym-name rule)]
[special (assoc q (fta-special-states fta))]
[n.label (cond
[(eq? f unlabeled-tree) f]
[(eq? f epsilon) f]
[(list? node) (car node)]
[else node])])
(and (eq? q state)
(if special
(apply (cdr special) (list n.label))
(equal? f n.label)))))
(module+ test
(check-true (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 0) '(a b c d) 'q0))
(check-false (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 1) '(a b c d) 'q0))
(check-false (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 1) '(a b c d) 'q1))
(check-false (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 0) '(b c d) 'q0))
(check-true (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 1) 'b 'q1))
(check-true (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 1) '(b c d) 'q1))
(check-true (rule-match? ex-fta (list-ref (fta-labeled-rules ex-fta) 3) 'd 'q6))
(define special-fta (fta '() '() (list (labeled-rule null 'qe 'qe 'qf)) (list (empty-rule 'qe)) '(qf) (list (cons 'qf number?)) '()))
(check-true (rule-match? special-fta (list-ref (fta-labeled-rules special-fta) 0) 42 'qf)))
(define (leaf? node) (or (not (list? node)) (empty? (cdr node))))
(define (children node (labeled #t))
(cond
[(not (list? node)) '()]
[(not labeled) node]
[else (cdr node)]))
(define (leftchild node) (cadr node))
(define (child-siblings node) (cddr node))
(define (error) (shift k #f))
(define-syntax (or/err stx)
(syntax-case stx ()
[(_ bodies ...) #'(or bodies ... (error))]))
(define (epsilon-closure fta state)
(let loop ([estates (list state)])
(define estates*
(set-union estates
(map (λ (r) (labeled-rule-out-state r))
(filter (λ (r) (and (eq? epsilon (labeled-rule-sym-name r))
(eq? state (labeled-rule-final-state r))))
(fta-labeled-rules fta)))))
(if (set=? estates estates*)
estates
(loop estates*))))
(define (match-empty fta state)
(or/err
(not (empty? (set-intersect (epsilon-closure fta state)
(map empty-rule-final-state (fta-empty-rules fta)))))))
(define-syntax-rule (combine-result child-result sibling-result) (begin child-result sibling-result))
(define (match-with-rule fta rule node siblings state)
(match rule
[(labeled-rule f qin qout _)
(define cs (children node (not (eq? unlabeled-tree f))))
(combine-result
(if (or (null? cs) (assoc state (fta-special-states fta)))
(match-empty fta qin)
(match-node fta (car cs) (cdr cs) qin))
(if (empty? siblings)
(match-empty fta qout)
(match-node fta (car siblings) (cdr siblings) qout)))]))
(define (match-node fta node siblings state)
;; predicates for partitioning rules
(define matches? (λ (r) (rule-match? fta r node state)))
(define repeats? (λ (r) (eq? (labeled-rule-out-state r) (labeled-rule-final-state r))))
(define eps? (λ (r) (eq? epsilon (labeled-rule-sym-name r))))
(define neither? (λ (r) (not (or (repeats? r) (eps? r)))))
(define matching-rules (filter matches? (fta-labeled-rules fta)))
(define-values (epsilon-rules non-epsilon-rules) (partition eps? matching-rules))
(define reordered-rules (let-values ([(a b) (partition repeats? non-epsilon-rules)]) (append a b)))
(or/err
(for/or ([r reordered-rules])
(reset (match-with-rule fta r node siblings state)))
(for/or ([r epsilon-rules])
(reset (match-node fta node siblings (labeled-rule-out-state r))))))
(module+ test
(check-not-false (match-node ex-fta '(a b c d) '() 'q0))
(check-not-false (match-node ex-fta 'b '(c d) 'q1))
(check-not-false (match-node ex-fta 'd '() 'q6))
(check-false (match-node ex-fta '(a b) '() 'q0))
(check-false (match-node ex-fta '(a c) '() 'q0)))
;; run automaton fta against tree
(define (fta-match fta tree)
(for/or ([q (fta-final-states fta)])
(reset (match-node fta tree '() q))))
(module+ test
(check-not-false (fta-match ex-fta '(a b c d)))
(check-false (fta-match ex-fta '(b d)))
(check-false (fta-match ex-fta 'b))
(check-false (fta-match ex-fta '(a b c)))
(check-false (fta-match ex-fta '(a c b d)))
;; =/= (rec q (| 1 (@ + (+ q))))
(define complex-fta
(fta null
null
(list (labeled-rule epsilon 'qe 'q1 'q0)
(labeled-rule 1 'qe 'q2 'q1)
(labeled-rule epsilon 'qe 'qe 'q2)
(labeled-rule epsilon 'qe 'q4 'q0)
(labeled-rule '+ 'q6 'q5 'q4)
(labeled-rule epsilon 'qe 'qe 'q5)
(labeled-rule epsilon 'qe 'q7 'q6)
(labeled-rule epsilon 'qe 'q8 'q7)
(labeled-rule 1 'qe 'q9 'q8)
(labeled-rule epsilon 'qe 'q10' q9)
(labeled-rule epsilon 'qe 'q7 'q10)
(labeled-rule epsilon 'qe 'q11 'q7)
(labeled-rule '+ 'q6 'q12 'q11)
(labeled-rule epsilon 'qe 'q10 'q12))
(list (empty-rule 'qe) (empty-rule 'q10))
'(q0)
null
null))
(check-not-false (fta-match complex-fta 1))
(check-not-false (fta-match complex-fta '(+ 1 1 1)))
(check-not-false (fta-match complex-fta '(+ 1 1 (+ 1))))
)
;; ---------------------------------------------------------------------------------------------------
;; abstract syntax
;; literal atom or 'symbol
(struct ast-lit-node
(literal
[private #:mutable])
#:transparent)
;; (@ symbol rte ...) or (symbol rte ...)
(struct ast-sym-node
(symbol
children
[private #:mutable])
#:transparent)
;; (^ rte ...)
(struct ast-unlabeled-node
(children
[private #:mutable])
#:transparent)
;; (* rte ...) or (+ rte ...) or (? rte ...)
(struct ast-seq-node
(quantifier
child
[private #:mutable])
#:transparent)
;; (| rte ...)
(struct ast-choice-node
(children
[private #:mutable])
#:transparent)
;; ,number? ,(λ (x) scheme code)
(struct ast-special-node
(proc
[private #:mutable])
#:transparent)
;; recursive rte (rec id rte)
(struct ast-rec-node
(id
child
[private #:mutable])
#:transparent)
(struct ast-ref-node
(id
[private #:mutable])
#:transparent)
(define (set-private! ast priv)
(cond
[(ast-lit-node? ast) (set-ast-lit-node-private! ast priv)]
[(ast-sym-node? ast) (set-ast-sym-node-private! ast priv)]
[(ast-unlabeled-node? ast) (set-ast-unlabeled-node-private! ast priv)]
[(ast-seq-node? ast) (set-ast-seq-node-private! ast priv)]
[(ast-choice-node? ast) (set-ast-choice-node-private! ast priv)]
[(ast-special-node? ast) (set-ast-special-node-private! ast priv)]
[(ast-rec-node? ast) (set-ast-rec-node-private! ast priv)]
[(ast-ref-node? ast) (set-ast-ref-node-private! ast priv)]
[else (raise-argument-error 'ast "ast must be an ast struct" ast)]
))
;;; compile
(define (ast->fta ast)
;(define (new-state) (gensym 'q))
(define new-state (generator () (let loop ([i 0]) (begin (yield i) (loop (+ i 1)))) ))
(define empty-state (new-state))
(define (compile-children-with-symbol ast priv state out-state symbol children id-ast-map)
(cond
[(priv ast)
(values state (list (labeled-rule symbol (priv ast) out-state state)) null null)]
[else
(set-private! ast (box #f))
(define child-end-state (new-state))
(define-values (child-state child-rules child-empty-symbols child-special-states)
(compile-nodes-in-sequence children child-end-state id-ast-map))
(set-box! (priv ast) child-state)
(values state
(cons (labeled-rule symbol child-state out-state state)
child-rules)
(cons child-end-state child-empty-symbols)
child-special-states)]))
(define (compile-nodes-in-sequence nodes out-state id-ast-map)
(for/fold ([out-state out-state]
[lrules '()]
[empty-symbols '()]
[special-states '()])
([node (reverse nodes)])
(define-values (s r e p)
(compile-node node out-state id-ast-map))
(values s
(append r lrules)
(append e empty-symbols)
(append p special-states))))
(define (compile-node ast out-state id-ast-map)
(define state (new-state))
(match ast
[(ast-lit-node literal priv)
(values state
(list (labeled-rule literal empty-state out-state state))
'()
'())]
[(ast-sym-node symbol children priv)
(compile-children-with-symbol ast ast-sym-node-private state out-state symbol children id-ast-map)]
[(ast-unlabeled-node children priv)
(compile-children-with-symbol ast ast-unlabeled-node-private state out-state unlabeled-tree children id-ast-map)]
[(ast-seq-node quantifier child priv)
(define child-out-state (if (eq? quantifier '+) out-state (new-state)))
(define-values (child-state child-rules child-empty-symbols child-special-states)
(compile-node child child-out-state id-ast-map))
(define maybe-skip-rules
(if (eq? quantifier '+)
child-rules
(cons (labeled-rule epsilon null out-state state)
(cons (labeled-rule epsilon null out-state child-out-state)
child-rules))))
(define maybe-repeat-rules
(if (eq? quantifier '?)
maybe-skip-rules
(cons (labeled-rule epsilon null child-state child-out-state)
maybe-skip-rules)))
(define rules (cons (labeled-rule epsilon null child-state state) maybe-repeat-rules))
(values state
rules
child-empty-symbols
child-special-states)]
[(ast-choice-node children priv)
(define-values (rules empty-symbols special-states)
(for/fold ([rules '()]
[empty-symbols '()]
[special-states '()])
([child children])
(define child-out-state (new-state))
(define-values (child-state child-rules child-empty-symbols child-special-states)
(compile-node child child-out-state id-ast-map))
(values (cons (labeled-rule epsilon null child-state state)
(cons (labeled-rule epsilon null out-state child-out-state)
(append child-rules rules)))
(append child-empty-symbols empty-symbols)
(append child-special-states special-states))))
(values state rules empty-symbols special-states)]
[(ast-special-node proc priv)
(values state
(list (labeled-rule null empty-state out-state state))
'()
(list (cons state proc))
)]
[(ast-rec-node id child priv)
(compile-node child out-state (cons (cons id child) id-ast-map))
;; (define-values (child-state child-rules child-empty-symbols child-special-states)
;; (compile-node child #f (cons (cons id child) id-ast-map)))
;; (values null null null null)
]
[(ast-ref-node id priv)
(cond
[priv
(values priv null null null)]
[else
(define ref-ast (assoc id id-ast-map))
(unless ref-ast (raise-arguments-error 'id-ast-map "no entry for reference node" "node" id "map" id-ast-map))
(set-private! ast (box #f))
(define-values (ref-state ref-rules ref-empty-symbols ref-special-states)
(compile-node (cdr ref-ast) out-state id-ast-map))
(set-box! (ast-ref-node-private ast) ref-state)
(values ref-state ref-rules ref-empty-symbols ref-special-states)
])
]
))
(define-values (state rules empty-symbols special-states)
(compile-node ast empty-state null))
(define (unbox-assert b)
(or (unbox b)
(error "boxed state value was null")))
(define (maybe-unbox b)
(if (box? b)
(unbox-assert b)
b))
(define (fixup-rule r)
(match r
[(labeled-rule f in out q)
(labeled-rule f (maybe-unbox in) (maybe-unbox out) (maybe-unbox q))]))
(fta null
null
(map fixup-rule rules)
(map empty-rule (cons empty-state empty-symbols))
(list state)
special-states
null)
)
(module+ test
(let ([ast (ast->fta (ast-sym-node 'a (list (ast-seq-node '+ (ast-lit-node 42 #f) #f)) #f))])
(check-not-false (fta-match ast '(a 42)))
(check-not-false (fta-match ast '(a 42 42)))
(check-not-false (fta-match ast '(a 42 42 42)))
(check-false (fta-match ast '(a)))
(check-false (fta-match ast '(a 42 1 42)))
(check-false (fta-match ast '(a 42 42 42 'x))))
(let ([ast (ast->fta (ast-sym-node 'a (list (ast-seq-node '+ (ast-lit-node 42 #f) #f)
(ast-lit-node 1 #f)) #f))])
(check-not-false (fta-match ast '(a 42 1)))
(check-not-false (fta-match ast '(a 42 42 1)))
(check-false (fta-match ast '(a 1)))
(check-false (fta-match ast '(a 42 42))))
(let ([ast (ast->fta (ast-sym-node 'a (list (ast-seq-node '? (ast-lit-node 42 #f) #f)
(ast-lit-node 1 #f)) #f))])
(check-not-false (fta-match ast '(a 1)))
(check-not-false (fta-match ast '(a 42 1)))
(check-false (fta-match ast '(a 42 42 1))))
(let ([ast (ast-sym-node '+ (list (ast-seq-node '+ (ast-special-node number? #f) #f)) #f)])
(check-not-false (fta-match (ast->fta ast) '(+ 1 2 42)))
(check-false (fta-match (ast->fta ast) '(+ 1 a 42))))
(let ([fta (ast->fta (ast-sym-node '+ (list (ast-seq-node '+ (ast-special-node (λ (x) (or (number? x) (string? x))) #f) #f)) #f))])
(check-not-false (fta-match fta '(+ 1 2 42)))
(check-not-false (fta-match fta '(+ 1 "a" 42)))
(check-false (fta-match fta '(+ 1 a 42))))
(let ([fta (ast->fta (ast-rec-node 'q
(ast-choice-node
(list
(ast-lit-node 'null #f)
(ast-sym-node 'cons
(list (ast-ref-node 'q #f)
(ast-ref-node 'q #f))
#f)
(ast-sym-node 'cons
(list (ast-lit-node 1 #f)
(ast-ref-node 'q #f))
#f))
#f)
#f))])
(check-not-false (fta-match fta '(cons 1 null)))
(check-not-false (fta-match fta '(cons null null)))
(check-not-false (fta-match fta '(cons (cons 1 null) null)))
(check-false (fta-match fta '(cons null 1)))
(check-false (fta-match fta '(cons (cons a null) (cons 1 null))))
)
)
;; ---------------------------------------------------------------------------------------------------
;; interface
(define-syntax (build-ast stx)
(syntax-parse stx
#:datum-literals (@ ^ any or * + ? quote unquote rec let let* letrec)
[(_ (rec ident:id rte))
#'(ast-rec-node (quote ident) (build-ast rte) #f)]
[(_ (unquote ex))
#'(ast-special-node ex #f)]
[(_ lit:str)
#'(ast-lit-node lit #f)]
[(_ lit:number)
#'(ast-lit-node lit #f)]
[(_ lit:boolean)
#'(ast-lit-node lit #f)]
[(_ lit:char)
#'(ast-lit-node lit #f)]
[(_ (quote symbol:id))
#'(ast-lit-node (quote symbol) #f)]
[(_ (^ rte ...))
#'(ast-unlabeled-node (list (build-ast rte) ...) #f)]
[(_ (any))
#'(ast-special-node (const #t) #f)]
[(_ (or rte ...))
#'(ast-choice-node (list (build-ast rte) ...) #f)]
[(_ (* rte))
#'(ast-seq-node '* (build-ast rte) #f)]
[(_ (+ rte))
#'(ast-seq-node '+ (build-ast rte) #f)]
[(_ (? rte))
#'(ast-seq-node '? (build-ast rte) #f)]
[(_ (@ symbol:id rte ...))
#'(ast-sym-node (quote symbol) (list (build-ast rte) ...) #f)]
[(_ (symbol:id rte ...))
#'(ast-sym-node (quote symbol) (list (build-ast rte) ...) #f)]
[(_ ident:id)
#'(ast-ref-node (quote ident) #f)]
))
(define-syntax (trx stx)
(syntax-case stx ()
[(_ rte) #'(ast->fta (build-ast rte))]))
(define (trx-match fta tree) (fta-match fta tree))
(module+ test
(check-not-false (trx-match (trx 'a) 'a))
(check-not-false (trx-match (trx (rec q (or (cons 1 q) (cons q q) 'null))) '(cons 1 null)))
(check-not-false (trx-match (trx (any)) 42))
(check-not-false (trx-match (trx (any)) '(+ 1 2 3))))