/********** lexer.c *************/

#include "global.h"

char lexbuf[BSIZE];
int  lineno=1;
int tokenval = NONE;
extern char lexemes[];
int lexan(void)   /*analyseur lexical*/
{

 int t;
 while(1) {
    t = getchar();
    if (t == ' ' || t == '\t')
        ; /*effacer les blancs*/
    else if (t == '\n')
      lineno = lineno + 1;
    else if (isdigit(t)) {  /* t est un chiffre*/
      ungetc(t, stdin);
      scanf("%d", &tokenval);
      return NUM;
    }
    else if (isalpha(t)) { /*t est une lettre*/
      int p, b =0;
      while (isalnum(t)){ /*t est alphanum. */
	lexbuf[b] = t;
         t = getchar();
          b = b+1;
         if (b >= BSIZE)
            error("erreur de compilation");
       }
       lexbuf[b] = EOS;
       if (t!= EOF)
           ungetc(t, stdin);
       p = lookup(lexbuf);
        if (p == 0)
            p=insert(lexbuf, ID);
        tokenval = p;
	return symtable[p].token;
    }
    else if (t == EOF)
      return DONE;
    else {
       tokenval = NONE;
       return t;
    }
  }
}
