Question: Make sure It's Java :) 2.2 What you will write In this assignment you need to write the DFA Simulator that can run a DFA



Make sure It's Java :)
2.2 What you will write In this assignment you need to write the DFA Simulator that can run a DFA against an input string. Given a DFA and an input string, the simulator will return yes if the DFA accepts the string, return false if the string is rejected. The Simulator algorithm in pseudo code is listed in Algorithm 1. You need to rewrite it into Java code, and make it work together with the DFA.java code. You can click the high-lighted link to download DFA.java. It is also listed at the end of this document. The code you should have the same class name and method name as is listed below: public class Simulator { public static boolean run (DFA dfa, String input) { \\you need to fill in the missing part here. } } Your code should work for any DFAs and input strings. You should test your code using sample DFAs and input strings. One example is the DFA that corresponds the regular expression (alb)(alb|0|1)*, whose transition diagram is as below. a,b,0.1 A Its corresponding dfa.txt is: a b 01 AB A B Ab B Ba B BOB B 1 B The format of the dfa.txt is like below: alphabet states start state final state(s) transition 1 transition_2 transition_n S = 80 The first line is a set of characters, the second line is the set of states. The third line is the start state, the fourth line is the set of final states in this case it happens that there is only one final state). Input: An input string 2, a DFA with start state so, move(s, c) function that moves state s to a new state on input c, accepting states F. Output: "yes" if D accepts I, "no" otherwise. while (c=next Char())!=of do | s= move(s,c); end if s E F then | return "yes"; end return "no" ; Algorithm 1: Simulate DFA (dragon book p. 151) 5 5 Appendix import java.util. TreeMap: import java.util.Arrays; import java.util. HashSet; import java.util.Set; import java.io.*; public class DFA { public static String [] alphabet; public String start State; public Set finalStates; public static TreeMap transitions-new TreeMap(); /** Construct a DFA from a text file public DFA(String filename) throws Exception BufferedReader br = new BufferedReader(new FileReader (filename)); alphabet-br.readLine().trim().split(" "); String [] states-br.readLine().split(" "); startState=br.readLine().trim(); String [] finals-br.readLine().trim().split(" "); finalStates= new HashSet (Arrays.asList (finals)); String line-""; while ((line-br.readLine()) !=null) { String [] transition=line.trim().split(" "); transitions.put(transition [0] +""+transition [1], transition [2]); } } public static void main(String[] args) throws Exception DFA dfa - new DFA (args [0]); String input = new BufferedReader(new FileReader (args[1])).readLine().strip(); boolean result Simulator.run(dfa, input); BufferedWriter bu=new BufferedWriter(new FileWriter (args [2])); bw.write(result+""); bw.close(); } } 2.2 What you will write In this assignment you need to write the DFA Simulator that can run a DFA against an input string. Given a DFA and an input string, the simulator will return yes if the DFA accepts the string, return false if the string is rejected. The Simulator algorithm in pseudo code is listed in Algorithm 1. You need to rewrite it into Java code, and make it work together with the DFA.java code. You can click the high-lighted link to download DFA.java. It is also listed at the end of this document. The code you should have the same class name and method name as is listed below: public class Simulator { public static boolean run (DFA dfa, String input) { \\you need to fill in the missing part here. } } Your code should work for any DFAs and input strings. You should test your code using sample DFAs and input strings. One example is the DFA that corresponds the regular expression (alb)(alb|0|1)*, whose transition diagram is as below. a,b,0.1 A Its corresponding dfa.txt is: a b 01 AB A B Ab B Ba B BOB B 1 B The format of the dfa.txt is like below: alphabet states start state final state(s) transition 1 transition_2 transition_n S = 80 The first line is a set of characters, the second line is the set of states. The third line is the start state, the fourth line is the set of final states in this case it happens that there is only one final state). Input: An input string 2, a DFA with start state so, move(s, c) function that moves state s to a new state on input c, accepting states F. Output: "yes" if D accepts I, "no" otherwise. while (c=next Char())!=of do | s= move(s,c); end if s E F then | return "yes"; end return "no" ; Algorithm 1: Simulate DFA (dragon book p. 151) 5 5 Appendix import java.util. TreeMap: import java.util.Arrays; import java.util. HashSet; import java.util.Set; import java.io.*; public class DFA { public static String [] alphabet; public String start State; public Set finalStates; public static TreeMap transitions-new TreeMap(); /** Construct a DFA from a text file public DFA(String filename) throws Exception BufferedReader br = new BufferedReader(new FileReader (filename)); alphabet-br.readLine().trim().split(" "); String [] states-br.readLine().split(" "); startState=br.readLine().trim(); String [] finals-br.readLine().trim().split(" "); finalStates= new HashSet (Arrays.asList (finals)); String line-""; while ((line-br.readLine()) !=null) { String [] transition=line.trim().split(" "); transitions.put(transition [0] +""+transition [1], transition [2]); } } public static void main(String[] args) throws Exception DFA dfa - new DFA (args [0]); String input = new BufferedReader(new FileReader (args[1])).readLine().strip(); boolean result Simulator.run(dfa, input); BufferedWriter bu=new BufferedWriter(new FileWriter (args [2])); bw.write(result+""); bw.close(); } }