CSC 320 Summer 2017: Turing Machine Simulator

If you want to copy all the files at once, get this zip file:

tm_files.zip

To run the simulator, get the four java files:

  1. TM.java
  2. Instruction.java
  3. Input.java
  4. JTM.java
  5. Copy this version of JTM.java instead if you have a MacIntosh since otherwise the Turing machine head will not be shown properly with the visual interface: JTM.java

NOTE: On a Microsoft-type browser, if I click java files to display them and then download them, then it thinks I want html and it changes < to the html code for it and the java won't compile. To copy the java files so that you can compile them, right click on the file name to display a menu which allows an option of "Save Target as" and save it like this.

Here is a sample input file: in.txt

The format of the input is as follows:

<Name of start state>
<current state> <current symbol> <next state> <head instruction>
...
<current state> <current symbol> <next state> <head instruction>
$
<input string w1>
<input string w2>
...

Rules for TM Descriptions

  1. The state name h is used to denote the halting state.
  2. Use the symbol # to represent a blank.
  3. If a line starts with // it is a comment. Comments can also be added on the same line as an instruction at the end of the line (start with //).
  4. Each of the state names is an arbitrary string. State names however cannot start with either // or $ since the // indicates a comment and $ indicates the end of the TM instructions. You can use any strings you want to represent states but the output will look nicer if you limit yourself to 6 characters or less.
  5. The current symbol and new symbol each must be a single symbol.
  6. The head instruction is either L (move the head one square left) or R (move the head one square right) or a symbol to replace the current tape square contents.
  7. The $ indicates the end of the TM description.
  8. The string w should be a single string- use # instead of a blank space if w should have a blank in it. A # by itself is treated as taking the empty string as input.

This java program will simulate the Turing machine on input w. I don't have much error detection with respect to the input, so please check your formatting carefully. If you have more than one transition for a state/symbol pair, the TM will choose to execute the first one you typed in and will always ignore any other ones. Please let me know asap if you run into any problems with the simulator- this is the first time I am using this version. The TM's from the previous text worked differently.

The sample input file in.txt contains:


s  // start state is s
// Machine OO: Created by Wendy Myrvold.
// This machine halts in state h when there is an odd
// number of a's and an odd number of b's in the input
// and it hangs in state hN otherwise (due to no transitions defined).
s  # ee L
ee a oe L // State ee means both a's and b's are even
ee b eo L // State ee means both a's and b's are even
eo a oo L // State eo means even a's and odd b's 
eo b ee L // State eo means even a's and odd b's 
oe a ee L // State oe means odd a's and even b's 
oe b oo L // State oe means odd a's and even b's 
oo a eo L // State oo means both a's and b's are odd
oo b oe L // State oo means both a's and b's are odd
// When we see the # at the RH end of the input we are done.
oo # h  # 
oe # hN # 
eo # hN # 
ee # hN # 
// This is the end of the TM
$
// Try it on w not in L
abbbaabaa
// Try it on w in L
ababab

To run it I use this (on a unix machine):

javac *.java
java TM < in.txt > out.txt 

This give you text-based output suitable for handing in with your assignment.

The program uses stdin and stdout but this makes it take the input from the file in.txt and put the output in the file out.txt.

You can do something similar on a Windows or a MAC (using the < to get input from a file instead of stdin and using > to put the output into a file). Ask for help from me or a consultant if you do not know how to do this. You don't want to keep typing in your TM programs each time you want to try running them!

Here is the output you should get from in.txt:

The TM:
Start state for the TM: s  // start state is s
        : STATE   S  STATE   HEAD
// Machine OO: Created by Wendy Myrvold.
// This machine halts in state h when there is an odd
// number of a's and an odd number of b's in the input
// and it hangs in state hN otherwise (due to no transitions defined).
Rule   1: s       #  ee      L  
Rule   2: ee      a  oe      L   // State ee means both a's and b's are even
Rule   3: ee      b  eo      L   // State ee means both a's and b's are even
Rule   4: eo      a  oo      L   // State eo means even a's and odd b's 
Rule   5: eo      b  ee      L   // State eo means even a's and odd b's 
Rule   6: oe      a  ee      L   // State oe means odd a's and even b's 
Rule   7: oe      b  oo      L   // State oe means odd a's and even b's 
Rule   8: oo      a  eo      L   // State oo means both a's and b's are odd
Rule   9: oo      b  oe      L   // State oo means both a's and b's are odd
// When we see the # at the RH end of the input we are done.
Rule  10: oo      #  h       #   
Rule  11: oe      #  hN      #   
Rule  12: eo      #  hN      #   
Rule  13: ee      #  hN      #   
// Try it on w not in L
Input 1 computation: w of length 9=[abbbaabaa]
Step 0  : (s,    #abbbaabaa[#])
Step 1  : (ee,   #abbbaaba[a])
Step 2  : (oe,   #abbbaab[a]a)
Step 3  : (ee,   #abbbaa[b]aa)
Step 4  : (eo,   #abbba[a]baa)
Step 5  : (oo,   #abbb[a]abaa)
Step 6  : (eo,   #abb[b]aabaa)
Step 7  : (ee,   #ab[b]baabaa)
Step 8  : (eo,   #a[b]bbaabaa)
Step 9  : (ee,   #[a]bbbaabaa)
Step 10 : (oe,   [#]abbbaabaa)
Step 11 : (hN,   [#]abbbaabaa)
No valid transition, TM hangs.
End computation 1
// Try it on w in L
Input 2 computation: w of length 6=[ababab]
Step 0  : (s,    #ababab[#])
Step 1  : (ee,   #ababa[b])
Step 2  : (eo,   #abab[a]b)
Step 3  : (oo,   #aba[b]ab)
Step 4  : (oe,   #ab[a]bab)
Step 5  : (ee,   #a[b]abab)
Step 6  : (eo,   #[a]babab)
Step 7  : (oo,   [#]ababab)
Step 8  : (h,    [#]ababab)
Halts because the state is final.
End computation 2

If you prefer to run it with the visual interface as demonstrated in class:

javac *.java
java JTM 

The TM is loaded using the New TM button to select the file which contains the TM description. The initial w will be the first one which you include after the $ (or empty string if the input is missing). Click on Input to type in alternate input strings. You have to click on the Run button to start the TM program running or can use the + button to just execute one step at a time. The slider controls how fast the TM runs.

Other Examples:

  1. The machine COPY which takes as input a string w over {a,b}* and makes a copy of w on the tape.
    The TM simulator output: COPY_OUT.
  2. The machine EVEN which takes as input a string w over {a,b}* and decides if the length of w is even or not.
    The TM simulator output: EVEN_OUT.
  3. The machine SHIFT. If you start this machine on an input of even length equal to uv where u and v are in {a,b}* and |u|= |v|, it halts with u#v with the tape head positioned on the leftmost symbol of u.
    The TM simulator output: SHIFT_OUT.
  4. The machine UV . If you start this machine with input u#v where u and v are in {a,b}* and |u|= |v|, it halts in state ww if u=v and state not_ww otherwise.
    The TM simulator output: UV_OUT.
  5. The machine WW combines the three machines EVEN, SHIFT and UV to create an algorithm for deciding if an input string is of the form ww where w is in {a,b}*.
    The TM simulator output: WW_OUT.

CSC 320 TM Simulator / maintained by Wendy Myrvold / wendym@cs.UVic.ca / revised July 6, 2017