Question: 1. (50 points) Translate the following Python code into Java code. file = open(dfa.txt, 'r') data = file.read().split(' ') # print(data) states = data[0].split(',') #
1. (50 points) Translate the following Python code into Java code.
file = open("dfa.txt", 'r') data = file.read().split(' ') # print(data) states = data[0].split(',') # print(states) variables = data[1].split(',') # print(variables) initial_state = data[2] final_states = data[3].split(',') # print(initial_state) # print(final_state) dfa = {} for i in range(4, len(data)): tran = data[i].split(',') dfa[(tran[0], tran[1])] = tran[2] # print(dfa) file1 = open("input.txt", 'r') file2 = open("output.txt", 'w+') output = [] data1 = file1.read().split(' ') for s in data1: curr_state = initial_state ans = 1 for i in s: if (curr_state, i) in dfa: curr_state = dfa[(curr_state, i)] else: ans = 0 break if ans: if curr_state in final_states: output.append("accept ") else: output.append("reject ") else: output.append("reject ") file2.writelines(output)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
