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
|
package compiler
import (
"fmt"
"sort"
"strings"
)
type symbolPosition uint8
const (
symbolPositionNil = symbolPosition(0)
symbolPositionMaskSymbol = uint8(0x00) // 0000 0000
symbolPositionMaskEndMark = uint8(0x80) // 1000 0000
symbolPositionMaskValue = uint8(0x7f) // 0111 1111
)
func newSymbolPosition(n uint8, endMark bool) symbolPosition {
if endMark {
return symbolPosition(n | symbolPositionMaskEndMark)
}
return symbolPosition(n | symbolPositionMaskSymbol)
}
func (p symbolPosition) String() string {
if p.isEndMark() {
return fmt.Sprintf("end#%v", uint8(p)&symbolPositionMaskValue)
}
return fmt.Sprintf("sym#%v", uint8(p)&symbolPositionMaskValue)
}
func (p symbolPosition) isEndMark() bool {
if uint8(p)&symbolPositionMaskEndMark > 1 {
return true
}
return false
}
func (p symbolPosition) describe() (uint8, bool) {
v := uint8(p) & symbolPositionMaskValue
if p.isEndMark() {
return v, true
}
return v, false
}
type symbolPositionSet map[symbolPosition]struct{}
func newSymbolPositionSet() symbolPositionSet {
return map[symbolPosition]struct{}{}
}
func (s symbolPositionSet) String() string {
if len(s) <= 0 {
return "{}"
}
ps := s.sort()
var b strings.Builder
fmt.Fprintf(&b, "{")
for i, p := range ps {
if i <= 0 {
fmt.Fprintf(&b, "%v", p)
continue
}
fmt.Fprintf(&b, ", %v", p)
}
fmt.Fprintf(&b, "}")
return b.String()
}
func (s symbolPositionSet) add(pos symbolPosition) symbolPositionSet {
s[pos] = struct{}{}
return s
}
func (s symbolPositionSet) merge(t symbolPositionSet) symbolPositionSet {
for p := range t {
s.add(p)
}
return s
}
func (s symbolPositionSet) intersect(set symbolPositionSet) symbolPositionSet {
in := newSymbolPositionSet()
for p1 := range s {
for p2 := range set {
if p1 != p2 {
continue
}
in.add(p1)
}
}
return in
}
func (s symbolPositionSet) hash() string {
if len(s) <= 0 {
return ""
}
sorted := s.sort()
var b strings.Builder
fmt.Fprintf(&b, "%v", sorted[0])
for _, p := range sorted[1:] {
fmt.Fprintf(&b, ":%v", p)
}
return b.String()
}
func (s symbolPositionSet) sort() []symbolPosition {
sorted := []symbolPosition{}
for p := range s {
sorted = append(sorted, p)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
return sorted
}
type astNode interface {
fmt.Stringer
children() (astNode, astNode)
nullable() bool
first() symbolPositionSet
last() symbolPositionSet
}
type symbolNode struct {
token *token
value byte
pos symbolPosition
}
func newSymbolNode(tok *token, value byte, pos symbolPosition) *symbolNode {
return &symbolNode{
token: tok,
value: value,
pos: pos,
}
}
func (n *symbolNode) String() string {
return fmt.Sprintf("{type: char, char: %v, int: %v, pos: %v}", string(n.token.char), n.token.char, n.pos)
}
func (n *symbolNode) children() (astNode, astNode) {
return nil, nil
}
func (n *symbolNode) nullable() bool {
return false
}
func (n *symbolNode) first() symbolPositionSet {
s := newSymbolPositionSet()
s.add(n.pos)
return s
}
func (n *symbolNode) last() symbolPositionSet {
s := newSymbolPositionSet()
s.add(n.pos)
return s
}
type endMarkerNode struct {
id int
pos symbolPosition
}
func newEndMarkerNode(id int, pos symbolPosition) *endMarkerNode {
return &endMarkerNode{
id: id,
pos: pos,
}
}
func (n *endMarkerNode) String() string {
return fmt.Sprintf("{type: end, pos: %v}", n.pos)
}
func (n *endMarkerNode) children() (astNode, astNode) {
return nil, nil
}
func (n *endMarkerNode) nullable() bool {
return false
}
func (n *endMarkerNode) first() symbolPositionSet {
s := newSymbolPositionSet()
s.add(n.pos)
return s
}
func (n *endMarkerNode) last() symbolPositionSet {
s := newSymbolPositionSet()
s.add(n.pos)
return s
}
type concatNode struct {
left astNode
right astNode
}
func newConcatNode(left, right astNode) *concatNode {
return &concatNode{
left: left,
right: right,
}
}
func (n *concatNode) String() string {
return fmt.Sprintf("{type: concat}")
}
func (n *concatNode) children() (astNode, astNode) {
return n.left, n.right
}
func (n *concatNode) nullable() bool {
return n.left.nullable() && n.right.nullable()
}
func (n *concatNode) first() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.left.first())
if n.left.nullable() {
s.merge(n.right.first())
}
return s
}
func (n *concatNode) last() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.right.last())
return s
}
type altNode struct {
left astNode
right astNode
}
func newAltNode(left, right astNode) *altNode {
return &altNode{
left: left,
right: right,
}
}
func (n *altNode) String() string {
return fmt.Sprintf("{type: alt}")
}
func (n *altNode) children() (astNode, astNode) {
return n.left, n.right
}
func (n *altNode) nullable() bool {
return n.left.nullable() || n.right.nullable()
}
func (n *altNode) first() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.left.first())
s.merge(n.right.first())
return s
}
func (n *altNode) last() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.left.last())
s.merge(n.right.last())
return s
}
type repeatNode struct {
left astNode
}
func newRepeatNode(left astNode) *repeatNode {
return &repeatNode{
left: left,
}
}
func (n *repeatNode) String() string {
return fmt.Sprintf("{type: repeat}")
}
func (n *repeatNode) children() (astNode, astNode) {
return n.left, nil
}
func (n *repeatNode) nullable() bool {
return true
}
func (n *repeatNode) first() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.left.first())
return s
}
func (n *repeatNode) last() symbolPositionSet {
s := newSymbolPositionSet()
s.merge(n.left.last())
return s
}
type followTable map[symbolPosition]symbolPositionSet
func genFollowTable(root astNode) followTable {
follow := followTable{}
calcFollow(follow, root)
return follow
}
func calcFollow(follow followTable, ast astNode) {
if ast == nil {
return
}
left, right := ast.children()
calcFollow(follow, left)
calcFollow(follow, right)
switch n := ast.(type) {
case *concatNode:
l, r := n.children()
for _, p := range l.last().sort() {
if _, ok := follow[p]; !ok {
follow[p] = newSymbolPositionSet()
}
follow[p].merge(r.first())
}
case *repeatNode:
for _, p := range n.last().sort() {
if _, ok := follow[p]; !ok {
follow[p] = newSymbolPositionSet()
}
follow[p].merge(n.first())
}
}
}
func positionSymbols(node astNode, n uint8) uint8 {
if node == nil {
return n
}
l, r := node.children()
p := n
p = positionSymbols(l, p)
p = positionSymbols(r, p)
switch n := node.(type) {
case *symbolNode:
n.pos = newSymbolPosition(p, false)
p++
case *endMarkerNode:
n.pos = newSymbolPosition(p, true)
p++
}
return p
}
|