Question
Im having trouble figuring out how this dfa implemented in java could accept a substring of aac or abcc while any other string would be
Im having trouble figuring out how this dfa implemented in java could accept a substring of aac or abcc while any other string would be rejected. Any help would be greatly appreciated!
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException;
public class FiniteStateMachine {
private final static int[][] STATE_TABLE = { // Transition Table // a b c { 1, 5, 0 }, //state 0 { 7, 2, 0 }, //state 1 { 1, 5, 3 }, //state 2 { 1, 5, 4 }, //state 3 { 0, 5, 7 }, //state 4 { 1, 5, 6 }, //state 5 { 4, 7, 7 }, //state 6 { 4, 4, 4 }, //state 7 }; private BufferedReader in;
public FiniteStateMachine() { in = new BufferedReader(new InputStreamReader(System.in)); }
public void run() throws IOException { char ch; int state; for (;;) { System.out.print("Your string> "); ch = (char) in.read();
//set statet to start state. state = 0;
while(ch != ' ') { state = STATE_TABLE[state][charToColumn(ch)]; ch = (char) in.read(); }
// determine whether to accept or reject. if (state == 1 || state == 4) { System.out.println("Accept "); } else System.out.println("Reject "); } }
public int charToColumn(char ch) { // column 2 is for some unexpected character int column = 2; switch (ch) { case 'a': column = 0; break; case 'b': column = 1; break; case 'c': column = 2; break; } return column; }
public static void main(String[] args){ try { FiniteStateMachine fsm = new FiniteStateMachine(); fsm.run(); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started