For class I'm working on an interpreter, currently working on the scanner. Being a class that will be called many many times, I would like for it to be optimized for speed. In the scanner, to categorize an operator you have to compare the current token to the 6 or so operators. What method is best for speed, but for also for readability.
These are the only cases I could think of. Which is best, or if you have a better approach please share. I implemented #2 because it takes up the least amount of lines of code.
Any sensible hand-written scanner is based on a switch statement. Note that if you return special characters directly to the parser as themselves, you can economize on case actions:
switch (ch) // the next incoming character
{
case '+':
case '-':
case '*':
case '/':
case '%':
// etc.
return ch;
case 'A':
case 'B':
// ...
case 'Z':
case 'a':
case 'b':
// ...
case 'z':
// start of an identifier: accumulate it, with a do/while loop,
// save it somewhere, return IDENTIFIER
return IDENTIFIER;
case '0':
case '1':
// ...
case '9':
// start of a numeric literal: ...
return NUMERIC_LITERAL;
// etc.
}