예제 #1
0
 def ingreso_terminales(self):
     '''
     __________________________________________________________________________________      
     ► Descripción: 
     
     → Método encargado de solicitar y guardar los terminales.               
     __________________________________________________________________________________    
     ► Parámetros:                                                                      
                                                                                       
         (Sin parámetros)            
     __________________________________________________________________________________
     '''
     while (True):
         os.system("cls")
         print('Ingrese el no terminal:')
         print(">> ", end="")
         terminal = input() 
         if terminal.islower():
             if terminal in self.no_terminales or terminal in self.terminales:
                 print('No terminal repetido o pertenece a los terminales.')
                 time.sleep(2)
             elif terminal== "":
                 #print (self.terminales)
                 break
             else:
                 self.terminales.append(terminal)
                 nuevoNodo = Nodo(terminal,1)
                 self.nodos.append(nuevoNodo)
         elif terminal == "":
             break
         else:
             print("Error, los no terminales tienen que ir en mayúscula.")
             time.sleep(2)
     self.verificar()
예제 #2
0
 def agregar_terminal(self,nombre):
     '''
     __________________________________________________________________________________      
     ► Descripción: 
     
     → Método que se encarga de agregar nuevos terminales.                
     __________________________________________________________________________________    
     ► Parámetros:                                                                      
                                                                                       
     • nombre: El valor del terminal.         
     __________________________________________________________________________________
     '''
     self.terminales.append(nombre)
     nuevoNodo = Nodo(nombre,1)
     self.nodos.append(nuevoNodo)
예제 #3
0
 def agregar_estados(self, estado):
     '''
     __________________________________________________________________________________      
     ► Descripción: 
     
     → Método encargado de agregar nuevos estados al afd.
     __________________________________________________________________________________    
     ► Parámetros:                                                                      
                                                                                       
     • estado = Nuevo valor a agregar.
     __________________________________________________________________________________
     '''
     self.estados.append(estado)
     nodo = Nodo(estado, 2)
     self.estadosN.append(nodo)
예제 #4
0
 def agregar_simbolos(self, simbolo):
     '''
     __________________________________________________________________________________      
     ► Descripción: 
     
     → Método encargado de agregar nuevos símbolos.
     __________________________________________________________________________________    
     ► Parámetros:                                                                      
                                                                                       
     • simbolo = Valor a agregar.
     __________________________________________________________________________________
     '''
     self.simbolos.append(simbolo)
     nodo = Nodo(simbolo, 3)
     self.simbolosN.append(nodo)
예제 #5
0
 def agregar_no_terminales(self,nombre):
     '''
     __________________________________________________________________________________      
     ► Descripción: 
     
     → Método que se encarga de agregar nuevos no terminales.                 
     __________________________________________________________________________________    
     ► Parámetros:                                                                      
                                                                                       
     • nombre = El valor del no terminal.           
     __________________________________________________________________________________
     '''
     #nuevo = nt(nombre)
     self.no_terminales.append(nombre)
     nuevoNodo = Nodo(nombre,0)
     self.nodos.append(nuevoNodo)
예제 #6
0
    def convertirGramatica(self):
        '''
        __________________________________________________________________________________      
        ► Descripción: 
        
        → Método encargado de convertir una gramática recursiva por izquierda a una gra-
            mática recursiva por derecha.
        __________________________________________________________________________________    
        ► Parámetros:                                                                      
                                                                                          
            (Sin parámetros)
        __________________________________________________________________________________
        '''
        if self.izquierda == True:
            prod = self.pc
            #print(self.pc)
            listaI = []
            letraI = ""
            nuevo_simbolo = ""
            direccion = []
            ordenada = []
            produccion = ""
            convertidas = []
            for i in prod: #Agregar todas los estados de las producciones
                if not i[0] in listaI:
                    listaI.append(i[0])
            for i in listaI:
                nuevo_simbolo = i+"'"
                if not nuevo_simbolo in self.no_terminales:
                    self.no_terminales.append(nuevo_simbolo)
                    nuevoNodo = Nodo(nuevo_simbolo,0)
                    self.nodos.append(nuevoNodo)
                for j in prod:
                    if i in j[0]:
                        direccion.append(j)
                if self.diccionario[i]==1:
                    tempSin = []
                    tempCon = []                    
                    for j in direccion:
                        temp = j.split(">")

                        if not i in temp[1]:
                            tempSin.append(i+">"+temp[1]+nuevo_simbolo)
                        else:
                            tempCon.append(nuevo_simbolo+">"+temp[1].replace(i,"")+nuevo_simbolo)
                    tempCon.append(nuevo_simbolo+">"+"épsilon")
                    for j in tempSin:
                        convertidas.append(j) 
                    for j in tempCon:
                        convertidas.append(j)   
                else:
                    for i in direccion:
                        convertidas.append(i)
                direccion=[]
            print("Gramática original")
            print(self.producciones)
            print("Gramática transformada")
            self.pc=convertidas
            self.guardar=convertidas
            self.pReporte=convertidas
            print(self.pc)
            self.guardar=self.pc
            input()
            self.transformada = True
        else:
            os.system("cls")
            print("La gramática no posee recursividad por la izquierda")
            self.guardar = self.pc
            print(self.pc)
            input()
            self.transformada = True
예제 #7
0
    def formatear_lista_nt(self):
        '''
        __________________________________________________________________________________      
        ► Descripción: 
        
        → Método encargado de formatear una lista al formato (nt,(prod,prod,prod.......))                
        __________________________________________________________________________________    
        ► Parámetros:                                                                      
                                                                                          
            (Sin parámetros)          
        __________________________________________________________________________________
        '''
        lista = self.grammarN.get_pcN()
        #print(self.grammarN.get_pc())
        listaR = []
        tempo = []
        tmp = []
        nt = ""
        lista_ordenada = []
        epsilon = Nodo("epsilon", 4)
        for i in lista:
            temporal = i
            #print(i)
            try:
                if len(temporal[2]) > 1:
                    temporal[2].append(temporal[1])
                    #print(temporal[2])
                else:
                    if not temporal[2] == "-" and not temporal[1] == "-":
                        temporal[2] = [temporal[2], temporal[1]]
                    elif temporal[1] == "-":
                        #temporal[2]=["-"] #cambio de caracter a nodo épsilon 25-03-2020
                        temporal[2] = [epsilon]
            except:
                if not temporal[2] == "-" and not temporal[1] == "-":
                    temporal[2] = [temporal[2], temporal[1]]
                elif temporal[2] == "-" and temporal[1] == "-":
                    #temporal[2]=["-"] #cambio por nodo epsilon
                    temporal[2] = [epsilon]
                elif temporal[1] == "-":
                    temporal[2] = [temporal[2]]
            temporal.pop(1)
            listaR.append(temporal)
            #print(temporal)
        #print(listaR)

        for i in listaR:
            if i[0].get_valor() in nt:
                for j in range(1, len(i)):
                    for k in range(0, len(i[j])):
                        tmp.append(i[j][k])
                    if i == listaR[len(listaR) - 1]:
                        tempo.append(tmp)
            else:
                tempo.append(tmp)
                tmp = []
                nt = i[0].get_valor()
                tmp.append(i[0])
                for j in range(1, len(i)):
                    for k in range(0, len(i[j])):
                        tmp.append(i[j][k])
                    if i == listaR[len(listaR) - 1]:
                        tempo.append(tmp)
        listaR = tempo[1:]
        print()
        for i in range(0, len(listaR) - 1):
            j = i + 1
            if j <= len(listaR) - 1:
                while j <= (len(listaR) - 1):
                    if listaR[i][0].get_valor() == listaR[j][0].get_valor():
                        l = 1
                        for k in listaR[j]:
                            if l <= len(listaR[j]) - 1:
                                listaR[i].append(listaR[j][l])
                                l = l + 1

                        listaR.pop(j)
                    j = j + 1

        self.grammarN.set_pcN(listaR)

        for i in listaR:
            for j in i:
                print()
                print(j.get_valor())
        return listaR
예제 #8
0
    def crearAfd(self, lista):
        '''
        __________________________________________________________________________________      
        ► Descripción: 
        
        → Método que se encarga de crear un afd a partir de los datos de entrada.               
        __________________________________________________________________________________    
        ► Parámetros:                                                                      
                                                                                          
        • lista = Lista con todos los elementos del afd en el archivo de entrada.           
        __________________________________________________________________________________
        '''
        nt1 = ""
        nt2 = ""
        sim = ""
        ntA = ""
        transicion = ""
        validacion = ""
        validacion2 = ""
        for i in lista:
            i = i.replace(" ", "")
            validacion = i.split(";")
            transicion = validacion[0]
            validacion = validacion[1].split(",")
            validacion2 = validacion[1]
            validacion = validacion[0]
            i = i.replace(";", ",")
            i = i.split(",")
            nt1 = i[0]
            nt2 = i[1]
            sim = i[2]
            ntA = i[3]
            ntA2 = i[4]
            if not nt1 in self.afdN.get_estados():
                self.afdN.agregar_estados(nt1)
            if not nt2 in self.afdN.get_estados() and nt2 != "-":
                self.afdN.agregar_estados(nt2)
            if not sim in self.afdN.get_simbolos():
                self.afdN.agregar_simbolos(sim)
            if not transicion in self.afdN.get_transiciones():
                self.afdN.agregar_transiciones(nt1 + "," + nt2 + ";" + sim)
                nodo1 = Nodo(nt1, 2)
                nodo2 = Nodo(nt2, 2)
                nodo3 = Nodo(sim, 3)
                self.afdN.agregar_transicionesN([nodo1, nodo2, nodo3])
                if validacion.lower(
                ) == "true" and not nt1 in self.afdN.get_estadoA():
                    self.afdN.agregar_estadoA(nt1)
                elif validacion.lower(
                ) == "false" and nt1 in self.afdN.get_estadoA():
                    nodo = self.afdN.buscar_nodo(nt1)
                    self.afdN.get_estadoA().remove(nt1)
                    self.afdN.get_estadoAN().remove(nodo)
                # Validaciones para nt2
                if validacion2.lower(
                ) == "true" and not nt2 in self.afdN.get_estadoA():
                    self.afdN.agregar_estadoA(nt2)
                elif validacion.lower(
                ) == "false" and nt2 in self.afdN.get_estadoA():
                    nodo = self.afdN.buscar_nodo(nt2)
                    self.afdN.get_estadoA().remove(nt2)
                    self.afdN.get_estadoAN().remove(nodo)

        self.afdN.agregar_estadoI(self.afdN.get_estados()[0])
        self.afd = True
예제 #9
0
    def crearGramatica(self, lista):
        '''
        __________________________________________________________________________________      
        ► Descripción: 
        
        → Método que se encarga de crear una gramática a partir del archivo de entrada.                 
        __________________________________________________________________________________    
        ► Parámetros:                                                                      
                                                                                          
        • lista = Lista de todos los elementos de la gramática en el archivo de entrada.           
        __________________________________________________________________________________
        '''
        for i in lista:
            i = i.replace(" ", "")
            terminal = ""
            nt = ""
            inicial = ""
            final = ""
            temporal = ""
            if "|" in i:
                temporal = i.split("|")
                for j in temporal:
                    if ">" in j:
                        inicial = j.split(">")
                        final = inicial[1]
                        inicial = inicial[0]
                    else:
                        final = j
                    if len(self.gramarN.get_no_terminales()) == 0:
                        self.gramarN.agreger_nt_inicial(inicial)
                    if not inicial in self.gramarN.get_no_terminales():
                        self.gramarN.agregar_no_terminales(inicial)
                        nodo = Nodo(inicial,
                                    0)  #Agregar no terminales y sus nodos
                        self.gramarN.agregar_nodo(nodo)
                    if final != "épsilon" and final != "epsilon":
                        for k in final:
                            if not k in self.gramarN.get_terminales(
                            ) and k.islower():
                                self.gramarN.agregar_terminal(
                                    k)  #Agregar terminales y sus nodos
                                nodo = Nodo(k, 1)
                                self.gramarN.agregar_nodo(nodo)
                            elif k.isupper(
                            ) and not k in self.gramarN.get_no_terminales():
                                self.gramarN.agregar_no_terminales(
                                    k)  #Agregar no terminales y sus nodos
                                nodo = Nodo(k, 0)
                                self.gramarN.agregar_nodo(nodo)
                            elif not k in [
                                    "'", "\\", "-"
                            ] and not k in self.gramarN.get_terminales(
                            ) and not j in self.gramarN.get_no_terminales():
                                self.gramarN.agregar_terminal(
                                    k)  #Agregar terminales y sus nodos
                                nodo = Nodo(k, 1)
                                self.gramarN.agregar_nodo(nodo)
                    produccion = inicial + ">" + final
                    if not produccion in self.gramarN.get_producciones():
                        self.gramarN.agregar_produccion(
                            produccion)  #Agregar producciones

            else:
                i = i.replace(" ", "")
                temporal = i.split(">")
                inicial = temporal[0]
                final = temporal[1]
                if len(self.gramarN.get_no_terminales()) == 0:
                    self.gramarN.agreger_nt_inicial(inicial)
                if not inicial in self.gramarN.get_no_terminales():
                    self.gramarN.agregar_no_terminales(
                        inicial)  #Agregar no terminales y nodos
                    nodo = Nodo(inicial, 0)
                    self.gramarN.agregar_nodo(nodo)
                if final != "épsilon" and final != "epsilon":
                    for j in final:
                        if j.isupper(
                        ) and not j in self.gramarN.get_no_terminales():
                            self.gramarN.agregar_no_terminales(
                                j)  #Agregar no terminales y nodos
                            nodo = Nodo(j, 0)
                            self.gramarN.agregar_nodo(nodo)
                        if j.islower(
                        ) and not j in self.gramarN.get_terminales():
                            self.gramarN.agregar_terminal(j)
                            nodo = Nodo(j, 1)
                            self.gramarN.agregar_nodo(nodo)
                        if not j in [
                                "'", "\\", "-"
                        ] and not j in self.gramarN.get_terminales(
                        ) and not j in self.gramarN.get_no_terminales():
                            self.gramarN.agregar_terminal(
                                j)  #Agregar terminales y sus nodos
                            nodo = Nodo(j, 1)
                            self.gramarN.agregar_nodo(nodo)

                if not i in self.gramarN.get_producciones():
                    self.gramarN.agregar_produccion(i)  #Agregar producciones
            self.gramarN.mostrar_gramatica(i[0], i)