fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. char keywords[5][10] = {"int", "float", "if", "else", "return"};
  6.  
  7. int isKeyword(char *word) {
  8. for (int i = 0; i < 5; i++) {
  9. if (strcmp(word, keywords[i]) == 0)
  10. return 1;
  11. }
  12. return 0;
  13. }
  14.  
  15. int main() {
  16. char ch, buffer[50];
  17. int i = 0;
  18.  
  19. FILE *fp = fopen("input.c", "r");
  20. if (fp == NULL) {
  21. printf("Cannot open file\n");
  22. return 1;
  23. }
  24.  
  25. while ((ch = fgetc(fp)) != EOF) {
  26.  
  27. // Identifier or keyword
  28. if (isalpha(ch)) {
  29. buffer[i++] = ch;
  30. while (isalnum(ch = fgetc(fp))) {
  31. buffer[i++] = ch;
  32. }
  33. buffer[i] = '\0';
  34. i = 0;
  35. fseek(fp, -1, SEEK_CUR);
  36.  
  37. if (isKeyword(buffer))
  38. printf("Keyword: %s\n", buffer);
  39. else
  40. printf("Identifier: %s\n", buffer);
  41. }
  42.  
  43. // Number
  44. else if (isdigit(ch)) {
  45. buffer[i++] = ch;
  46. while (isdigit(ch = fgetc(fp))) {
  47. buffer[i++] = ch;
  48. }
  49. buffer[i] = '\0';
  50. i = 0;
  51. fseek(fp, -1, SEEK_CUR);
  52.  
  53. printf("Number: %s\n", buffer);
  54. }
  55.  
  56. // Operators
  57. else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=') {
  58. printf("Operator: %c\n", ch);
  59. }
  60.  
  61. // Special characters
  62. else if (ch == ';' || ch == ',' || ch == '(' || ch == ')' ||
  63. ch == '{' || ch == '}') {
  64. printf("Special Symbol: %c\n", ch);
  65. }
  66. }
  67.  
  68. fclose(fp);
  69. return 0;
  70. }
  71.  
Success #stdin #stdout #stderr 0.03s 6780KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/35I34E/prog:70:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit