fork download
  1. %{
  2. #include <stdio.h>
  3. #include <ctype.h>
  4.  
  5. int blank_spaces = 0; // Counter for blank spaces
  6. int word_count = 0; // Counter for words
  7. int line_count = 0; // Counter for lines
  8. %}
  9.  
  10. %%
  11.  
  12. \n { line_count++; } // Increment line count on encountering newline
  13. [ \t]+ { blank_spaces++; } // Increment blank spaces count for spaces and tabs
  14. [A-Za-z0-9]+ { word_count++; } // Increment word count for words (alphanumeric)
  15. . { } // Ignore all other characters
  16.  
  17. %%
  18.  
  19. int main(int argc, char *argv[]) {
  20. if (argc < 2) {
  21. printf("Usage: %s <filename>\n", argv[0]);
  22. return 1;
  23. }
  24.  
  25. FILE *file = fopen(argv[1], "r");
  26. if (file == NULL) {
  27. perror("Error opening file");
  28. return 1;
  29. }
  30.  
  31. yyin = file; // Set the input file for lex to read from
  32. yylex(); // Call the lexer to start scanning
  33.  
  34. printf("Lines: %d\n", line_count);
  35. printf("Words: %d\n", word_count);
  36. printf("Blank Spaces: %d\n", blank_spaces);
  37.  
  38. fclose(file);
  39. return 0;
  40. }
  41.  
Success #stdin #stdout #stderr 0.02s 6924KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/XuZxix/prog:2:1: Syntax error: Operator expected
ERROR: /home/XuZxix/prog:40:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit