Java Reference
In-Depth Information
FIGURE 2.1 State transition diagram for identifiers and integers.
digits and repeatedly go back into the integer state until a non-digit character is reached;
at this point the machine would go into the (final) intEnd state without scanning another
character.
An advantage of basing our program on such a state transition diagram is that it takes
account of state in deciding what to do with the next input character. Clearly, what the
scanner does with an incoming letter depends on whether it is in the start state (it would
go into the id state), the id state (it would remain there), or the integer state (where it
would go into the intEnd state, recognizing that it has come to the end of the integer).
It is relatively simple to implement a state transition diagram in code. For example, the
code for our diagram above might look something like
if(isLetter(ch)||ch=='_'||ch=='$'){
buffer=newStringBuffer();
while(isLetter(ch)||isDigit(ch)||ch=='_'||ch=='$'){
buffer.append(ch);
nextCh();
}
returnnewTokenInfo(IDENTIFIER,buffer.toString(),line);
}
elseif(ch=='0'){
nextCh();
returnnewTokenInfo(INT_LITERAL,"0",line);
}
elseif(isDigit(ch)){
buffer=newStringBuffer();
while(isDigit(ch)){
buffer.append(ch);
nextCh();
}
returnnewTokenInfo(INT_LITERAL,buffer.toString(),line);
}
Choices translate to if-statements and cycles translate to while-statements. Notice that
the TokenInfo object encapsulates the value of an integer as the string of digits denoting
it. For example, the number 6449 is represented as the String \6449". Translating this to
binary is done later, during code generation.
 
Search WWH ::




Custom Search