-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
208 lines (148 loc) · 7.55 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
import time, threading, random
from tkinter import *
root = Tk()
#global derivacao
derivacao = {}
class Application():
stop_threads = False
def __init__(self):
self.root = root
self.tela()
self.frame()
self.inputs()
self.buttons()
root.mainloop()
def tela(self):
self.root.title('Derivador')
self.root.geometry('700x350')
self.root.resizable(False,False)
# cria os frames para adcionar os componentes
def frame(self):
self.frame = Frame(self.root)
self.frame.place(relx = 0 , rely = 0,relwidth = 1, relheight = 1)
def buttons(self):
#Bottao Clear
self.btnClear = Button(self.frame, text = 'Limpar Gramatica', command = self.clear)
self.btnClear.place ( relx = 0.8, rely = 0.1, width = 130, height = 50)
#Bottao Start
self.btnStart = Button(self.frame, text = 'Start', command = self.start)
self.btnStart.place ( relx = 0.8, rely = 0.3, width = 130, height = 50)
#Bottao Stop
self.btnStop = Button(self.frame, text = 'Stop', command = self.stop)
self.btnStop.place ( relx = 0.8, rely = 0.5, width = 130, height = 50)
#Bottao Input
self.btnInput = Button(self.frame, text = 'Input', command = self.inputGramatica)
self.btnInput.place( relx = 0.37, rely = 0.07, width = 80, height = 30)
#Bottao Input
self.btnInput = Button(self.frame, text = 'Input', command = self.inputGramatica)
self.btnInput.place( relx = 0.37, rely = 0.07, width = 80, height = 30)
#Exemplos
#Bottao Exemplo1
self.btnExemplo1 = Button(self.frame, text = 'Exemplo 1', command = self.Exemplo1)
self.btnExemplo1.place ( relx = 0.8, rely = 0.65, width = 130, height = 30)
#Bottao Exemplo2
self.btnExemplo2 = Button(self.frame, text = 'Exemplo 2', command = self.Exemplo2)
self.btnExemplo2.place ( relx = 0.8, rely = 0.75, width = 130, height = 30)
#Bottao Exemplo3
self.btnExemplo3 = Button(self.frame, text = 'Exemplo3', command = self.Exemplo3)
self.btnExemplo3.place ( relx = 0.8, rely = 0.85, width = 130, height = 30)
def inputs(self):
#input das gramticas
self.labelInputGramatica = Label(self.frame, text = 'Informe a Gramatica (linha a linha):')
self.labelInputGramatica.place(relx = 0.02, rely = 0.02)
self.inputGramaticaText = Entry(self.frame, width = 25)
self.inputGramaticaText.place(relx = 0.02, rely = 0.09)
#input do Nao terminal inicial
self.labelInputNaoTerminalInicial = Label(self.frame, text = 'Nao Terminal Inicial:')
self.labelInputNaoTerminalInicial.place(relx = 0.50, rely = 0.09)
self.inputNaoTerminalInicial = Entry(self.frame, width = 2)
self.inputNaoTerminalInicial.place(relx = 0.7, rely = 0.09)
#Gramatica
self.labelGramatica = Label(self.frame, text = 'Gramatica:')
self.labelGramatica.place(relx = 0.02, rely = 0.18)
self.textBoxGramatica = Text(self.frame,height=13, width =30)
self.textBoxGramatica.place(relx = 0.02, rely = 0.25)
#Derivacao
self.labelDerivacao = Label(self.frame, text = 'Derivacao:')
self.labelDerivacao.place(relx = 0.4, rely = 0.18)
self.textBoxDerivacao = Text(self.frame,height=13, width =30)
self.textBoxDerivacao.place(relx = 0.4, rely = 0.25)
def clear(self):
global derivacao
derivacao.clear()
self.inputNaoTerminalInicial.delete(0,END)
#self.textBoxGramatica.config(state = 'normal')
self.textBoxGramatica.delete('1.0',END)
#self.textBoxGramatica.config(state = 'disabled')
#self.textBoxDerivacao.config(state = 'normal')
self.textBoxDerivacao.delete('1.0',END)
#self.textBoxDerivacao.config(state = 'disabled')
def start(self):
self.startThreading = threading.Thread(target=self.derivacao)
self.startThreading.start()
def derivacao(self):
self.textBoxDerivacao.delete('1.0',END)
if (len(derivacao[self.inputNaoTerminalInicial.get()]) == 1):
tamanhoVetor = 0
else:
tamanhoVetor = len(derivacao[self.inputNaoTerminalInicial.get()])-1
gramaticaASerDerivada = derivacao[self.inputNaoTerminalInicial.get()][random.randint(0, tamanhoVetor)]
self.textBoxDerivacao.insert(END,"{} = {} \n".format(self.inputNaoTerminalInicial.get(), gramaticaASerDerivada))
while True:
for letter in list(gramaticaASerDerivada):
if (letter.isupper()):
gramaticaASerDerivada = gramaticaASerDerivada.replace(letter, derivacao[letter][random.randint(0, len(derivacao[letter])-1)] , 1)
self.textBoxDerivacao.insert(END,"{} = {} \n".format(self.inputNaoTerminalInicial.get(), gramaticaASerDerivada))
self.textBoxDerivacao.insert(END,"{} = {} \n".format(self.inputNaoTerminalInicial.get(), gramaticaASerDerivada))
if (gramaticaASerDerivada.islower()):
break
if self.stop_threads:
#print("Loop Terminado")
break
def stop(self):
self.stop_threads = True
def inputGramatica(self):
#self.textBoxGramatica.config(state = 'normal')
self.textBoxGramatica.insert(END,"{}{}".format(self.inputGramaticaText.get(), '\n') )
#self.textBoxGramatica.config(state = 'disabled')
# Faz o split para dividir o terminal do nao terminal
inputDeGramatica = self.inputGramaticaText.get().split('=')
# cria as variaveis nao terminal e faz stip para tirar os espacos
naoTerminal = inputDeGramatica[0].strip()
# faz o split pra criar os terminais
terminais = inputDeGramatica[1].split('|')
# tira os espacos dos terminais caso haja
for x in range (len(terminais)):
terminais[x] = terminais[x].strip()
# Adciona os valores do array
derivacao[naoTerminal] = terminais
#print(derivacao)
self.inputGramaticaText.delete(0,END)
def Exemplo1(self):
self.textBoxGramatica.delete('1.0',END)
self.textBoxDerivacao.delete('1.0',END)
global derivacao
derivacao.clear()
derivacao = {'S': ['aCb'], 'C': ['ab']}
self.textBoxGramatica.insert(END,"S = aCb\nC = ab")
self.inputNaoTerminalInicial.delete(0,END)
self.inputNaoTerminalInicial.insert(END,'S')
def Exemplo2(self):
self.textBoxGramatica.delete('1.0',END)
self.textBoxDerivacao.delete('1.0',END)
global derivacao
derivacao.clear()
derivacao = {'S': ['Cde', 'deA'], 'A': ['Ea', 'D', 'S'], 'C': ['ded'], 'D': ['deA', 'S'], 'E': ['ead']}
self.textBoxGramatica.insert(END,"S = Cde|deA\nA = Ea|D|S\nC = ded\nD = deA|S\nE = ead")
self.inputNaoTerminalInicial.delete(0,END)
self.inputNaoTerminalInicial.insert(END,'S')
def Exemplo3(self):
self.textBoxGramatica.delete('1.0',END)
self.textBoxDerivacao.delete('1.0',END)
global derivacao
derivacao.clear()
derivacao = {'S': ['ABC'], 'C': ['BaB', 'c'], 'B': ['b', 'bb'], 'A': ['a']}
self.textBoxGramatica.insert(END,"S = ABC\nC = BaB|c\nB = b|bb\nA = a")
self.inputNaoTerminalInicial.delete(0,END)
self.inputNaoTerminalInicial.insert(END,'S')
Application()