-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNFA.java
More file actions
79 lines (71 loc) · 1.47 KB
/
NFA.java
File metadata and controls
79 lines (71 loc) · 1.47 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
/**
正则表达式的模式匹配 -- grep
*/
public class NFA{
private char[] re; // 匹配转换
private Digraph G; // epsilon转换
private int M; // 状态数量
public NFA(String regexp){
Stack<Integer> ops = new Stack<Integer>();
re = regexp.toCharArray();
M = re.length;
G = new Digraph(M+1);
for (int i = 0; i < M; i++){
int lp = i;
if (re[i] == '(' || re[i] == '|'){
ops.push(i);
}
else if (re[i] == ')'){
int or = ops.pop();
if (re[or] == '|'){
lp = ops.pop();
G.addEdge(lp, or+1);
g.addEdge(or, i);
}
else{
lp = or;
}
}
if (i < M-1 && re[i+1] == '*'){
G.addEdge(lp, i+1);
G.addEdge(i+1, lp);
}
if (re[i] == '(' || re[i] == '*' || re[i] == ')'){
G.addEdge(i, i+1);
}
}
}
public boolean recognizes(String txt){
// TODO
Bag<Integer> pc = new Bag<Integer>();
DirectedDFS dfs = new DirectedDFS(G, 0); // 起点可达
for (int v = 0; v < G.V(); v++){
if (dfs.marked(v)){
pc.add(v);
}
}
for (int i = 0; i < txt.length(); i++){
Bag<Integer> match = new Bag<Integer>();
for (int v : pc){
if (v < M){
if (re[v] == txt.charAt(i) || re[v] == '.'){
match.add(v+1);
}
}
}
pc = new Bag<Integer>();
dfs = new DirectedDFS(G, match); // 多点可达
for (int v = 0; v < G.V(); v++){
if (dfs.marked(v)){
pc.add(v);
}
}
}
for (int v : pc){
if (v == M){
return true;
}
}
return false;
}
}