import java.io.*; /* A simple Turing machine simulator Run from the command line with one argument, as: java tm where is the name of a file containing a quintuple listing representing a Turing machine. Once loaded, the machine can be run on successive tapes. The tape may be entered at the command line or input from a file. For the former, just enter the tape at the "Enter tape" prompt; for the latter enter "@". NOTE: In the quintuple listings, the blank symbol is represented by '-'; hence this cannot be used as a separate tape symbol. Quintuples may contain a 'wildcard' character '*'; thus the quintuple 2 * 2 * 1 means that any input symbol is allowed; the output symbolo will be the same. The execution mode can be changed at any time when the execution is halted; entering a carriage-return only will keep the execution mode unchanged. Antony Galton, 2005 */ public class tm { static Quintuple[] quins; // the quintuple listing static Tape tape; // the tape, with the current head position marked static int qCount; // counts the number of quintuples in the listing static int state; // the current state of the Turing machine static int mode; // the execution mode ('s','c','q','a','n' or 'h') static int leftmost; // leftmost tape square visited static int rightmost; // rightmost tape square visited static int netmove; // the net displacement from starting position public static void main(String[] args) { String quinfile = args[0]; readFromFile(quinfile); String tapeinput = ""; while (tapeinput.length()==0 || tapeinput.charAt(0)!='q') { BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.println(); System.out.print("Enter tape (q to quit): "); try { tapeinput = console.readLine(); } catch (IOException e){} if (tapeinput.length()>0 && tapeinput.charAt(0)=='@') { String tapefile = tapeinput.substring(1,tapeinput.length()); try { BufferedReader in = new BufferedReader(new FileReader(tapefile)); tapeinput = in.readLine(); System.out.println(tapeinput); in.close(); } catch(IOException e){System.out.println("File not found");} } if (tapeinput.length()==0 || tapeinput.charAt(0)!='q') { tape = Tape.parseTape(tapeinput); runTM(); // this is where the action happens! } } } public static void readFromFile(String filename) { try{ BufferedReader in = new BufferedReader(new FileReader(filename)); String line; // Find out number of quintuples (=qCount). qCount = 0; boolean counting = true; while ((line = in.readLine())!=null && counting) { Reader inline = new StringReader(line); if (inline.read()=='0') counting=false; if (counting) qCount++; } System.out.println(qCount+" quintuples:"); in.close(); // Set up array for quintuples, read them in, and display them. quins = new Quintuple[qCount]; BufferedReader in2 = new BufferedReader(new FileReader(filename)); int qNumber = 0; while ((line = in2.readLine())!=null && qNumber0) { m=chooseMode.charAt(0); if (m=='h') displayModeMenu(); else if (m!='n' && m!='q' && m!='a' && m!='c') m='s'; } else m=oldMode; // if just enter key pressed, don't change mode } return m; } public static void displayModeMenu() { System.out.println("s Step (one step displayed at a time)"); System.out.println("c Change (only state-changes displayed)"); System.out.println("q Quick (all steps displayed without stopping)"); System.out.println("n No display (only halting step displayed)"); System.out.println("a Abort (terminate execution immediately)"); System.out.println("h Help (display this menu and await further input)"); } public static void displayConfiguration() { System.out.println(); tape.writeTape(); for (int j=0;j=0;i--) System.out.print(this.left.charAt(i)); System.out.print(this.head); System.out.println(this.right); for (int i=0;i