Exemple #1
0
def main():
    mainCMD = -1
    while mainCMD != 0:
        printCommand()
        mainCMD = int(input("command: "))
        cmd = -1
        if mainCMD == 1:
            print("1 -> Read grammar from file")
            print("2 -> Read grammar from console")
            readCmd = int(input())
            if readCmd == 1:
                grammar = Grammar.readFromFile('grammar.txt')
            else:
                grammar = Grammar.readFromConsole()
            while cmd != 0:
                printCommandGrammar()
                cmd = int(input("command: "))
                if cmd == 1:
                    print("The set of non-terminals (N): ", grammar.N)
                elif cmd == 2:
                    print("The set of terminals (E): ", grammar.E)
                elif cmd == 3:
                    print("The set of productions (P): ", grammar.P)
                elif cmd == 4:
                    symbol = input("The symbol: ")
                    print(grammar.getProductions(symbol))
                elif cmd == 5:
                    print(grammar.checkRegular())
                elif cmd == 6:
                    convertedFA = FiniteAutomata.fromRegularGrammar(grammar)
                    print("Q -> ", convertedFA.Q)
                    print("E -> ", convertedFA.E)
                    print("q0 -> ", convertedFA.q0)
                    print("F ->", convertedFA.F)
                    print("D -> ", convertedFA.D)
        elif mainCMD == 2:
            print("1 -> Read FA from file")
            print("2 -> Read FA from console")
            readCmd = int(input())
            if readCmd == 1:
                fa = FiniteAutomata.readFromFile('fa.txt')
            else:
                fa = FiniteAutomata.readFromConsole()
            while cmd != 0:
                printCommandFA()
                cmd = int(input("command: "))
                if cmd == 1:
                    print("The set of states (Q): ", fa.Q)
                elif cmd == 2:
                    print("Alphabet: ", fa.E)
                elif cmd == 3:
                    print("The transitions (D): ", fa.D)
                elif cmd == 4:
                    print("The set of final states (F):", fa.F)
                elif cmd == 5:
                    convertedRG = Grammar.fromFA(fa)
                    print("N -> ", convertedRG.N)
                    print("E -> ", convertedRG.E)
                    print("S -> ", convertedRG.S)
                    print("P ->", convertedRG.P)