import java.util.StringTokenizer; import java.util.NoSuchElementException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** * Tokenizes an expression represented by a string. * The special end-of-line token ("end-of-line") is returned * when the end-of-line is reached. */ public class ExpressionTokenizer { public boolean debug; // Prints each token if true private StringTokenizer tokenizer; // Uses java.util.StringTokenizer private String currentToken; // Most recent token private boolean pushed; // True iff re-using token /** * Constructor. * @param str the string to be interpreted as an expression */ public ExpressionTokenizer( String str ) { debug = false; tokenizer = new StringTokenizer( str, "+-*/(), ", true ); currentToken = null; pushed = false; } /** * Returns the next non-space token in this expression. * The special token "end-of-line" is returned at end-of-line. * @return the next non-space token in this expression * @exception NoSuchElementException if an attempt is made to * read past the end-of-line token. */ public String nextToken() { if( pushed ) pushed = false; else try { // Look for next nonblank token do { currentToken = tokenizer.nextToken(); } while( currentToken.equals(" ") ); // If we reach eol, we return special end-of-line token. // If we read past it, we throw an exception } catch (NoSuchElementException e) { if (currentToken.equals("end-of-line")) throw new NoSuchElementException("Read past end-of-line"); currentToken = "end-of-line"; } if (debug) System.out.println("Token = " + currentToken); return currentToken; } /** * Holds onto the current token so that the next call to nextToken() * will return the current token instead of the next. */ public void pushBack() { pushed = true; } /** * Main program used for testing the tokenizer. */ public static void main (String[] arg) { ExpressionTokenizer tokenizer; String expression; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Divide input lines into tokens until a blank line is read while (true) { try { System.out.print("Input: "); expression = in.readLine(); if (expression.equals("")) break; tokenizer = new ExpressionTokenizer(expression); // Print tokens until we read past end-of-line tokenizer.debug = true; while (true) tokenizer.nextToken(); } catch (NoSuchElementException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Unknown IO problem"); } } } }