#include char nextChar; /* next character read from input */ int charClass; /* character classes: */ #define LETTER 0 /* [a-zA-Z] */ #define DIGIT 1 /* [0-9] */ #define UNKNOWN 2 /* .* */ #define SLASH 3 #define STAR 4 #define HYPHEN 5 /* also EOF from stdio.h */ /* tokens: */ #define GENERIC_TOKEN 1 #define INTEGER_LITERAL 2 #define UNKNOWN_TOKEN 3 #define INT_DATA_TYPE_TOKEN 4 #define SLASH_OPERATOR 5 #define COMMENT 6 #define MINUS_OPERATOR 7 #define MAXLENGTH 80 char lexeme[MAXLENGTH]; int lexemeLength; /* getChar() function */ void getChar() { /* get next character from stdin */ nextChar = getc(stdin); /* determine the character class */ if (nextChar == EOF) charClass = EOF; else if (nextChar == '/') charClass = SLASH; else if (nextChar == '*') charClass = STAR; else if (nextChar == '-') charClass = HYPHEN; else if (isalpha(nextChar)) charClass = LETTER; else if (isdigit(nextChar)) charClass = DIGIT; else charClass = UNKNOWN; } /* addChar() function */ void addChar() { if (lexemeLength < MAXLENGTH) { lexeme[lexemeLength++] = nextChar; lexeme[lexemeLength] = '\0'; } else fprintf(stderr, "ERROR: Lexeme is too long\n"); } /* lookup() function */ int lookup(char *lexeme) { if ( strcmp( lexeme, "int" ) == 0 ) return INT_DATA_TYPE_TOKEN; else return GENERIC_TOKEN; } int lex() { lexemeLength = 0; static int first = 1; if (first) { getChar(); first = 0; } /* skip whitespace */ /* while (isspace(nextChar)) getChar(); */ switch (charClass) { case LETTER: addChar(); getChar(); while ( charClass == LETTER || charClass == DIGIT ) { addChar(); getChar(); } return lookup(lexeme); case HYPHEN: case DIGIT: { int isNegative = (charClass == HYPHEN); addChar(); getChar(); if (isNegative && charClass != DIGIT) return MINUS_OPERATOR; while (charClass == DIGIT) { addChar(); getChar(); } return INTEGER_LITERAL; } case SLASH: addChar(); getChar(); if (charClass == STAR) { int comment_done = 0; while (!comment_done) { addChar(); getChar(); while (charClass == STAR) { addChar(); getChar(); if (charClass == SLASH) { addChar(); getChar(); comment_done = 1; } } } return COMMENT; } else return SLASH_OPERATOR; case UNKNOWN: case STAR: addChar(); getChar(); return UNKNOWN_TOKEN; case EOF: return EOF; } } main() { int x; int y = -5; int q = 5-y; int r = q-5; int s = q - -5; do { x = lex(); if (x != COMMENT) { if (x == INTEGER_LITERAL) printf("[INTEGER_LITERAL] "); else if (x == MINUS_OPERATOR) printf("[MINUS_OPERATOR] "); printf("%s", lexeme); } } while (x != EOF); }