This solution categorizes all tokens from a file into identifiers, operators, numbers, and special characters.
code.txtint x = 10;
float y = 3.14;
x++; y += 2.5;
result = x * y;
report.txt===== TOKEN ANALYSIS REPORT =====
IDENTIFIERS (6 unique, 7 total):
float 1
int 1
result 1
x 2
y 2
OPERATORS (5 unique, 6 total):
* 1
++ 1
+= 1
= 3
NUMBERS (3 unique, 3 total):
10 1
2.5 1
3.14 1
SPECIAL CHARACTERS (1 unique, 4 total):
; 4
TOTAL TOKENS: 20
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct Token {
char value[50];
int count;
};
int isIdentifierStart(char c) {
return isalpha(c) || c == '_';
}
int isIdentifierChar(char c) {
return isalnum(c) || c == '_';
}
int isOperatorChar(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' ||
c == '%' || c == '=' || c == '&' || c == '|');
}
int isSpecialChar(char c) {
return (c == '.' || c == ',' || c == ';' || c == ':');
}
Searches for a token in the array and returns its index, or -1 if not found.
int findToken(struct Token* tokens, int count, char* value) {
for (int i = 0; i < count; i++) {
if (strcmp(tokens[i].value, value) == 0) {
return i;
}
}
return -1;
}
If token exists, increment count; otherwise, add new token.