-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegExToNfa.java
More file actions
282 lines (217 loc) · 7.26 KB
/
RegExToNfa.java
File metadata and controls
282 lines (217 loc) · 7.26 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
package csen1002.main.task1;
import java.util.*;
/**
* Write your info here
*
* @name Tarteel Elattar
* @id 49-2019
* @labNumber 17
*/
class Transition {
int startState;
char transit;
int finalState;
public Transition(int s, char t, int f) {
this.startState = s;
this.transit = t;
this.finalState = f;
}
}
//an operator is a set of transitions, has general start and final states. assume it is an NFA on its own
class Operator {
int starting;
int accepted;
ArrayList<Transition> transitions;
public Operator(int s, int f) {
this.starting = s;
this.transitions = new ArrayList<Transition>();
this.accepted = f;
}
}
public class RegExToNfa {
/**
* Constructs an NFA corresponding to a regular expression based on Thompson's
* construction
*
* @param input The alphabet and the regular expression in postfix notation for
* which the NFA is to be constructed
*/
// checks if its input is an alphabet
public boolean isAlphabet(String alpha, char input) {
alpha += "e";
for (int i = 0; i < alpha.length(); i++) {
if (alpha.charAt(i) == input)
return true;
}
return false;
}
String alphabet;
Operator finalResult;
ArrayList<Integer> acceptedStates = new ArrayList<Integer>();
public RegExToNfa(String input) {
// stack of current operators
Stack<Operator> s = new Stack<Operator>();
// a counter for each number of state
int counter = 0;
int start = 0;
int accept = 0;
String[] splitted = input.split("#");
String regex = splitted[1];
alphabet = splitted[0];
for (int i = 0; i < regex.length(); i++) {
Transition t;
Operator n;
// if it's an alphabet, create an NFA for it.
if (isAlphabet(alphabet, regex.charAt(i))) {
t = new Transition(counter++, regex.charAt(i), counter);
n = new Operator(counter - 1, counter);
n.transitions.add(t);
counter++;
s.push(n);
}
else if (regex.charAt(i) == '|') {
// union operation
// pop the latest two operators out of the current stack
Operator operator2 = s.pop();
Operator operator1 = s.pop();
// i will modify operator2 then push it at the end
operator2.transitions.addAll(operator1.transitions);
// a new start state that transits into the whole thingy
t = new Transition(counter, 'e', operator1.starting);
operator2.transitions.add(t);
t = new Transition(counter, 'e', operator2.starting);
operator2.transitions.add(t);
start = counter;
counter++;
// a new end state that the states go into
t = new Transition(operator1.accepted, 'e', counter);
operator2.transitions.add(t);
t = new Transition(operator2.accepted, 'e', counter);
operator2.transitions.add(t);
accept = counter;
operator2.starting = start;
operator2.accepted = accept;
s.push(operator2);
counter++;
}
else if (regex.charAt(i) == '*') {
// kleene closure -- pop the latest operator
Operator current = s.pop();
// add a transition from the final state to the initial state
current.transitions.add(new Transition(current.accepted, 'e', current.starting));
// a new state transit into the current initial state
current.transitions.add(new Transition(counter, 'e', current.starting));
current.starting = counter;
counter++;
// the end state transit into a new state
current.transitions.add(new Transition(current.accepted, 'e', counter));
current.accepted = counter;
// the new start and end states should transit to each other
current.transitions.add(new Transition(current.starting, 'e', current.accepted));
counter++;
s.push(current);
}
else if (regex.charAt(i) == '.') {
// concatenation
// pop the latest two operators out of the current stack
Operator operator2 = s.pop();
Operator operator1 = s.pop();
// remove operator 1's start state
// any transitions going out of operator 1's start state should go out of
// operator 2's end state
for (Transition tr : operator2.transitions) {
if (tr.startState == operator2.starting)
tr.startState = operator1.accepted;
}
operator2.transitions.addAll(operator1.transitions);
operator2.starting = operator1.starting;
s.push(operator2);
}
}
finalResult = s.pop();
if (!s.isEmpty())
System.out.println("The stack should be empty at this point...");
}
/**
* @return
* @return Returns a formatted string representation of the NFA. The string
* representation follows the one in the task description
*/
@Override
public String toString() {
// integer array of the accepted states. should be sorted.
String result = getAcceptedStates(finalResult);
result += "#" + alphabet + "#";
String transitions = getSortedTransitions(finalResult);
result += transitions + "#" + finalResult.starting + "#" + finalResult.accepted;
return result;
}
public String getAcceptedStates(Operator a) {
//returns a string of the sorted, accepted states
ArrayList<Integer> states = new ArrayList<Integer>();
//insert all the accepted states
for (Transition t : a.transitions) {
if (!states.contains(t.startState))
states.add(t.startState);
if (!states.contains(t.finalState))
states.add(t.finalState);
}
//bubble sort like in cs3
int temp;
for (int i=0; i<states.size(); i++) {
for (int j=i+1; j<states.size(); j++) {
temp = 0;
if (states.get(i) > states.get(j)) {
//swap
temp = states.get(i);
states.set(i, states.get(j));
states.set(j, temp);
}
}
}
String result = "0;";
for (int i=1; i<states.size()-1; i++)
result+= states.get(i) + ";";
return result+states.get(states.size()-1);
}
public String getSortedTransitions(Operator a) {
//returns the transitions, sorted as required, in a string
ArrayList<Transition> tr = a.transitions;
Transition temp;
//bubble sort
for (int i=0; i<tr.size(); i++) {
for (int j=i+1; j<tr.size(); j++) {
temp = null;
//sort by the start state
if (tr.get(i).startState > tr.get(j).startState) {
//swap
temp = tr.get(i);
tr.set(i, tr.get(j));
tr.set(j, temp);
}
//if the start states are equal, sort by the alphabet
else if (tr.get(i).startState == tr.get(j).startState) {
if (tr.get(i).transit > tr.get(j).transit) {
//swap
temp = tr.get(i);
tr.set(i, tr.get(j));
tr.set(j, temp);
}
// if the transits are equal, sort by the destination
else if (tr.get(i).transit == tr.get(j).transit) {
if (tr.get(i).finalState > tr.get(j).finalState) {
//swap
temp = tr.get(i);
tr.set(i, tr.get(j));
tr.set(j, temp);
}
}
}
}
}
String result = tr.get(0).startState + "," + tr.get(0).transit + "," + tr.get(0).finalState+ ";";
for (int i=1; i<tr.size()-1; i++)
result+= tr.get(i).startState + "," + tr.get(i).transit + "," + tr.get(i).finalState+ ";";
return result+tr.get(tr.size()-1).startState + "," + tr.get(tr.size()-1).transit + "," + tr.get(tr.size()-1).finalState;
}
}