forked from lfkdsk/PracticeCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDfaBuilder.java
More file actions
133 lines (111 loc) · 3.5 KB
/
Copy pathDfaBuilder.java
File metadata and controls
133 lines (111 loc) · 3.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
package sample;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by liufengkai on 16/7/10.
*/
public class DfaBuilder {
/**
* NFA 状态机的起始状态
*/
public DfaState startState = null;
/**
* 状态机的当前状态
*/
public DfaState currentState = null;
/**
* 接受状态
*/
public HashMap<Integer, DfaState> acceptState;
private static final int RETURN_ID = 13;
private static final int CHANGE_LINE_ID = 10;
private static final int TAB_ID = 9;
private static final int SPACE_ID = 32;
private ArrayList<Integer> endIdList;
private DfaCallBack dfaCallBack = null;
public DfaBuilder() {
// parent is null
this(new DfaState(null, null));
}
public DfaBuilder(DfaState startState) {
this.startState = startState;
this.currentState = startState;
initial();
}
/**
* 添加接受状态
*/
public void addAcceptState(int input, DfaState accept) {
if (!acceptState.containsKey(input)) {
acceptState.put(input, accept);
}
}
private void initial() {
this.acceptState = new HashMap<>();
this.endIdList = new ArrayList<>();
initialEndIdList();
}
private void initialEndIdList() {
endIdList.add(RETURN_ID);
endIdList.add(CHANGE_LINE_ID);
endIdList.add(TAB_ID);
endIdList.add(SPACE_ID);
}
public DfaState input(int input) {
// parser 了所有特殊情况 对于单词的提示
// 一个单词内是不会出现空格制表符和换行的
// System.out.println(input + "sss");
if (endIdList.contains(input)) {
this.currentState = startState;
return null;
}
// 处理了当输入串还在起始状态的情况
if (currentState.getStateId() == startState.getStateId()) {
return startInput(input);
}
// 说明状态不在起始状态
DfaState tempCurrent = currentState.getTransitionInput(input);
if (tempCurrent == null) {
tempCurrent = new DfaState(input, currentState);
currentState.addTransition(input, tempCurrent);
} else {
if (dfaCallBack != null) dfaCallBack.onMultipleSetBack(tempCurrent, tempCurrent.getTransitionSet());
}
currentState = tempCurrent;
return currentState;
}
/**
* 处理还在输入串起始状态的情况
*
* @param input 输入
* @return current状态
*/
public DfaState startInput(int input) {
DfaState current;
// 转入第一个起始状态
if (!acceptState.containsKey(input)) {
current = new DfaState(input, currentState);
this.addAcceptState(input, current);
} else {
current = acceptState.get(input);
if (dfaCallBack != null) dfaCallBack.onMultipleSetBack(current, current.getTransitionSet());
}
this.currentState = current;
return current;
}
public void setDfaCallBack(DfaCallBack dfaCallBack) {
this.dfaCallBack = dfaCallBack;
}
public void printDfa() {
for (Integer integer : acceptState.keySet()) {
System.out.println("接受状态 " + acceptState.get(integer).getStateId());
acceptState.get(integer).printState();
}
}
/**
* 重设startState
*/
public void resetStartState() {
this.currentState = startState;
}
}