-
Notifications
You must be signed in to change notification settings - Fork 0
/
funciones.py
383 lines (344 loc) · 12.9 KB
/
funciones.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
"""
Nombre: Alejandro Tejada
Curso: Diseño lenguajes de programacion
Fecha: Abril 2021
Programa: functions.py
Propósito: ESte programa tiene funciones útiles
V 1.0
"""
import json
class funciones():
def __init__(self) -> None:
self.arrayOperandos = []
self.precedenceV2 = {'|': 1, '.': 2,
'*': 3, '?': 3, "+": 3} # diccionario de precedencia version 2
self.dictionaryNTL = {"0": "A", "1": "B", "2": "C",
"3": "D", "4": "E", "5": "F",
"6": "G", "7": "H",
"8": "I", "9": "J"}
self.ANYSET = set([chr(char) for char in range(0, 256)])
def get_ANYSET(self):
"""
Retorna el set de ANYSET
"""
return self.ANYSET
def getLastTokenValueFromDict(self, dictionary):
"""
Retorna el valor de la ultima llave de un diccionario
"""
arrayInterno = []
nombreToken = ""
for llave, valor in dictionary.items():
arrayInterno.append(llave)
return arrayInterno[-1]
def printPrettyDictionary(self, d, indent=0):
"""
this function prints pretty the dictionary,
extracted from: https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries
"""
for key, value in d.items():
print('\t' * indent + str(key))
if isinstance(value, dict):
self.printPrettyDictionary(value, indent+1)
else:
print('\t' * (indent+1) + str(value))
def getDumpJson(self, dictionary):
"""
REtorna el dump de un diccionario para ser impreso en formato JSON
dictionary: el diccionario para ser droppeado
"""
return json.dumps(dictionary, indent=2, default=str)
def removerPalabraSingle(self, string, palabra):
"""
Remueve la palabra del string y los espacios extra que contenga el string.
Útil si la linea solamente tiene dos valores. Como:
COMPILER AdaCS
Nos retorna: AdaCS
"""
string = string.replace(str(palabra), "")
newString = string.replace(" ", "")
return newString
def isOperand(self, ch):
"""
REtorna TRUE si el caracter ingresado es un alfanumerico, FALSE de lo contrario
*@param ch: el caracter a ser probado
"""
if ch != "+" and ch != "-":
return True
return False
def isOperandPosftixToken(self, ch):
"""
REtorna TRUE si el caracter ingresado es un alfanumerico, FALSE de lo contrario
*@param ch: el caracter a ser probado
"""
if ch.isalnum() or ch == "ε" or ch == "#":
return True
return False
def isOperandPosftixTokenFinal(self, ch):
"""
REtorna TRUE si el caracter ingresado es un alfanumerico, FALSE de lo contrario
*@param ch: el caracter a ser probado
"""
if ch.getIdenficador() == "IDENT" or ch.getIdenficador() == "EPSILON" or ch.getIdenficador() == "STRING" or ch.getIdenficador() == "ACEPTACION":
return True
return False
def is_op(self, a):
"""
Testeamos si el caracter de entrada es un operando
*@param a: caracter a ser probado
"""
if (a == '+' or a == '-') and (a != "'+'" or a != "'-'"):
return True
return False
def is_op_PosftixToken(self, a):
"""
Testeamos si el caracter de entrada es un operando
*@param a: caracter a ser probado
"""
if a == '+' or a == '.' or a == '*' or a == '?' or a == '|':
return True
return False
def is_op_PosftixTokenFinal(self, a):
"""
Testeamos si el caracter de entrada es un operando
*@param a: caracter a ser probado
"""
if a.getIdenficador() == 'APPEND' or a.getIdenficador() == 'KLEENE' or a.getIdenficador() == 'OR':
return True
return False
def getLanguage(self, RE):
"""
DAda una expresion regular, esto extrae el lenguaje, osea los caracteres unicos
"""
#lenguaje = ''.join(set(str(RE)))
lenguaje = RE
newLenguaje = []
arrayLocal = []
for x in lenguaje:
if(x.getIdenficador() == "IDENT" or x.getIdenficador() == "EPSILON" or x.getIdenficador() == "STRING"):
if(x.getNombreIdentificador() not in arrayLocal):
arrayLocal.append(x.getNombreIdentificador())
newLenguaje.append(x)
return newLenguaje
def alterateAskChain(self, exp):
"""
ESta funcion altera una cadena o expresion regular, y si encuentra un simbolo de + entonces lo sustitye por el equivalente
*@param: exp: la expresion a ser valuada y que se le quite el simbolo de más
"""
value = "?" in exp
if(value == False):
return exp
else:
index = exp.find('?')
if exp[index-1] == ")":
dentro = ""
for i in reversed(exp[0:index-1]):
dentro += i
if i == "[":
dentro = dentro[::-1]
cantidad1 = len(dentro)
cantidad2 = len(exp[0:index-1])
ignore = cantidad2 - cantidad1
dentro = dentro[1:len(dentro)]
# print(dentro)
# print(exp[0:2])
return self.alterateAskChain(exp[0:ignore] + "(" + "(" + dentro + ")" + "|ε)" + exp[index+1:len(exp)])
else:
return self.alterateAskChain(exp[0:index-1] + "(" + "(" + exp[index-1] + ")" + "|ε)" + exp[index+1:len(exp)])
def substituLlavesCorchetes(self, expresion):
"""
Sustiuye las aberturas de corchetes y de llaves y sus cerraduras por los equivalentes.
*@param expresion: la expresion a cambiar
"""
newExpresion = ""
for x in expresion:
if(x == "]"):
newExpresion = newExpresion+')?'
elif(x == "}"):
newExpresion = newExpresion+')*'
else:
newExpresion += x
return newExpresion
def substituLlavesCorchetesV2(self, expresion):
"""
Sustiuye las aberturas de corchetes y de llaves y sus cerraduras por los equivalentes.
*@param expresion: la expresion a cambiar
"""
newExpresion = ""
for x in expresion:
if(x == "]"):
newExpresion = newExpresion+')?'
elif(x == "}"):
newExpresion = newExpresion+')*'
if(x == "["):
newExpresion = newExpresion+'('
elif(x == "{"):
newExpresion = newExpresion+'('
else:
newExpresion += x
return newExpresion
def fromSetToOrd(self, setToOrd):
"""
Esta funcion convierte un set con characteres a un set de ords
*@param setToOrd: el set a ser convertido
"""
newSet = set()
for x in setToOrd:
newSet.add(ord(x))
return newSet
def fromOrdToString(self, ordToString):
"""
Esta funcion convierte un set de ints en strings
*@param setToOrd: el set a ser convertido
"""
newSet = set()
for x in ordToString:
newSet.add(chr(x))
return newSet
def fromSetToSTring(self, setToTransform):
"""
Esta funcion convierte un set a un string concatenado
*@param setToTransform: el set a convertir
"""
nuevoString = ""
for x in setToTransform:
nuevoString += x
return nuevoString
def fromSetNumbersToSTring(self, setToTransform):
"""
Esta funcion convierte un set a un string concatenado
*@param setToTransform: el set a convertir
"""
if(isinstance(setToTransform, int)):
return str(setToTransform)
else:
nuevoString = ""
for x in setToTransform:
nuevoString += chr(x)
return nuevoString
def alterateRE(self, RE):
"""
Altera una expresión regular agregándole puntos a la concatenacion para ser mas legible para el postfix
*@param RE: la expresión regular original
"""
nuevaRE = ""
contador = 0
if('.' in RE):
for x in range(0, len(RE)):
# print("EN EL CICLO S ", RE[x])
# print("EN el ITER ES : ", RE[x+1:x+2])
nuevaRE += RE[x]
if(self.isOperand(RE[x]) or RE[x] == ")" or RE[x] == "*" or RE[x] == "#"):
contador += 1
if(RE[x+1:x+2] != " " and contador == 1):
if(self.isOperand(RE[x+1:x+2]) or RE[x+1:x+2] == "("):
nuevaRE += "_"
contador = 0
else:
contador = 0
else:
for x in range(0, len(RE)):
# print("EN EL CICLO S ", RE[x])
# print("EN el ITER ES : ", RE[x+1:x+2])
nuevaRE += RE[x]
if(self.isOperand(RE[x]) or RE[x] == ")" or RE[x] == "*" or RE[x] == "#"):
contador += 1
if(RE[x+1:x+2] != " " and contador == 1):
if(self.isOperand(RE[x+1:x+2]) or RE[x+1:x+2] == "("):
nuevaRE += "."
contador = 0
else:
contador = 0
return nuevaRE
def alterateREPosftixToken(self, RE):
"""
Altera una expresión regular agregándole puntos a la concatenacion para ser mas legible para el postfix
*@param RE: la expresión regular original
"""
nuevaRE = ""
contador = 0
for x in range(0, len(RE)):
#print("EN EL CICLO S ", RE[x])
#print("EN el ITER ES : ", RE[x+1:x+2])
nuevaRE += RE[x]
if(self.isOperandPosftixToken(RE[x]) or RE[x] == ")" or RE[x] == "*" or RE[x] == "?" or RE[x] == "+" or RE[x] == "#"):
contador += 1
if(RE[x+1:x+2] != " " and contador == 1):
if(self.isOperandPosftixToken(RE[x+1:x+2]) or RE[x+1:x+2] == "("):
nuevaRE += "."
contador = 0
else:
contador = 0
return nuevaRE
def getRangeFromLetters(self, linea):
"""
ESta funcion obtiene el rango de entre dos valores y entre lineas
*@param linea: es la linea
"""
arryLine = linea.split("..")
newLine = ""
arrayIntegers = []
for letra in arryLine:
for char in letra:
if(char.isalpha()):
print(char)
arrayIntegers.append(ord(char))
break
for char in range(arrayIntegers[0], arrayIntegers[1]+1):
newLine += chr(char)
return newLine
def sortString(self, str):
"""
Hace sort de un string
extraido de: https://www.geeksforgeeks.org/sort-string-characters/
*@param: str: el string hacer sort
"""
str = ''.join(sorted(str))
return str
def unionTwoStrings(self, string1, string2):
"""
Une dos strings. Elimina duplicados.
*@param: string1: el string a unir
*@param: string2: el string a sumarle
"""
res = ""
temp = string1
for i in string2:
if i not in temp:
string1 += i
return string1
def differenceTwoStrings(self, string1, string2):
"""
Une dos strings. Elimina duplicados.
*@param: string1: el string a unir
*@param: string2: el string a sumarle
"""
res = ""
temp = string1
for i in string2:
if i in temp:
string1 = string1.replace(i, '')
return string1
def getBetweenComillaSandComillaDoble(self, value):
"""
VErifica si en un posible valor a sumar lo que hay es comilla doble o comilla simple. Si es simple,
retornamos solo eso, si es doble quitamos lo doble
El criterio es que si hay dos, es porque esta entre ellos el operando
*@param: value: el string a valuar
"""
contadorComilla = 0
contadorComillaSimple = 0
for x in value:
if x == '"':
contadorComilla += 1
elif x == "'":
contadorComillaSimple += 1
if(contadorComilla == 2 and contadorComillaSimple < 2):
return value.replace('"', '')
elif(contadorComilla < 2 and contadorComillaSimple >= 2):
value = value.replace("'", '')
return value
elif(contadorComilla > 2):
print("ACA")
print(value)
return value