fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int keyword_count = 0;
  6.  
  7. // List of C keywords
  8. char *keywords[] = {
  9. "auto", "break", "case", "char", "const", "continue", "default",
  10. "do", "double", "else", "enum", "extern", "float", "for", "goto",
  11. "if", "int", "long", "register", "return", "short", "signed",
  12. "sizeof", "static", "struct", "switch", "typedef", "union",
  13. "unsigned", "void", "volatile", "while"
  14. };
  15.  
  16. int is_keyword(char *word) {
  17. for (int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
  18. if (strcmp(word, keywords[i]) == 0) {
  19. return 1;
  20. }
  21. }
  22. return 0;
  23. }
  24. %}
  25.  
  26. %%
  27.  
  28. [a-zA-Z_][a-zA-Z0-9_]* {
  29. if (is_keyword(yytext)) {
  30. keyword_count++;
  31. }
  32. }
  33.  
  34. . ; // Ignore all other characters
  35.  
  36. %%
  37.  
  38. int yywrap() {
  39. return 1;
  40. }
  41.  
  42. int main(int argc, char *argv[]) {
  43. if (argc > 1) {
  44. FILE *file = fopen(argv[1], "r");
  45. if (!file) {
  46. printf("Could not open file %s\n", argv[1]);
  47. return 1;
  48. }
  49. yyin = file;
  50. }
  51.  
  52. yylex();
  53. printf("Total number of C keywords: %d\n", keyword_count);
  54. return 0;
  55. }
  56.  
  57.  
Success #stdin #stdout #stderr 0.03s 6780KB
stdin
int main() {
    int a = 10;
    float b = 20.5;
    if (a < b) {
        return 0;
    }
}

stdout
Standard output is empty
stderr
ERROR: /home/TAm4Ee/prog:2:1: Syntax error: Operator expected
ERROR: /home/TAm4Ee/prog:56:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit