








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Its value Its kind, or type
"while", keyword "(", punctuation "x", name ">=", operator "0", integer ")", punctuation
public class Tokenizer { private String input; private int position;
public Tokenizer(String input) { this.input = input.trim() + " "; // to simplify getting last token position = -1; }
public boolean hasNext() { return position < input.length() - 2; }
public Token next() { ... } }
public class Tokenizer { private enum States { READY, IN_NUMBER, IN_VARIABLE, ERROR }; public Token next() { States state; String value = ""; if (!hasNext()) { throw new IllegalStateException("No more tokens!"); } state = States.READY; while ((++position) < input.length()) { char ch = input.charAt(position); switch (state) { case READY: { ... } case IN_VARIABLE: { ... } case IN_NUMBER: { ... } default: { ... } return new Token(TokenType.ERROR, value); } } assert false; // should never get here return null; } }
Everything is either a “token” or a “delimiter”
Delimiters are whitespace characters; any sequence of non-whitespace characters is returned as a token
Same as above, except you get to specify which characters are delimiters
Same as above, except you get to say you also want the delimiters returned as tokens
It is basically capable of tokenizing C and Java programs, including integers, doubles, and comments There are a large number of possible settings, so that the tokenizer can be customized The constructor is StreamTokenizer(Reader r), where Reader is an abstract class for reading character streams The most important method is int nextToken(), where the returned int tells you what kind of token it found Once you know what kind of token has been found, you access fields of the tokenizer to get its value
All the details are in the Java API You may want to use StreamTokenizer in subsequent assignments