#include /* global variables... * ...could be avoided */ char nextChar; /* next character read from input stream */ int charClass; /* character class of nextChar */ /* character classes: */ #define LETTER 0 /* [a-zA-Z] */ #define DIGIT 1 /* [0-9] */ #define UNKNOWN 2 /* .* */ #define SLASH 3 #define STAR 4 /* 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 MAXLENGTH 100 char lexeme[MAXLENGTH]; int lexemeLength; /* getChar() function gets the next character and * determines its character class */ void getChar() { /*** Fill in code here to get the next character *** from input in global variable nextChar ***/ nextChar = getc(stdin); /* Determine what the next character is... */ if (nextChar == EOF) charClass = EOF; else if (nextChar == '/') charClass = SLASH; else if (nextChar == '*') charClass = STAR; else if (isalpha(nextChar)) charClass = LETTER; else if (isdigit(nextChar)) charClass = DIGIT; else charClass = UNKNOWN; } /* addChar() function adds nextChar to lexeme */ void addChar() { if (lexemeLength < MAXLENGTH) { lexeme[lexemeLength++] = nextChar; lexeme[lexemeLength] = '\0'; } else fprintf(stderr, "ERROR: Lexeme is too long\n"); } /* lookup() function returns token for keywords, * identifiers, etc. */ int lookup(char *lexeme) { if ( strcmp( lexeme, "int" ) == 0 ) return INT_DATA_TYPE_TOKEN; else return GENERIC_TOKEN; } /* lex -- a simple lexical analyzer in C */ int lex() { lexemeLength = 0; static int first = 1; if (first) { /* if we're just starting up, */ getChar(); /* get the first character from */ first = 0; /* the input stream */ } /* Keep reading input until a non-whitespace char */ /* while (isspace(nextChar)) getChar(); */ /* lex() function continued... */ switch (charClass) { case LETTER: addChar(); getChar(); while (charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } return lookup(lexeme); case DIGIT: addChar(); getChar(); 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; do { x = lex(); if (x != COMMENT) printf("%s", lexeme); } while (x != EOF); }