Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1.  
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.Queue;
  13. import java.util.Set;
  14.  
  15. public class MyProgram {
  16.  
  17. public static void main(String[] args) throws IOException {
  18.  
  19. // Interpret the DFA in the file name given in the command line. If no such file is found
  20. // or no filename argument is given, use the default file. If no command line input is given,
  21. // assume an empty string given to the default DFA.
  22.  
  23. if (args.length == 0) {
  24. interpretDFA("Default.txt", "");
  25. return;
  26. }
  27. try {
  28. interpretDFA(args[1], args[0]);
  29. } catch (FileNotFoundException e1) {
  30. interpretDFA("Default.txt", args[0]);
  31. } catch (ArrayIndexOutOfBoundsException e2) {
  32. interpretDFA("Default.txt", args[0]);
  33. } catch (Exception e) {
  34. System.out.println("Invalid command line input. Quitting.");
  35. return;
  36. }
  37.  
  38. }
  39.  
  40. public static void interpretDFA(String filename, String inputString)
  41. throws FileNotFoundException, IOException {
  42.  
  43. char[] input = inputString.toCharArray();
  44.  
  45. // Create a BufferedReader to parse the DFA text file
  46. File DFAfile = new File(filename);
  47. FileReader DFAreader = new FileReader(DFAfile);
  48. BufferedReader reader = new BufferedReader(DFAreader);
  49.  
  50. // Retrieve the state names from the first line of the text file, using commas as delimiters
  51. // Ignore white space. Prepare an array to store each state
  52. String[] stateNames = reader.readLine().split("\\s*,\\s*");
  53. State[] states = new State[stateNames.length];
  54. for (int i = 0; i < stateNames.length; ++i) {
  55. states[i] = new State(stateNames[i], false, null);
  56. }
  57.  
  58. // Retrieve the alphabet set from the second line of the text file
  59. String[] alphabetStringArray = reader.readLine().split("\\s*,\\s*");
  60. ArrayList<Character> alphabet = new ArrayList<Character>();
  61. for (String s : alphabetStringArray) {
  62. alphabet.add(s.charAt(0));
  63. }
  64.  
  65. // Retrieve the name of the initial state from the third line
  66. String initialStateString = reader.readLine();
  67. State initialState = null;
  68. for (State state: states) {
  69. if (state.getName().equals(initialStateString)) {
  70. initialState = state;
  71. break;
  72. }
  73. }
  74.  
  75. // Retrieve the set of final states from the fourth line
  76. String[] finalStatesStringArray = reader.readLine().split("\\s*,\\s*");
  77. Set<String> finalStates = new HashSet<String>();
  78. for (String s : finalStatesStringArray) {
  79. finalStates.add(s);
  80. }
  81.  
  82. // Update any states which are accept states
  83. String stateName;
  84. for (State state2: states) {
  85. stateName = state2.getName();
  86. if (finalStates.contains(stateName)) {
  87. state2.setAcceptState(true);
  88. }
  89. }
  90.  
  91. // Set the transition for each state by parsing the rest of the lines in the text file. Every line
  92. // represents a state and contains one destination state for each letter of the given alphabet.
  93. String[] s;
  94. State destinationState;
  95. for (State state3 : states) {
  96. Transition[] transitions = new Transition[alphabet.size()];
  97. s = reader.readLine().split("\\s*,\\s*");
  98. for (int i = 0; i < alphabet.size(); ++i) {
  99. for (State state4 : states) {
  100. if (state4.getName().equals(s[i])) {
  101. destinationState = state4;
  102. transitions[i] = new Transition(destinationState, alphabet.get(i));
  103. }
  104. }
  105. }
  106. state3.setTransitions(transitions);
  107. }
  108.  
  109. reader.close();
  110.  
  111. // Simulate the DFA once the file is parsed
  112. simulateDFA(input, states, initialState, alphabet);
  113. }
  114.  
  115.  
  116. public static void simulateDFA(char[] input, State[] states, State initial,
  117. ArrayList<Character> alphabet) {
  118.  
  119. // Set the current state to the given initial state
  120. State cursor = initial;
  121.  
  122. // Create a queue based on the input array
  123. Queue<Character> q = new LinkedList<Character>();
  124. for (char c: input) {
  125. q.add(c);
  126. }
  127.  
  128. // Initialise the prefix string as a string with length one larger than the input string
  129. String prefix = "";
  130. for (int i = 1; i < input.length + 1; ++i) {
  131. prefix += " ";
  132. }
  133. StringBuilder prefixBuild = new StringBuilder(prefix);
  134.  
  135. char c;
  136. int counter = 0;
  137.  
  138. // Build the output strings by applying the input to the transitions of the DFA
  139. for (Iterator<Character> i = q.iterator(); i.hasNext(); ) {
  140. c = q.poll();
  141. if (!alphabet.contains(c)) {
  142. break;
  143. }
  144. System.out.print(prefixBuild.toString());
  145. System.out.print(cursor.getName() + " -- " + c + " --> ");
  146. for (Transition t : cursor.getTransitions()) {
  147. if (c == t.getCharacter()) {
  148. cursor = t.getState();
  149. System.out.print(cursor.getName() + " ");
  150. for (char unscanned: q) {
  151. System.out.print(unscanned);
  152. }
  153. System.out.println();
  154. }
  155. }
  156. prefixBuild.setCharAt(counter++, c);
  157. }
  158.  
  159. // Determine whether the input was accepted by checking whether or not the final state
  160. // reached was an accept state
  161. if (cursor.isAcceptState()) {
  162. System.out.println("accepted");
  163. } else {
  164. System.out.println("rejected");
  165. }
  166.  
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement