/
main.py
413 lines (298 loc) · 13.1 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
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix import actionbar
from kivy.uix.boxlayout import BoxLayout
from Production import Production
from Grammar import Grammar
#from Tree import Tree
from Graph import Graph
import logging
class RootWidget(BoxLayout):
'''This is the class representing the root widget.
By default it is inherited from BoxLayout
'''
cont1 = ObjectProperty(None)
cont2 = ObjectProperty(None)
action_bar = ObjectProperty(None)
carousel = ObjectProperty(None)
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
self.cont1 = actionbar.ContextualActionView(
action_previous=actionbar.ActionPrevious(title='Go Back'))
self.cont2 = actionbar.ContextualActionView(
action_previous=actionbar.ActionPrevious(title='Go Back'))
self.action_bar.bind(on_previous=self.on_previous)
self.prev_index = 0
self.from_actionbar = False
def testStringOnGrammar(self):
myGrammar=self.extractGrammar()
if myGrammar:
myGrammar.VT.append('$')
result=myGrammar.belongsTo(self.testString.text)
if result!= False:
self.logBox.text+='THE STRING CORRESPONDS TO THE GRAMMAR\n\n'
self.logBox.text+='Pila Cadena Produccion\n'
for r in result:
self.logBox.text+=str(r[0])+' '+str(r[1])+' '+str(r[2])+'\n'
else:
self.logBox.text+="ERROR\nTHE STRING '"+self.testString.text+"' DOESN'T CORRESPONDS TO THE GRAMMAR\n\n"
def gotoRegEx(self,value):
self.carousel.load_next()
value.background_color=(0,.8,.1,1)
self.gButton.background_color= (0,0,0,0)
def gotoGrammar(self,value):
self.carousel.load_previous()
value.background_color=(.2,.5,.9,1)
self.reButton.background_color =(0,0,0,0)
def on_previous(self, *args):
self.from_actionbar = True
self.carousel.load_previous()
def insertEpsilon(self):
self.grammarInput.insert_text('ε')
def refocusTextInput(self):
self.grammarInput.focus = True
def ShowAFN(self):
if self.regExInputBox.text !='':
AFN=Graph.fromPostFixed(Grammar.genPostFixed(self.regExInputBox.text))
self.regExImage.source=AFN.print('AFN')
self.regExImage.reload()
self.regExCarousel.load_next()
print('DEBUG')
for state in AFN.Nodes:
print('state: '+str(state))
print(AFN.cerraduraEpsilon([state],[state]))
def ShowAFD(self):
if self.regExInputBox.text !='':
AFN=Graph.fromPostFixed(Grammar.genPostFixed(self.regExInputBox.text))
AFD=AFN.getAFDfromAFN()
self.logBox.text=AFD[1]
self.regExImage.source=AFD[0].print('AFD')
self.regExImage.reload()
self.regExCarousel.load_next()
def ShowMinAFD(self):
if self.regExInputBox.text !='':
AFN=Graph.fromPostFixed(Grammar.genPostFixed(self.regExInputBox.text))
AFD=AFN.getAFDfromAFN()
AFDMin=AFD[0].minimize()
self.regExImage.source=AFDMin.print('MinimizedAFD')
self.regExImage.reload()
self.regExCarousel.load_next()
def regExpressionCopy(self):
self.regExpression.select_all()
self.regExpression.copy()
self.regExpression.select_text(0,0)
def regExInputBoxTextInput(self):
print('im getting some text')
if self.regExInputBox.text!='':
PF=Grammar.genPostFixed(self.regExInputBox.text) #Post Fixed Grammar
self.regExPostFixedBox.text=PF
AuGrammar='('+self.regExInputBox.text+')#' #Augmented Grammar
augPFGrammar=PF+' #·' # Augmented PF
self.regAugmentedBox.text=augPFGrammar
Tree=Grammar.genTreefromER(self.regExInputBox.text)
self.regExTreeImg.source=Tree.printStart(self.regExInputBox.text,'ERTree')
self.regExTreeImg.reload()
def extractGrammar(self):
self.regExpression.text=''
self.logBox.text = ''
self.grammarInfo.text = ''
if self.grammarInput.text != '':
myGrammar = Grammar.fromString(self.grammarInput.text)
if type(myGrammar) is Grammar:
self.grammarInfo.text = myGrammar.description
print('Prods')
for p in myGrammar.Productions:
print(p)
print('Original F')
for p in myGrammar.ProdsJoined:
print(p)
print('\n')
if myGrammar.Type == 'Regular Grammar':
ER = myGrammar.genReBgularExpression()
self.grammarInfo.text += '\nRegular Expresion: S--> ' + ' | '.join(ER[0])
self.logBox.text = ER[1]
self.regExpression.text=' | '.join(ER[0])
print(Grammar.genPostFixed(' | '.join(ER[0])))
return myGrammar
else:
self.logBox.text = myGrammar
return False
def showLR1Table(self):
self.logBox.text=''
myGrammar=self.extractGrammar()
if myGrammar:
myGrammar.Primero()
myGrammar.Sig()
E=myGrammar.Elementos()
print("These are the Sets")
for e in E:
Grammar.printLRElementSet(e)
Gp=myGrammar.copy()
Gp.Productions.insert(0,Production([Gp.Productions[0].Left[0]+'`'],[Gp.Productions[0].Left[0]]))
Gp.VT.append('$')
Table= Gp.LR1Table()
self.logBox.text+='THE TABLE FOR LR1 IS:\n\n'
for key in Table[0].keys():
self.logBox.text+=key+' '
self.logBox.text+='\n\n'
for el in Table:
el
for value in el.keys():
self.logBox.text+=el[value]+' '
self.logBox.text+='\n'
def testStringLR1(self):
self.logBox.text=''
myGrammar=self.extractGrammar()
if myGrammar:
myGrammar.Primero()
myGrammar.Sig()
Gp=myGrammar.copy()
Gp.Productions.insert(0,Production([Gp.Productions[0].Left[0]+'`'],[Gp.Productions[0].Left[0]]))
Gp.VT.append('$')
result=Gp.belongsToLR1(self.testString.text)
if result!=False:
self.logBox.text+='THE STRING '+self.testString.text+' CORRESPONDS TO THE GRAMMAR\n\n '
for row in result:
self.logBox.text+=str(row)+'\n'
print(row)
else:
self.logBox.text+='THE STRING '+self.testString.text+' DOES NOT CORRESPONDS TO THE GRAMMAR :(\n\n '
def primSigTable(self):
myGrammar=self.extractGrammar()
if myGrammar:
print('After Killing Recursion On Left: ')
for p in myGrammar.ProdsJoined:
print(p)
print('\n')
print('start prim')
p=myGrammar.Primero()
print('end prim')
for var in p:
print(var)
myGrammar.Sig()
print(myGrammar.SiguienteSet)
self.logBox.text+='Primero:\n\n'
for p in myGrammar.PrimeroSet:
self.logBox.text+=str(p)+' '+str(myGrammar.PrimeroSet.get(p))+'\n'
self.logBox.text+='\n'
self.logBox.text+='Siguiente:\n\n'
for p in myGrammar.SiguienteSet:
self.logBox.text+=str(p)+' '+str(myGrammar.SiguienteSet.get(p))+'\n'
self.logBox.text+='\n\n\n'
self.logBox.text+='Tabla:\n\n'
myGrammar.VT.append('$')
table=myGrammar.tabla()
self.logBox.text+=' '
for col in myGrammar.VT:
self.logBox.text+=col+' '
self.logBox.text+='\n\n'
for ren in table:
self.logBox.text+=ren+' '
for col in table[ren]:
if type (table[ren].get(col)) is list:
self.logBox.text+='->'.join(table[ren].get(col))+' '
else:
self.logBox.text+=table[ren].get(col)+' '
self.logBox.text+=('\n')
# for t in table:
# self.logBox.text+=str(t)+'\n'
def testGrammar(self):
self.leftRecursion.text=''
self.leftFact.text=''
self.regExpression.text=''
self.logBox.text = ''
self.grammarInfo.text = ''
if self.grammarInput.text != '':
myGrammar = Grammar.fromString(self.grammarInput.text)
if type(myGrammar) is Grammar:
self.grammarInfo.text = myGrammar.description
print('Prods')
for p in myGrammar.Productions:
print(p)
print('Original F')
for p in myGrammar.ProdsJoined:
print(p)
print('\n')
print("Test point insertion")
print(myGrammar.Productions[0].dotNextChar())
myGrammar.Productions[0].dotInit()
print("Alpha:")
print(myGrammar.Productions[0].getDotAlpha())
print("B:")
print(myGrammar.Productions[0].dotNextChar())
print("Betha:")
print(myGrammar.Productions[0].getDotBetha())
for i in range(10):
myGrammar.Productions[0].dotAdvance()
print("Alpha:")
print(myGrammar.Productions[0].getDotAlpha())
print("B:")
print(myGrammar.Productions[0].dotNextChar())
print("Betha:")
print(myGrammar.Productions[0].getDotBetha())
if myGrammar.Type == 'Regular Grammar':
ER = myGrammar.genRegularExpression()
self.grammarInfo.text += '\nRegular Expresion: S--> ' + ' | '.join(ER[0])
self.logBox.text = ER[1]
for h in ER:
print(h)
self.regExpression.text=' | '.join(ER[0])
print(Grammar.genPostFixed(' | '.join(ER[0])))
recur=myGrammar.copy()
recur.KillRecursionOnLeft()
for p in recur.ProdsJoined:
for d in p.Right:
self.leftRecursion.text+=str(p.Left)+'-->'+str(d)+'\n'
factor=myGrammar.copy()
factor.leftFactorize()
for p in factor.ProdsJoined:
for d in p.Right:
self.leftFact.text+=str(p.Left)+'-->'+str(d)+'\n'
# for p in factor.ProdsJoined:
# for d in p.Right:
# self.leftRecursion.text+=str(p.Left)+'-->'+str(d)+'\n'
# myGrammar.leftFactorize()
# print('After Killing Recursion On Left: ')
# for p in myGrammar.ProdsJoined:
# print(p)
# print('\n')
# p=myGrammar.Primero()
# for var in p:
# print(var)
# myGrammar.Sig()
# print(myGrammar.SiguienteSet)
# myGrammar.VT.append('$')
# table=myGrammar.tabla()
# print('tabla')
# for col in myGrammar.VT:
# print(col+' ')
# for ren in range(len(table)):
# print(myGrammar.VN[ren])
# for col in range(len(table[ren])):
# print(table[ren][col]+' ')
# print('\n')
# print(table)
else:
self.logBox.text = myGrammar
class MainApp(App):
title='CompiTools'
'''This is the main class of your app.
Define any app wide entities here.
This class can be accessed anywhere inside the kivy app as,
in python::
app = App.get_running_app()
print (app.title)
in kv language::
on_release: print(app.title)
Name of the .kv file that is auto-loaded is derived from the name
of this class::
MainApp = main.kv
MainClass = mainclass.kv
The App part is auto removed and the whole name is lowercased.
'''
def build(self):
return RootWidget()
if __name__ == '__main__':
MainApp().run()