-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniversalTM.java
324 lines (256 loc) · 9.39 KB
/
UniversalTM.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;
class Transition {
int state;
byte output;
Direction direction;
public Transition() {
// Default
setTransition(State.REJECT, (byte)0, Direction.LEFT);
}
public void setTransition(int state, byte output, Direction direction) {
this.state = state;
this.output = output;
this.direction = direction;
}
}
/**
* State class contains an array indicating for every input character,
* the character to be written, the direction the tape head is to
* move, and the next state to go to. By default, every transition is
* to the REJECT state.
*/
class State {
static final int ACCEPT = -2;
static final int REJECT = -1;
Transition[] transitions = new Transition[256];
public State() {
for (int i=0; i<256; i++) {
this.transitions[i] = new Transition();
}
}
}
enum Direction {
LEFT,
RIGHT
}
class TuringMachine {
private State[] states;
// An index into states array
private int currentState = 0;
// Tape
private byte[] tape;
// The position of the head on the tape
private int headPosition;
// Number of steps in the computation
public int steps = 0;
// Print configurations while stepping through
public boolean traceEnabled;
// Initialize the states array
public TuringMachine(int numberOfStates) {
this.states = new State[numberOfStates];
for (int i = 0; i < numberOfStates; i++) {
this.states[i] = new State();
}
}
public void modifyState(int stateNumber, byte inputSymbol, int targetState, byte outputSymbol, Direction direction) {
// Set transition
if (stateNumber < states.length) {
states[stateNumber].transitions[inputSymbol].setTransition(targetState, outputSymbol, direction);
}
}
public void initializeTape(byte[] inputString) {
// Copy the input to a new array. Add 1 to the length to make
// sure there is at least one cell in case the input is empty.
tape = Arrays.copyOf(inputString, inputString.length*2+1);
}
public void printConfiguration() {
System.out.printf("%5d | ", steps);
for (int i=0; i<tape.length; i++) {
if (i == headPosition) {
System.out.print("[" + currentState + "]");
}
if (tape[i] == 0) {
System.out.print(" ");
} else {
System.out.print(" " + (char)tape[i] + " ");
}
}
System.out.println();
}
public void stepComputation() {
steps += 1;
// Read transition function
Transition thisTransition = states[currentState].transitions[tape[headPosition]];
// Write to tape
tape[headPosition] = thisTransition.output;
// Update state
currentState = thisTransition.state;
// Move tape head
moveHead(thisTransition.direction);
}
public int run(int stepLimit) {
if (traceEnabled) {
System.out.println();
System.out.printf("%5s | %s%n", "Step", "Configuration");
System.out.println("------+-----------------------------------------------------");
printConfiguration();
}
for (int i = 0; i < stepLimit; i++) {
stepComputation();
if (traceEnabled) {
printConfiguration();
}
if (currentState < 0) {
break;
}
}
if (traceEnabled) {
System.out.println();
}
return currentState;
}
private void moveHead(Direction direction) {
switch (direction) {
case RIGHT:
headPosition++;
// If we have reached the end of the array, double it to
// create the illusion of an infinite tape.
if (headPosition == tape.length) {
tape = Arrays.copyOf(tape, tape.length * 2);
}
break;
case LEFT:
if (headPosition > 0) {
headPosition--;
}
break;
}
}
static TuringMachine createTMFromFile(String filename) throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename), "UTF-8");
// First line contains the number of states
int numberOfStates = Integer.parseInt(sc.nextLine());
// Number of lines read so far
int lineNumber = 1;
// Create Turing machine
TuringMachine tm = new TuringMachine(numberOfStates);
// Rest of file contains the transition function
while (sc.hasNextLine()) {
// Trim and split string into array with whitespace as delimiter
String[] fields = sc.nextLine().trim().split(" +");
lineNumber += 1;
// Skip empty lines
if (fields.length == 0 || fields[0].compareTo("") == 0) {
continue;
}
// Skip if line is a comment (begins with three dashes)
if (fields[0].length() >= 3 && fields[0].substring(0, 3).compareTo("---") == 0) {
continue;
}
// Error checking
if (fields.length != 5) {
System.err.println("Error: Line " + lineNumber + ": Wrong number of fields");
return null;
}
// Error checking
if (fields[1].length() != 1 || fields[3].length() != 1 || fields[4].length() != 1) {
System.err.println("Error: Line " + lineNumber + ": Field too long");
return null;
}
int currentState;
int nextState;
try {
currentState = Integer.parseInt(fields[0]);
nextState = Integer.parseInt(fields[2]);
} catch (NumberFormatException e) {
System.err.println("Error: Line " + lineNumber + ": Failed to parse number");
return null;
}
char inputSymbol = fields[1].charAt(0);
char outputSymbol = fields[3].charAt(0);
// Parse direction field
Direction direction;
switch (fields[4]) {
case "L":
direction = Direction.LEFT;
break;
case "R":
direction = Direction.RIGHT;
break;
default:
System.err.println("Error: Line " + lineNumber + ": Failed to parse direction");
return null;
}
if (inputSymbol == '_') {
inputSymbol = 0;
}
if (outputSymbol == '_') {
outputSymbol = 0;
}
tm.modifyState(currentState, (byte)inputSymbol, nextState, (byte)outputSymbol, direction);
}
return tm;
}
}
public class UniversalTM {
static final int lineWidth = 60;
public static void printUsage() {
System.out.println("Usage: java UniversalTM [--trace] <filename> <inputString> <maxSteps>");
System.out.println("\nOptional:");
System.out.println(" --silent Do not print trace of the computation");
System.out.println("\nRequired:");
System.out.println(" <filename> File containing TM encoding");
System.out.println(" <inputString> Input to tape");
System.out.println(" <maxSteps> Max number of steps");
System.out.println("\nNOTE: Use underscore '_' as blank when encoding a TM.");
}
static void printMessage(String message) {
String pre = "--- ";
String post = " ";
for (int i=pre.length() + message.length() + post.length(); i<lineWidth; i++) {
post += "-";
}
System.out.println(pre + message + post);
}
public static void main(String[] args) throws FileNotFoundException {
if (args.length != 3 && (args.length != 4 || args[0].compareTo("--silent") != 0)) {
printUsage();
System.exit(1);
}
boolean traceEnabled = true;
int startIndex = 0;
if (args[0].compareTo("--silent") == 0) {
if (args.length != 4) {
printUsage();
System.exit(1);
}
traceEnabled = false;
startIndex = 1;
}
String filename = args[startIndex++];
String inputString = args[startIndex++];
int maxSteps = Integer.parseInt(args[startIndex++]);
TuringMachine tm = TuringMachine.createTMFromFile(filename);
if (traceEnabled) {
tm.traceEnabled = true;
}
tm.initializeTape(inputString.getBytes(StandardCharsets.UTF_8));
printMessage("Started on input \"" + inputString + "\"");
int finalState = tm.run(maxSteps);
switch (finalState) {
case State.ACCEPT:
printMessage("Accepted input \"" + inputString + "\" in " + tm.steps + (tm.steps == 1 ? " step" : " steps"));
break;
case State.REJECT:
printMessage("Rejected input \"" + inputString + "\" in " + tm.steps + (tm.steps == 1 ? " step" : " steps"));
break;
default:
printMessage("Did not finish within " + maxSteps + " steps. Computation aborted");
break;
}
}
}