-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFAtoRegex.java
More file actions
303 lines (261 loc) · 11.5 KB
/
NFAtoRegex.java
File metadata and controls
303 lines (261 loc) · 11.5 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
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package nfatoregex;
/**
*
* @author ChristinaLyu
*/
public class NFAtoRegex {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
String filename = args[0];
Scanner reader = new Scanner(new InputStreamReader(System.in));
BufferedReader file = new BufferedReader(new FileReader(filename));
ArrayList<String> arrows = new ArrayList<String> ();
ArrayList<String> states = new ArrayList<String> ();
String line;
ArrayList<String> s = new ArrayList<String> ();
ArrayList<String> reg = new ArrayList<String> ();
ArrayList<String> e = new ArrayList<String> ();
String star = "";
String endd = "";
String regg = "";
String change = "";
String rege = "";
String changee = "";
String changes = "";
int n = 1000000000;
int j = 1000000000;
int l = 1000000000;
try {
//The first section
while ((line = file.readLine()) != null){
arrows.add(line);
}
String first = arrows.get(0);
String[] two = first.split(" ");
String startState = two[0];
String acceptState = two[1];
System.out.println("First get the input start state and accepting state: "
+ startState + " and " + acceptState);
arrows.remove(0);
System.out.println("This is the original NFA");
for (String stat: arrows){
String[] parts = stat.split(" ");
String start = parts[0];
String regex = parts[1];
String end = parts[2];
s.add(start);
reg.add(regex);
e.add(end);
star = star + start + " ";
endd = endd + end;
System.out.println(stat);
if (!states.contains(end)){
states.add(end);
}
if (!states.contains(start)){
states.add(start);
}
}
System.out.println("The start states are " + s);
System.out.println("The end states are " + e);
//The second section
System.out.println("Start to eliminate the states one by one.");
for (String state: states){
int aS = arrows.size();
if (!state.equals(startState) && !state.equals(acceptState)){
String regs = "";
System.out.println("working on State " + state);
for (int i = 0; i < aS; i ++){
String stat = arrows.get(i);
String[] strings = stat.split(" ");
String starting = strings[0];
String ending = strings[2];
if (starting.equals(state)
&& ending.equals(state)){
regs = reg.get(i);
regs = "(" + regs + ")" + "*";
l = i;
}
}
if (l != 1000000000){
System.out.println("The regex for arc going from State " + state + " to itself changes from "
+ reg.get(l) + " to " + regs);
arrows.remove(l);
//states.remove(n);
s.remove(l);
reg.remove(l);
e.remove(l);
star = star.substring(0, l) + star.substring(l + 1);
endd = endd.substring(0, l) + endd.substring(l + 1);
}
ArrayList<Integer> mint = new ArrayList<Integer> ();
ArrayList<Integer> hint = new ArrayList<Integer> ();
ArrayList<String> newRegL = new ArrayList<String> ();
String newString;
for (int m = 0; m < aS; m++){
if (e.get(m).equals(state)){
changee = reg.get(m);
String startst = s.get(m);
for (int h = 0; h < aS; h ++){
if (s.get(h).equals(state)){
changes = reg.get(h);
String endst = e.get(h);
String newReg = changee + regs + changes;
System.out.println("The new arc from State " + startst +
" to State " + " has regex of " + newReg);
newString = startst + " " + newReg + " " + endst;
arrows.add(newString);
reg.add(newReg);
s.add(startst);
e.add(endst);
}
}
}
}
while (s.contains(state)){
int u = s.indexOf(state);
arrows.remove(u);
reg.remove(u);
e.remove(u);
s.remove(u);
}
while (e.contains(state)){
int u = e.indexOf(state);
arrows.remove(u);
reg.remove(u);
e.remove(u);
s.remove(u);
}
System.out.println("The new NFA is: ");
for (String arrow: arrows){
System.out.println(arrow);
}
}
}
System.out.println("After removing all other states, start to remove the " +
"recurring arcs from the start and accept state.");
for (int i = 0; i < arrows.size(); i ++){
String stat = arrows.get(i);
String[] strings = stat.split(" ");
String starting = strings[0];
String ending = strings[2];
if (starting.equals(startState)
&& ending.equals(startState)){
regg = reg.get(i);
regg = "(" + regg + ")" + "*";
j = i;
}
}
//System.out.println("second for loop");
if (j != 1000000000){
arrows.remove(j);
//states.remove(j);
s.remove(j);
reg.remove(j);
e.remove(j);
for (String arrow: arrows){
System.out.println(arrow);
}
star = star.substring(0, j) + star.substring(j + 1);
endd = endd.substring(0, j) + endd.substring(j + 1);
}
System.out.println("for the start state");
for (int m = 0; m < s.size(); m++){
change = reg.get(m);
if (s.get(m).equals(startState)){
System.out.println("change = " + change + "s = " + s);
String en = e.get(m);
if (!regg.equals("")){
reg.remove(m);
change = regg + change;
//System.out.println("change = " + change);
reg.add(m, change);
arrows.set(m, startState + " " + change + " " + en);
System.out.println("The revised arc connecting State "
+ startState + " and State " + en + " has regex of "
+ change);
}
}
}
for (int i = 0; i < arrows.size(); i ++){
String stat = arrows.get(i);
String[] strings = stat.split(" ");
//System.out.println("split state: " + strings[1]);
String starting = strings[0];
String ending = strings[2];
if (starting.equals(acceptState)
&& ending.equals(acceptState)){
rege = reg.get(i);
rege = "(" + rege + ")" + "*";
n = i;
}
}
if (n != 1000000000){
arrows.remove(n);
//states.remove(n);
s.remove(n);
reg.remove(n);
e.remove(n);
star = star.substring(0, n) + star.substring(n + 1);
endd = endd.substring(0, n) + endd.substring(n + 1);
}
System.out.println("for accepting state");
for (int m = 0; m < s.size(); m++){
if (e.get(m).equals(acceptState)){
changee = reg.get(m);
String st = s.get(m);
if (!rege.equals("")){
reg.remove(m);
changee = changee + rege;
reg.add(m, changee);
arrows.set(m, st + " " + changee + " " + acceptState);
System.out.println("The revised arc connecting State "
+ st + " and State " + acceptState + " has regex of "
+ change);
}
}
}
System.out.println("The final NFA is: ");
for (String arrow: arrows){
System.out.println(arrow);
}
String regggex = "";
String reggex = "";
for (int o = 0; o < arrows.size(); o ++){
if (s.get(o).equals(startState) && e.get(o).equals(acceptState)){
regggex = regggex + reg.get(o) + "U";
}
if (s.get(o).equals(acceptState) && e.get(o).equals(startState)){
reggex = reggex + reg.get(o) + "U";
}
}
if (!regggex.equals("")){
regggex = regggex.substring(0, regggex.length()-1);
}
if (!reggex.equals("")){
reggex = reggex.substring(0, reggex.length()-1);
regggex = regggex + "((" + reggex +")(" + regggex + "))*";
}
System.out.println("regex = " + regggex);
} catch (IOException ex) {
Logger.getLogger(NFAtoRegex.class.getName()).log(Level.SEVERE, null, ex);
}
}
}