-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
410 lines (382 loc) · 8.63 KB
/
main.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
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
package main
import (
"fmt"
"stack"
"time"
)
var (
startSymbol = "expr"
EOF = "$"
productions = map[string][][]string{
"expr": {
{"term", "expr'"}},
"expr'": {
{"add", "term", "expr'"},
{"epsilon"}},
"term": {
{"factor", "term'"}},
"term'": {
{"multi", "factor", "term'"},
{"epsilon"}},
"factor": {
{"(", "expr", ")"},
{"digit"}},
"add": {
{"+"},
{"-"}},
"multi": {
{"*"},
{"/"}}}
/*productions map[string][][]string = map[string][][]string{
"stmt": {
{"if-stmt", "$"},
{"other"}},
"if-stmt": {
{"if", "(", "exp", ")", "stmt", "else-part"}},
"else-part": {
{"else", "stmt"},
{"epsilon"}},
"exp": {
{"0"},
{"1"}}}*/
firstsLog = make(map[string][]bool)
followsPos = make(map[string]map[string][]int)
nonTerminals, terminals = Set{}, Set{}
firsts = make(map[string][]Set)
follows = make(map[string]Set)
followsFinding = Set{}
table = make(map[string]map[string][]string)
null = make(map[string]bool)
)
type Set map[string]struct{}
func NewSet(i ...string) Set {
s := Set{}
for _, x := range i {
s[x] = struct{}{}
}
return s
}
func (s Set) String() string {
str := "[ "
for i := range s {
str += i + " "
}
return str + "]"
}
func (a Set) has(s string) bool {
_, ok := a[s]
return ok
}
func (a Set) insert(s string) {
a[s] = struct{}{}
}
func (a Set) union(b Set) Set {
r := Set{}
for i := range a {
r[i] = struct{}{}
}
for i := range b {
r[i] = struct{}{}
}
return r
}
func main() {
makeNonTerminals()
makeTerminals()
makeNulls()
fmt.Println("nonTerminal\n", nonTerminals)
fmt.Println("terminal\n", terminals)
fmt.Println("Null\n", null)
makeFirsts()
fmt.Println("First\n", firsts)
makeFollows()
fmt.Println("Follows\n", follows)
makeTable()
fmt.Println("Table\n", table)
show()
parse([]string{"digit", "+", "digit", "*", "digit"})
fmt.Println("parsing done")
}
func makeNonTerminals() {
for nonTerminal := range productions {
nonTerminals.insert(nonTerminal)
}
}
func makeTerminals() {
for nonTerminal := range nonTerminals {
for _, production := range productions[nonTerminal] {
for _, token := range production {
if !nonTerminals.has(token) && token != "epsilon" {
terminals.insert(token)
}
}
}
}
terminals.insert(EOF)
}
func makeNulls() {
for nonTerminal := range nonTerminals {
if _, exist := null[nonTerminal]; exist {
continue
}
null[nonTerminal] = makeNull(nonTerminal)
}
}
func makeNull(nonTerminal string) bool {
//A -> B|C|epsilon, Null(A) = T
for _, production := range productions[nonTerminal] {
if len(production) == 1 && production[0] == "epsilon" {
return true
}
}
var (
isBorC_Null = false
isBandC_Null = true
)
//A -> B|C, Null(A) = Null(B) || Null(C)
for _, production := range productions[nonTerminal] {
isBandC_Null = true
//A -> BC, Null(A) = Null(B) && Null(C)
for _, token := range production {
//A -> Ba|xC, since a and x, Null(A) = F
if terminals.has(token) {
isBandC_Null = false
break
}
if _, exist := null[token]; !exist {
null[token] = makeNull(token)
}
isBandC_Null = isBandC_Null && null[token]
}
isBorC_Null = isBorC_Null || isBandC_Null
}
return isBorC_Null
}
func isNull(production []string) bool {
//A -> B|C|epsilon, Null(A) = T
if len(production) == 1 && production[0] == "epsilon" {
return true
}
isBandC_Null := true
//A -> BC, Null(A) = Null(B) && Null(C)
for _, token := range production {
//A -> Ba|xC, since a and x, Null(A) = F
if terminals.has(token) {
isBandC_Null = false
break
}
isBandC_Null = isBandC_Null && null[token]
}
return isBandC_Null
}
func makeFirsts() {
//init firstsLog = {expr:{false}, expr':{false, false}...}
for nonTerminal := range nonTerminals {
for i := 0; i < len(productions[nonTerminal]); i++ {
firstsLog[nonTerminal] = append(
firstsLog[nonTerminal], false)
}
}
for nonTerminal := range nonTerminals {
for i, production := range productions[nonTerminal] {
if !firstsLog[nonTerminal][i] {
firsts[nonTerminal] = append(
firsts[nonTerminal],
first(nonTerminal, production, i))
}
}
}
}
func first(nonTerminal string, production []string, I int) Set {
if nonTerminals.has(nonTerminal) {
firstsLog[nonTerminal][I] = true
}
s := NewSet()
//first() = {}
if len(production) == 0 {
return s
}
//first(epsilon) = {}
if len(production) == 1 && production[0] == "epsilon" {
return s
}
//first(a beta) = {a}
if terminals.has(production[0]) {
s.insert(production[0])
return s
}
//first(alpha beta) = ?
for _, token := range production {
//first(alpha) = first(alpha1) U ... U first(alpha n)
for i, production := range productions[token] {
if !firstsLog[token][i] {
firsts[token] = append(
firsts[token], first(token, production, i))
}
s = s.union(firsts[token][i])
}
//if Null(alpha) = F, first(alpha beta) = first(alpha)
if !null[token] {
return s
}
//if Null(alpha) = T,
// first(alpha beta) = first(alpha) U first(beta)
}
return s
}
func makeFollows() {
//make followsPos
for nonTerminal := range nonTerminals {
for I, production := range productions[nonTerminal] {
for i, token := range production {
//N -> alpha Y beta, N != Y
if token == nonTerminal ||
!nonTerminals.has(token) {
continue
}
if _, ok := followsPos[token]; ok {
followsPos[token][nonTerminal] = []int{I, i}
} else {
followsPos[token] = map[string][]int{
nonTerminal: {I, i}}
}
}
}
}
for token, inWhichNonTerminal := range followsPos {
if _, exist := follows[token]; !exist {
followsFinding.insert(token)
s := NewSet()
for nonTerminal, pos := range inWhichNonTerminal {
if _, exist := followsFinding[nonTerminal]; exist {
continue
}
s = s.union(follow(nonTerminal, token, pos))
}
follows[token] = s
delete(followsFinding, token)
}
}
}
func follow(nonTerminal, token string, pos []int) Set {
production := productions[nonTerminal][pos[0]]
s := NewSet()
if token == startSymbol {
s.insert(EOF)
}
if pos[1] < len(production)-1 {
s = s.union(first("", production[pos[1]+1:], pos[0]))
//N -> alpha Y beta, Null(beta) = F,
//follow(Y) U= first(beta)
isBandC_Null := true
//A -> BC, Null(A) = Null(B) && Null(C)
for _, token := range production[pos[1]+1:] {
//A -> Ba|xC, since a and x, Null(A) = F
if terminals.has(token) {
isBandC_Null = false
break
}
isBandC_Null = isBandC_Null && null[token]
}
if !isBandC_Null {
return s
}
}
//N -> alpha Y beta, Null(beta) = T,
//follow(Y) U= first(beta) U follow(N)
if _, exist := follows[nonTerminal]; !exist {
followsFinding.insert(token)
s2 := NewSet()
for nonTerminal_, pos := range followsPos[nonTerminal] {
if _, exist := followsFinding[nonTerminal_]; exist {
continue
}
s2 = s2.union(follow(nonTerminal_, nonTerminal, pos))
}
follows[nonTerminal] = s2
delete(followsFinding, token)
return s.union(s2)
}
return s.union(follows[nonTerminal])
}
func makeTable() {
for nonTerminal := range nonTerminals {
for terminal := range terminals {
if _, exist := table[terminal]; !exist {
table[terminal] = map[string][]string{
nonTerminal: []string{}}
} else {
table[terminal][nonTerminal] = []string{}
}
}
}
for nonTerminal, NTfirst := range firsts {
for i, first := range NTfirst {
s := NewSet()
s = s.union(first)
if isNull(productions[nonTerminal][i]) {
s = s.union(follows[nonTerminal])
}
for terminal := range s {
table[terminal][nonTerminal] = append(
table[terminal][nonTerminal],
productions[nonTerminal][i]...)
}
}
}
}
func Null(production []string) bool {
if len(production) == 1 && production[0] == "epsilon" {
return true
}
return false
}
func contains(token string, lst []string) bool {
for _, value := range lst {
if token == value {
return true
}
}
return false
}
func show() {
for terminal := range table {
fmt.Println("===============")
fmt.Println(terminal)
for nonTerminal, val := range table[terminal] {
fmt.Printf("%-10s%-5s\n", nonTerminal, val)
}
}
}
func parse(s []string) {
s = append(s, EOF)
stk := new(stack.Stack)
stk.Push(EOF)
stk.Push(startSymbol)
for {
time.Sleep(time.Second)
top := stk.Top()
fmt.Println(stk)
if top == "epsilon" {
stk.Pop()
}
if terminals.has(top) {
if top == EOF {
break
}
if top == s[0] {
stk.Pop()
s = s[1:]
} else {
fmt.Println("invalid string")
break
}
}
if nonTerminals.has(top) {
stk.Pop()
for i := len(table[s[0]][top]) - 1; i >= 0; i-- {
stk.Push(table[s[0]][top][i])
}
}
}
}