Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Set;
- public class MyProgram {
- public static void main(String[] args) throws IOException {
- // Interpret the DFA in the file name given in the command line. If no such file is found
- // or no filename argument is given, use the default file. If no command line input is given,
- // assume an empty string given to the default DFA.
- if (args.length == 0) {
- interpretDFA("Default.txt", "");
- return;
- }
- try {
- interpretDFA(args[1], args[0]);
- } catch (FileNotFoundException e1) {
- interpretDFA("Default.txt", args[0]);
- } catch (ArrayIndexOutOfBoundsException e2) {
- interpretDFA("Default.txt", args[0]);
- } catch (Exception e) {
- System.out.println("Invalid command line input. Quitting.");
- return;
- }
- }
- public static void interpretDFA(String filename, String inputString)
- throws FileNotFoundException, IOException {
- char[] input = inputString.toCharArray();
- // Create a BufferedReader to parse the DFA text file
- File DFAfile = new File(filename);
- FileReader DFAreader = new FileReader(DFAfile);
- BufferedReader reader = new BufferedReader(DFAreader);
- // Retrieve the state names from the first line of the text file, using commas as delimiters
- // Ignore white space. Prepare an array to store each state
- String[] stateNames = reader.readLine().split("\\s*,\\s*");
- State[] states = new State[stateNames.length];
- for (int i = 0; i < stateNames.length; ++i) {
- states[i] = new State(stateNames[i], false, null);
- }
- // Retrieve the alphabet set from the second line of the text file
- String[] alphabetStringArray = reader.readLine().split("\\s*,\\s*");
- ArrayList<Character> alphabet = new ArrayList<Character>();
- for (String s : alphabetStringArray) {
- alphabet.add(s.charAt(0));
- }
- // Retrieve the name of the initial state from the third line
- String initialStateString = reader.readLine();
- State initialState = null;
- for (State state: states) {
- if (state.getName().equals(initialStateString)) {
- initialState = state;
- break;
- }
- }
- // Retrieve the set of final states from the fourth line
- String[] finalStatesStringArray = reader.readLine().split("\\s*,\\s*");
- Set<String> finalStates = new HashSet<String>();
- for (String s : finalStatesStringArray) {
- finalStates.add(s);
- }
- // Update any states which are accept states
- String stateName;
- for (State state2: states) {
- stateName = state2.getName();
- if (finalStates.contains(stateName)) {
- state2.setAcceptState(true);
- }
- }
- // Set the transition for each state by parsing the rest of the lines in the text file. Every line
- // represents a state and contains one destination state for each letter of the given alphabet.
- String[] s;
- State destinationState;
- for (State state3 : states) {
- Transition[] transitions = new Transition[alphabet.size()];
- s = reader.readLine().split("\\s*,\\s*");
- for (int i = 0; i < alphabet.size(); ++i) {
- for (State state4 : states) {
- if (state4.getName().equals(s[i])) {
- destinationState = state4;
- transitions[i] = new Transition(destinationState, alphabet.get(i));
- }
- }
- }
- state3.setTransitions(transitions);
- }
- reader.close();
- // Simulate the DFA once the file is parsed
- simulateDFA(input, states, initialState, alphabet);
- }
- public static void simulateDFA(char[] input, State[] states, State initial,
- ArrayList<Character> alphabet) {
- // Set the current state to the given initial state
- State cursor = initial;
- // Create a queue based on the input array
- Queue<Character> q = new LinkedList<Character>();
- for (char c: input) {
- q.add(c);
- }
- // Initialise the prefix string as a string with length one larger than the input string
- String prefix = "";
- for (int i = 1; i < input.length + 1; ++i) {
- prefix += " ";
- }
- StringBuilder prefixBuild = new StringBuilder(prefix);
- char c;
- int counter = 0;
- // Build the output strings by applying the input to the transitions of the DFA
- for (Iterator<Character> i = q.iterator(); i.hasNext(); ) {
- c = q.poll();
- if (!alphabet.contains(c)) {
- break;
- }
- System.out.print(prefixBuild.toString());
- System.out.print(cursor.getName() + " -- " + c + " --> ");
- for (Transition t : cursor.getTransitions()) {
- if (c == t.getCharacter()) {
- cursor = t.getState();
- System.out.print(cursor.getName() + " ");
- for (char unscanned: q) {
- System.out.print(unscanned);
- }
- System.out.println();
- }
- }
- prefixBuild.setCharAt(counter++, c);
- }
- // Determine whether the input was accepted by checking whether or not the final state
- // reached was an accept state
- if (cursor.isAcceptState()) {
- System.out.println("accepted");
- } else {
- System.out.println("rejected");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement