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 statename: name of the state
RULE [read] := [write] [shift] [next]: defines a rule on the state- Note:
RULEdirectives must follow aSTATEdirective; a file is invalid if there is aRULEdirective with noSTATEdirective above it. read: character to read off the tapewrite: character to write onto the tapeshift:L,R, orNto shift left, shift right, or no-op.next: state to move to
- Note:
ACCEPT [name]: sets the accept statename: name of accept state
REJECT [name]: sets the reject statename: name of reject state
START [name]: sets the starting statename: name of the start state
LOAD [string]: loads the give string onto the tapestring: string to load
INFO [ON|OFF]: turns on or off printing out machine information each cycle.PASS: does nothing; a blank line is the same as aPASSdirective
\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
TuringMachineandTapeclasses fromturing. Optionally, you can import theStateclass. - Create a new machine by defining an instance of
TuringMachine. - States can be created by calling the
new_statemethod on your machine (which will automatically name themq0,q1, ...). - States can be manually created by defining an instance of the
Statemethod, then adding it to the machine instance by calling theadd_statemethod and passing the state instance as an argument. - Rules can be added by calling the
add_rulemethod on a state instance and passing the following arguments:read: character to read off the tapewrite: character to write onto the tapedirection:Tape.LEFTorTape.RIGHTto shift left or right on the tapenext_state: state to move to
- Use the
set_accept,set_reject, andset_statemethods to set the accept, reject, and start states. - Use the
load_tapemethod to load a string onto the tape. TheNULcharacter represents a blank. - Call the
runmethod on the machine to run.
Reference the example in palindrome.py.