// Abb.java: recognize (a|b)*abb import java.io.*; public class Abb { private GetChar getChar; private char ch; private int state = 0; public Abb() { getChar = new GetChar(); state = 0; } public void process() { while (true) { ch = getChar.getNextChar(); if (ch == '$') break; // out of while switch (state) { case 0: if (ch == 'a') state = 1; else if (ch == 'b') state = 0; break; case 1: if (ch == 'a') state = 1; else if (ch == 'b') state = 2; break; case 2: if (ch == 'a') state = 1; else if (ch == 'b') state = 3; break; case 3: if (ch == 'a') state = 1; else if (ch == 'b') state = 0; break; } System.out.print("ch: " + ch + ", next state: " + state); if (state == 3) System.out.print(" Terminal"); System.out.print("\n"); } if (state == 3) System.out.print("Accept\n"); else System.out.print("Reject\n"); } public static void main(String[] args) { Abb abb = new Abb(); abb.process(); } } /* /home/neal/java_sdk/jdk1.5.0_08/bin/javac Abb.java /home/neal/java_sdk/jdk1.5.0_08/bin/java Abb */