A Turing machine simulator implemented in Python to recognize a language over the alphabet {a, b, c}.
The machine accepts strings where the number of a symbols is exactly three times the combined number of b and c symbols:
|w|a = 3 × (|w|b + |w|c)
The project models the transition function explicitly and provides a step-by-step visualization of the tape, head position, and current state during execution.
Accepted strings satisfy the relationship above.
Examples:
aaab
aaaaaabb
Rejected examples include:
aab
bbb
For each b or c encountered, the machine marks that symbol and searches the tape for three unprocessed a symbols.
Temporary symbols are used to track processed input:
xmarks processedasymbolsymarks processedbandcsymbols
Once all b and c symbols have been processed, the machine verifies that no unprocessed input symbols remain.
The implementation contains 11 states:
q1— initial scanning stateq2–q8— tape traversal and symbol-marking statesq9— final verificationq10— accept stateq11— reject state
The transition function is represented directly in Python:
def delta(input_tuple: TuringInput) -> TuringOutput:
...Python's match-case syntax is used to express transitions based on the current state and tape symbol.
The simulator uses:
Enumfor machine states and tape symbolsIntEnumfor head movementsNamedTuplefor transition inputs and outputs- Python type hints
- Dynamic tape expansion
- Explicit accept and reject states
The simulated tape automatically expands when the head moves beyond its current boundaries.
For each transition, the program displays:
- Current state
- Current tape contents
- Read/write head position
A maximum of 1,000 transitions is enforced to prevent a non-terminating execution from running indefinitely.
Python 3.10 or newer is recommended because the implementation uses structural pattern matching.
Clone the repository:
git clone https://github.com/saw-cdt/turing-machine-simulator.git
cd turing-machine-simulatorRun:
python main.pyEnter a string containing only a, b, and c. Submit an empty input to exit.
Input: aaab
State: q1
...
Result: accepted
This project provides a direct implementation of several theoretical computer science concepts:
- Turing machines
- Formal languages
- Transition functions
- State machines
- Tape-based computation
- Symbol marking
- Language recognition
The implementation intentionally models the machine at the transition level rather than replacing the recognition process with a direct arithmetic check.