fork(1) download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct
  7. {
  8. char lexeme[100];
  9. char type[20];
  10. } Token;
  11.  
  12.  
  13. const char* keywords[] = {"int","float","if","else","while","return","void","char","for","double"};
  14. const int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
  15.  
  16.  
  17. int isKeyword(const char* token)
  18. {
  19. for (int i = 0; i < num_keywords; i++)
  20. {
  21. if (strcmp(token, keywords[i]) == 0)
  22. return 1;
  23. }
  24. return 0;
  25. }
  26.  
  27.  
  28. Token classifyToken(const char *token)
  29. {
  30. Token t;
  31. strcpy(t.lexeme, token);
  32.  
  33. if (isKeyword(token))
  34. strcpy(t.type, "KEYWORD");
  35.  
  36. else if (isdigit(token[0]))
  37. strcpy(t.type, "NUMBER");
  38.  
  39. else if (isalpha(token[0]) || token[0] == '_')
  40. strcpy(t.type, "IDENTIFIER");
  41.  
  42. else if (strchr("+-*/=<>!", token[0]))
  43. strcpy(t.type, "OPERATOR");
  44.  
  45. else if (strchr(";(),{}", token[0]))
  46. strcpy(t.type, "DELIMITER");
  47.  
  48. else
  49. strcpy(t.type, "UNKNOWN");
  50.  
  51. return t;
  52. }
  53.  
  54.  
  55. void lexicalAnalyzer(const char *input)
  56. {
  57. char token[100];
  58. int index = 0;
  59.  
  60. for (int i = 0; i < strlen(input); i++)
  61. {
  62. char ch = input[i];
  63.  
  64.  
  65. if (isspace(ch))
  66. {
  67. if (index > 0)
  68. {
  69. token[index] = '\0';
  70. Token t = classifyToken(token);
  71. printf("%s: %s\n", t.type, t.lexeme);
  72. index = 0;
  73. }
  74. }
  75.  
  76.  
  77. else if (strchr("+-*/=<>!", ch))
  78. {
  79. if (index > 0)
  80. {
  81. token[index] = '\0';
  82. Token t = classifyToken(token);
  83. printf("%s: %s\n", t.type, t.lexeme);
  84. index = 0;
  85. }
  86.  
  87.  
  88. if (input[i+1] == '=')
  89. {
  90. token[0] = ch;
  91. token[1] = '=';
  92. token[2] = '\0';
  93.  
  94. Token t = classifyToken(token);
  95. printf("%s: %s\n", t.type, t.lexeme);
  96.  
  97. i++;
  98. }
  99. else
  100. {
  101. token[0] = ch;
  102. token[1] = '\0';
  103.  
  104. Token t = classifyToken(token);
  105. printf("%s: %s\n", t.type, t.lexeme);
  106. }
  107. }
  108.  
  109.  
  110. else if (strchr(";(),{}", ch))
  111. {
  112. if (index > 0)
  113. {
  114. token[index] = '\0';
  115. Token t = classifyToken(token);
  116. printf("%s: %s\n", t.type, t.lexeme);
  117. index = 0;
  118. }
  119.  
  120. token[0] = ch;
  121. token[1] = '\0';
  122.  
  123. Token t = classifyToken(token);
  124. printf("%s: %s\n", t.type, t.lexeme);
  125. }
  126.  
  127.  
  128. else
  129. {
  130. token[index++] = ch;
  131. }
  132. }
  133.  
  134.  
  135. if (index > 0)
  136. {
  137. token[index] = '\0';
  138. Token t = classifyToken(token);
  139. printf("%s: %s\n", t.type, t.lexeme);
  140. }
  141. }
  142.  
  143. int main()
  144. {
  145. char input[200];
  146.  
  147. printf("Enter a simple C code snippet: ");
  148. fgets(input, sizeof(input), stdin);
  149.  
  150. printf("\nTokens Found:\n");
  151. lexicalAnalyzer(input);
  152.  
  153. return 0;
  154. }
Success #stdin #stdout 0s 5308KB
stdin
int a +=b
stdout
Enter a simple C code snippet: 
Tokens Found:
KEYWORD: int
IDENTIFIER: a
OPERATOR: +=
IDENTIFIER: b