fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6. char ch, str[20];
  7. FILE *fp = fopen("input.c", "r");
  8. if (!fp) return 1;
  9.  
  10. while ((ch = fgetc(fp)) != EOF) {
  11. if (isalpha(ch)) { // identifier or keyword
  12. int i = 0;
  13. str[i++] = ch;
  14. while (isalnum(ch = fgetc(fp))) str[i++] = ch;
  15. str[i] = '\0';
  16. fseek(fp, -1, SEEK_CUR);
  17. printf("Identifier: %s\n", str);
  18. }
  19. else if (isdigit(ch)) { // constant
  20. int i = 0;
  21. str[i++] = ch;
  22. while (isdigit(ch = fgetc(fp)) || ch == '.') str[i++] = ch;
  23. str[i] = '\0';
  24. fseek(fp, -1, SEEK_CUR);
  25. printf("Constant: %s\n", str);
  26. }
  27. else if (ch == '/' && (ch = fgetc(fp)) == '/') { // single-line comment
  28. printf("Comment: //");
  29. while ((ch = fgetc(fp)) != '\n') putchar(ch);
  30. printf("\n");
  31. }
  32. else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=')
  33. printf("Operator: %c\n", ch);
  34. }
  35.  
  36. fclose(fp);
  37. return 0;
  38. }
  39.  
Success #stdin #stdout #stderr 0.03s 6872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/W2FqDl/prog:38:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit