Exemplo n.º 1
0
def main():
    dfa1 = DFA()  #This is the initial DFA defined by the exampleDFA.txt
    testString = ""
    '''
	Opening the files and prompting the user if there are any errors.
	'''

    try:
        dfa_filename = sys.argv[
            1]  #First command line argument that will be the file containing the DFA
        testFilename = sys.argv[
            2]  #Second command line argument that will be the filecontaining the test cases
        dfa_file = open(dfa_filename, 'r')
        testFile = open(testFilename, 'r')
    except IndexError:
        #This error occurs when one of the command line arguments are missing
        sys.exit(
            "The program needs two arguments to run.\nExample: dfaSim.py dfa.txt tests.txt"
        )
    except FileNotFoundError as err:
        #This error occurs if any of the files do not exist
        sys.exit("Unable to locate file: {0}".format(str(err).split()[-1]))

    dfa1.makeDfa(dfa_file)  #Creating the dfa from the file using the dfa class

    #dfa1.printDfa()

    for line in testFile:
        line = line.replace("\n", "")
        if acceptReject(dfa1, line):
            print("accept")
        else:
            print("reject")
Exemplo n.º 2
0
def main():
    dfa1 = DFA()  #This is the initial DFA defined by the exampleDFA.txt
    testString = ""
    '''
	Opening the files and prompting the user if there are any errors.
	'''

    try:
        dfa_filename = sys.argv[
            1]  #command line argument that will be the filecontaining the test cases
        dfa_file = open(dfa_filename, 'r')
    except IndexError:
        #This error occurs when one of the command line arguments are missing
        sys.exit(
            "The program needs two arguments to run.\nExample: dfaSim.py dfa.txt tests.txt"
        )
    except FileNotFoundError as err:
        #This error occurs if any of the files do not exist
        sys.exit("Unable to locate file: {0}".format(str(err).split()[-1]))

    dfa1.makeDfa(dfa_file)  #Creating the dfa from the file using the dfa class

    #dfa1.printDfa()

    #print (testInf(dfa1,'0',[],False,[False,False]))

    tmpAnswer = testInf(dfa1, '0', [False, False])

    if tmpAnswer[0] == True and tmpAnswer[1] == True:
        print("nonempty infinite")
    if tmpAnswer[0] == True and tmpAnswer[1] == False:
        print("nonempty finite")
    if tmpAnswer[0] == False and tmpAnswer[1] == True:
        print("empty infinite")
    if tmpAnswer[0] == False and tmpAnswer[1] == False:
        print("empty finite")
Exemplo n.º 3
0
def main():
    dfa1 = DFA()  #This is the initial DFA defined by the exampleDFA.txt
    dfa2 = DFA(
    )  #If this is the intersection it houses the second DFA if there is no second DFA it will go unused
    testString = ""
    '''
	Opening the files and prompting the user if there are any errors.
	'''

    try:
        dfa_filename = sys.argv[
            1]  #First command line argument that will be the file containing the DFA
        dfa_file = open(dfa_filename, 'r')
        if len(sys.argv
               ) != 2:  #If there is no second argument given in the command
            secondFilename = sys.argv[
                2]  #Second command line argument that will be the filecontaining the test cases
            secondFile = open(secondFilename, 'r')
            dfa2.makeDfa(
                secondFile
            )  #Creating the dfa from the second file using the dfa class
    except IndexError:
        #This error occurs when one of the command line arguments are missing
        sys.exit(
            "The program needs two arguments to run.\nExample: dfaSim.py dfa.txt tests.txt"
        )
    except FileNotFoundError as err:
        #This error occurs if any of the files do not exist
        sys.exit("Unable to locate file: {0}".format(str(err).split()[-1]))

    dfa1.makeDfa(dfa_file)  #Creating the dfa from the file using the dfa class

    if dfa2.transitionStates == []:
        dfa1.acceptingStates = comp(dfa1)
        dfa1.printDfa()

    newTmpTransitionTable = []
    '''
	for i in range(len(dfa1.transitionStates)):
		for j in range(len(dfa1.alphabet) - 1):
			tmpArray = []
			for k in range(len(dfa2.alphabet) - 1):
				tmpArray2 = []
				tmpArray2.append(int(dfa1.transitionStates[i][j]))
				tmpArray2.append(int(dfa2.transitionStates[i][k]))
				tmpArray.append(tmpArray2)
			newTmpTransitionTable.append(tmpArray)
	'''
    '''
				for l in range(len(dfa2.transitionStates)):
					tmpArray2 = []
					tmpArray2.append(int(dfa1.transitionStates[i][j]))
					tmpArray2.append(int(dfa2.transitionStates[k][l]))
					tmpArray.append(tmpArray2)
				newTmpTransitionTable.append(tmpArray)
	'''
    '''
			for k in range(len(dfa1.alphabet) - 1):
				tmpArray2 = []
				tmpArray2.append(int(dfa1.transitionStates[i][j]))
				tmpArray2.append(int(dfa2.transitionStates[i][k]))
				tmpArray.append(tmpArray2)
			newTmpTransitionTable.append(tmpArray)
	'''

    newTransitionTable = []
    transitions = []

    for i in range(len(dfa1.transitionStates)):
        for j in range(len(dfa1.transitionStates)):
            transitions.append([i, j])

    for i in range(len(newTmpTransitionTable)):
        tmp = []
        for j in range(len(newTmpTransitionTable[i])):
            tmp.append(transitions.index(newTmpTransitionTable[i][j]))
        newTransitionTable.append(tmp)

    for i in newTransitionTable:
        counter = 0
        for j in i:
            if counter == len(i) - 1:
                print(j, end="")
            else:
                print(j, end=" ")
            counter = counter + 1
        print()

    print(transitions)
    print(len(transitions))
Exemplo n.º 4
0
def invHom():
	dfa1 = DFA()			#This is the initial DFA defined by the exampleDFA.txt
	inputAlphabet = ""
	outputAlphabet = ""
	testStrings = []
	lineCount = 0
	

	'''
	Opening the files and prompting the user if there are any errors.
	'''

	try:
		dfa_filename = sys.argv[1]	#First command line argument that will be the file containing the DFA
		testFilename = sys.argv[2]	#Second command line argument that will be the filecontaining the test cases
		dfa_file = open(dfa_filename,'r')
		testFile = open(testFilename,'r')
	except IndexError:
		#This error occurs when one of the command line arguments are missing
		sys.exit("The program needs two arguments to run.\nExample: dfaSim.py dfa.txt tests.txt")
	except FileNotFoundError as err:
		#This error occurs if any of the files do not exist
		sys.exit("Unable to locate file: {0}".format(str(err).split()[-1]))

	dfa1.makeDfa(dfa_file)	#Creating the dfa from the file using the dfa class

	#dfa1.printDfa()

	for line in testFile:
			lineCount = lineCount + 1

			if lineCount == 1:
				inputAlphabet = line.replace("Input alphabet: ","").replace("\n","")
			if lineCount == 2:
				outputAlphabet = line.replace("Output alphabet: ","").replace("\n","")		
			if lineCount > 2:
				testStrings.append(line.replace("\n",""))
	
	answer = [[0 for i in range(len(testStrings))] for j in range(int(dfa1.getNumStates()))]

	countStrings = 0
	for string in testStrings:
		for j in range(int(dfa1.getNumStates())):
			#print(countStrings, ":", j)
			answer[j][countStrings] = findFinal(dfa1,string,j)
		#print (countStrings)
		countStrings = countStrings + 1
	
	print("Number of states:",dfa1.getNumStates())
	
	tmpStr = ""
	numCount = 0
	for i in dfa1.getAcceptingStates():
		tmpStr = tmpStr + i
		if numCount != len(dfa1.getAcceptingStates()) - 1:
			tmpStr = tmpStr + " "
		numCount = numCount + 1
	print("Accepting states:",tmpStr)
	print("Alphabet:",inputAlphabet)
	for i in answer:
		counter = 0
		for j in i:
			if counter == len(i) - 1:
				print(j, end = "")
			else:
				print(j, end = " ")
			counter = counter + 1
		print()