Yes.
Here is the automaton from chapter 1 that recognizes Java identifiers.
Assume that the reject state is q2
.
Here is the recognize method.
In order to make the logic of the else if
statements easier,
transitions labeled with several groups of characters are implemented
as one else if
per group.
public boolean recognize(String str) { final int reject = ; // rejection state int state = 0; // the current state char current; // the current character int index = 0 ; // index of the current character // continue while there are input characters // and the reject state has not been reached while ( index < str.length() && state != reject ) { current = str.charAt( index++ ) ; // State 0 if ( state== && current == ) state = ; else if ( state== && current == ) state = ; else if ( state== && current >= && current <= ) state = ; else if ( state== && current >= && current <= ) state = ; // State 1 else ( state== && current == ) state = ; else if ( state== && current == ) state = ; else if ( state== && current >= && current <= ) state = ; else if ( state== && current >= && current <= ) state = ; else if ( state== && current >= && current <= ) state = ; else state = reject ; } // if the final state was reached with the last character, // the string is accepted. if ( index == str.length() && state == ) return true; else return false; }
Fill in the blanks so that recoginze()
accepts Java identifiers.