-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpression.java
More file actions
174 lines (156 loc) · 5.57 KB
/
RegularExpression.java
File metadata and controls
174 lines (156 loc) · 5.57 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
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
class Or extends RegularExpression {
RegularExpression left;
RegularExpression right;
public Or(RegularExpression l, RegularExpression r) {
left = l; right = r;
}
@Override boolean computeEmpty() { return isEmpty = left.computeEmpty() | right.computeEmpty(); }
@Override HashSet<Leaf> computeFirst() {
first.addAll(left.computeFirst());
first.addAll(right.computeFirst());
return first;
}
@Override HashSet<Leaf> computeLast() {
last.addAll(left.computeLast());
last.addAll(right.computeLast());
return last;
}
@Override void computeNext(HashSet<Leaf> n) {
next = n;
left.computeNext(next);
right.computeNext(next);
}
}
class Concat extends RegularExpression {
RegularExpression left;
RegularExpression right;
public Concat(RegularExpression l, RegularExpression r) {
left = l; right = r;
}
@Override boolean computeEmpty() { return isEmpty = left.computeEmpty() & right.computeEmpty(); }
@Override HashSet<Leaf> computeFirst() {
if (!left.isEmpty) {
first = left.computeFirst();
right.computeFirst();
}
else {
first.addAll(left.computeFirst());
first.addAll(right.computeFirst());
}
return first;
}
@Override HashSet<Leaf> computeLast() {
if (!right.isEmpty) {
left.computeLast();
last = right.computeLast();
}
else {
last.addAll(left.computeLast());
last.addAll(right.computeLast());
}
return last;
}
@Override void computeNext(HashSet<Leaf> n) {
right.computeNext(n);
next = n;
if (!right.isEmpty) left.computeNext(right.first);
else {
HashSet<Leaf> temp = new HashSet<>();
temp.addAll(n);
temp.addAll(right.first);
left.computeNext(temp);
}
}
}
class Star extends RegularExpression {
RegularExpression content;
public Star(RegularExpression c) {
content = c;
}
@Override boolean computeEmpty() {
content.computeEmpty();
return isEmpty=true;
}
@Override HashSet<Leaf> computeFirst() { return first = content.computeFirst(); }
@Override HashSet<Leaf> computeLast() { return last = content.computeLast(); }
@Override void computeNext(HashSet<Leaf> n) {
next.addAll(n);
next.addAll(content.first);
content.computeNext(next);
}
}
class RangeExpr extends Leaf {
Range range;
public RangeExpr(Range r) {
range = r;
}
@Override boolean computeEmpty() { return isEmpty=false; }
}
abstract class Leaf extends RegularExpression {
@Override HashSet<Leaf> computeFirst() { first.add(this); return first; }
@Override HashSet<Leaf> computeLast() { last.add(this); return last; }
@Override void computeNext(HashSet<Leaf> n) { next = n; }
}
class EmptyWord extends Leaf {
@Override boolean computeEmpty() { return isEmpty = true; }
}
public abstract class RegularExpression {
boolean isEmpty;
HashSet<Leaf> first = new HashSet<>();
HashSet<Leaf> last = new HashSet<>();
HashSet<Leaf> next = new HashSet<>();
abstract boolean computeEmpty();
abstract HashSet<Leaf> computeFirst();
abstract HashSet<Leaf> computeLast();
abstract void computeNext(HashSet<Leaf> n);
FiniteAutomata berrySethi(int accept) {
computeEmpty();
computeFirst();
computeLast();
computeNext(new HashSet<>());
int stateCounter = 1;
State start;
if (isEmpty) start = new State("S"+stateCounter++, accept);
else start = new State("S"+stateCounter++);
FiniteAutomata fa = new FiniteAutomata(start);
HashMap<Leaf,State> map = new HashMap<>();
HashSet<Leaf> markedStates = new HashSet<>();
LinkedList<Leaf> stack = new LinkedList<>();
// initialize stack
stack.addAll(first);
for(Leaf n:first) {
if (n.isEmpty || !((RangeExpr)n).range.isEmpty()) {
State nState = new State("S" + stateCounter++);
if (last.contains(n)) nState.accept = accept;
if (n.isEmpty) fa.addTransitions(new EpsilonTransition(start,nState));
else if (!((RangeExpr)n).range.isEmpty()) fa.addTransitions(new Transition(start,nState,((RangeExpr)n).range));
map.put(n,nState);
stack.push(n);
}
}
while (!stack.isEmpty()) {
Leaf curLeaf = stack.pop();
if (!markedStates.contains(curLeaf)) {
markedStates.add(curLeaf);
for(Leaf n:curLeaf.next) {
if (n.isEmpty || !((RangeExpr)n).range.isEmpty()) {
State nState;
if (map.containsKey(n)) nState = map.get(n);
else {
nState = new State("S" + stateCounter++);
map.put(n, nState);
}
if (last.contains(n)) nState.accept = accept;
if (n.isEmpty) fa.addTransitions(new EpsilonTransition(map.get(curLeaf),nState));
else if (!((RangeExpr)n).range.isEmpty()) fa.addTransitions(new Transition(map.get(curLeaf),nState,((RangeExpr)n).range));
if (!markedStates.contains(n)) stack.push(n);
}
}
}
}
return fa;
}
}