Search code examples
javacompiler-constructionlexical-analysis

How to best handle many accept cases


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.

  1. Many cases in an if statement
  2. Loop through a char array of every operator and compare
  3. Switch statement

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.


Solution

  • 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. 
    }