title: Trouble with a switch statement in a lexical analyzer
body: <p>I am trying to make a lexical analyzer in C, and I have tried going through the code step by step to see where the issue is but I can't see it. This code reads a line from a file, assuming the first line and nothing else is in the file. So I tested it with <code>"a = (b + 2) * c"</code>.</p>
  
  <p>It works, and prints out <code>a</code>, <code>=</code> but then nothing. I figured its a problem with the <code>switch</code> statement in my <code>lookup()</code> function because it seems to be working fine with anything not <code>UNKNOWN</code> in <code>lex()</code>. Any insight would be helpful and appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  int charClass;
  char lexeme[100];
  char nextChar;
  int lexLen;
  int token;
  int nextToken;
  FILE *fp;
  
  void addChar();
  void getChar();
  void getNonBlank();
  int lex();
  
  #define LETTER 0
  #define DIGIT 1
  #define UNKNOWN 99
  
  #define INT_LIT 10
  #define IDENT 11
  #define ASSIGN_OP 20
  #define ADD_OP 21
  #define SUB_OP 22
  #define MULT_OP 23
  #define DIV_OP 24
  #define LEFT_PARENT 25
  #define RIGHT_PARENT 26
  
  int main(int argc, char *argv[])
  {
      fp = fopen(argv[1], "r");
  
      if (fopen == NULL)
          printf("File can not be opened");
      else {
          getChar();
  
          while (nextToken != EOF) {
              lex();
          }
      }
      return 0;
  }
  
  int lookup(char ch) {
      switch (ch) {
      case '(':
          addChar();
          nextToken = LEFT_PARENT;
          break;
      case ')':
          addChar();
          nextToken = RIGHT_PARENT;
          break;
      case '+':
          addChar();
          nextToken = ADD_OP;
          break;
      case '-':
         addChar();
          nextToken = SUB_OP;
          break;
      case '*':
          addChar();
          nextToken = MULT_OP;
          break;
      case '/':
          addChar();
          nextToken = DIV_OP;
          break;
      default:
          addChar();
          nextToken = EOF;
      }
      return nextToken;
  }
  
  void addChar() {
      if (lexLen &lt;= 98) {
          lexeme[lexLen++] = nextChar;
          lexeme[lexLen] = 0;
      } else
          printf("Error- lexele is too long...\n");
  }
  
  void getChar() {
      if ((nextChar = getc(fp)) != EOF) {
          if (isalpha(nextChar))
              charClass = LETTER;
          else if(isdigit(nextChar))
               charClass = DIGIT;
          else
              charClass = UNKNOWN;
      } else
          charClass =EOF;
  }
  
  void getNonBlank() {
      while (isspace(nextChar))
          getChar();
  }
  
  int lex() {
      lexLen = 0;
      getNonBlank();
      switch (charClass) {
      case LETTER:
          addChar();
          getChar();
          while (charClass == LETTER || charClass == DIGIT) {
              addChar();
              getChar();
          }
          nextToken = IDENT;
          break;
      case DIGIT:
          addChar();
          getChar();
          while (charClass == DIGIT) {
              addChar();
              getChar();
          }
          nextToken = INT_LIT;
          break;
      case UNKNOWN:
          lookup(nextChar);
          getChar();
          break;
      case EOF:
          nextToken = EOF;
          lexeme[0] = 'E';
          lexeme[1] = 'O';
          lexeme[2] = 'F';
          lexeme[3] = 0;
      }
      printf("Next token is :%d, next lexeme is %s\n", nextToken, lexeme);
      return nextToken;
  }
  </code></pre>