-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTM.java
More file actions
152 lines (117 loc) · 2.94 KB
/
Copy pathTM.java
File metadata and controls
152 lines (117 loc) · 2.94 KB
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
import java.io.*;
import java.util.*;
/**
*
* @author Anjali Tewari axt6472
*
*/
public class TM {
public static void main( String[] args ) {
char[] tapeCharArr;
String l1 = null;
try {
BufferedReader r1 = new BufferedReader( new FileReader( args[0] ) );
l1 = r1.readLine().trim();
r1.close();
}
catch( Exception e ) {
e.printStackTrace();
}
tapeCharArr = l1.toCharArray();
String[][] tm = readTM( args[1] );
runTM( tapeCharArr, tm );
}
public static String[][] readTM( String file ) {
BufferedReader br = null;
ArrayList<String> al = new ArrayList<String>();
String line = null;
String[] temp = null;
String[][] tmCode = null;
try {
br = new BufferedReader( new FileReader( file ) );
while( (line = br.readLine() ) != null ) {
temp = line.split("\\s");
for(int a = 0; a < 5; a++) {
al.add( temp[a] );
}
line = null;
}
br.close();
tmCode = new String[ al.size()/5 ][ 5 ];
int c = 0;
int x = 0;
while( c < al.size()/5 ) {
for( int i = 0; i < 5; i++ ) {
//System.out.println(c + "," + i);
tmCode[c][i] = al.get( x );
x++;
}
c++;
}
}
catch( Exception e ) {
e.printStackTrace();
}
return tmCode;
}
public static void runTM( char[] tape, String[][] tm ) {
int head = 0;
char read;
String currState = tm[0][0];
String replace = null;
String move = null;
String nextState = null;
Boolean halt = false;
Boolean match = false;
System.out.println( "PresentState\tTapeHead\tTapeStatus" );
int count = 0;
try {
while( !halt ) {
read = tape[ head ];
for( int a = 0; a < tm.length; a++ ) {
//System.out.println(tm[a][a]);
if( currState.equals( tm[a][0] ) && read == tm[a][1].charAt(0) ) {
match = true;
move = tm[a][2];
replace = tm[a][3];
read = replace.charAt(0);
tape[ head ] = replace.charAt(0);
nextState = tm[a][4];
System.out.println( currState + "\t\t" + read + "\t\t" + nextState );
}
}
if( !match ) {
System.out.println( "No match for state and input : " + currState + " " + read );
System.out.print( "String Rejected" );
break;
}
match = false;
if( move.equals("R") ) {
head++;
}
else if( move.equals("L") ) {
head--;
}
if( nextState.equals( "accept") || Character.toString(tape[ head ]).equals("$")) {
halt = true;
System.out.print( "String Accepted" );
break;
}
else {
currState = nextState;
nextState = null;
move = null;
}
count++;
}
}
catch( ArrayIndexOutOfBoundsException ae ) {
System.out.print("String rejected");
System.exit(0);
}
catch ( Exception e ) {
System.out.print(e.getMessage());
System.exit(0);
}
}
}