// THIS PROGRAM WILL ONLY COMPILE WITH JDK 1.8 OR LATER ///////////////////////////////////////////////////////////////// // SAMPLE SUBCLASS OF FSM3: DFA to recognized regular expression a(a|b)*b public class dfa3 extends FSM3 // will cast chars to strings { String instring; // input string int ci; // current char position in instring public dfa3(String x) { instring = x; ci = 0; // current character in instring. states = 4; // state 0 is always start state maxinputs = 2; // input restricted to 'a', 'b' setHash(); setStateTable(); } protected boolean acceptState(int s) { return s==2; } // 2 is accept state protected void setHash() { Hash = new JHMadaptor(10); Hash.set("a",0); Hash.set("b",1); } protected void setStateTable() { M = new action[states][maxinputs]; int a = Hash.get("a"), b = Hash.get("b"); M[0][a] = new action() { public int act() { return 1; } }; // in Java 1.8, above same as M[0][a] = () -> 1; M[0][b] = () -> { return 3; }; M[1][a] = () -> 1; // same as { return 1; } M[1][b] = () -> 2; M[2][a] = () -> 1; M[2][b] = () -> 2; for(int i=a;i<=b;i++) M[3][i] = () -> 3; }//setStateTable protected String nextInput() // gets next available input, null if none { if (ci > instring.length()-1) return null; return ""+instring.charAt(ci++); } public static void main(String[] argv) { dfa3 automaton = new dfa3(argv[0]); automaton.trace = true; // prints current input and state automaton.keeprunning = true; // process entire input automaton.skipbadinput = false; // terminates on invalid input boolean result = automaton.run(); System.out.println(result); } }//dfa3