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

update

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