
Compilation 21
Changes to Scanner
Here we only discuss the changes to the hand-written scanner. Scanners can also be gener-
ated; this is discussed in Chapter 2. Before changing Scanner.java, we must register DIV as
a new token, so we add the following to the TokenKind enumeration in the TokenInfo.java
file.
enum TokenKind {
EOF (" < EOF >") ,
... ,
STAR ("*") ,
DIV ("/") ,
...
}
The method that actually recognizes and returns tokens in the input is getNextToken
(). Currently, getNextToken() does not recognize / as an operator, and reports an error
when it encounters a single / in the source program. In order to recognize the operator, we
replace the getNextToken() ...