class SyntaxAnalyse {    public static final boolean DEBUG = true;		private String  expressionS = null; // enthaelt die Expression	public  int     pos  = 0;           // aktuelle Position in expressionS	public  char    next = ' ';         // naechstes Zeichen	private boolean eof  = false;       // signalisiert Lese-Ende		/**	 *  Konstruktor: uebergeben wird der String, der analysiert werden soll	 */	public SyntaxAnalyse(String s) {		expressionS = s;		scan();	}		/**	 *  zeigt an, ob das Ende des Ausdrucks erreicht wurde	 */	public boolean eof() {		return eof;	}		/**	 *  kopiert das naechste Zeichen nach next	 */	public void scan() {		while(!eof()) {			if ( pos < expressionS.length() ) {				next = expressionS.charAt(pos); 				pos++;			} else {				eof = true;				next = ' ';			}			if (next == ' ') continue; // Leerzeichen ueberlesen			else             break;		}	}		/**	 *  prueft auf Atom (Folge von Ziffern)	 */	public boolean isAtom() {		String atom = "";		while(!eof() && next >= '0' && next <= '9') {			atom += next;			scan();		}		if (atom.length() == 0)			return false;		else {			if(DEBUG)System.out.println(" Atom '"+atom+"' recognized!");			return true;		}			}		/**	 *  prueft auf Term	 */	public boolean isTerm() {		if ( next=='(' ) {			scan();			if ( isExpression() ) {				if ( next==')' ) {					scan();					if(DEBUG)System.out.println(" '(' Expression ')' recognized!");					return true;				} else {					System.out.println(" Syntax Error: Missing ')' in Term!");					return false;				}			} else {				System.out.println(" Syntax Error: Missing Expression"+								   " followed by '('!"+next);				return false;			}		} else {			return isAtom();		}	}								   	/**	 *  prueft auf Expression	 */	public boolean isExpression() {		if (isTerm()) {			switch (next) {				case '+':				case '-':					if(DEBUG)System.out.println(" Operator "+next+												" in Expression recognized!");					scan();					return isExpression();			}			return true; // default		} else {			return false;		}	}		/**	 *  starte Syntaxanalyse	 */	public boolean solve() {		return isExpression() && eof();			}	public static void main(String[] argv) {		String inputString = "help";			java.io.BufferedReader keyboard =			new java.io.BufferedReader(new java.io.InputStreamReader(System.in));		while (!inputString.equals("exit")) {			if (inputString.equals("help")) {				System.out.println();							System.out.println(" Expression ::= Term                |");				System.out.println("                Term '+' Expression |");				System.out.println("                Term '-' Expression ");				System.out.println("       Term ::= Atom                |");				System.out.println("                '(' Expression ')'");				System.out.println("       Atom ::= <a_number>");			}			System.out.print("\n\nType in your Expression, 'help' or 'exit': ");			// read line from keyboard			try {				inputString = keyboard.readLine();			}			catch (java.io.IOException ioe) {				System.out.println(" Error: "+ioe);							}			if (inputString.equals("exit")) break;			if (inputString.equals("help")) continue;			if (inputString.length() == 0) continue;			// syntax analyse 			SyntaxAnalyse sa = new SyntaxAnalyse(inputString);			if (sa.solve())				System.out.println("\n\n RESULT: This is a VALID expression!");			else				System.out.println("\n\n RESULT: SYNTAX ERROR!");		}		System.out.println("Bye!");	}}	
