/
Grammar.py
1388 lines (1042 loc) · 33.9 KB
/
Grammar.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import string
from Production import Production
from Tree import Tree
import logging
import Util
logging.basicConfig(level=logging.DEBUG)
class Grammar:
"""This class is used to represent a grammar, it contains a list of productions,
a list of terminal symbols, a list of non terminal symbols, description, grammar type,
'Primero' and 'Siguiente sets' """
NotTerminals=string.ascii_uppercase;
Terminals= string.ascii_lowercase+string.digits+string.punctuation+'ε'
LOG=logging.getLogger() # We isntantiate a logging object for debugging purposes
LOG.setLevel('WARNING')
Productions=[] # Ceate a Productions list
VT=[]
VN=[]
ProdsJoined=[]
Type=''
description=''
PrimeroSet={}
SiguienteSet={}
def __init__(self):
""""Class constructor"""
VT=[]
VN=[]
ProdsJoined=[]
Type=''
description=''
PrimeroSet={}
SiguienteSet={}
def copy(self):
""" Method for making a dereferenced copy of the grammar, this means that the result of the method
is a Grammar with the same values but not the same references to the same objects"""
Aux=Grammar()
Aux.VT=self.VT[0:len(self.VT)]
Aux.VN=self.VN[0:len(self.VN)]
Aux.PrimeroSet=self.PrimeroSet.copy() #this section was comented, i dont know why
Aux.SiguienteSet=self.SiguienteSet.copy() # but if something breaks coment it again
Prods=[]
for P in self.Productions:
Prods.append(P.copy())
Aux.Productions=Prods
ja=[]
for P in self.ProdsJoined:
ja.append(P.copy())
Aux.ProdsJoined=ja
return Aux
def onlyOneNTinLeft(self):
"""Function to chech if a grammar contains only items of lenght 1 in the left part of the producion """
for P in self.Productions:
if len(P.Left)>1:
return False
return True
def clear(self):
"""Clears the following grammar attributes: Productions,VT,VN,description,ProdsJoined """
self.Productions.clear()
self.VT.clear()
self.VN.clear()
self.description=''
self.ProdsJoined.clear()
def fromString(input_rules):
"""Generates a Grammar object from a text string"""
lines=input_rules.split('\n') # Partimos la entrada en renglones
for i in range(0,len(lines)): # Quitamos los espacios al final y principio de cada renglon
lines[i]=lines[i].strip()
linesAux=lines.copy() # removemos las cadenas vacias de la coleccion de cadenas
lines.clear()
for line in linesAux:
if line !='':
lines.append(line)
#self.LOG.debug("Lines content: {} ".format(lines))
#Errors=False
myNewGrammar=Grammar()
myNewGrammar.clear()
for line in lines:
processStatus=myNewGrammar.processLine(line)
if processStatus:
myNewGrammar.LOG.error(processStatus+' in line {} '.format(lines.index(line)+1))
return processStatus+' in line {} '.format(lines.index(line)+1)+'\n'
#Errors=True
#break
myNewGrammar.findTermAndNotTerm()
#if not Errors:
# for prod in myNewGrammar.Productions:
# for symbol in prod.Left+prod.Right:
# if myNewGrammar.containsTerminal(symbol):
# if symbol not in myNewGrammar.VT:
# myNewGrammar.VT.append(symbol)
# elif symbol not in myNewGrammar.VN :
# myNewGrammar.VN.append(symbol)
if len(myNewGrammar.Productions)==0:
return 'Not Sentences were processed, please write some valid sentences '
myNewGrammar.Type=myNewGrammar.gramaticType()
myNewGrammar.description+='VT: '
myNewGrammar.description+=str(myNewGrammar.VT)+'\n'
myNewGrammar.description+='VN: '
myNewGrammar.description+=str(myNewGrammar.VN)+'\n'
myNewGrammar.description+='S: '+myNewGrammar.Productions[0].Left[0]+'\n'
myNewGrammar.description+='Type: '+ myNewGrammar.Type
myNewGrammar.productionsJoin()
return myNewGrammar
def findTermAndNotTerm(self):
"""Finds the symbols in the Grammar and clasifies them as terminal and non-terminal then updates
the attributes VT and VN"""
for prod in self.Productions:
for symbol in prod.Left+prod.Right:
if self.containsTerminal(symbol):
if symbol not in self.VT:
self.VT.append(symbol)
elif symbol not in self.VN :
self.VN.append(symbol)
def productionsJoin(self):
""" Joins all productions of the same Not Terminal: A->a A->b A->c in one single production
with the form A-> a | b |c"""
for prod in self.Productions:
if len(self.ProdsJoined)==0:
self.ProdsJoined.append(Production(prod.Left[0], [''.join(prod.Right)]))
else:
found=False
for joined in self.ProdsJoined:
if joined.Left[0]==prod.Left[0]:
found=True
joined.Right.append(''.join(prod.Right))
if found==False:
self.ProdsJoined.append(Production(prod.Left[0], [''.join(prod.Right)]))
print('BEGIN Aqui las factorizadas')
for prod in self.ProdsJoined:
# prod.factorize()
print(prod)
print('Aqui las factorizadas END')
def containsTerminal(self, strng):
"""Checks if a string contains at least one terminal symbol"""
for char in strng:
if char in Grammar.Terminals and char!='`': # modified for srtting "`" symbol as not termina
return True
return False
def isPureTerminal(self, strng):
"""Checks if a string contains only terminal symbols"""
for c in strng:
if c not in Grammar.Terminals:
return False
return True
def separateOperands(self,line):
"""Separates the Left Part and Right part of a string deleimited with '-->' """
parts=line.split("-->") # Separamos la cadena en la dos partes a la derecha y a la izquierda
# de la secuencia -->
if len(parts) ==2: # Formato correcto simbolo --> producto
parts[0]=(parts[0].rstrip()).lstrip() #se eliminan los espacios al final e inicio de la caddena
parts[1]=(parts[1].rstrip()).lstrip() #se eliminan los espacios al final e inicio de la caddena
if parts[0].find('ϵ')!=-1:
return 'Left part of the operand contais E (Epsilon) '
else:
if parts[1].find('ε')!=-1:
if len(parts[1])==1:
return (parts)
else:
return 'Sentence not valid, derivations with E (Epsilon) can not contain other characters'
else:
return (parts)
else: #la cadena no contiene --> o se encuentra mas de una vez
return('Sentence not valid, missing separator "-->" in line ')
def extractOperands(self,word,symbols):
"""Extracts the symbols of a string to form a part of a production"""
#self.LOG.debug('string received as operand {}'.format(word))
if word!='':
if word.startswith('<'):
i=0
for i in range(1,len(word)):
if word[i] =='>':
symbols.append(word[1:i])
return self.extractOperands(word[i+1:len(word)],symbols)
elif word[i] not in string.ascii_uppercase and word[i] not in string.ascii_lowercase and word[i]!='`': # modified for accepting prime derivations of the kind "E`"
return 'Delimitated words must only contain UPPERCASE symbols'
return 'Missing delimitator'
else:
i=0
for i in range(0,len(word)):
if word[i]!='>':
break
else:
return 'Not correctly delimitated operands in '
if word[i]!=' ' :
symbols.append(word[i])
return self.extractOperands(word[i+1:len(word)],symbols)
def processLine(self,line):
"""Pocesess a line of text to potentially generate a Production Object"""
parts=self.separateOperands(line)
if type(parts) is list:
#self.LOG.debug('lne: {} was split into {}'.format(line,parts))
leftSymbols=[]
result=self.extractOperands(parts[0],leftSymbols)
if len(leftSymbols) > 0:
if self.isPureTerminal (''.join(leftSymbols))==False:
rightSymbols=[]
result=self.extractOperands(parts[1],rightSymbols)
for var in rightSymbols:
self.LOG.debug('Right symbols found: {} '.format(var))
if type(result)!=str:
self.Productions.append(Production(leftSymbols,rightSymbols))
else:
return result
else:
return 'Pure terminals cannot be on left side '
else:
return result
else:
return parts
def gramaticType(self):
"""Clasifies the grammar based on its characteristics and returns a string describing the
type of grammar"""
LeftCount=0
RightCount=0
AllNotTerminalsInLeft=True
IsNotTerminalFirstInRight=False
for prod in self.Productions:
if len(prod.Left)> LeftCount:
LeftCount=len(prod.Left)
if len(prod.Right)> RightCount:
RightCount=len(prod.Right)
if len(prod.Left) > len(prod.Right) or 'ε' in prod.Right:
return 'Unrestricted Grammar'
if self.containsTerminal(''.join(prod.Left)):
AllNotTerminalsInLeft=False
if self.isPureTerminal(prod.Right[0])==False:
IsNotTerminalFirstInRight=True
if LeftCount>1 or AllNotTerminalsInLeft==False or IsNotTerminalFirstInRight==True:
return 'Context Sensitive Grammar'
elif RightCount>2 :
return 'Context Free Grammar'
return'Regular Grammar'
def genRegularExpression(self):
"""Generates a regular expression based on the productions of the grammar"""
# Step 1: this is already done, we got this in ProdsJoined, so we just copy it
logStr='Step #1\n\n'
B=[]
for production in self.ProdsJoined:
B.append(production.copy())
logStr+=(production.__str__()+'\n')
logStr+='\n\n'
# Step2
logStr+='Step #2 - Up Iteration\n'
for i in range(0,len(B)-1):
logStr+=('\ni: '+ str(i)+' ')
for strng in B[i].Right: # We check for recursivity
if B[i].Left in strng: # and make the reducion
newRight=B[i].Right
newRight.remove(strng)
reducedStr='{'+strng.strip(B[i].Left)+'}'
for k in range(len(newRight)):
newRight[k]=reducedStr+newRight[k]
logStr+=('reduced '+B[i].Left+ ' to '+ str(reducedStr)+': ')
logStr+=(str(B[i])+'')
for j in range(i+1,len(B)):
logStr+=('\n j: '+str(j)+'') #we check if a substitution can
newElements=B[j].Right[0:len(B[j].Right)]
for strng in B[j].Right: # be made, and do it if is the case
if B[i].Left in strng:
newElements.remove(strng)
for der in B[i].Right:
# B[j].Right.append(strng.replace(B[i].Left,der))
newElements.append(strng.replace(B[i].Left,der))
logStr+=(' replaced '+B[i].Left+ ' on '+ str(B[j].Left)+', '+str(B[j].Left)+' --> '+' ')
logStr+=(str(newElements)+'')
else:
pass
B[j].Right=newElements
# Step3
logStr+='\n\nStep #3 - Down Iteration\n'
for i in reversed(range(len(B))):
logStr+=('\ni: '+str(i)+' ')
for strng in B[i].Right: # We check for recirsivity
if B[i].Left in strng: # and make the reducion
newRight=B[i].Right
reducedStr='{'+strng.strip(B[i].Left)+'}'
for k in range(len(newRight)):
newRight[k]=reducedStr+newRight[k]
logStr+=('reduced '+B[i].Left+ ' to '+ str(reducedStr)+': ')
logStr+=(str(B[i])+'')
for j in reversed(range(i)):
logStr+=('\n j: '+str(j)+'') #we check if a substitution can
newElements=B[j].Right[0:len(B[j].Right)] #be made, and do it if is the case
for strng in B[j].Right:
if B[i].Left in strng:
newElements.remove(strng)
for der in B[i].Right:
# B[j].Right.append(strng.replace(B[i].Left,der))
newElements.append(strng.replace(B[i].Left,der))
logStr+=(' replaced '+B[i].Left+ ' on '+ str(B[j].Left)+', '+str(B[j].Left)+' --> '+' ')
logStr+=(str(newElements)+'')
else:
pass
B[j].Right=newElements
# Step 4 Reduction
logStr+='\n\nStep #4 - Simplification\n'
ER=[]
for term in B[0].Right:
index=B[0].Right.index(term)+1
trimStart=0
trimEnd=len(term)
#logStr+=('Term: '+ term)
while '{' in term or '}' in term:
print('im in the loop: '+term)
#Open=False
for i in range(len(term)):
if term[i]=='{':
trimStart=i
#Open=True
if term[i]=='}':
trimEnd=i
break
termX=term[trimStart+1:trimEnd]
#logStr+=('TermX: '+termX)
print('TermX: '+termX)
print('S: '+str(trimStart)+' E: '+str(trimEnd))
if trimEnd+len(termX) <= len(term) or True:
print('TemrOr:'+term[trimEnd+1:trimEnd+len(termX)+1])
if termX== term[trimEnd+1:trimEnd+len(termX)+1]:
if len(termX)==1:
term=term[0:trimStart]+termX+'+'+term[trimEnd+1:trimEnd+len(termX)]+term[trimEnd+len(termX)+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
else:
term=term[0:trimStart]+'('+termX+')+'+term[trimEnd+1:trimEnd+len(termX)]+term[trimEnd+len(termX)+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
elif termX== term[trimStart-len(termX):trimStart]:
if len(termX)==1:
term=term[0:trimStart-len(termX)]+termX+'+'+term[trimEnd+1:trimEnd]+term[trimEnd+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
else:
term=term[0:trimStart-len(termX)]+'('+termX+')+'+term[trimEnd+1:trimEnd]+term[trimEnd+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
else:
if len(termX)==1:
term=term[0:trimStart]+termX+'*'+term[trimEnd+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
else:
term=term[0:trimStart]+'('+termX+')*'+term[trimEnd+1:len(term)]
logStr+=('\nSimplified Term '+ str(index)+' To: '+ term)
print('\nSimplified Term '+ str(index)+' To: '+ term)
ER.append(term)
return [ER,logStr]
def genPostFixed(ER):
"""Generates the post fixed expression of a given grammar passed as string and returns it as a string"""
print('ER-----------------:'+ER)
pF=''
tope=0
for i in range(len(ER)):
if i>=tope:
if ER[i] in string.ascii_lowercase:
if pF=='':
if i+1<len(ER):
if ER[i+1]=='+':
pF+=ER[i]+'+'
elif ER[i+1]=='*':
pF+=ER[i]+'*'
else:
pF+=ER[i]
else:
pF+=ER[i]
else:
if i+1<len(ER):
if ER[i+1]=='+':
pF+=ER[i]+'+·'
elif ER[i+1]=='*':
pF+=ER[i]+'*·'
else:
pF+=ER[i]+'·'
else:
pF+=ER[i]+'·'
# if ER[i]=='*' and ER[i-1]==')':
# pF+='*·'
# if ER[i]=='+' and ER[i-1]==')':
# pF+='+·'
if ER[i]=='(':
endChar=''
cont=1
for j in range(i+1,len(ER)):
if ER[j]=='(':
cont+=1
if ER[j]==')':
cont-=1
if j+1<len(ER):
if ER[j+1]=='+':
endChar='+'
if ER[j+1]=='*':
endChar='*'
if cont==0:
break
print('():'+ER[i+1:j])
if pF!='':
pF+=Grammar.genPostFixed(ER[i+1:j])+endChar+'·'
print('hgh')
else:
pF+=Grammar.genPostFixed(ER[i+1:j])+endChar
# print('j1:'+str(i)+'j2:'+str(j))
tope=j
if ER[i]=='|':
for j in range(i+2,len(ER)):
if ER[j]==' ':
break
if j==len(ER)-1:
tope=j=len(ER)
break
print('|:'+ER[i+1:j])
pF+=Grammar.genPostFixed(ER[i+1:j])+'|'
print('j1:'+str(i+2)+'j2:'+str(j))
tope=j
print('PF: '+pF)
return pF
def genTreefromER(RE):
"""Generates a tree that represents a regular expression, The regular expression is sent as parameter
with type string"""
operands='*|·+'
Pila=[]
#RE=Grammar.genRegularExpression()
PF=Grammar.genPostFixed(RE)
PF+='#·'
print(PF)
for s in PF:
if s not in operands:
Pila.append(Tree(s))
else:
if s=='*':
c1=Pila.pop()
T=Tree('*')
T.C1=c1
Pila.append(T)
if s=='+':
c1=Pila.pop()
T=Tree('+')
T.C1=c1
Pila.append(T)
if s=='|':
c2=Pila.pop()
c1=Pila.pop()
T=Tree('|')
T.C1=c1
T.C2=c2
Pila.append(T)
if s=='·':
c2=Pila.pop()
c1=Pila.pop()
T=Tree('·')
T.C1=c1
T.C2=c2
Pila.append(T)
print('Pila:\n')
print(Pila)
return Pila[0]
def sustitute(prod,prodList):
""" Sustitutes a Not Terminal symbol 'prod' with its derivations when its found
in an element of the list 'prodList'"""
print('i Receive')
print('prod:')
print(prod)
print('prodList:')
print(prodList)
newList=prodList[0:len(prodList)]
for strng in prodList:
if prod.Left[0] in strng:
newList.remove(strng)
for der in prod.Right:
newList.append(strng.replace(prod.Left[0],der))
print('Result:')
print (newList)
return newList
def KillRecursionOnLeft(self):
""" Checks for recursion on the left and creates a new production to eliminate recursion """
if self.onlyOneNTinLeft():
PrimProductions=[]
for i in range(len(self.ProdsJoined)):
print('this is i:'+str(i))
Alphas=[]
Betas=[]
Nlist=[]
NT=self.ProdsJoined[i].Left[0]
if i==0:
Nlist=self.ProdsJoined[i].Right
for j in range (0,i):
# print(len(self.ProdsJoined))
# print(str(i)+':'+str(j))
# print(j==None)
Nlist=Grammar.sustitute(self.ProdsJoined[j],self.ProdsJoined[i].Right)
# print('Nlist after sustitutions')
# print(Nlist)
for Deriv in Nlist:
if Deriv[0]==NT:
Alphas.append(Deriv.replace(NT,'',1))
else:
Betas.append(Deriv)
if len(Betas)>0 and len(Alphas)>0: # The production has the form A-> Aα | β otherwise
# Nothing is done to the production
lAux=[]
for B in Betas:
lAux.append(B+'<'+NT+'`'+'>')
self.ProdsJoined[i].Right=lAux[0:len(lAux)]
lAux.clear()
for A in Alphas:
lAux.append(A+'<'+NT+'`'+'>')
lAux.append('ε')
p=Production('<'+NT+'`'+'>', lAux[0:len(lAux)])
PrimProductions.append(p)
self.ProdsJoined.extend(PrimProductions)
for V in PrimProductions:
self.VN.append(V.Left)
# self.Productions.extend(PrimProductions)
# self.productionsJoin()
# self.findTermAndNotTerm()
def leftFactorize(self):
""" Apllies a left factorization algorithm to the Grammar """
newProds=[]
for P in self.ProdsJoined:
Alpha=Grammar.findLongestFactorOnLeft(P.Right)
if Alpha!='':
Gammas=[]
Betas=[]
for prod in P.Right:
if Alpha in prod:
newTerm=prod.replace(Alpha,'',1)
if newTerm=='':
if 'ε' not in Betas:
Betas.append('ε')
else:
Betas.append(newTerm)
else:
Gammas.append(prod)
P.Right=[Alpha+'<'+P.Left+'`'+'>']+Gammas
newP=Production('<'+P.Left+'`'+'>',Betas)
newProds.append(newP)
self.ProdsJoined.extend(newProds)
for V in newProds:
self.VN.append(V.Left)
# self.Productions.extend(newProds)
# self.productionsJoin()
# self.findTermAndNotTerm()
def findLongestFactorOnLeft(strList):
""" Receives a list of strings and finds the longest common factor in the left
part of them, this is the value of return """
L=sorted(strList,key=len)
Alpha=''
descarted=[]
for i in range(len(L)): #stng in L-descarted:
for w in reversed(range(len(L[i]))):
found=False
for j in range(len(L)): #strng2 in L-descarted:
if L[j].find(L[i][0:w])>=0 and j!=i:
found=True
break
if found and len(L[i][0:w])>len(Alpha):
print('Alpha: '+Alpha)
print('newAlpha: '+L[i][0:w])
Alpha=L[i][0:w]
break
return Alpha
def Primero(self):
"""Non recursive function to calculate all the 'Primero' sets in the grammar, this function calls
the recursive function
'primeroR'"""
self.PrimeroSet.clear()
primero=[]
for Nt in self.VN:
prim=self.primeroR(Nt)
primero.append([[Nt,],prim])
self.PrimeroSet[Nt]=prim
print(self.PrimeroSet)
return primero
def primeroR(self, X):
"""Calculates the set 'Primero' for a given symbol based on the productions of the Grammar
this function is recursive"""
primero=[]
if self.isPureTerminal(X):
return [X]
else:
for prod in self.ProdsJoined:
if prod.Left==X:
print('its a match: '+ X)
for derivation in prod.Right:
if 'ε'==derivation:
primero.append('ε')
elif X not in derivation[0]:
symbolPrimero=[]
allEpsilon=True
for symbol in derivation:
Aux=self.primeroR(symbol)
symbolPrimero.extend(Aux)
if 'ε' not in Aux:
allEpsilon=False
break
for val in symbolPrimero:
if val!='ε' and val not in primero:
primero.append(val)
if allEpsilon:
primero.append('ε')
return primero
# def Siguiente(self):
# """"""
# self.SiguienteSet.clear()
# for i in range(len(self.VN)):
# if i==0:
# self.SiguienteSet[self.VN[i]]=['$']
# else:
# self.SiguienteSet[self.VN[i]]=[]
# for i in range(len(self.VN)):
# if self.VN[i] not in self.SiguienteSet: #No se ha calculado siguiente para el NT[i]
# self.siguienteR(self.VN[i])
# def siguienteR(self,X):
# print('Term:'+str(X))
# if X not in self.SiguienteSet or X==self.VN[0]:
# for Prod in self.ProdsJoined:
# for Term in Prod.Right:
# NTpos= Term.find(X)
# if NTpos!= -1 and NTpos+len(X)<len(Term) and Term [NTpos+len(X)]!='`':
# sigPos=NTpos+len(X)
# A=Prod.Left
# Alpha=Term[NTpos:sigPos]
# B=Term[NTpos:sigPos]
# Betha=Term[sigPos:len(Term)]
# sig=[]
# if X==self.VN[0] and '$' not in sig:
# sig.append('$')
# if Betha!='': # production with the form A--> αBβ
# print('Producion type: A->αBβ ')
# print('A:'+A)
# print('Alpha:'+Alpha)
# print('B:'+B)
# print('Betha:'+Betha)
# sig.extend(self.gimmePrim(Betha))
# if 'ε' in sig:
# sig.remove('ε')
# if A==self.VN[0]:
# sig.append('$')
# else:
# sig.extend(self.siguienteR(A))
# print('Prim de '+Betha+' = '+str(sig))
# self.SiguienteSet[B]=sig # Here ends the procedure for this case
# return sig
# else: # production with the form A-->ab
# print('Producion type: A->αB ')
# print('A:'+A)
# print('Alpha:'+Alpha)
# print('B:'+B)
# print('Betha:'+Betha)
# if A==self.VN[0]:
# sig.append('$')
# else:
# sig.extend(self.siguienteR(A))
# print('Prim de '+Betha+' = '+str(sig))
# self.SiguienteSet[B]=sig
# return sig
# else:
# return self.SiguienteSet[X]
def gimmePrim(self,Betha):
"""Finds 'Primero' set for a string based on the existing 'Primero' sets, if there is not primero
existing the function then calculate sit"""
# print('i receive as β :'+Betha)
primerosDeI=[]
prim=[]
for i in range(len(Betha)):
if Betha[i]=='`':
continue
if self.isPureTerminal(Betha[i]):
if Betha[i] not in prim:
prim.append(Betha[i])
break
else:
X=''
if i+1 < len(Betha):
if Betha[i+1]=='`': # prime Not Terminal
X=Betha[i]+Betha[i+1]
else:
X=Betha[i]
elif Betha[i]!='`': #normal Not Terminal
X=Betha[i]
primerosDeI.append(self.PrimeroSet.get(X))
if 'ε' not in primerosDeI:
break
# # for s in Aux:
# # if s not in prim:
# prim.append(s)
# print('XXdfX:'+ str(primerosDeI))
allEpsilon=True
for P in primerosDeI:
if 'ε' not in P:
allEpsilon=False
for foo in P:
if foo not in prim:
prim.append(foo)
if not allEpsilon and 'ε' in prim:
prim.remove('ε')
# print('an d prim of β = '+str(prim))
return prim
def Sig(self):
"""Calculates the 'siguiente' sets for the symbols in the grammar and updates this info in the
attributes of the grammar"""
self.SiguienteSet.clear()
for i in range(len(self.VN)):
if i==0:
self.SiguienteSet[self.VN[i]]=['$']
else:
self.SiguienteSet[self.VN[i]]=[]
modifiedFlag=True
while modifiedFlag:
modifiedFlag=False
for P in self.ProdsJoined:
for prod in P.Right:
for i in range(len(prod)) :
if i+1 < len(prod) and prod[i+1]=='`':
X=prod[i]+prod[i+1]
elif prod[i]=='`':
continue
else:
X=prod[i]
if not self.isPureTerminal(X):
print('X:'+X)
sigX=self.SiguienteSet.get(X)
primK=[]
if i+len(X)<len(prod):
Rp=prod[i+len(X):len(prod)]
print('Rp:'+Rp)
primK.extend(self.gimmePrim(Rp))
else:
if 'ε'not in sigX:
primK=['ε']
for m in primK:
if m not in sigX and m!='ε':
sigX.append(m)
modifiedFlag=True
print('SetPrim:'+str(primK))
if 'ε' in primK:
Aux=self.SiguienteSet.get(P.Left)
print('Sig '+P.Left+': '+str(Aux))
for a in Aux:
if a not in sigX:
sigX.extend(a)
modifiedFlag=True
def tabla(self):
"""Calculates the table for the non recursive predictive syntactic analisys algorithm and
returns it on a list"""
Table={}
for col in self.VN:
Table[col]={}
for col in self.VN:
for row in self.VT:
Table[col][row]='--------'
# print('Table PrrooodskcfvpasdvjadwlkNVDksdsj')
# for x in Table:
# print(Table[x])
# # print(Table)
Matrix = [[0 for x in range(len(self.VT))] for y in range(len(self.VN))]
for produccion in self.ProdsJoined:
for P in produccion.Right:
primero=self.gimmePrim(P)
siguiente=[]
for pr in primero:
if self.isPureTerminal(pr) and pr!='ε':
Matrix[self.VN.index(produccion.Left)][self.VT.index(pr)]=produccion.Left+'->'+P
Table[produccion.Left][pr]=[produccion.Left,P]
if 'ε' in primero:
siguiente=self.SiguienteSet.get(produccion.Left)
for s in siguiente:
if self.isPureTerminal(s):
Matrix[self.VN.index(produccion.Left)][self.VT.index(s)]=produccion.Left+'->'+P
Table[produccion.Left][s]=[produccion.Left,P]
if 'ε' in primero and '$' in siguiente:
Matrix[self.VN.index(produccion.Left)][self.VT.index('$')]=produccion.Left+'->'+P
Table[produccion.Left]['$']=[produccion.Left,P]
print(Table)
return Table
def findSymbols(strng):
"""Finds in a string the symbols of a grammar and returns a list with the symbols found and ordered
according to the string"""
symbols=[]
for i in range(len(strng)):
if strng[i]!= '`':
if i+1< len(strng):
if strng[i+1]=='`':
symbols.append(strng[i]+strng[i+1])
else:
symbols.append(strng[i])