fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char lexeme[100];
  7. char type[20];
  8. } Token;
  9.  
  10. const char* keywords[] = {"int", "float", "if", "else", "while", "return", "void", "char", "for", "double"};
  11. const int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
  12.  
  13. int isKeyword(const char* token) {
  14. for (int i = 0; i < num_keywords; i++) {
  15. if (strcmp(token, keywords[i]) == 0) return 1;
  16. }
  17. return 0;
  18. }
  19.  
  20. Token classifyToken(const char *token) {
  21. Token t;
  22. strcpy(t.lexeme, token);
  23.  
  24. if (isKeyword(token))
  25. strcpy(t.type, "KEYWORD");
  26. else if (isdigit(token[0]))
  27. strcpy(t.type, "NUMBER");
  28. else if (isalpha(token[0]) || token[0] == '_')
  29. strcpy(t.type, "IDENTIFIER");
  30. else if (strchr("+-*/=<>!", token[0]))
  31. strcpy(t.type, "OPERATOR");
  32. else if (strchr(";(),{}", token[0]))
  33. strcpy(t.type, "DELIMITER");
  34. else
  35. strcpy(t.type, "UNKNOWN");
  36.  
  37. return t;
  38. }
  39.  
  40. void lexicalAnalyzer(const char *input) {
  41. char token[100];
  42. int index = 0;
  43.  
  44. for (int i = 0; i < strlen(input); i++) {
  45. char ch = input[i];
  46.  
  47. if (isspace(ch)) {
  48. if (index > 0) {
  49. token[index] = '\0';
  50. Token t = classifyToken(token);
  51. printf("%s: %s\n", t.type, t.lexeme);
  52. index = 0;
  53. }
  54. }
  55. // Multi-character operators check
  56. else if ((ch == '+' && input[i+1] == '=') ||
  57. (ch == '-' && input[i+1] == '=') ||
  58. (ch == '*' && input[i+1] == '=') ||
  59. (ch == '/' && input[i+1] == '=') ||
  60. (ch == '=' && input[i+1] == '=') ||
  61. (ch == '!' && input[i+1] == '=') ||
  62. (ch == '>' && input[i+1] == '=') ||
  63. (ch == '<' && input[i+1] == '=')) {
  64. token[0] = ch;
  65. token[1] = input[i+1];
  66. token[2] = '\0';
  67. Token t = classifyToken(token);
  68. printf("%s: %s\n", t.type, t.lexeme);
  69. i++; // skip next char
  70. }
  71. // Single-character operators or delimiters
  72. else if (strchr("+-*/=<>!;(),{}", ch)) {
  73. if (index > 0) {
  74. token[index] = '\0';
  75. Token t = classifyToken(token);
  76. printf("%s: %s\n", t.type, t.lexeme);
  77. index = 0;
  78. }
  79. token[0] = ch;
  80. token[1] = '\0';
  81. Token t = classifyToken(token);
  82. printf("%s: %s\n", t.type, t.lexeme);
  83. }
  84. else {
  85. token[index++] = ch;
  86. }
  87. }
  88.  
  89. if (index > 0) {
  90. token[index] = '\0';
  91. Token t = classifyToken(token);
  92. printf("%s: %s\n", t.type, t.lexeme);
  93. }
  94. }
  95.  
  96. int main() {
  97. char input[200];
  98. printf("Enter a simple C code snippet: ");
  99. fgets(input, sizeof(input), stdin);
  100.  
  101. printf("\nTokens Found:\n");
  102. lexicalAnalyzer(input);
  103.  
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0s 5316KB
stdin
+= -= /= *= == >= <= !=
stdout
Enter a simple C code snippet: 
Tokens Found:
OPERATOR: +=
OPERATOR: -=
OPERATOR: /=
OPERATOR: *=
OPERATOR: ==
OPERATOR: >=
OPERATOR: <=
OPERATOR: !=