/** * File: LexState.java * * Figure 7.35 of "Computer Systems", J. Stanley Warford, * Fourth Edition, Jones & Bartlett, Publishers, 2010. */ public enum LexState { eLS_START, eLS_IDENT, eLS_SIGN, eLS_INTEGER, eLS_STOP } /** * File: Tokenizer.java * * Figure 7.35 of "Computer Systems", J. Stanley Warford, * Fourth Edition, Jones & Bartlett, Publishers, 2010. */ public class Tokenizer { private InBuffer b; public Tokenizer(InBuffer inBuffer) { b = inBuffer; } public AToken getToken() { char nextChar; int sign = +1; int localIntValue = 0; StringBuffer localIdentValue = new StringBuffer(""); AToken aToken = new TEmpty(); LexState state = LexState.eLS_START; do { nextChar = b.advanceInput(); switch (state) { case eLS_START: if (Util.isAlpha(nextChar)) { localIdentValue.append(nextChar); state = LexState.eLS_IDENT; } else if (nextChar == '-') { sign = -1; state = LexState.eLS_SIGN; } else if (nextChar == '+') { sign = +1; state = LexState.eLS_SIGN; } else if (Util.isDigit(nextChar)) { localIntValue = nextChar - '0'; sign = +1; state = LexState.eLS_INTEGER; } else if (nextChar == ',') { aToken = new TComma(); state = LexState.eLS_STOP; } else if (nextChar == '(') { aToken = new TLeftParen(); state = LexState.eLS_STOP; } else if (nextChar == ')') { aToken = new TRightParen(); state = LexState.eLS_STOP; } else if (nextChar == '\n') { state = LexState.eLS_STOP; } else if (nextChar != ' ') { aToken = new TInvalid(); } break; case eLS_IDENT: if (Util.isAlpha(nextChar) || Util.isDigit(nextChar)) { localIdentValue.append(nextChar); } else { b.backUpInput(); aToken = new TIdentifier(localIdentValue); state = LexState.eLS_STOP; } break; case eLS_SIGN: if (Util.isDigit(nextChar)) { localIntValue = nextChar - '0'; state = LexState.eLS_INTEGER; } else { aToken = new TInvalid(); } break; case eLS_INTEGER: if (Util.isDigit(nextChar)) { localIntValue = 10 * localIntValue + nextChar - '0'; } else { b.backUpInput(); aToken = new TInteger(sign * localIntValue); state = LexState.eLS_STOP; } break; } } while ((state != LexState.eLS_STOP) && !(aToken instanceof TInvalid)); return aToken; } }