Java Reference
In-Depth Information
1 private static class Token
2 {
3 public Token( )
4 { this( EOL ); }
5 public Token( int t )
6 { this( t, 0 ); }
7 public Token( int t, long v )
8 { type = t; value = v; }
9
10 public int getType( )
11 { return type; }
12 public long getValue( )
13 { return value; }
14
15 private int type = EOL;
16 private long value = 0;
17 }
18
19 private static class EvalTokenizer
20 {
21 public EvalTokenizer( StringTokenizer is )
22 { str = is; }
23
24 /**
25 * Find the next token, skipping blanks, and return it.
26 * For VALUE token, place the
27 * processed value in currentValue.
28 * Print error message if input is unrecognized.
29 */
30 public Token getToken( )
31 { /* Figure 11.16 */ }
32
33 private StringTokenizer str;
34 }
figure 11.15
The Token and
EvalTokenizer nested
classes
Figure 11.16 shows the getToken routine. Line 10 checks for the end of the
input line. When getToken gets past line 11, we know that more tokens are available.
If we have not reached the end of line, we check to see whether we match any of the
one-character operators. If so, we return the appropriate token. Otherwise, we
expect that what remains is an operand, so we use Long.parseLong to get the value,
and then return a Token object by explicitly constructing a Token object based on the
value read.
We can now discuss the methods of the Evaluator class. The only publicly
visible method is getValue . Shown in Figure 11.17, getValue repeatedly reads
a token and processes it until the end of line is detected. At that point the item
at the top of the stack is the answer.
 
Search WWH ::




Custom Search