Esempio n. 1
0
def make_it_proper_to_grammar(x):
    """ 
    :param x: {string} grammar
    """
    grammar = x.split(' ')
    initial_simbol = grammar[0]
    non_terminals = set()
    terminals = set()
    temporary_alfa = ''
    temporary_beta = set()

    productionDict = dict()

    grammar_length = len(grammar)

    for i in range(0, grammar_length):
        if grammar[i].isupper():            
            non_terminals.add(grammar[i])

        if not grammar[i] == '|' and not grammar[i] == '->' and not i == 0 and not grammar[i].isupper():
            temporary_beta.add(grammar[i])
            terminals.add(grammar[i][0])

        if grammar[i] == '->' or i == grammar_length - 1:
            if len(temporary_beta) > 0:
                productionDict[temporary_alfa] = temporary_beta

            temporary_beta = set()
            temporary_alfa = grammar[i-1]

    return regular_grammar(non_terminals, terminals, productionDict, initial_simbol)
Esempio n. 2
0
    def to_grammar(self):
        """
        :return: Productions of grammar
        """
        terminals = self.get_alphabet()
        initial_simbol = self.initial_state
        non_terminals = set()
        productions = dict()
        temp = list()
        string = ''
        next_state = set()

        for current_state in self.delta: #each state
            non_terminals.add(current_state)
            for alphabet_simbol in self.delta[current_state]: #each simbol of the alphabet
                for next_state_simbol in self.delta[current_state][alphabet_simbol]: #each next state
                    string += next_state_simbol
                    next_state.add(next_state_simbol)
                    if next_state in self.accept_states and alphabet_simbol not in temp:
                        temp.append(alphabet_simbol)
                temp.append(alphabet_simbol + string)
                string = ''
            productions[current_state] = temp
            temp = list()
            next_state = set()

        return regular_grammar(non_terminals, terminals, productions, initial_simbol)