Write a parser for a simplified regular expression
On an alphabet set [a
It has only two meta characters:
舉個例子
二
解析給定的表達式
生成對應的 NFA(Nondeterministic Finite Automation)
NFA轉化為DFA(Deterministic Finite Automation)
利用DFA判斷輸入字符串
三
FiniteAutomata
FiniteAutomata
package parser;
import java
import java
import java
import java
import java
import java
import java
import java
public class FiniteAutomata {
private State start;
private final static char[] inputs;
static {
inputs = new char[
for (char i =
inputs[i] = (char) (
}
}
private final char episilon =
private char stateId;
public void compile(String pattern) {
this
State NFA = toNFA(pattern)
this
}
private State convertToDFA(State NFA) {
Map<Set<State>
Queue<Set<State》 queue = new LinkedList<Set<State》()
// start state of DFA
Set<State> start = episilon(NFA)
State starter = nextState()
cache
queue
while (!queue
Set<State> currentEpisilon = queue
State current = cache
// create new State
for (char input : inputs) {
Set<State> nextEpisilon = move(currentEpisilon
if (nextEpisilon
continue;
}
State next;
if (!ntainsKey(nextEpisilon)) {
next = nextState()
cache
queue
} else {
next = cache
}
boolean isAccept = containsAcceptState(nextEpisilon)
next
current
}
}
return starter;
}
private Set<State> move(Set<State> currentEpisilon
Set<State> result = new HashSet<State>()
for (State s : currentEpisilon) {
State next = s
if (next != null) {
result
}
}
return episilon(result)
}
private boolean containsAcceptState(Set<State> nextEpisilon) {
for (State state : nextEpisilon) {
if (state
return true;
}
}
return false;
}
private Set<State> episilon(State s) {
Set<State> cache = new HashSet<State>()
cache
return episilon(s
}
private Set<State> episilon(Set<State> states) {
Set<State> cache = new HashSet<State>()
for (State s : states) {
cache
cache
}
return cache;
}
private Set<State> episilon(State s
State next = s
if (next != null && !ntains(next)) {
cache
cache
}
return cache;
}
private State toNFA(String pattern) {
char[] expr = pattern
State currentState = nextState()
State starter = currentState;
for (char c : expr) {
if (c ==
State nextState = nextState()
// add each transition for all inputs
for (char i : inputs) {
currentState
}
currentState = nextState;
} else if (c ==
State nextState = nextState()
// add each transition for all inputs
for (char i : inputs) {
currentState
}
currentState
nextState
currentState = nextState;
} else if (c >=
State nextState = nextState()
currentState
currentState = nextState;
} else {
throw new RuntimeException(
}
}
currentState
return starter;
}
private State nextState() {
return new State(stateId++)
}
public void constructDFA(Map<State
Iterator<Map
while (it
Entry<State
State state = entry
Map<Character
state
}
}
public boolean match(String c) {
char[] candidate = c
for (char d : candidate) {
start = start
if (start == null) {
return false;
}
}
return start
}
public static String[] patterns = {
public static String[] candidates = {
public static void main(String[] args) {
FiniteAutomata fa = new FiniteAutomata()
for (int i =
pile(patterns[i])
System
}
}
}
State
package parser;
import java
public class State {
private boolean accept = false;
private char id;
private Map<Character
public State(char c) {
this
}
public State(char c
this
this
}
public State transit(char input) {
return mapping
}
public void setTransition(char input
this
}
public void setTransition(Map<Character
this
}
public boolean isAccept() {
return this
}
public void setAccept(boolean b) {
this
}
@Override
public int hashCode() {
final int prime =
int result =
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj
return false;
State other = (State) obj;
if (id != other
return false;
return true;
}
@Override
public String toString() {
return
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12363.html