/* * LEXER FOR THE * ===== === === * * DATA DECLARATION BUILDER * ==== =========== ======= * * This module is the lexer used by the parser to * interprete the high level data declarations. * ******* Tokens: * * tERROR tEOF tSEMI tID tEQ tOR tOPENBR tCOMMA tCLOSEBR * tSTR tNUM tEXPORT tGLOBAL tBEGIN tOPENCURLY tCLOSECURLY tTYPE */ #include #include #include "struct.h" #include "lexer.h" #ifdef DEBUGGING static char *tokenname[] = { "tERROR", "tEOF", "tSEMI", "tID", "tEQ", "tOR", "tOPENBR", "tCOMMA", "tCLOSEBR", "tSTR", "tNUM", "tEXPORT", "tGLOBAL", "tBEGIN", "tOPENCURLY", "tCLOSECURLY", "tTYPE" }; #endif /* ----------------- Exported variables ---------------- */ #define MAXIDSIZE 100 int lineno = 1; char lexidval[ MAXIDSIZE ]; int lexintval; FILE *lexfile; /* ----------------- Private variables ---------------- */ static BOOL havepushedtok = FALSE; static TOKEN curtok; static char curid[ MAXIDSIZE ]; static int curint; /* ----------------- Private procedures ---------------- */ static void white_space() { int c; for(;;) { c = getc(lexfile); if( c == '/' || c == '#' ) /* comment to end of line */ { while( (c=getc(lexfile)) != EOF && c != '\n' ); } if( c == EOF ) break; if( c == '\n' ) lineno++; if( c != ' ' && c != '\t' && c != '\n' ) break; } ungetc( c, lexfile ); } /* ----------------- Public procedures ---------------- */ void ungettok() { if( havepushedtok ) { fprintf( stderr, "ungettok: can't push 2 tokens\n" ); exit(1); } havepushedtok = TRUE; #ifdef DEBUGGING printf( "lexer: ungot token %s\n", tokenname[ curtok ] ); #endif } TOKEN nexttok() { int c; int pos; if( havepushedtok ) { havepushedtok = FALSE; } else { white_space(); c = getc(lexfile); switch( c ) { case EOF: curtok = tEOF; break; case ';': curtok = tSEMI; break; case '{': curtok = tOPENCURLY; break; case '}': curtok = tCLOSECURLY; break; case '(': curtok = tOPENBR; break; case ')': curtok = tCLOSEBR; break; case ',': curtok = tCOMMA; break; case '|': curtok = tOR; break; case '=': curtok = tEQ; break; case '"': for( pos=0; (c=getc(lexfile)) != '\"'; ) { if( pos