Beispiel #1
0
import sys
sys.path.append("..")

from regular.grammar import CFG
from utils import file_loader

grammar = file_loader.load('l', 'cfg')
grammar.print_grammar()
print('Tabela SLR:')
grammar.create_table()
Beispiel #2
0
import sys
sys.path.append("..")

from regular.automata import Automata
from regular.grammar import RegGrammar
from utils import file_loader

automata: Automata = file_loader.load('c', 'automata')
automata.print_automata()
print('AF -> GR')
_grammar = automata.to_RG()
_grammar.print_grammar()

grammar = file_loader.load('c', 'grammar')
grammar.print_grammar()
print('GR -> AF')
_automata = grammar.to_FA()
_automata.print_automata()
Beispiel #3
0
import sys
sys.path.append("..")

from regular.automata import Automata
from utils import file_loader

automata = file_loader.load('e', 'automata')
automata.print_automata()
print("Minimizado:")
automata.to_minimal()
automata.print_automata()
Beispiel #4
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"
                    )
Beispiel #5
0
    def __init__(self):
        self.temp_grammar = serializer.load_all('cfg')
        self.terminal_text = 'cfg -> '
        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 == 'chomsky':
                        self.active_grammar.to_chomsky_normal_form()
                    
                    elif command == 'remove_recursion':
                        self.active_grammar.remove_left_recursion()
                    
                    elif command == 'remove_non_determinism':
                        self.active_grammar.remove_non_determinism()
                    

                    flag = True
                
                if command == 'help':
                    self.print_help()
            
                elif command == 'q':
                    print('...')

                elif command == 'des':
                    self.active_grammar = None
                    self.terminal_text = 'cfg -> '
                
                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, 'cfg')
                    if _grammar:
                        self.activate_grammar = _grammar
                        self.terminal_text = 'cfg/' + 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")
Beispiel #6
0
import sys
sys.path.append("..")

from regular.automata import *
from utils import file_loader

automata1 = file_loader.load('f1', 'automata')
automata2 = file_loader.load('f2', 'automata')
print('Automato 1')
automata1.print_automata()
print('Automato 2')
automata2.print_automata()
print("União:")
union = afd_union_intersection(automata1, automata2, True)
union.print_automata()
print("Intersecção:")
intersection = afd_union_intersection(automata1, automata2, False)
intersection.print_automata()