A Python implementation of a Turing machine computing model with infinite tape.
The easiest way is probably using the machine_def format defined below:
- STATE [name]: defines a new state
- RULE [read] := [write] [shift] [next]: defines a rule on the state
- Note: RULE directives must follow a STATE directive;
a file is invalid if there is a RULE directive with no STATE directive above it.
- read: character to read off the tape
- write: character to write onto the tape
- shift: L, R, or N to shift left, shift right, or no-op.
- next: state to move to
- ACCEPT [name]: sets the accept state
- name: name of accept state
- REJECT [name]: sets the reject state
- name: name of reject state
- START [name]: sets the starting state
- name: name of the start state
- LOAD [string]: loads the give string onto the tape
- INFO [ON|OFF]: turns on or off printing out machine information each cycle.
- PASS: does nothing; a blank line is the same as a PASS directive
\0 can be used to represent a blank value. Spaces are not a valid character.
To run the machine: python -m turing [file] where [file] is the path of your machine_def file.
An API is provided to create a machine and define states.
- Import the TuringMachine and Tape classes from turing. Optionally, you can import the State class.
- Create a new machine by defining an instance of TuringMachine.
- States can be created by calling the new_state method on your machine (which will automatically name them q0, q1, ...).
- States can be manually created by defining an instance of the State method, then adding it to the machine instance
by calling the add_state method and passing the state instance as an argument.
- Rules can be added by calling the add_rule method on a state instance and passing the following arguments:
- read: character to read off the tape
- write: character to write onto the tape
- direction: Tape.LEFT or Tape.RIGHT to shift left or right on the tape
- next_state: state to move to
- Use the set_accept, set_reject, and set_state methods to set the accept, reject, and start states.
- Use the load_tape method to load a string onto the tape. The NUL character represents a blank.
- Call the run method on the machine to run.
Reference the example in palindrome.py.