-
Notifications
You must be signed in to change notification settings - Fork 0
/
scannerProyecto2Tejada.py
218 lines (188 loc) · 7.38 KB
/
scannerProyecto2Tejada.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
# Nombre: Alejandro Tejada
# Curso: Diseño lenguajes de programacion
# Fecha: Abril 2021
# Programa: scannerProyecto2Tejada.py
# Propósito: Este programa tiene como fin leer el file de entrada
# V 1.0
# imports
import pickle
from pprint import pprint as pp
from scannerToken import *
class Scanner():
def __init__(self) -> None:
self.diccionarioSiguientePos = {}
self.AFDConstruidoFinal = []
self.nodosAceptacion = {}
self.stringPrueba = ""
self.tokensLeidos = []
self.abrirFiles()
self.abrirArchivoPrueba()
self.simular()
self.dumpTokensLeidos()
def abrirFiles(self):
# abrimos todos los files y asignamos
infile = open("dicionarioAFDFinal", 'rb')
self.AFDConstruidoFinal = pickle.load(infile)
infile.close()
infile = open("diccionarioSiguientePos", 'rb')
self.diccionarioSiguientePos = pickle.load(infile)
infile.close()
infile = open("diccionarioEstadosAceptacion", 'rb')
self.nodosAceptacion = pickle.load(infile)
infile.close()
def abrirArchivoPrueba(self):
with open('pruebas.txt', 'r') as f:
self.stringPrueba = f.read()
f.close()
def getStateNumberForArray(self, array):
for valor in self.AFDConstruidoFinal:
if(valor[1] == array):
return valor[0]
def mover(self, estado, caracter):
# Esta funcion retorna el siguiente estado
arrayEvaluar = self.AFDConstruidoFinal
arrayMover = []
for estados in estado:
for x in arrayEvaluar:
variableIn = (ord(caracter)) in x[2]
if(variableIn and len(x[3]) > 0 and estados == x[0]):
estadoSiguiente = self.getStateNumberForArray(x[3])
if(estadoSiguiente not in arrayMover):
arrayMover.append(estadoSiguiente)
return arrayMover
def getFinalStateAFN(self):
arrayValores = []
estadosFinales = self.getFinalStateNumber()
for valor in self.AFDConstruidoFinal:
for x in estadosFinales:
if(str(x) in valor[1]):
arrayValores.append(valor[0])
arrayValores = list(
dict.fromkeys(arrayValores))
return arrayValores
def getFinalStateAFNV2(self):
arrayValores = []
estadosFinales = self.getFinalStateNumber()
for valor in self.AFDConstruidoFinal:
for x in estadosFinales:
if(str(x) in valor[1]):
if(valor[1] not in arrayValores):
arrayValores.append(valor[1])
return arrayValores
def getFinalToken(self, tokenArray):
arrayValores = []
valorRetornar = ""
estadosFinales = self.getFinalStateNumber()
for valor in self.AFDConstruidoFinal:
for x in estadosFinales:
if(str(x) in valor[1]):
for w in tokenArray:
if(w == valor[0]):
if(valor[1] not in arrayValores):
arrayValores.append(valor[1])
dictAceptacion = {}
arrayNumeros = []
# ahora, miramos cual token es
for z in arrayValores:
for estadoPosibles in z:
for llave, valor in self.nodosAceptacion.items():
if(int(estadoPosibles) == llave):
dictAceptacion[llave] = valor
arrayNumeros.append(llave)
if(len(dictAceptacion) > 1):
valorMinimo = min(arrayNumeros)
for llave, valor in dictAceptacion.items():
if(valorMinimo == llave):
valorRetornar = valor
else:
for llave, valor in dictAceptacion.items():
valorRetornar = valor
return valorRetornar
def getFinalStateNumber(self):
array = []
for numero, valor in self.diccionarioSiguientePos.items():
if len(valor) == 0:
array.append(numero)
return array
def dumpTokensLeidos(self):
filename = 'arrayTokensLeidos'
outfile = open(filename, 'wb')
pickle.dump(self.tokensLeidos, outfile)
outfile.close()
def simular(self):
# este método simula
print("------------------SIMULACION TOKENS INICIADA-------------------")
S = [0]
S2 = [0]
acumulador = ""
SAcumulado = []
EstadoACeptacion = []
for w in self.stringPrueba:
SAcumulado.append(w)
SAcumulado.append(" ")
contador = 0
while len(SAcumulado) > 0:
if(contador == len(self.stringPrueba)-1):
caracterValuar = self.stringPrueba[contador]
acumulador += caracterValuar
S = self.mover(S, caracterValuar)
token = self.getFinalToken(S)
if(len(token) == 0):
print("TOKEN INVALIDO del valor ", acumulador)
newToken = tokenForScanner()
newToken.setTipoToken("ERROR")
newToken.setValor(acumulador)
self.tokensLeidos.append(newToken)
break
else:
pp("El token del valor ----> " +
acumulador + " <--- es: " + token)
newToken = tokenForScanner()
newToken.setTipoToken(token)
newToken.setValor(acumulador)
self.tokensLeidos.append(newToken)
break
caracterValuar = self.stringPrueba[contador]
caracterValuar2 = self.stringPrueba[contador+1]
acumulador += caracterValuar
S = self.mover(S, caracterValuar)
S2 = self.mover(S, caracterValuar2)
if(len(S2) == 0 and len(S) > 0):
token = self.getFinalToken(S)
if(len(token) == 0):
print("TOKEN INVALIDO del valor: ")
print(acumulador)
S = [0]
S2 = [0]
newToken = tokenForScanner()
newToken.setTipoToken("ERROR")
newToken.setValor(acumulador)
self.tokensLeidos.append(newToken)
acumulador = ""
contador -= 1
else:
pp("El token del valor ----> " +
acumulador + " <---- es: " + token)
S = [0]
S2 = [0]
newToken = tokenForScanner()
newToken.setTipoToken(token)
newToken.setValor(acumulador)
self.tokensLeidos.append(newToken)
acumulador = ""
# contador += 1
elif(len(S) == 0):
print("TOKEN INVALIDO del valor: ")
print(acumulador)
S = [0]
S2 = [0]
newToken = tokenForScanner()
newToken.setTipoToken("ERROR")
newToken.setValor(acumulador)
self.tokensLeidos.append(newToken)
acumulador = ""
contador += 1
popCharacter = SAcumulado.pop()
print("---------------------------------------------------------------")
print("")
objeSCanner = Scanner()