I have a bit of a problem. I’m making a Finite Automata checker.
Given an input, and the DFA, does it end on a accepting state.
My problem is creating a new DFA_State from another’s target.
DFA_State state0, state1, curr_state, init_state, temp; //fine, I think
state0 = new DFA_State();
state1 = new DFA_State();
state0 = new DFA_State("State 0",true, state0, state1); //fine, I think
init_state = new DFA_State(state0); //fine, I think
but, this bit is throwing up problems.
temp = new DFA_State(curr_state.nextState(arr1[i]));
*
*
curr_state = new DFA_State(temp);
Thanks for any help,
Dave
Edit:
God I was retarded when I did this, AFAIK, I just wasn’t thinking straight, added methods to set the values to the DFA_State object.
//in DFA_State class
public void set(DFA_State on_0, DFA_State on_1, Boolean is_accepting, String name){
this.on_0 = on_0;
this.on_1 = on_1;
this.is_accepting = is_accepting;
this.name = name;
}
//in main
DFA_State state0, state1, curr_state;
state0 = new DFA_State();
state1 = new DFA_State();
state0.set(state0, state1, false, "State 0");
state1.set(state1, state0, true, "State 1");
curr_state = state0;//initial state
//iterate across string input changing curr_state depending on char c
curr_state = getNextState(c);
//at end
if(curr_state.isAccepting())
System.out.println("Valid, " + curr_state.getName() + " is accepting);
else
System.out.println("Invalid, " + curr_state.getName() + " is not accepting);
In that first line, you declare the variables
state0,state1,curr_state,init_stateandtempas being variables of typeDFA_State. However, that only declares them, they are not yet initialized. The next few lines are all okay. Second line creates a state without anything in it and assigns it tostate0, so does the third line forstate1. Fourth line overwrites your previousstate0assignment with a newDFA_Statethat has actual contents. Fifth line creates aDFA_Stateas a copy ofstate0and assigns it toinit_state.Assuming there’s nothing in between this and the first line of your second code block, now you’ll get a problem. You’re assigning
tempwith a newDFA_Statethat uses a copy-constructor with an argument relying oncurr_state. But at that point, that variable hasn’t been initialized yet. Just because it was declared doesn’t mean it has somehow already been structured in memory. When you callnextStateon it, there’s simply no variable to resolve this to. Don’t expect to get something like a pointer that will eventually point to a part of what you put incurr_state.I’m just guessing, but from your code style I’d say you have a background in C or C++. Look into the differences between those languages and Java. If possible, I’d also advise you to make your
DFA_Stateclass immutable, since this is more reliable and will avoid mistakes. That means getting rid of the no-args constructor. Here’s a reworking of it (not actually compiled, might contain errors):Even if you can’t make the instance variables final, it’s best to at least make them private, since you already have methods for getting them.