public class Nfa<MATCHRESULT>
extends java.lang.Object
A set of Matchable
patterns is converted to an NFA as an
intermediate step toward creating the DFA.
You can also build an NFA directly with this class and convert it to a DFA
with DfaBuilder.buildFromNfa(Nfa, int[], DfaAmbiguityResolver, com.nobigsoftware.util.BuilderCache)
.
See NFA on Wikipedia
Constructor and Description |
---|
Nfa() |
Modifier and Type | Method and Description |
---|---|
void |
addEpsilon(int from,
int to)
Add an epsilon transition to the NFA
|
int |
addState(MATCHRESULT accept)
Add a new state to the NFA
|
void |
addTransition(int from,
int to,
char firstChar,
char lastChar)
Add a transition to the NFA
|
int |
Disemptify(int state)
Make modified state, if necessary, that doesn't match the empty string
|
MATCHRESULT |
getAccept(int state)
Get the result attached to the given state
|
java.lang.Iterable<java.lang.Integer> |
getStateEpsilons(int state)
Get all the epsilon transitions from a state
|
java.lang.Iterable<NfaTransition> |
getStateTransitions(int state)
Get all the non-epsilon transitions from a state
|
boolean |
hasTransitionsOrAccepts(int state)
Check whether a state has any non-epsilon transitions or has a result attached
|
int |
numStates()
Get the number of states in the NFA
|
public int numStates()
addState(Object)
public int addState(MATCHRESULT accept)
accept
- if non-null, then the NFA will produce this result for any string that reaches the new statepublic void addTransition(int from, int to, char firstChar, char lastChar)
A new transition will be created from state from to state to for all characters with code points in [from,to] (inclusive)
from
- the number of the state to transition fromto
- the number of the state to transition to, for characters in the accepted rangefirstChar
- the first character in the accepted rangelastChar
- the last character in the accepted rangepublic void addEpsilon(int from, int to)
An epsilon transition is created from state from to state to.
This will cause any string that is accepted by to to be accepted by from as well
from
- the number of the state to transition fromto
- the number of the state to transition topublic MATCHRESULT getAccept(int state)
state
- the state numberaddState(Object)
when the state was createdpublic boolean hasTransitionsOrAccepts(int state)
state
- the state numberpublic java.lang.Iterable<java.lang.Integer> getStateEpsilons(int state)
state
- the state numberpublic java.lang.Iterable<NfaTransition> getStateTransitions(int state)
state
- the state numberpublic int Disemptify(int state)
If state has a non-null result attached, or can reach such a state through epsilon transitions, then a DFA made from that state would match the empty string. In that case a new NFA state will be created that matches all the same strings except the empty string.
state
- the number of the state to disemptify