-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmExec.py
45 lines (37 loc) · 1.03 KB
/
tmExec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Turing machine interpreter
"""
import sys
from collections import defaultdict
def check_tm(word):
curWord = []
for i in word:
curWord.append(i)
word = ['$'] + curWord + ['@']
print(word)
word = list(word)
tmDelta = defaultdict(tuple)
with open('TuringMachine.txt', 'r') as f:
for line in f:
if line != '\n':
line = line[:-1]
splitted = line.split(',')
tmDelta[(splitted[0], splitted[1])] = splitted[2:]
pos = 0
state = 'start'
tmp = word
tmp = tmp[:pos] + ['>'] + tmp[pos:]
print(' '.join(tmp) + ' ' + state)
while True:
nextState = tmDelta[(state, word[pos])]
if nextState == ():
break
state = nextState[0]
word[pos] = nextState[1]
pos = pos + 1 if nextState[2] == 'r' else pos - 1
tmp = word
tmp = tmp[:pos] + ['>'] + tmp[pos:]
print(' '.join(tmp) + ' ' + state)
return state
if __name__ == '__main__':
check_tm(sys.argv[1])