-
Notifications
You must be signed in to change notification settings - Fork 2
/
flo
executable file
·271 lines (246 loc) · 5.79 KB
/
flo
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
#!/local/bin/kalypso
;
; Copyright © 2019 Keith Packard <[email protected]>
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License along
; with this program; if not, write to the Free Software Foundation, Inc.,
; 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
;
;
; flo
;
; factor left common expressions out of a grammar
;
;
; remove the first element from each of a list
; of productions (unless the production is nil)
;
(defun clip-firsts (productions)
(cond (productions
(cond ((car productions)
(cons (caar productions) (clip-firsts (cdr productions)))
)
(t (clip-firsts (cdr productions)))
)
)
(t nil)
)
)
;
; return the first duplicated element from a list of elements
;
(defun has-common (firsts)
(cond ((nil? firsts) nil)
((member? (car firsts) (cdr firsts)) (car firsts))
(t (has-common (cdr firsts)))
)
)
;
; return a list of productions which have the given
; element as their first member
;
(defun with-first (productions first)
(cond ((nil? productions) nil)
((= (caar productions) first)
(cons (car productions) (with-first (cdr productions) first))
)
(t (with-first (cdr productions) first))
)
)
;
; return a list of productions which *don't* have the given
; element as their first member
;
(defun without-first (productions first)
(cond ((nil? productions) nil)
((not (= (caar productions) first))
(cons (car productions) (without-first (cdr productions) first))
)
(t (without-first (cdr productions) first))
)
)
;
; strip the first 'count' elements off a list of productions
;
(defun remove-firsts (productions count)
(cond ((nil? productions) nil)
(t (cons (nthcdr count (car productions))
(remove-firsts (cdr productions)
count
)
)
)
)
)
;
; return 't if each production in the list has the same first
; element
;
(defun all-common-first (productions)
(cond ((and productions (cdr productions))
(cond ((or (and (nil? (car productions))
(nil? (cadr productions))
)
(and (car productions)
(cadr productions)
(= (caar productions) (caadr productions))
)
)
(all-common-first (cdr productions))
)
(t nil)
)
)
(t)
)
)
;
; return the maximal list of common first sub-lists
; from a set of productions
;
(defun max-common (productions)
(cond ((all-common-first productions)
(cons (caar productions)
(max-common (remove-firsts productions 1))
)
)
(t
nil)
)
)
;
; factor out common left sub-lists from the list
; of productions associated with a particular non-terminal
;
(defun eliminate-common (non-terminal dictionary)
(let ((productions (eval non-terminal))
(firsts)
(common)
(common-list)
(removed)
(new)
(new-name)
)
(setq firsts (clip-firsts productions))
(setq common (has-common firsts))
(cond (common
(setq removed (with-first productions common))
(setq common-list (max-common removed))
(setq new-name (get-name non-terminal))
(setq new t)
;
; generate a name for the new non-terminal
; keep appending "p" until it's new
;
(while new
(setq new-name (strcat new-name "p"))
(setq new (dictionary-lookup dictionary new-name))
)
(setq new (symbol new-name dictionary))
(set new (remove-firsts removed (length common-list)))
(set non-terminal (cons (conc common-list (list new))
(without-first productions common))
)
(cons new (conc (eliminate-common new dictionary)
(eliminate-common non-terminal dictionary)
)
)
)
(t nil)
)
)
)
;
; remove common left sub-expressions from each non-terminal
; in the grammar
;
(defun factor-left (non-terminals)
(let ((l) (new))
(setq l non-terminals)
(while l
(setq new (eliminate-common (car l) flo-dictionary))
(cond (new
(setq non-terminals (conc non-terminals new))
)
)
(setq l (cdr l))
)
non-terminals
)
)
(defun to-non-terminal-list (grammar)
(if grammar
(let ((name (caar grammar)))
(set name (cdar grammar))
(cons name (to-non-terminal-list (cdr grammar)))
)
else
nil
)
)
(defun from-non-terminal-list (non-terminals)
(cond (non-terminals
(cons (cons (car non-terminals) (eval (car non-terminals)))
(from-non-terminal-list (cdr non-terminals))
)
)
(nil)
)
)
(defun print-productions (productions)
(cond (productions
(patom "\t")
(cond ((car productions)
(print (car productions))
)
(t (patom "()"))
)
(terpr)
(print-productions (cdr productions))
)
)
)
(defun print-one-set (set)
(patom " (")
(print (car set)) (terpr)
(print-productions (cdr set))
(patom "\t)\n")
)
(defun print-grammars (grammar)
(cond (grammar
(print-one-set (car grammar))
(print-grammars (cdr grammar))
)
)
)
(defun print-grammar (grammar)
(patom "(\n")
(print-grammars grammar)
(patom " )\n")
)
(defun flo-file (fd)
(setq flo-dictionary (new-dictionary))
(setq grammar (fread-dictionary fd flo-dictionary))
(setq non-terminals (to-non-terminal-list grammar))
(setq result (from-non-terminal-list (factor-left non-terminals)))
(print-grammar result)
)
(setq file-in stdin)
(setq argv (cdr argv))
(if argv
(setq file-in (fopen (car argv) 'r))
(if (nil? file-in)
(error (strcat "flo: can't open " (sprint (car argv))))
)
)
(flo-file file-in)