-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
694 lines (561 loc) · 20.8 KB
/
main.py
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
import re
import sys
tokens = []
pretokens = []
stringList = []
regexes = []
epsilon = '`'
start_symbol = 0
class Item(object):
def __init__(self, lhs, rhs, dpos, origin, ref=[], cc=0):
self.lhs = lhs
self.rhs = rhs
self.dpos = dpos
self.origin = origin
self.ref = ref #Array of Items
self.column = cc
def __str__(self):
return str(self.lhs)+str(self.rhs)
class Token(object):
def __init__(self, sym, lex, lnum):
self.symbol = sym
self.lexeme = lex
self.linenumber = lnum
def __str__(self):
strr = ""
strr += "SYMBOL: "
strr += str(self.symbol)
strr += " LEXEME: "
strr += str(self.lexeme)
strr += " LINE: "
strr += str(self.linenumber)
return str(strr)
def printIdentity(self):
print("(sym=\""+self.symbol+"\", lexeme=\""+self.lexeme+"\")")
class Grammar(object):
def __init__(self, txt, terms, nonterms, prods):
self.text = txt
self.terminals = terms
self.nonterminals = nonterms
self.productions = prods
self.start = 'S'
def __str__(self):
st = ""
for x in self.text:
st += '\n'
st += x
return st
class Production(object):
def __init__(self, thing):
self.thing = thing
self.dpos = 0
self.lhs = ""
self.rhs = ""
self.rhsString = ""
statemnt = r"(.+)(\s->\s)(.+)"
regexx = re.compile(statemnt)
s = self.thing
m = regexx.search(s, 0, len(s))
if m:
self.lhs = str(m.group(1))
self.rhs = str(m.group(3))
self.rhsString = self.rhs
self.rhs = self.rhs.split(' ')
else:
raise TypeError("ERROR! BAD PRODUCTION IN FORM: ", self.thing)
def __str__(self):
return str(self.thing)
class TreeNode(object):
def __init__(self, sym, tok, parent=None, children=[]):
self.sym = sym
self.token = tok
self.parent = parent
self.children = children
def __str__(self):
return str(self.sym)
def prepend_child(self, chile):
self.children.append(chile)
def tokenize(dotest, debug, fnm):
global pretokens, tokens, stringList, regexes
LineCounter = 0
terms = []
FILE = 0
if dotest:
FILE = open("testRegexes.txt")
else:
FILE = open("regexes.txt")
for line in FILE:
LineCounter += 1
xx = re.match(r'(.+) -> (.*)', line)
pretokens.append([xx.group(1), re.compile(xx.group(2), re.IGNORECASE)])
terms.append(xx.group(1))
#print(LineCounter, "lines in the Regular Expression file.")
#print(terms)
# Populate terminals and nonterminals
nonT = open('germinals.txt', 'w')
for nn in range(len(terms)):
nonT.write(terms[nn])
if nn < len(terms)-1:
nonT.write(' ')
nonT.close()
effr = open(fnm, 'r+')
inputString = ""
for l in effr:
inputString += str(l)
lengthofstr = len(inputString)
#print("Input string:", inputString)
token_container = []
i = 0
line = 1
while i < lengthofstr:
for sym, regex in pretokens:
m = regex.match(inputString, i)
if m:
if debug:
print(sym)
if m.start == i or m.start() == i:
if sym != "whitespace" and sym != "comments":
token_container.append(Token(sym, m.group(0), line))
line += m.group(0).count("\n")
i = m.end()
break
else:
errorTok = ""
for i in range(i, lengthofstr):
if inputString[i] == ' ' or inputString[i] == '\n' or inputString[i] == '.':
break
if inputString[i] == '!':
break
if inputString[i] == '?':
break
if inputString[i] == ',':
break
errorTok += inputString[i]
print("ERROR: Cannot match token", errorTok)
return 1
effr.close()
FILE.close()
tokens = token_container
return token_container
def computeNullable(gramm):
global epsilon
nullable = set()
loop_broke = False
stable = False
ctr = 0
xctr = 0
fexilon = False
for p in gramm.productions:
for x in p.rhs:
if x == epsilon:
fexilon = True
c = set([p.lhs])
nullable = nullable.union(c)
# print(nullable)
break
# If lambda does not exist in any production -> return
if not fexilon:
return nullable
while not stable:
for n in gramm.nonterminals:
if n not in nullable:
for p in gramm.productions:
if p.lhs == n:
x = p.rhs
for xx in x:
if xx not in nullable:
loop_broke = True
xctr += 1
if xctr >= (len(gramm.nonterminals) * 2):
stable = True
break
if not loop_broke:
bb = set([n])
cc = nullable.union(bb)
nullable = cc
ctr += 1
if ctr >= 1:
stable = True
foundE = False
loop_broke = False
return nullable
def computeFirst(gramm):
''' Return a dict(list) of each nonterminal with its corresponding first set '''
global start_symbol
first = {}
nulls = computeNullable(gramm)
#print("NULLABLE:", nulls)
stable = False
udates = 0
for t in gramm.terminals:
first[t] = set([t])
# Initialize NonTerminals in first set
for tt in gramm.nonterminals:
first[tt] = set()
print('')
while not stable:
for n in gramm.nonterminals:
for p in gramm.productions:
if p.lhs == n:
for x in p.rhs:
if x != epsilon and x != ' ':
c = first[n].union(first[x])
first[n] = c
# print("first[",n,"]:", first[n])
udates += 1
if x not in nulls:
break
if udates >= len(gramm.nonterminals) * len(gramm.terminals):
stable = True
return [first, nulls]
def computeFollow(gramm, firstSet, nullableSet):
'''Computes and returns a set of follows for each production in the
specified grammar. '''
global start_symbol
follow = {start_symbol: set(['$'])}
# Initialize NonTerminals in follow set
for tt in gramm.nonterminals:
if tt != start_symbol:
follow[tt] = set()
stable = False
broke_complex = False
udates = 0
while not stable:
for n in gramm.nonterminals:
for p in gramm.productions:
if p.lhs == n:
for i in range(len(p.rhs)):
x = p.rhs[i]
if x in gramm.nonterminals:
for y in p.rhs[i + 1:]:
c = follow[x].union(set(firstSet[y]))
follow[x] = c
udates += 1
if y not in nullableSet:
broke_complex = True
break
if not broke_complex:
c = follow[x].union(set(follow[n]))
follow[x] = c
udates += 1
else:
broke_complex = False
if udates >= len(gramm.nonterminals) * (len(gramm.terminals) * 2):
stable = True
return follow
def makeTree(Item, TNode):
global tokens
if Item.origin == "INITIAL":
print("INITIAL")
TNode.sym = "S'"
TNode.parent = None
TNode.children.append("S")
return TNode
elif Item.origin[0] == "S": #symbol token
#print("Item LHS:", Item.lhs)
#print("Item RHS:", Item.rhs)
#print("Item DPOS:", Item.dpos)
#print("Item Ref:", Item.ref)
if Item.dpos > len(Item.rhs):
tnode = TreeNode(Item.rhs[Item.dpos - 2], tokens[Item.column])
elif Item.dpos == len(Item.rhs):
tnode = TreeNode(Item.rhs[Item.dpos - 1], tokens[Item.column])
elif Item.dpos == 0:
tnode = TreeNode(Item.rhs[0], tokens[Item.column])
else:
tnode = TreeNode(Item.rhs[Item.dpos - 1], tokens[Item.column])
#print("Prepending Scan Node:", tnode)
TNode.prepend_child(tnode)
return makeTree(Item.ref[0], tnode)
elif Item.origin[0] == "P":
print("Returning None")
return 0
elif Item.origin[0] == "C":
completed = Item.ref[0] # Play with index
partial = Item.ref[1] # Play with index
print("Completed:", completed)
print("Partial:", partial)
print("Completed Ref:", completed.ref)
print("Partial Ref:", partial.ref)
Q = TreeNode(completed.lhs, "")
Q.parent = TNode
print("Prepending Complete Node:", Q)
TNode.prepend_child(Q)
if len(completed.ref) == 0:
completed.ref = Item.ref
if len(partial.ref) == 0:
partial.ref = Item.ref
makeTree(completed, Q)
return makeTree(partial, TNode)
#print("No Ret")
def earleyParse(iput, gramm, SSym, genTree, printCols):
global tokens
# • <- a very friendly dot :)
Ilen = len(iput)
if Ilen <= 1:
return [iput, False, iput[0]]
T = {}
#for i in range(Ilen+1):
# for j in range(Ilen+1):
# if j >= i:
# T[(i, j)] = set()
bSym = SSym
SSym = "S'"
#Start_Node = TreeNode("S'", Token("S_PRIME", "S'", -1))
# Add the initial Start Symbol to the parse table.
T[(0, 0)] = [(SSym, bSym, 0)]
#col = 0
for col in range(0, Ilen+1):
flag = True
if printCols:
if col % 10 == 0 or col > Ilen - 10:
print(Ilen, col)
while flag:
flag = False
for row in range(0, col + 1):
if (row, col) in T:
for I in T[(row, col)].copy():
lhs = I[0]
rhs = I[1].split(' ')
dpos = I[2]
#how = "INITIAL"
tadpos = ''
if dpos == 0:
tadpos = rhs[0]
elif dpos == len(rhs):
tadpos = ''
else:
tadpos = rhs[dpos]
if col < Ilen:
if iput[col].symbol == tadpos or iput[col].lexeme == tadpos:
# SCAN
#how = ["S"]
#print("SCANNING")
nrhs = ""
for xx in rhs:
nrhs += xx
nrhs += ' '
nrhs = nrhs[:-1]
I2New = (lhs, nrhs, dpos + 1)
if T.get((row, col+1)) is not None:
if I2New not in T[(row, col + 1)]:
T[(row, col + 1)].append(I2New)
#print("Added Scan set", I2New, "to T[", row, "][", col + 1, "]")
flag = True
#print("Set Flag in SCAN.\n")
#how.append(I2New)
else:
T[(row, col + 1)] = []
T[(row, col + 1)].append(I2New)
# print("Added Scan set", I2New, "to T[", row, "][", col + 1, "]")
flag = True
# print("Set Flag in SCAN.\n")
# how.append(I2New)
if tadpos in gramm.nonterminals:
# PREDICT
# Add all productions with lhs of rhs[dpos] to T[col][col]
# Might set flag to True
# print("PREDICTING")
#how = ["P"]
for prodd in gramm.productions:
nrhs = ""
for kl in range(len(prodd.rhs)):
nrhs += prodd.rhs[kl]
nrhs += ' '
nrhs = nrhs[:-1]
newThing = (prodd.lhs, nrhs, 0)
if prodd.lhs == rhs[dpos]:
if T.get((col, col)) is not None:
if newThing not in T[(col, col)]:
#if T.get((col, col)) is None:
# T[(col, col)] = []
T[(col, col)].append(newThing)
flag = True
#how.append(newThing)
else:
T[(col, col)] = []
T[(col, col)].append(newThing)
# print("Added Scan set", I2New, "to T[", row, "][", col + 1, "]")
flag = True
# COMPLETE
if tadpos == '':
# print("Completing?")
#how = ["C"]
for k in range(0, row+1):
if T.get((k, row)) is not None:
for Itwo in T[(k, row)].copy():
lhs2 = Itwo[0]
rhs2 = Itwo[1].split(' ')
dpos2 = Itwo[2]
# I2.rhs[I2.dpos]
ItwoDposSide = 0
if dpos2 < len(rhs2):
ItwoDposSide = rhs2[dpos2]
# if I.lhs matches I2.rhs[I2.dpos]:
if lhs == ItwoDposSide:
Nrhs2 = ""
for fk in rhs2:
Nrhs2 += fk
Nrhs2 += ' '
Nrhs2 = Nrhs2[:-1]
newerthing = (lhs2, Nrhs2, dpos2 + 1)
if T.get((k, col)) is not None:
if newerthing not in T[(k, col)]:
T[(k, col)].append(newerthing)
flag = True
else:
T[(k, col)] = []
T[(k, col)].append(newerthing)
flag = True
if genTree:
ref = []
ref.append(Item(lhs2, Nrhs2, dpos2 + 1, lhs)) # Complete
#if Start_Node != None:
# ref.append(Item(lhs, rhs, dpos2, Start_Node.sym)) # Partial
#else:
# ref.append(Item(lhs, rhs, dpos2, lhs)) # Partial
#titem = Item(lhs, rhs, dpos, how, ref, col)
#print(type(Start_Node))
#if Start_Node != None:
# print("Node's Children amount:", len(Start_Node.children))
# Start_Node = makeTree(titem, Start_Node)
cParse = False
LCell = []
if T.get((0, Ilen)) is not None:
LCell = T[(0, Ilen)]
#print(T[(0, 0)])
if len(LCell) > 0:
lastCell = []
lCol = LCell
while lCol:
x = lCol.pop()
lastCell.append(x)
#print("LastCell:", lastCell)
for gg in lastCell:
if gg[0] == SSym:
if gg[1] == bSym and gg[2] == 1:
cParse = True
return [iput, cParse, iput[col-1], T]
return [iput, cParse, iput[col-1], T]
def createGrammar():
textList = []
grammy = open('grammar.txt', 'r+')
for g in grammy:
#print(g)
if g == "\n":
#print("EMPTY")
continue
else:
gg = ""
for xx in range(len(g)-1):
gg += g[xx]
textList.append(gg)
termsList = []
nontermsList = []
productionList = []
terms = open('germinals.txt', 'r+')
for t in terms:
xdf = t.split(' ')
for xx in xdf:
termsList.append(xx)
terms.close()
nts = open('nonterminals.txt', 'w')
wroteSet = set()
for txx in textList:
prod = txx
arrow = " -> "
splitOne = prod.split(arrow)
lhs = splitOne[0]
rhs = splitOne[1]
sides = rhs.split(' | ')
for xc in range(len(sides)):
tmp = ""
tmp += lhs
tmp += arrow
tmp += sides[xc]
if lhs not in wroteSet:
nts.write(lhs)
#if xc < len(sides)-1:
nts.write(' ')
wroteSet.add(lhs)
productionList.append(Production(tmp))
nts.close()
nterms = open('nonterminals.txt', 'r+')
for n in nterms:
xdf = n.split(' ')
if '' in xdf:
xdf.remove('')
for xx in xdf:
nontermsList.append(xx)
nterms.close()
nontermsSet = set(nontermsList)
termsSet = set(termsList)
gramm = Grammar(textList, termsSet, nontermsSet, productionList)
return gramm
def printGrammar(g):
print("Grammar:", g.text, g.terminals, g.nonterminals)
print("Productions:")
for ppp in g.productions:
print(ppp)
def main(fname, asdf):
global start_symbol
tokes = tokenize(False, False, fname)
if tokes == 1:
print("Failed to Tokenize.")
return 1
tkes = open('bokens.txt', 'w')
for tt in tokes:
tkes.write(str(tt)+'\n')
tkes.close()
#asdf = createGrammar()
#printGrammar(asdf)
start_symbol = asdf.productions[0].lhs
#print("Start Symbol:", start_symbol)
#fir = computeFirst(asdf)
#print("First set:", fir[0])
#print('')
# Grammar, FirstSet, NullableSet
#fol = computeFollow(asdf, fir[0], fir[1])
#print("Follow Set:", fol)
#print('')
print("Parsing!")
prs = earleyParse(tokes, asdf, start_symbol, False, True)
if not prs[1]:
#print("Failed to Parse. Gave up around", prs[2])
#print("Failed to Parse.")
return 1
return 0
#batchParse = True
batchParse = False
asdf = createGrammar()
if not batchParse:
x = main(sys.argv[1], asdf)
if x == 1:
print("Failure.")
sys.exit(1)
else:
print("Success.")
sys.exit(0)
else:
fails = open("fails.txt", 'w')
passes = open("pass.txt", 'w')
crashList = ['x157.txt', 't40.txt', 't106.txt', 't116.txt', 't126.txt', 't136.txt', 't159.txt']
for i in range(1, 160):
xstr = "t"
xstr += str(i)
print("Current File:", xstr + '.txt')
fstr = xstr+'.txt'
if fstr not in crashList:
x = main(fstr, asdf)
if x == 0:
print("Success.")
passes.write(xstr)
passes.write('\n')
passes.flush()
else:
print("Failure.")
fails.write(xstr)
fails.write('\n')
passes.flush()
fails.close()
passes.close()