-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathENFAConverter.java ( EXPT2)
225 lines (175 loc) · 6.85 KB
/
ENFAConverter.java ( EXPT2)
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
use java ENFAConverter " your string ( note: here + means | ) " to run
import java.util.*;
public class ENFAConverter {
static class Edge {
int state;
Character transition;
public Edge(int state, Character transition) {
this.state = state;
this.transition = transition;
}
}
static class RegexToENFA {
static List<List<Edge>> enfa = new ArrayList<>();
static Deque<Integer> stack = new ArrayDeque<>();
static Character epsilon = '\u03b5';
static int initialState;
static int finalState;
private static void addState() {
List<Edge> adjList = new ArrayList<Edge>();
enfa.add(adjList);
}
private static void addTransition(int source, int destination, Character character) {
List<Edge> adjList = enfa.get(source);
Edge edge = new Edge(destination, character);
adjList.add(edge);
}
private static void basicMachine(Character character) {
for(int i = 0; i < 2; i++) {
addState();
}
addTransition(enfa.size() - 2, enfa.size() - 1, character);
stack.addFirst(enfa.size() - 1);
stack.addFirst(enfa.size() - 2);
}
private static void kleeneClosure() {
int head = stack.removeFirst();
int tail = stack.removeFirst();
for(int i = 0; i < 2; i++) {
addState();
}
addTransition(enfa.size() - 2, head, epsilon);
addTransition(enfa.size() - 2, enfa.size() - 1, epsilon);
addTransition(tail, head, epsilon);
addTransition(tail, enfa.size() - 1, epsilon);
stack.addFirst(enfa.size() - 1);
stack.addFirst(enfa.size() - 2);
}
private static void concatenation() {
int head2 = stack.removeFirst();
int tail2 = stack.removeFirst();
int head1 = stack.removeFirst();
int tail1 = stack.removeFirst();
addTransition(tail1, head2, epsilon);
stack.addFirst(tail2);
stack.addFirst(head1);
}
private static void alternation() {
int head2 = stack.removeFirst();
int tail2 = stack.removeFirst();
int head1 = stack.removeFirst();
int tail1 = stack.removeFirst();
for(int i = 0; i < 2; i++) {
addState();
}
addTransition(enfa.size() - 2, head1, epsilon);
addTransition(enfa.size() - 2, head2, epsilon);
addTransition(tail1, enfa.size() - 1, epsilon);
addTransition(tail2, enfa.size() - 1, epsilon);
stack.addFirst(enfa.size() - 1);
stack.addFirst(enfa.size() - 2);
}
private static void printAdjList() {
System.out.println("Adjacency List");
for(int i = 0; i < enfa.size(); i++) {
List<Edge> edgeList = enfa.get(i);
System.out.println(i);
for(int j = 0; j < edgeList.size(); j++) {
Edge edge = edgeList.get(j);
System.out.println(edge.transition + " -> " + edge.state);
}
}
}
public static void regexToENFA(String postfixRegex) {
for(int i = 0; i < postfixRegex.length(); i++) {
char c = postfixRegex.charAt(i);
switch(c) {
case '*':
kleeneClosure();
break;
case '.':
concatenation();
break;
case '|':
alternation();
break;
default:
basicMachine(c);
}
}
// Find initial and final states by popping the contents of the stack
if(!stack.isEmpty()) {
initialState = stack.removeFirst();
finalState = stack.removeFirst();
}
System.out.println("Initial State: " + initialState);
System.out.println("Final State: " + finalState);
printAdjList();
System.out.println();
}
// ... (rest of the RegexToENFA methods and logic)
}
static class Helpers {
public static String useDotForConcatenation(String regex) {
// ... (useDotForConcatenation method)
for(int i = 0; i < regex.length() - 1; i++) {
char cur = regex.charAt(i);
if(cur != '|' && cur != '(') { // Don't add '.' after '|' and '('
char next = regex.charAt(i + 1);
if(next != '|' && next != '*' && next != ')') { // Don't add '.' if '|', '*', ')' are the next characters
regex = new StringBuilder(regex).insert(i + 1, ".").toString();
i++;
}
}
}
return regex;
}
private static int precedence(char operator) {
switch(operator) {
case '.':
return 2;
case '|':
return 1;
default:
return -1;
}
}
public static String infixToPostfix(String infixRegex) {
Deque<Character> stack = new ArrayDeque<Character>();
String postfixRegex = "";
for(int i = 0; i < infixRegex.length(); i++) {
char c = infixRegex.charAt(i);
switch(c) {
case '.':
case '|':
while(!stack.isEmpty() && precedence(c) <= precedence(stack.peek())) {
postfixRegex += stack.removeFirst();
}
stack.addFirst(c);
break;
case '(':
stack.addFirst(c);
break;
case ')':
while(!stack.isEmpty() && stack.peek() != '(') {
postfixRegex += stack.removeFirst();
}
stack.removeFirst();
break;
default:
postfixRegex += c;
}
}
while(!stack.isEmpty()) {
postfixRegex += stack.removeFirst();
}
return postfixRegex;
}
}
public static void main(String args[]) {
String regex = args[0];
String infixRegex = Helpers.useDotForConcatenation(regex);
String postfixRegex = Helpers.infixToPostfix(infixRegex);
RegexToENFA.regexToENFA(postfixRegex);
}
}