import java.util.Scanner;
/**
* DEA accepting the symbols A,E,1,0
*/
public class DEA {
public static final String START = "START";
public static final String ERROR = "ERROR";
public static final String ACCEPTED = "ACCEPTED";
public static final String UNDEFINED = "UNDEFINED";
private String state = START;
/**
* Reads a sequence of symbols from the stdin/console
* @return array of symbols
*/
private char[] getInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Input: ");
String input = scanner.next();
char[] chars = input.toCharArray();
return chars;
}
/**
* Processes a sequence of input symbols
* @param input the sequence of input symbols
* @return true if the DEA is in the ACCEPTED state after consuming all symbols, false otherwise
*/
private boolean accepted(char[] input) {
boolean accepted = true;
for (char c : input) {
String currentState = this.state;
try {
if (consume(c)) {
System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
} else {
System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
// now we are in the error state, since we can not
// leave again, just return that we are not happy
// with the input, if all symbols should be checked
// only set accepted = false;
//return false; // skip further error transitions
accepted = false; // yes, i need to see all of them
}
} catch (TokenNotAcceptedException e) {
System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
// undefined symbol used - not part of our alphabet,
// so this is not accepted and crashes our DEA
return false;
}
}
return accepted;
}
/**
* Consumes a single symbol
* @param c the symbol to consume
* @return true if the consuming the symbol does not lead to an ERROR state
* @throws TokenNotAcceptedException if the symbol is not part of the alphabet of this DEA
*/
private boolean consume(char c) throws TokenNotAcceptedException {
if (c != 'A' && c != 'E' && c != '1' && c != '0') {
this.state = UNDEFINED;
throw new TokenNotAcceptedException("Token " + c
+ " not accepted by this DEA");
}
if (this.state.equals(START)) {
if (c == 'A' || c == '1' || c == '0') {
this.state = ACCEPTED;
return true;
} else {
// if (c == 'E')
this.state = ERROR;
return false;
}
} else if (this.state.equals(ERROR)) {
this.state = ERROR; // no need to check anymore, we are in an error
// state and will stay here until the end;
return false;
} else if (this.state.equals(ACCEPTED)) {
if (c == 'E') {
this.state = ERROR;
return false;
} else {
// nothing to do, A, 0, 1 are ok
return true;
}
}
return false;
}
/**
* starts this DEA
*/
private void start() {
char[] chars = getInput();
boolean accepted = accepted(chars);
System.out.println("Input " + (accepted ? "accepted" : "not accepted")+" -> final State is " + this.state);
}
public static void main(String[] args) {
DEA dea = new DEA();
dea.start();
}
/**
* Exception used if a symbol is not part of the alphabet
*/
static class TokenNotAcceptedException extends Exception {
public TokenNotAcceptedException(String message) {
super(message);
}
}
}