A command-line Turing Machine simulator implemented in Java. This project allows users to define a Turing Machine's transition function, provide an input string on the tape, and watch the step-by-step execution as the machine processes the input.
This implementation was created to better understand the fundamental principles of the theory of computation.
- Configurable Machine: Set the tape size and the number of states for the TM via command-line arguments.
- Interactive Transition Definition: A guided, runtime prompt to define the states and transition functions for your machine.
- Step-by-Step Visualization: The program prints the state of the tape after every move, with the current head position highlighted for clarity.
- Robust Input Handling: Gracefully handles invalid transition formats and provides clear feedback.
javac *.javaThe program takes two command-line arguments: the tape size and the number of states.
java TuringMachine <tape_size> <number_of_states>For example, to run with a tape of size 80 and 5 states:
java TuringMachine 80 5The transitions are defined using a compact TCMN format:
- T: Tape Symbol read by the head. Use
_for the blank symbol. - C: Character to Write on the tape.
- M: Move Direction for the head (
Lfor left,Rfor right). - N: Next State number (e.g.,
0for stateq0).
After defining all transitions for a state, type done to move to the next state.
This machine checks if a string consists of one or more a's followed by an equal number of b's. It works by matching each a with a corresponding b in a back-and-forth cycle. For this example, you need 5 states.
-
State
q0(Start of cycle, find nexta):Is q0 a Final state? (yes/no):noq0>AXR1(Ona, writeX, move Right toq1)q0>YYR3(OnY, writeY, move Right toq3for final check)q0>done
-
State
q1(Scan right to find ab):Is q1 a Final state? (yes/no):noq1>AAR1(Skipa's)q1>YYR1(SkipY's)q1>BYL2(Onb, writeY, move Left toq2)q1>done
-
State
q2(Scan left to return to start marker):Is q2 a Final state? (yes/no):noq2>AAL2(Skipa's)q2>YYL2(SkipY's)q2>XXR0(OnX, writeX, move Right back toq0)q2>done
-
State
q3(Verification scan, ensure nob's are left):Is q3 a Final state? (yes/no):noq3>YYR3(SkipY's)q3>__R4(On_blank, write_, move to final stateq4)q3>done
-
State
q4(Final/Accept state):Is q4 a Final state? (yes/no):yesq4>done
Now, the machine is defined. When prompted, you can test it:
-
Enter the initial tape string (or 'exit' to quit):aaabbb- The machine will run, and the final tape will show
XXXYYY. - Result: String ACCEPTED
- The machine will run, and the final tape will show
-
Enter the initial tape string (or 'exit' to quit):aaab- The machine will halt because in state
q1it will find a blank instead of ab. - Result: String REJECTED
- The machine will halt because in state