-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingmachine.py
183 lines (148 loc) · 4.06 KB
/
turingmachine.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
Python3 Turing Machine
Copyright (c) 2018 James Warp
github.com/jnwarp/turing
"""
class TuringMachine:
"""
Turing Machine accepts a tape (string) and transition function.
INPUT:
tape = 'aaa'
transition_function = {
'q0': {
'a': ('q0', 'a', 'R'),
'b': ('q1', 'a', 'R'),
'*': ('qrej', '*', 'L')
},
'q1': {
'a': ('q1', 'b', 'R'),
'b': ('qacc', 'b', 'L'),
'*': ('qrej', '*', 'L')
}
}
SPECIAL STATES:
q0 = initial state
qacc = Accepts the input
qrej = Rejects the input
TRANSITION FUNCTION:
{
state: {
letter: (new_state, write_letter, L/R)
}
}
"""
def __init__(self, tape, transition_function):
# convert tape to list for iteration purposes
self.tape = list(tape)
# save inputs
self.funct = transition_function
self.state = 'q0'
self.q = -1
# print out the initial state
print('state\ttape')
print('='*12)
self.printState()
# start q at zero
self.q = 0
def printState(self, nextpos = 0):
out = ''
for i, q in enumerate(self.tape):
if i == self.q:
# optionally print direction
if nextpos > 0:
out += ' ' + q + '>'
elif nextpos < 0:
out += '<' + q + ' '
else:
out += '[' + q + ']'
else:
out += ' ' + q + ' '
print(self.state + '\t' + out)
def step(self):
# stop if state reaches acc / rej
if self.state == 'qacc':
print('\nState accepted!')
return self.state
elif self.state == 'qrej':
print('\nState rejected!')
return self.state
# select the funct to use
funct = self.funct[self.state][self.tape[self.q]]
# replace the tape element
self.state = funct[0]
self.tape[self.q] = funct[1]
# print state before head is moved
self.printState()
"""
#optionally print direction
if funct[2] == 'R':
self.printState(1)
else:
self.printState(-1)
"""
# move the head
if (funct[2] == 'R'):
self.q += 1
# append element if q goes beyond string
if len(self.tape) <= self.q:
self.tape += '*'
else:
self.q -= 1
# throw error if element tries to go too far
if self.q < 0:
raise ValueError('Out of tape, L\n' + self.tape + ' ' + self.funct)
def stepAll(self, limit=100):
cnt = 0
flag = True
while flag and cnt < limit:
# step one, stop if result
result = t.step()
if (result != None):
flag = False
# prevent inf loop
cnt += 1
if flag:
print('State limit reached!')
return result
def stepPause(self):
flag = True
while flag:
# pause for user to press enter
input('')
# step one, stop if result
result = t.step()
if (result != None):
flag = False
return result
"""
# infinite problem
transition_function = {
'q0': {
'a': ('qr', '*', 'R'),
'*': ('qr', '*', 'R')
},
'qr': {
'a': ('qr', 'a', 'R'),
'*': ('ql', 'a', 'L')
},
'ql': {
'a': ('ql', 'a', 'L'),
'*': ('qr', '*', 'R')
}
}
"""
transition_function = {
'q0': {
'a': ('q0', 'a', 'R'),
'b': ('q1', 'a', 'R'),
'*': ('qrej', '*', 'L')
},
'q1': {
'a': ('q1', 'b', 'R'),
'b': ('qacc', 'b', 'L'),
'*': ('qrej', '*', 'L')
}
}
tape = 'aba*'
t = TuringMachine(tape, transition_function)
t.stepAll()