-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.scm
211 lines (197 loc) · 5.33 KB
/
regex.scm
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
(module
regex
(include "nfa.sch")
(include "dfa.sch")
(import (dfa "dfa.scm")
(nfa "nfa.scm")
(utils "utils.scm"))
; (main main-regex)
(export (nfa-plus nfa1)
(nfa-concat nfaA nfaB . rest)
(nfa-star nfa1)
(nfa-or nfaA nfaB)
(nfa-for-one-symbol sym)
(regex->dfa str)
(regex->nfa str)
(str->parse-tree str)
(parse-tree->nfa tree)))
(define regex-lexer
(regular-grammar ()
((+ (or #\tab #\space #\newline)) (ignore))
((+ (or alpha digit (in "_-"))) (cons 'CHARS (the-string)))
("[" 'OPEN_SQBRACKET)
("]" 'CLOSE_SQBRACKET)
("(" 'OPENPARAN)
(")" 'CLOSEPARAN)
("|" 'BAR)
("." 'DOT)
("*" 'STAR)
("+" 'PLUS)))
(define regex-grammar
(lalr-grammar
; terminals (PLUS isn't implemented in the grammar)
(CHARS DOT STAR BAR OPENPARAN CLOSEPARAN OPEN_SQBRACKET CLOSE_SQBRACKET PLUS)
;productions
(regex ((orr) orr))
(orr ((concat orlist)
(if (equal? #f orlist)
concat
(list 'orr concat orlist))))
(orlist ((BAR concat orlist)
(if (equal? #f orlist)
concat
(list 'orr concat orlist)))
(() #f))
(concat ((star concatlist)
(if (equal? #f concatlist)
star
(list 'concat star concatlist))))
;; dot is concatenation character
(concatlist ((star concatlist)
(if (equal? #f concatlist)
star
(list 'concat star concatlist)))
((DOT star concatlist)
(if (equal? #f concatlist)
star
(list 'concat star concatlist)))
(()
#f))
(star ((sym STAR)
(list 'star sym))
((sym)
sym))
(sym ((OPEN_SQBRACKET snapshot CLOSE_SQBRACKET)
snapshot)
((OPENPARAN regex CLOSEPARAN)
regex))
(snapshot
((CHARS snapshot)
(cons CHARS snapshot))
(()
(list)))))
(define (parse-tree->nfa tree)
(match-case tree
;; accepts regexA*
((star ?A)
(nfa-star (parse-tree->nfa A)))
;; accepts regexA.regexB
((concat ?A ?B)
(nfa-concat (parse-tree->nfa A) (parse-tree->nfa B)))
;; accepts regexA or regexB
((orr ?A ?B)
(nfa-or (parse-tree->nfa A) (parse-tree->nfa B)))
;; accepts a symbol
(?sym (nfa-for-one-symbol sym))))
(define (nfa-concat nfaA nfaB . rest)
;(print nfaA nfaB rest)
;(print-nfa (car rest))
(let loop ((result (%nfa-concat nfaA nfaB))
(rest rest))
(cond ((null? rest) result)
(else (loop
(%nfa-concat result (car rest))
(cdr rest))))))
(define (%nfa-concat nfaA nfaB)
;; build an nfa which accepts La.Lb, where La are strings accepted
;; by nfaA and Lb are strings accepted by nfaB
(let* ((startA (nfa-start-state nfaA))
(startB (nfa-start-state nfaB))
(endB (nfa-final-states nfaB))
;; Add epsilon transitions from each of the final
;; states of nfaA to the start state of nfaB
(trans (append (map (lambda (endA)
(list endA 'epsilon startB))
(nfa-final-states nfaA))
(nfa-transition-list nfaA)
(nfa-transition-list nfaB))))
(nfa-rename-states
(nfa
(union (nfa-alphabet nfaA) (nfa-alphabet nfaB))
(append (nfa-states nfaA) (nfa-states nfaB))
startA
trans
endB))))
(define (nfa-star nfaA)
;; build an nfa which accepts L*
;; where L are strings accepted by the nfa
(let* ((q0 (gensym "q"))
(qfinal (gensym "q"))
(startA (nfa-start-state nfaA))
(finalA (nfa-final-states nfaA))
(trans (append
(map (lambda (endA)
(list endA 'epsilon startA))
finalA)
(map (lambda (endA)
(list endA 'epsilon qfinal))
finalA)
(list (list q0 'epsilon qfinal)
(list q0 'epsilon startA))
(nfa-transition-list nfaA))))
(nfa-rename-states
(nfa
(nfa-alphabet nfaA)
(append (list q0 qfinal) (nfa-states nfaA))
q0
trans
(list qfinal)))))
(define (nfa-or nfaA nfaB)
;; build an nfa which accepts La|Lb where La are strings accepted nfaA
;; and Lb are strings accepted by nfaB
(let* ((q0 (gensym "q"))
(qfinal (gensym "q"))
(startA (nfa-start-state nfaA))
(startB (nfa-start-state nfaB))
(finalA (nfa-final-states nfaA))
(finalB (nfa-final-states nfaB))
(trans (append
(map (lambda (endA)
(list endA 'epsilon qfinal))
finalA)
(map (lambda (endB)
(list endB 'epsilon qfinal))
finalB)
(list (list q0 'epsilon startA)
(list q0 'epsilon startB))
(nfa-transition-list nfaA) (nfa-transition-list nfaB))))
(nfa-rename-states
(nfa (union (nfa-alphabet nfaA) (nfa-alphabet nfaB))
(append (nfa-states nfaA) (nfa-states nfaB) (list q0 qfinal))
q0
trans
(list qfinal)))))
(define (nfa-for-one-symbol sym)
;; build an nfa which accepts one symbol
(let* ((q0 (gensym "q"))
(qfinal (gensym "q"))
(trans (list (list q0 sym qfinal))))
(nfa (list sym)
(list q0 qfinal)
q0
trans
(list qfinal))))
(define (nfa-plus nfa)
;; builds an nfa which accepts L+ where L are the strings accepted by
;; the nfa
(nfa-concat nfa (nfa-star nfa)))
(define (str->parse-tree str)
(read/lalrp regex-grammar
regex-lexer
(open-input-string str)))
;; returns an nfa
(define (regex->nfa str)
(parse-tree->nfa
(str->parse-tree str)))
;; returns a dfa
(define (regex->dfa str)
(nfa->dfa
(regex->nfa str)))
(define (main-regex argv)
(let ((r #f))
(print
(read/lalrp regex-grammar
regex-lexer
(open-input-string (string-join (cdr argv) " ")))))
(print "blah"))
;; end