/
earley.clj
135 lines (111 loc) · 4.82 KB
/
earley.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
(ns clearley.earley
(require [clearley.collections.ordered-set :as os]
[clearley.collections.ordered-multimap :as omm]
[clearley.npda :as npda])
(use [clearley utils rules]))
; ===
; Parse items
; An Item is a rule together with some instrumentation.
; Items are the atoms of LR-automaton parsing.
; ===
; rulehead: the clause predicting this item
; TODO reexamine ruleheads?
; rule: the rule for this item
; original: the original (unadvanced) rule, used to populate matches
; match-count: the number of times this rule has been scanned or advanced
(defrecord Item [rulehead rule original match-count]
PStrable
(pstr [_]
(str rulehead " -> " (rule-str rule))))
(defn new-item [head-sym clause]
(let [rule (to-rule clause)]
(Item. head-sym rule rule 0)))
(defn predict-item [item grammar]
(let [clause (predict (:rule item))]
(map #(new-item (rulehead-clause clause) %)
(predict-clause clause grammar))))
(defn scan-item [item input-token]
(map #(update (assoc item :rule %) :match-count inc)
(scan (:rule item) input-token)))
(defn advance-item [item]
(update-all item {:rule advance, :match-count inc}))
; ===
; Item sets
; ===
(defn pstr-item-set-item [item predictor-map]
(let [predictor-str
(cutoff (separate-str ", " (map pstr (omm/get-vec predictor-map
(:original item)))))]
(str (pstr item) (if (seq predictor-str) (str " | " predictor-str)))))
(declare shift-item-set reduce-item-set item-set-reductions)
; items: a vector of items
; predictor-map: ordered multimap, items -> internal predicting items
(defrecord ItemSet [items predictor-map grammar]
npda/Node
(npda/shift [self input] (shift-item-set self input))
(npda/reduce [self output] (reduce-item-set self output))
(npda/reductions [self] (item-set-reductions self))
PStrable
(pstr [self]
(with-out-str
(runmap println (map #(pstr-item-set-item % predictor-map) items)))))
(defn predict-into-item-set [{:keys [items predictor-map] :as item-set}
{original :original :as item} predictor]
(if (empty? (omm/get-vec predictor-map original))
(update-all item-set {:items #(conj % item)
:predictor-map #(omm/assoc % original predictor)})
(update item-set :predictor-map #(omm/assoc % original predictor))))
(defn current-item [{items :items} dot]
(when-not (>= dot (count items)) (get items dot)))
; TODO: predicting completed items seems to cause combinatorial explosion
(defn close-item-set [item-set]
(loop [c item-set, dot 0]
(if-let [s (current-item c dot)]
(recur (if (is-complete? (:rule s))
c
(reduce #(predict-into-item-set % %2 s)
c (predict-item s (:grammar item-set))))
(inc dot))
c)))
; seed items don't go in predictor-map, closed items do
(defn new-item-set [items grammar]
(close-item-set (ItemSet. (vec items) omm/empty grammar)))
; scans an input character, seeding a new state
(defn shift-item-set [{:keys [items grammar] :as item-set} input-token]
(when-let [r (seq (mapcat #(scan-item % input-token) items))]
(new-item-set r grammar)))
; Reduces an item given a stack-top item-set
(defn reduce-item-set [item-set {:keys [original]}]
(when-let [new-items
(seq (map advance-item (omm/get-vec (:predictor-map item-set) original)))]
[(new-item-set new-items (:grammar item-set))]))
; TODO something broken here?
(defn item-set-reductions [{items :items}]
(map (fn [{:keys [match-count] :as item}] [item match-count])
(filter (fn-> :rule is-complete?) items)))
; ===
; Using the automaton
; ===
(defn is-goal [state]
(some (fn-> :rulehead (= ::goal)) (-> state npda/peek :items)))
; Builds a rule match from the output stack and pushes the match to the top
; (think of a Forth operator reducing the top of a stack)
; Final output (for a valid parse) will be a singleton list
(defn reduce-ostream-helper [ostream item]
(if (instance? clearley.earley.Item item)
(let [{:keys [match-count original]} item]
(cons (match original (vec (reverse (take match-count ostream))))
(drop match-count ostream)))
(cons (match item []) ostream)))
(defn reduce-ostream [ostream]
(first (reduce reduce-ostream-helper '() ostream)))
#_(defn parse [input-str grammar tokenizer goal]
(npda/run-automaton-2 (new-item-set [(new-item ::goal goal)] grammar)
input-str tokenizer))
(defn parse-charts [input-str grammar tokenizer goal]
(npda/run-automaton (new-item-set [(new-item ::goal goal)] grammar)
input-str tokenizer))
; Searches states for completed parse of the goal rule, returning all matches
(defn scan-goal [chart]
(map (fn-> npda/popone npda/stream reduce-ostream)
(filter is-goal (npda/states chart))))