/* FPAE_Scanner.java ** CMPS 144L ** Authors: R. McCloskey and < students' names > ** Known flaws: ... ** ** An instance of this class is intended to "scan" a given FPAE ** (fully-parenthesized arithmetic expression) for the purpose of ** identifying the tokens contained therein, one after the other. ** ** The categories of tokens expected to appear in an FPAE are left and right ** parentheses ('(' and ')', respectively), operator symbols (any of '+', '-', ** '*', '/'), and unsigned integer literals (e.g., "3783", "2"). Consecutive ** tokens need not be separated from each other by spaces, but they could be. ** ** As an example, the tokens in "((167* 3 ) -15 ) " are, in order, "(", "(", ** "167", "*", "3", ")", "-", "15", and ")". */ public class FPAE_Scanner { // instance variables // ------------------ private String expr; // the string containing the expression to be scanned private int pos; // the position, within expr, where the next token // begins (or expr.length() if there are no more) // constructor // ----------- public FPAE_Scanner(String s) { expr = s; pos = 0; advanceToNextToken(); // sets 'pos' to the position at which the } // first token begins // observer // -------- /* Reports whether or not there are any more tokens in the expression ** being scanned. */ public boolean hasNext() { return pos != expr.length(); } // observer/mutator // ---------------- /* Returns that substring of expr corresponding to the "next" token ** of the expression being scanned (i.e., the token following the one ** returned by the previous call to this method, or, in the case that ** this method had not been called previously, the first token) ** pre: hasNext() && ** every character in 'expr' is either a parenthesis, ** an operator symbol, a digit, or a space && ** the next token (i.e., the one to be returned) begins at ** the position of 'expr' indicated by the value of 'pos'. */ public String next() { STUB!! } // private // ------- /* Increases the value of instance variable 'pos' until either it is ** equal to expr.length() or expr.charAt(pos) is a non-space. */ public void advanceToNextToken() { final int N = expr.length(); while (pos != N && expr.charAt(pos) == ' ') { pos = pos + 1; } } }