/
lr.clj
648 lines (574 loc) · 25.5 KB
/
lr.clj
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
(ns active.lawrence.lr
(:require [clojure.set :as set]
[active.clojure.record :refer :all]
[active.lawrence.grammar :refer :all]
[active.lawrence.runtime :refer :all]
[clojure.pprint :refer (pprint)]))
; LR support code needed at generation time
(declare number-coll<?)
(defn production<?
[p1 p2]
(number-coll<? (cons (production-lhs p1) (production-rhs p1))
(cons (production-lhs p2) (production-rhs p2))))
; LR items
; They must be distinguishable from pairs.
(define-record-type Item
(make-item production position lookahead)
item?
[production item-production
position item-position
lookahead item-lookahead])
(defn predict-item?
[item]
(zero? (item-position item)))
(defn lookahead<?
[la1 la2]
(number-coll<? la1 la2))
(defn item-lhs
[item]
(production-lhs (item-production item)))
(defn item-rhs
[item]
(production-rhs (item-production item)))
(defn item-rhs-rest
[item]
(nthrest (production-rhs (item-production item))
(item-position item)))
(defn item-shift
[item]
(make-item (item-production item)
(+ 1 (item-position item))
(item-lookahead item)))
(defn item<?
[item-1 item-2]
(or (production<? (item-production item-1)
(item-production item-2))
(< (item-position item-1)
(item-position item-2))
(lookahead<? (item-lookahead item-1) (item-lookahead item-2))))
(defn- partition-coll
[pred lis]
(let [mp (group-by pred lis)]
[(mp true) (mp false)]))
(defn partition-items
[item items]
(let [production (item-production item)
position (item-position item)]
(partition-coll
(fn [item]
(and (= production (item-production item))
(= position (item-position item))))
items)))
(defn compute-lr-closure
[state grammar k]
(let [initial-items (fn [symbol lookahead-suffix]
(set
(mapcat (fn [production]
(map
(fn [la]
(make-item production 0 la))
(sequence-first lookahead-suffix k grammar)))
(grammar-productions-with-lhs symbol grammar))))
next-predict (fn [item-set-0]
(loop [item-set (seq item-set-0)
predict-set item-set-0]
(if (empty? item-set)
predict-set
(let [item (first item-set)
rhs-rest (item-rhs-rest item)]
(if (empty? rhs-rest)
(recur (rest item-set) predict-set)
(let [lhs (first rhs-rest)]
(if (terminal? lhs grammar)
(recur (rest item-set) predict-set)
(let [new-items
(initial-items
lhs
(concat (rest rhs-rest)
(item-lookahead item)))]
(recur (rest item-set)
(set/union new-items predict-set))))))))))]
(loop [predict-set state]
(let [new-predict-set (next-predict predict-set)]
(if (= predict-set new-predict-set)
predict-set
(recur new-predict-set))))))
(defn sequences-initial-nonterminals
[sequences grammar]
(map first
(filter
(fn [sequence]
(and (not (empty? sequence))
(not (terminal? (first sequence) grammar))))
sequences)))
(defn productions->slr-predict-items
[productions k grammar]
(let [production->slr-predict-items (fn [production]
(set
(map (fn [lookahead]
(make-item production 0 lookahead))
(nonterminal-follow (production-lhs production) k grammar))))]
(apply set/union (map production->slr-predict-items productions))))
(defn compute-slr-closure
[state grammar k]
(let [^objects predict-sets (grammar-fetch-property
grammar
:predict-sets
(fn [grammar]
(object-array (grammar-number-of-nonterminals grammar))))
offset (grammar-nonterminal-offset grammar)
compute-predict-lhses (fn [lhs]
(let [already-done (atom #{})]
(letfn [(recurse
[lhs]
(or (and (empty? @already-done)
(aget predict-sets (- lhs offset)))
(if (contains? @already-done lhs)
'()
(let [lhses
(filter
(fn [lhs]
(not (contains? @already-done lhs)))
(sequences-initial-nonterminals
(map production-rhs
(grammar-productions-with-lhs lhs grammar))
grammar))]
(swap! already-done conj lhs)
(cons lhs (mapcat recurse lhses))))))]
(recurse lhs))))
get-predict-lhses (fn [lhs]
(or (aget predict-sets (- lhs offset))
(let [lhses (compute-predict-lhses lhs)]
(aset predict-sets (- lhs offset) lhses)
lhses)))
initial-predict-lhses (sequences-initial-nonterminals (map item-rhs-rest state) grammar)
;; (dummy (begin (write initial-predict-lhses) (newline)))
predict-lhses (set (mapcat get-predict-lhses initial-predict-lhses))
;; (dummy (begin (write predict-lhses) (newline)))
predict-productions (mapcat (fn [lhs]
(grammar-productions-with-lhs lhs grammar))
predict-lhses)
;; (dummy (begin (write predict-productions) (newline)))
predict-items (productions->slr-predict-items predict-productions k grammar)
;; (dummy (begin (write predict-items) (newline)))
]
(set/union state
predict-items)))
; Operations on LR states
(defn goto
[state-closure symbol]
(set
(map item-shift
(filter (fn [item]
(and (not (empty? (item-rhs-rest item)))
(= symbol
(first (item-rhs-rest item)))))
state-closure))))
(defn active
[state]
(loop [item-set (seq state)
m 0]
(if (empty? item-set)
m
(recur (rest item-set)
(long (max (item-position (first item-set)) m))))))
(defn next-symbols
[state-closure grammar]
(loop [item-set (seq state-closure)
symbols #{}]
(if (empty? item-set)
symbols
(let [item (first item-set)
rhs-rest (item-rhs-rest item)]
(recur (rest item-set)
(if (and (not (empty? rhs-rest)))
(conj symbols (first rhs-rest))
symbols))))))
(defn next-terminals
[state-closure grammar]
(set/select (fn [symbol]
(and (not (= (grammar-error grammar) symbol))
(terminal? symbol grammar)))
(next-symbols state-closure grammar)))
(defn next-nonterminals
[state-closure grammar]
(set/select (fn [symbol]
(nonterminal? symbol grammar))
(next-symbols state-closure grammar)))
(defn handles-error?
[state-closure grammar]
(contains? (next-symbols state-closure grammar) (grammar-error grammar)))
(defn accept
[state-closure]
(filter (fn [item]
(empty? (item-rhs-rest item)))
state-closure))
(defn find-eoi-lookahead-item
[accept-items]
(boolean
(some (fn [item]
(empty? (item-lookahead item)))
accept-items)))
(defn expected-terminals
"Compute a set of terminals that might occur next in this state."
[closure grammar]
(set/union (next-terminals closure grammar)
(set (filter some?
(map (comp first item-lookahead)
(accept closure))))))
(defn initial?
[state grammar]
(some (fn [item]
(= (grammar-start grammar) (item-lhs item)))
state))
; Code generation
(def ^:dynamic *display-item-closures* false)
(def ^:dynamic *trace-states* false)
(declare trace-state check-for-reduce-reduce-conflict check-for-shift-reduce-conflict)
(defn make-lookahead-matcher
[closure k input-name generate-matching else]
;; FIXME: k > 1
(assert (= k 1))
(let [accept-items (accept closure)
[empty non-empty] (partition-coll (fn [item] (empty? (item-lookahead item)))
accept-items)
empty (sort item<? empty)
non-empty (sort item<? non-empty)
non-empty-pairs (loop [items (seq non-empty)
lookaheads #{} ; case can't handle duplicates
cases []]
(if (empty? items)
cases
(let [item (first items)
la (first (item-lookahead item))]
(if (contains? lookaheads la)
(recur (rest items) lookaheads cases)
(recur (rest items) (conj lookaheads la)
(conj cases [la (generate-matching item)]))))))
non-empty-case `(~'case (~'pair-token (~'first ~input-name))
;; group by lookahead
~@(doall
(mapcat (fn [entry]
[(let [lis (map first (val entry))]
(if (empty? (rest lis))
(first lis)
lis))
(key entry)])
(group-by second non-empty-pairs)))
~else)]
(if (empty? empty)
non-empty-case
`(~'if (~'empty? ~input-name)
~(generate-matching (first empty))
~non-empty-case))))
(defn- parse-bar-name
[id]
(symbol (str "ds-parse-bar-" id)))
(defn- parse-name
[id]
(symbol (str "ds-parse-" id)))
(defn make-ds-parse
"Returns [code new-state-map todo-states]."
[state-map-0 grammar k compute-closure state]
(let [state-map-atom (atom state-map-0)
todo (atom '())
state-id (fn [state]
(or (get @state-map-atom state)
(let [next-id (count @state-map-atom)]
(swap! state-map-atom assoc state next-id)
(swap! todo conj state)
next-id)))
id (state-id state)
closure (compute-closure state grammar k)
attribute-names (map (fn [i]
(symbol (str "av-" i)))
(range 0 (active state)))
next-terms (next-terminals closure grammar)
next-nonterms (next-nonterminals closure grammar)
next-symbols (concat next-terms (next-nonterminals closure grammar))
input-name `in#
error-status-name `es#
pair-name `p#
parse `(~'defn- ~(parse-name id)
[~@attribute-names ~error-status-name ~input-name]
;; FIXME: lift this to the top level
(~'let [reduce# (~'fn []
~(make-lookahead-matcher closure k input-name
(fn [item]
(let [rhs-length (count (item-rhs item))
lhs (item-lhs item)
attribution (production-attribution (item-production item))
attribute-value `(~attribution
~@(reverse (take rhs-length attribute-names)))]
(if (zero? rhs-length)
`(~(parse-bar-name id) ~lhs ~attribute-value ~@attribute-names ~error-status-name ~input-name)
`(~'->RetVal ~lhs ~rhs-length ~attribute-value ~error-status-name nil ~input-name))))
`(~'parse-error "parse error" ~(expected-terminals closure grammar) ~input-name)))]
(~'if (~'empty? ~input-name)
;; FIXME: the reduce invocation knows whether input is empty or not
(reduce#)
(~'let [~pair-name (~'first ~input-name)
symbol# (~'pair-token ~pair-name)]
(~'case symbol#
~@(doall
(mapcat (fn [t]
[t
(let [next-state (goto closure t)
retval-name `rv#]
`(~'let [~(with-meta retval-name {:tag 'RetVal})
(~(parse-name (state-id next-state))
(~'pair-attribute-value ~pair-name)
~@(take (- (active next-state) 1) attribute-names)
~error-status-name
(~'rest ~input-name))]
~(if (empty? next-nonterms)
`(~'dec-dot ~retval-name)
`(~'cond
(~'> (.dot ~retval-name) 1)
(~'dec-dot ~retval-name)
~@(if (initial? closure grammar)
[`(~'= ~(grammar-start grammar) (.-lhs ~retval-name))
`(~'if (~'empty? (.-input ~retval-name))
~retval-name
(~'parse-error "input beyond EOF" #{} (.-input ~retval-name)))]
[])
:else
(~(parse-bar-name id)
(.lhs ~retval-name) (.-attribute-value ~retval-name)
~@attribute-names
~error-status-name
(.-input ~retval-name))))))])
next-terms))
(reduce#))))))
parse-bar (let [av-name `av#
retval-name `rv#
nonterm-name `nt#]
`(~'defn- ~(parse-bar-name id)
[~nonterm-name ~av-name ~@attribute-names ~error-status-name ~input-name]
(~'let [~(with-meta retval-name {:tag 'RetVal})
;; FIXME: group by cases
(~'case (~'int ~nonterm-name)
;; FIXME: optimize for when next-nonterms only has 1 element
~@(doall ; note that parse-name has a side effect
(mapcat (fn [nonterm]
(let [next-state (goto closure nonterm)]
[nonterm
`(~(parse-name (state-id next-state))
~av-name
~@(take (- (active next-state) 1) attribute-names)
~error-status-name
~input-name)]))
next-nonterms)))]
~(if (empty? next-nonterms)
`(~'dec-dot ~retval-name)
`(~'cond
(~'> (.dot ~retval-name) 1)
(~'dec-dot ~retval-name)
~@(if (initial? closure grammar)
[`(~'= ~(grammar-start grammar) (.-lhs ~retval-name))
`(~'if (~'empty? (.-input ~retval-name))
~retval-name
(~'parse-error "input beyond EOF" ~(expected-terminals closure grammar) (.-input ~retval-name)))]
[])
:else
(recur (.lhs ~retval-name) (.-attribute-value ~retval-name)
~@attribute-names
~error-status-name
(.-input ~retval-name)))))))
code [parse parse-bar]]
[code @state-map-atom @todo]))
(defn generate-ds-parse-functions
[grammar k compute-closure]
(let [start-state #{(make-item (grammar-start-production grammar) 0 '())}]
(loop [state-map {start-state 0}
todo (list start-state)
code []]
(if (empty? todo)
(do
(when *trace-states*
(doseq [[state index] (sort-by second state-map)]
(let [closure (compute-closure state grammar k)
accept-items (accept closure)]
(trace-state 1 closure index grammar)
(check-for-reduce-reduce-conflict closure accept-items grammar k)
(check-for-shift-reduce-conflict closure accept-items grammar k))))
code)
(let [[new-code state-map new-todos] (make-ds-parse state-map grammar k compute-closure (first todo))]
(recur state-map
(concat (rest todo) new-todos)
(concat code new-code)))))))
(defn write-ds-parse-ns
[grammar k method ns-name reqs writer-arg]
(let [compute-closure (case method
:lr compute-lr-closure
:slr compute-slr-closure)
fns (generate-ds-parse-functions grammar k compute-closure)]
(with-open [writer (clojure.java.io/writer writer-arg)]
(binding [*out* writer
*print-meta* true
*print-length* nil
*print-level* nil]
(doseq [form
`((~'ns ~ns-name
(:require
[active.lawrence.runtime :refer :all]
~@reqs)
(:import [active.lawrence.runtime ~'RetVal]))
(~'declare ~@(map second fns))
~@fns
~(let [input-name `input#]
`(~'defn ~'parse
[~input-name]
(~'let [^active.lawrence.runtime.RetVal retval# (~(parse-name 0) 3 (~'seq ~input-name))]
(.-attribute-value retval#)))))]
(prn form))))))
; Conflict handling
(defn conflict-items=?
[item-1 item-2]
(and (= (item-production item-1)
(item-production item-2))
(= (item-position item-1)
(item-position item-2))))
(defn conflict-items-present?
[item-1 item-2 list]
(some (fn [p]
(and (conflict-items=? item-1 (first p))
(conflict-items=? item-2 (first p))))
list))
(declare display-conflict)
(defn check-for-reduce-reduce-conflict
[closure accept-items grammar k]
(loop [items (seq accept-items)
done '()]
(when-not (empty? items)
(if-let [conflict-item (let [lookahead (item-lookahead (first items))]
(some (fn [item]
(and (= lookahead (item-lookahead item))
item))
(rest items)))]
(if (conflict-items-present? (first items) conflict-item done)
(recur (rest items) done)
(do
(display-conflict "Reduce-reduce" closure (first items) conflict-item grammar)
(recur (rest items)
(cons [(first items) conflict-item] done))))
(recur (rest items) done)))))
(defn check-for-shift-reduce-conflict
[closure accept-items grammar k]
(let [done (atom '())]
(doseq [item closure]
(let [rhs-rest (item-rhs-rest item)
lookahead (item-lookahead item)]
(if (and (not-empty rhs-rest)
(terminal? (first rhs-rest) grammar))
(let [lookaheads (sequence-first (concat rhs-rest lookahead)
k grammar)]
(doseq [conflict-item accept-items]
(if (and (contains? lookaheads (item-lookahead conflict-item))
(not (conflict-items-present? item conflict-item done)))
(do
(swap! done conj [item conflict-item])
(display-conflict "Shift-reduce" closure item conflict-item
grammar))))))))))
(defn tracing-states
[thunk]
(binding [*trace-states* true]
(thunk)))
(declare display-item display-closure display-items)
(defn display-conflict
[name closure item-1 item-2 grammar]
(print name "conflict between items")
(display-item item-1 grammar)
(print " and ")
(display-item item-2 grammar)
(println)
(when *display-item-closures*
(do
(print "State closure: ")
(display-closure closure true grammar))))
(defn display-closure
[closure predict? grammar]
(let [[predict core] (partition-coll predict-item? closure)]
(display-items core grammar)
(when predict?
(do
(println "----------------")
(display-items predict grammar)))))
(defn display-items
[items grammar]
(loop [items (sort-by item-lhs (seq items))]
(when (not-empty items)
(let [item (first items)]
(let [[this-items other-items] (partition-coll
(fn [other-item]
(and (= (item-production item)
(item-production other-item))
(= (item-position item)
(item-position other-item))))
items)]
(display-item item grammar)
(println " "
(mapv (fn [item]
(map (fn [s]
(grammar-symbol->name s grammar))
(item-lookahead item)))
this-items))
(recur other-items))))))
(defn display-item
[item grammar]
(print (grammar-symbol->name (item-lhs item) grammar)
"->")
(loop [rhs-symbols (item-rhs item)
position (item-position item)]
(cond
(not (empty? rhs-symbols))
(do
(print " ")
(when (zero? position)
(print ". "))
(print (grammar-symbol->name (first rhs-symbols) grammar))
(recur (rest rhs-symbols) (- position 1)))
(zero? position)
(print " ."))))
(defn trace-state
[trace-level closure index grammar]
(println "State " index)
(display-closure closure true grammar))
(declare display-trace-input)
(defn trace-enter
[trace-level closure input grammar]
(print "Entering state")
(display-trace-input input grammar)
(println ":")
(display-closure closure (>= trace-level 3) grammar))
(defn trace-reduce
[trace-level closure nonterminal attribute-value input grammar]
(print "Reducing with" (grammar-symbol->name nonterminal grammar))
(display-trace-input input grammar)
(println " yielding:")
(println attribute-value)
(println "after returning to:")
(newline)
(display-closure closure (>= trace-level 3) grammar))
(defn display-trace-input
[input grammar]
(print " (looking at ")
(if (not-empty input)
(do
(println (grammar-symbol->name (ffirst input) grammar) "," (second (first input))))
(print "EOF"))
(print ")"))
(defn trace-shift
[trace-level closure terminal grammar]
(println "Shifting with ") (grammar-symbol->name terminal grammar))
; List utilities
(defn number-coll<?
[ts1 ts2]
(loop [ts1 (seq ts1)
ts2 (seq ts2)]
(cond
(empty? ts1) (not (empty? ts2))
(empty? ts2) false
(< (first ts1) (first ts2)) true
(> (first ts1) (first ts2)) false
:else (recur (rest ts1) (rest ts2)))))