Java Reference
In-Depth Information
The code corresponding to this logic would look something like the following:
if(isLetter(ch)||ch=='_'||ch=='$'){
buffer=newStringBuffer();
while(isLetter(ch)||isDigit(ch)||
ch=='_'||ch=='$'){
buffer.append(ch);
nextCh();
}
Stringidentifier=buffer.toString();
if(reserved.containsKey(identifier)){
returnnewTokenInfo(reserved.get(identifier),
line);
}
else{
returnnewTokenInfo(IDENTIFIER,identifier,
line);
}
}
This relies on a map (hash table), reserved , mapping reserved identifiers to their rep-
resentations:
reserved=newHashtable<String,Integer>();
reserved.put("abstract",ABSTRACT);
reserved.put("boolean",BOOLEAN);
reserved.put("char",CHAR);
...
reserved.put("while",WHILE);
We follow this latter method, of looking up identifiers in a table of reserved words, in
our hand-written lexical analyzer.
Separators and Operators
The state transition diagram deals nicely with operators. We must be careful to watch
for certain multi-character operators. For example, the state transition diagram fragment
for recognizing tokens beginning with `;' , `==' , `=' , `!' , or `*' would look like that in
Figure 2.4.
The code corresponding to the state transition diagram in Figure 2.4 would look like
the following. Notice the use of the switch-statement for deciding among first characters.
switch(ch){
...
case';':
nextCh();
returnnewTokenInfo(SEMI,line);
case'=':
nextCh();
if(ch=='='){
nextCh();
returnnewTokenInfo(EQUAL,line);
}
else{
returnnewTokenInfo(ASSIGN,line);
}
case'!':
nextCh();
returnnewTokenInfo(LNOT,line);
case'*':
 
Search WWH ::




Custom Search