3cse080ex3
3cse080ex3
h>
#include <ctype.h>
#include <string.h>
// Token types
typedef enum {
TOKEN_IDENTIFIER,
TOKEN_NUMBER,
TOKEN_KEYWORD,
TOKEN_OPERATOR,
TOKEN_UNKNOWN,
TOKEN_EOF
} TokenType;
// Keywords list
const char *keywords[] = { "int", "float", "if", "else", "while", "return" };
const int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
// Skip whitespaces
while ((c = fgetc(input)) != EOF && isspace(c));
if (c == EOF) {
return TOKEN_EOF;
}
// Handle numbers
if (isdigit(c)) {
token[pos++] = c;
while ((c = fgetc(input)) != EOF && isdigit(c)) {
token[pos++] = c;
}
token[pos] = '\0';
if (c != EOF) ungetc(c, input);
return TOKEN_NUMBER;
}
// Unknown characters
token[0] = c;
token[1] = '\0';
return TOKEN_UNKNOWN;
}
return 0;
}