提交 435cbdf5 编写于 作者: xindoo's avatar xindoo

update

上级 bf9db5a7
...@@ -3,6 +3,7 @@ package xyz.xindoo.re; ...@@ -3,6 +3,7 @@ package xyz.xindoo.re;
import xyz.xindoo.re.common.Constant; import xyz.xindoo.re.common.Constant;
import xyz.xindoo.re.common.Reader; import xyz.xindoo.re.common.Reader;
import xyz.xindoo.re.common.State; import xyz.xindoo.re.common.State;
import xyz.xindoo.re.common.StateType;
import xyz.xindoo.re.dfa.DFAGraph; import xyz.xindoo.re.dfa.DFAGraph;
import xyz.xindoo.re.dfa.DFAState; import xyz.xindoo.re.dfa.DFAState;
import xyz.xindoo.re.nfa.NFAGraph; import xyz.xindoo.re.nfa.NFAGraph;
...@@ -28,7 +29,7 @@ public class Regex { ...@@ -28,7 +29,7 @@ public class Regex {
return null; return null;
} }
NFAGraph nfaGraph = regex2nfa(regex); NFAGraph nfaGraph = regex2nfa(regex);
nfaGraph.end.setStateType(); nfaGraph.end.setStateType(StateType.END); // 将NFA的end节点标记为终止态
DFAGraph dfaGraph = convertNfa2Dfa(nfaGraph); DFAGraph dfaGraph = convertNfa2Dfa(nfaGraph);
return new Regex(nfaGraph, dfaGraph); return new Regex(nfaGraph, dfaGraph);
} }
...@@ -463,4 +464,8 @@ public class Regex { ...@@ -463,4 +464,8 @@ public class Regex {
} }
return -1; return -1;
} }
// todo, 使用hopcraft算法将dfa最小化
private DFAGraph hopcroft(DFAGraph dfaGraph){
return new DFAGraph();
}
} }
...@@ -51,14 +51,15 @@ public class RegexTest { ...@@ -51,14 +51,15 @@ public class RegexTest {
} }
public static void main(String[] args) { public static void main(String[] args) {
Options options = new OptionsBuilder().include(RegexTest.class.getSimpleName()).build(); test();
try { // Options options = new OptionsBuilder().include(RegexTest.class.getSimpleName()).build();
new Runner(options).run(); // try {
} catch (Exception e) { // new Runner(options).run();
System.out.println(e.fillInStackTrace()); // } catch (Exception e) {
} finally { // System.out.println(e.fillInStackTrace());
System.out.println("finshed"); // } finally {
} // System.out.println("finshed");
// }
} }
private static void test() { private static void test() {
......
package xyz.xindoo.re.common; package xyz.xindoo.re.common;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class State { public class State {
protected static int idCnt = 0; protected static int idCnt = 0;
protected int id; protected int id;
protected int stateType; protected StateType stateType;
public State() { public State() {
this.id = idCnt++; this.id = idCnt++;
this.stateType = StateType.GENERAL;
} }
public Map<String, Set<State>> next = new HashMap<>(); public Map<String, Set<State>> next = new HashMap<>();
...@@ -27,12 +26,12 @@ public class State { ...@@ -27,12 +26,12 @@ public class State {
set.add(nfaState); set.add(nfaState);
} }
public void setStateType() { public void setStateType(StateType stateType) {
stateType = 1; this.stateType = stateType;
} }
public boolean isEndState() { public boolean isEndState() {
return stateType == 1; return stateType == StateType.END;
} }
public int getId() { public int getId() {
......
package xyz.xindoo.re.common;
public enum StateType {
GENERAL, END
}
package xyz.xindoo.re.dfa; package xyz.xindoo.re.dfa;
import xyz.xindoo.re.common.State; import xyz.xindoo.re.common.State;
import xyz.xindoo.re.common.StateType;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -9,7 +11,7 @@ public class DFAState extends State { ...@@ -9,7 +11,7 @@ public class DFAState extends State {
// 保存对应NFAState的id,一个DFAState可能是多个NFAState的集合,所以拼接成String // 保存对应NFAState的id,一个DFAState可能是多个NFAState的集合,所以拼接成String
private String allStateIds; private String allStateIds;
public DFAState() { public DFAState() {
this.stateType = 2; this.stateType = StateType.GENERAL;
} }
public DFAState(String allStateIds, Set<State> states) { public DFAState(String allStateIds, Set<State> states) {
...@@ -17,8 +19,8 @@ public class DFAState extends State { ...@@ -17,8 +19,8 @@ public class DFAState extends State {
this.nfaStates.addAll(states); this.nfaStates.addAll(states);
for (State state : states) { for (State state : states) {
if (state.isEndState()) { if (state.isEndState()) { // 如果有任意节点是终止态,新建的DFA节点就是终止态
this.stateType = 1; this.stateType = StateType.END;
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册