Exemplo n.º 1
0
Arquivo: CFG.py Projeto: SKREFI/things
 def __getAllWordsHelper(self, word: str, max_length: int) -> None:
     # has no more nts so we add if it if not bigger then max_length
     Log.print('Proccessing: ' + word)
     if self.__countNTS(word) == 0:
         if len(word) <= max_length:
             self.words.add(word)
     if self.__countTS(word) <= max_length:
         nts = self.__getNTS(word)
         if nts is not None:
             for right in self.p[nts[1:-1]]:
                 self.__getAllWordsHelper(word.replace(nts, right),
                                          max_length)
Exemplo n.º 2
0
    def getRegex(self) -> str:
        """ Filtering
        0. Delte dead states
        1. For each finals wich have an outgoing edge, point them
        with E(psilon) (empty string) to a new uniq final state
        2. If initial state has incoming edges, create a new one """

        self.__createNewInitialState()
        self.__createUniqFinalState()
        self.removeDeadStates()
        self.sumSelfDirectionEdges()
        self.__spreadSelfPointingEdge()

        while len(self.states) > 0:
            state = self.states[0]
            for going in [x for x in self.edges[state] if x[1] != state]:
                for coming in [x for x in self.getAllEdges() if x[2] == state]:
                    self.addEdge(coming[0], coming[1] + going[0], going[1])
            self.removeState(state)
            self.__spreadSelfPointingEdge()

        try_it, i = True, 0
        while try_it:
            try:
                if self.edges[S_NODE][i][1] != F_NODE:
                    self.edges[S_NODE].remove(self.edges[S_NODE][i])
                else:
                    i += 1
            except:
                try_it = False

        Log.print(self)

        solution = ''
        for edge in self.edges[S_NODE]:
            if edge[1] == F_NODE:
                solution += edge[0] + ' + '
        solution = solution[:-3]
        return solution