예제 #1
0
파일: __main__.py 프로젝트: kundlatsch/UFSC
from regular import automata


def main():
    print(
        '------------------------\nTrabalho de Formais v0.1\n------------------------'
    )
    print('Para obter ajuda digite \'help\'')
    run()


if __name__ == "__main__":
    # Create grammar save file for tests
    # _grammar = grammar.grammar('teste')
    # _grammar.add_state('A', [['a', 'A'], ['b', 'B']], 1)
    # _grammar.add_state('B', [['b', 'B']])
    # _grammar.set_start('A')
    # _grammar.compute("aabc")
    # serializer.save(_grammar)

    _grammar = grammar.CFG('teste')
    _grammar.add_productions('S', ['abA', 'a', 'A', 'Sa'])
    _grammar.add_productions('A', ['SaA', 'AA', 'a'])
    _grammar.set_start_symbol('S')
    #_grammar.print_grammar()
    #print('-------------')
    #_grammar.to_chomsky_normal_form()
    # _grammar.remove_left_recursion()
    _grammar.remove_non_determinism()
    serializer.save(_grammar)
    main()
예제 #2
0
    def __init__(self):
        self.temp_automata = serializer.load_all('automata')
        self.terminal_text = 'automata -> '
        self.activate_automata = None
        operation = ''

        while operation != 'q':
            operation = input(self.terminal_text)
            splited_operation = operation.split()
            command = splited_operation[0]

            # COMANDOS SEM ARGUMENTO
            if len(splited_operation) == 1:
                flag = False
                if self.loaded_automata:
                    if command == 'rm_start':
                        self.activate_automata.remove_start()

                    elif command == 'to_dfa':
                        self.loaded_automata = self.loaded_automata.to_DFA()
                        self.loaded_automata.name = self.loaded_automata.name + '_dfa'
                        self.terminal_text = 'automata/' + self.loaded_automata.name + ' -> '

                    elif command == 'to_minimal':
                        self.loaded_automata = self.loaded_automata.to_minimal(
                        )
                        self.loaded_automata.name = self.loaded_automata.name + '_mini'
                        self.terminal_text = 'automata/' + self.loaded_automata.name + ' -> '

                    elif command == 'to_RG':
                        grammar = self.loaded_automata.to_RG()
                        serializer.save(grammar)
                        print('Expressão regular criada e salva!')

                    flag = True

                if command == 'help':
                    self.print_help()

                elif command == 'q':
                    print('...')

                elif command == 'des':
                    self.loaded_automata = None
                    self.terminal_text = 'automata -> '

                elif command == 'print':
                    self.loaded_automata.print_automata()

                else:
                    if not flag:
                        print(
                            "1 - Comando inválido. Digite \'help\' para obter ajuda"
                        )

            # COMANDOS COM ARGUMENTO
            else:
                argument = splited_operation[1]
                args = splited_operation
                if command in self.FUNCTIONS:
                    self.FUNCTIONS[command](self, argument)

                # TODO args2 virar uma lista de tuplas ou lista de listas
                elif self.activate_automata:
                    if command == 'add_state':
                        args[2].replace(' ', '')
                        args2 = args[2].split(',')
                        self.activate_automata.add_state(args[1], args2)

                    elif command == 'rm_state':
                        self.activate_automata.remove_state(args[1])

                    # TODO args2 virar uma lista de tuplas ou lista de listas
                    # args[1] = state_name (string) args[2] = a,[A,B,C];b,B;
                    elif command == 'add_transition':
                        args[2].replace(' ', '')
                        args2 = args[2].split(',')
                        self.activate_automata.add_transition(args[1], args2)

                    elif command == 'rm_transition':
                        args[2].replace(' ', '')
                        args2 = args[2].split(',')
                        self.activate_automata.remove_transition(
                            args[1], args2)

                    elif command == 'set_start':
                        self.activate_automata.set_start(args[1])

                    elif command == 'set_end':
                        self.activate_automata.set_end_state(args[1])

                    elif command == 'rm_end':
                        self.activate_automata.remove_end_state(args[1])

                    elif command == 'compute':
                        self.activate_automata.compute(args[1])

                else:
                    print(
                        "2 - Comando inválido. Digite \'help\' para obter ajuda"
                    )
예제 #3
0
    def __init__(self):
        self.temp_grammar = serializer.load_all('reggrammar')
        self.terminal_text = 'grammar -> '
        self.active_grammar = None
        operation = ''
        while operation != 'q':
            operation = input(self.terminal_text)
            splited_operation = operation.split()
            command = splited_operation[0]
            if len(splited_operation) == 1:
                flag = False
                if self.active_grammar:

                    if command == 'remove_start_symbol':
                        self.active_grammar.remove_start_symbol()

                    elif command == 'to_fa':
                        automata = self.active_grammar.to_fa()
                        serializer.save(automata)
                        print('Autômato criado e salvo!')

                    flag = True

                if command == 'help':
                    print_help()

                elif command == 'q':
                    print('...')

                elif command == 'des':
                    self.active_grammar = None
                    self.terminal_text = 'grammar -> '

                elif command == 'save':
                    self.save_grammar(self.active_grammar.name)

                elif command == 'print':
                    self.active_grammar.print_grammar()

                else:
                    if not flag:
                        print(
                            "1 - Comando inválido. Digite \'help\' para obter ajuda"
                        )

            else:
                argument = splited_operation[1]
                args = splited_operation
                if command in self.FUNCTIONS:
                    self.FUNCTIONS[command](self, argument)

                elif command == 'act-file':
                    _grammar = file_loader.load(argument, 'grammar')
                    if _grammar:
                        self.activate_grammar = _grammar
                        self.terminal_text = 'grammar/' + argument + ' -> '

                elif self.active_grammar:

                    if command == 'set_start_symbol':
                        self.active_grammar.set_start_symbol(argument)

                    elif command == 'add_production':
                        args[2].replace(' ', '')
                        args2 = args[2].split(',')
                        self.active_grammar.add_productions(argument, args2)

                    elif command == 'remove_productions':
                        args[2].replace(' ', '')
                        args2 = args[2].split(',')
                        self.active_grammar.remove_production(argument, args2)

                else:
                    print(
                        "2 - Comando inválido. Digite \'help\' para obter ajuda"
                    )
예제 #4
0
 def save_automata(self, name):
     if self.loaded_automata:
         serializer.save(self.activate_automata)
     else:
         print('Erro: não há automato ativo')
예제 #5
0
 def save_grammar(self, name):
     if self.active_grammar:
         serializer.save(self.active_grammar)
     else:
         print('Erro: não há gramática ativa')