-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdfa_helpers.go
279 lines (266 loc) · 7.98 KB
/
dfa_helpers.go
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
package dfa
import (
"fmt"
"github.com/timtadh/lexmachine/frontend"
)
// LabeledAST is a post-order labeled version of the AST. The root the node will be Order[len(Order)-1].
type LabeledAST struct {
Root frontend.AST // root of the AST
Order []frontend.AST // post-order labeling of the all the nodes
Kids [][]int // a lookup table of location for each of the nodes children
Positions []int // a labeling of all the position nodes (Character/Range) in the AST.
posmap map[int]int // maps an order index to a pos index.
Matches []int // the index (in Positions) of all the EOS nodes
nullable []bool
first [][]int
last [][]int
follow []map[int]bool
}
// Label a tree and its "positions" (Character/Range nodes) in post-order
// notation.
func Label(ast frontend.AST) *LabeledAST {
type entry struct {
n frontend.AST
i int
kids []int
}
order := make([]frontend.AST, 0, 10)
children := make([][]int, 0, 10)
positions := make([]int, 0, 10)
matches := make([]int, 0, 10)
posmap := make(map[int]int)
stack := make([]entry, 0, 10)
stack = append(stack, entry{ast, 0, []int{}})
for len(stack) > 0 {
var c entry
stack, c = stack[:len(stack)-1], stack[len(stack)-1]
kids := c.n.Children()
for c.i < len(kids) {
kid := kids[c.i]
stack = append(stack, entry{c.n, c.i + 1, c.kids})
c = entry{kid, 0, []int{}}
kids = c.n.Children()
}
oid := len(order)
if len(stack) > 0 {
stack[len(stack)-1].kids = append(stack[len(stack)-1].kids, oid)
}
order = append(order, c.n)
children = append(children, c.kids)
switch c.n.(type) {
case *frontend.Character, *frontend.Range:
posmap[oid] = len(positions)
positions = append(positions, oid)
case *frontend.EOS:
pid := len(positions)
posmap[oid] = pid
positions = append(positions, oid)
matches = append(matches, pid)
}
}
return &LabeledAST{
Root: ast,
Order: order,
Kids: children,
Positions: positions,
posmap: posmap,
Matches: matches,
}
}
func (a *LabeledAST) pos(oid int) int {
if pid, has := a.posmap[oid]; !has {
panic("Passed a bad order id into Position (likely used a non-position node's id)")
} else {
return pid
}
}
// Follow computes look up tables for each Position (leaf node) in the tree
// which indicates what other Position could follow the current position in a
// matching string. It also computes what positions appear first in the tree.
func (a *LabeledAST) Follow() (firstOfTree []int, follow []map[int]bool) {
positions := a.Positions
nullable := a.MatchesEmptyString()
first := a.First()
last := a.Last()
// get the first of the whole ast by retrieving the first for the root (len(order)-1).
firstOfTree = make([]int, 0, len(first[len(a.Order)-1]))
for _, p := range first[len(a.Order)-1] {
firstOfTree = append(firstOfTree, p)
}
if a.follow != nil {
return firstOfTree, a.follow
}
follow = make([]map[int]bool, len(positions))
for i := range follow {
follow[i] = make(map[int]bool)
}
for i, node := range a.Order {
switch n := node.(type) {
case *frontend.Concat:
for x := 0; x < len(n.Items)-1; x++ {
j := a.Kids[i][x]
kFirst := make([]int, 0, 10)
for y := x + 1; y < len(n.Items); y++ {
k := a.Kids[i][y]
for _, p := range first[k] {
kFirst = append(kFirst, p)
}
if !nullable[k] {
break
}
}
for _, p := range last[j] {
for _, q := range kFirst {
follow[p][q] = true
}
}
}
case *frontend.Star, *frontend.Plus:
nFirst := make([]int, 0, 10)
for _, p := range first[i] {
nFirst = append(nFirst, p)
}
for _, p := range last[i] {
for _, q := range nFirst {
follow[p][q] = true
}
}
}
}
a.follow = follow
return firstOfTree, follow
}
// MatchesEmptyString computes a look up table for each node in the tree (in
// post order, matching the Order member) on whether or not the subtree rooted
// in each node matches the empty string.
func (a *LabeledAST) MatchesEmptyString() (nullable []bool) {
if a.nullable != nil {
return a.nullable
}
nullable = make([]bool, 0, len(a.Order))
for i, node := range a.Order {
switch n := node.(type) {
case *frontend.EOS:
nullable = append(nullable, true)
case *frontend.Character:
nullable = append(nullable, false)
case *frontend.Range:
nullable = append(nullable, false)
case *frontend.Maybe:
nullable = append(nullable, true)
case *frontend.Plus:
nullable = append(nullable, nullable[a.Kids[i][0]])
case *frontend.Star:
nullable = append(nullable, true)
case *frontend.Match:
nullable = append(nullable, nullable[a.Kids[i][0]])
case *frontend.Alternation:
nullable = append(nullable, nullable[a.Kids[i][0]] || nullable[a.Kids[i][1]])
case *frontend.AltMatch:
nullable = append(nullable, nullable[a.Kids[i][0]] || nullable[a.Kids[i][1]])
case *frontend.Concat:
epsilon := true
for _, j := range a.Kids[i] {
epsilon = epsilon && nullable[j]
}
nullable = append(nullable, epsilon)
default:
panic(fmt.Errorf("Unexpected type %T", n))
}
}
a.nullable = nullable
return nullable
}
// First computes a look up table for each node in the tree (in post order,
// matching the Order member) indicating for the subtree rooted at each node
// which positions (leaf nodes indexes in the Positions slice) will appear at
// the beginning of a string matching that subtree.
func (a *LabeledAST) First() (first [][]int) {
if a.first != nil {
return a.first
}
nullable := a.MatchesEmptyString()
first = make([][]int, 0, len(a.Order))
for i, node := range a.Order {
switch n := node.(type) {
case *frontend.EOS:
first = append(first, []int{a.pos(i)})
case *frontend.Character:
first = append(first, []int{a.pos(i)})
case *frontend.Range:
first = append(first, []int{a.pos(i)})
case *frontend.Maybe:
first = append(first, first[a.Kids[i][0]])
case *frontend.Plus:
first = append(first, first[a.Kids[i][0]])
case *frontend.Star:
first = append(first, first[a.Kids[i][0]])
case *frontend.Match:
first = append(first, first[a.Kids[i][0]])
case *frontend.Alternation:
first = append(first, append(first[a.Kids[i][0]], first[a.Kids[i][1]]...))
case *frontend.AltMatch:
first = append(first, append(first[a.Kids[i][0]], first[a.Kids[i][1]]...))
case *frontend.Concat:
f := make([]int, 0, len(n.Items))
for _, j := range a.Kids[i] {
f = append(f, first[j]...)
if !nullable[j] {
break
}
}
first = append(first, f)
default:
panic(fmt.Errorf("Unexpected type %T", n))
}
}
a.first = first
return first
}
// Last computes a look up table for each node in the tree (in post order,
// matching the Order member) indicating for the subtree rooted at each node
// which positions (leaf nodes indexes in the Positions slice) will appear at
// the end of a string matching that subtree.
func (a *LabeledAST) Last() (last [][]int) {
if a.last != nil {
return a.last
}
nullable := a.MatchesEmptyString()
last = make([][]int, 0, len(a.Order))
for i, node := range a.Order {
switch n := node.(type) {
case *frontend.EOS:
last = append(last, []int{a.pos(i)})
case *frontend.Character:
last = append(last, []int{a.pos(i)})
case *frontend.Range:
last = append(last, []int{a.pos(i)})
case *frontend.Maybe:
last = append(last, last[a.Kids[i][0]])
case *frontend.Plus:
last = append(last, last[a.Kids[i][0]])
case *frontend.Star:
last = append(last, last[a.Kids[i][0]])
case *frontend.Match:
last = append(last, last[a.Kids[i][0]])
case *frontend.Alternation:
last = append(last, append(last[a.Kids[i][0]], last[a.Kids[i][1]]...))
case *frontend.AltMatch:
last = append(last, append(last[a.Kids[i][0]], last[a.Kids[i][1]]...))
case *frontend.Concat:
l := make([]int, 0, len(n.Items))
for x := len(n.Items) - 1; x >= 0; x-- {
j := a.Kids[i][x]
l = append(l, last[j]...)
if !nullable[j] {
break
}
}
last = append(last, l)
default:
panic(fmt.Errorf("Unexpected type %T", n))
}
}
a.last = last
return last
}