Question: Java code Regular Expression to NFA (Pease do not just paste somthing you found on github that is not helpful) Class NFAState java.lang.Object fa.State fa.nfa.NFAState
Java code Regular Expression to NFA (Pease do not just paste somthing you found on github that is not helpful)
Class NFAState
java.lang.Object
fa.State
fa.nfa.NFAState
public class NFAState extends State
Constructor Summary
| Constructor and Description |
|---|
| NFAState(java.lang.String name) |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| void | addTransition(char c, NFAState to) |
| java.lang.String | getTransitions() |
| boolean | isFinal() |
| void | setFinal() Sets this state as a final |
| void | setNonFinal() Sets this state as non-final |
| java.util.Set | toStates(char c) |
| java.lang.String | toString() |
Methods inherited from class fa.State
getName
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
NFAState
public NFAState(java.lang.String name)
Method Detail
setFinal
public void setFinal()
Sets this state as a final
isFinal
public boolean isFinal()
addTransition
public void addTransition(char c, NFAState to)
toStates
public java.util.SettoStates(char c)
toString
public java.lang.String toString()
Overrides:
toString in class State
setNonFinal
public void setNonFinal()
Sets this state as non-final
getTransitions
public java.lang.String getTransitions()
Class NFA
java.lang.Object
fa.nfa.NFA
All Implemented Interfaces:
FAInterface, NFAInterface
public class NFA extends java.lang.Object implements FAInterface, NFAInterface
Constructor Summary
| Constructor and Description |
|---|
| NFA() |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| void | addAbc(java.util.Set Adds a set of symbols to its alphabet. |
| void | addFinalState(java.lang.String finalSateName) Creates a new final state with finalSateName label if no state with such label exists. |
| void | addNFAStates(java.util.Set Adds a set of NFA states to this NFA. |
| void | addStartState(java.lang.String startStateName) Creates a new start state with startStateName label if no state with such label exists. |
| void | addState(java.lang.String stateName) Creates a new regular state with stateSTate label if no state with such label exists. |
| void | addTransition(java.lang.String fromName, char c, java.lang.String toName) Creates a transition from fromName state to toName state on symbol c |
| java.util.Set | eClosure(NFAState s) Does all epsilon transitions and determine what states can be reached from s |
| java.util.Set | getABC() Getter for Sigma |
| DFA | getDFA() |
| java.util.Set | getFinalStates() Getter for F |
| State | getStartState() Getter for q0 |
| java.util.Set | getStates() Returns the set of states of this NFA |
| java.util.Set | getToState(NFAState from, char onSymb) Return delta entries |
| java.lang.String | toString() |
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
NFA
public NFA()
Method Detail
addFinalState
public void addFinalState(java.lang.String finalSateName)
Creates a new final state with finalSateName label if no state with such label exists. Otherwise it finds an NFA state with such label and set it to final.
Specified by:
addFinalState in interface FAInterface
Parameters:
finalSateName - - the label of a new finals state
addStartState
public void addStartState(java.lang.String startStateName)
Creates a new start state with startStateName label if no state with such label exists. Otherwise it finds an NFA state with such label and set it to start state.
Specified by:
addStartState in interface FAInterface
Parameters:
startStateName - - the label of a new finals state
addState
public void addState(java.lang.String stateName)
Creates a new regular state with stateSTate label if no state with such label exists. Otherwise it finds an NFA state with such label and set it to start state.
Specified by:
addState in interface FAInterface
Parameters:
startStateName - - the label of a new finals state
addTransition
public void addTransition(java.lang.String fromName, char c, java.lang.String toName)
Creates a transition from fromName state to toName state on symbol c
Specified by:
addTransition in interface FAInterface
Parameters:
fromName - - the label of the from state
c - - the symbol of the transtion
toName - - the label of the to state
getStartState
public State getStartState()
Description copied from interface: FAInterface
Getter for q0
Specified by:
getStartState in interface FAInterface
Returns:
the start state of FA
getFinalStates
public java.util.SetgetFinalStates()
Description copied from interface: FAInterface
Getter for F
Specified by:
getFinalStates in interface FAInterface
Returns:
a set of final states that FA has
getDFA
public DFA getDFA()
Specified by:
getDFA in interface NFAInterface
Returns:
equivalent DFA
eClosure
public java.util.SeteClosure(NFAState s)
Does all epsilon transitions and determine what states can be reached from s
Specified by:
eClosure in interface NFAInterface
Parameters:
s -
Returns:
set of states that can be reached from s on epsilon trans.
toString
public java.lang.String toString()
Overrides:
toString in class java.lang.Object
addNFAStates
public void addNFAStates(java.util.SetaddStates)
Adds a set of NFA states to this NFA. If a state is not NFA state then the method will throw an exception.
Parameters:
addStates - - the set of NFA states to be added
addAbc
public void addAbc(java.util.SetmoreAbc)
Adds a set of symbols to its alphabet.
Parameters:
moreAbc - - set of symbols to be added.
getStates
public java.util.SetgetStates()
Returns the set of states of this NFA
Specified by:
getStates in interface FAInterface
Returns:
set of states
getToState
public java.util.SetgetToState(NFAState from, char onSymb)
Description copied from interface: NFAInterface
Return delta entries
Specified by:
getToState in interface NFAInterface
Parameters:
from - - the source state
onSymb - - the label of the transition
Returns:
a set of sink states
getABC
public java.util.SetgetABC()
Description copied from interface: FAInterface
Getter for Sigma
Specified by:
getABC in interface FAInterface
Returns:
the alphabet of FA
Here is the code I have so far:
public class RE implements REInterface {
private NFA nfa = null;
private String regEx;
public RE(String regEx) {
this.regEx = regEx ;
}
@Override
public NFA getNFA() {
nfa = regex();
return nfa;
}
/* Recursive descent parsing internals. */
/**
* returns the next item of input without consuming it
*/
private char peek() {
return regEx.charAt(0) ;
}
/**
* consumes the next item of input, failing if not equal to item.
*/
private void eat(char c) {
if (peek() == c)
this.regEx = this.regEx.substring(1) ;
else
throw new
RuntimeException("Expected: " + c + "; got: " + peek()) ;
}
/**
* returns the next item of input and consumes it
*/
private char next() {
char c = peek() ;
eat(c) ;
return c ;
}
private boolean more() {
return regEx.length() > 0 ;
}
/* Regular expression term types. */
/**
* must parse at least one term,
* and whether we parse another
* depends only on what we find
* afterward
*
* @return NFA
*/
private NFA regex() {
NFA term = term();
if (more() && peek() == '|') {
eat('|');
NFA regex = regex();
return regex; //return new Choice(term,regex) ;
} else {
return term;
}
}
/**
* check that it has not reached the boundary
* of a term or the end of the input
* @return NFA
*/
private NFA term() {
NFA factor = null; //NFA.blank???
while (more() && peek() != ')' && peek() != '|') {
NFA nextFactor = factor() ;
factor = nextFactor; //factor = new Sequence(factor,nextFactor) ;
}
return factor;
}
/**
* parse a base and then any number of Kleene stars
* @return NFA
*/
private NFA factor() {
NFA baseNFA = base() ;
while (more() && peek() == '*') {
eat('*') ;
//base = new Repetition(base) ;
}
return baseNFA ;
}
/**
* checks to see which of the three cases it has encountered
* @return NFA
*/
private NFA base() {
switch (peek()) {
case '(':
eat('(') ;
NFA r = regex() ;
eat(')') ;
return r ;
default:
return new Primitive(next());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
