fork download
  1. %{
  2. #include <stdio.h> #include <string.h>
  3.  
  4. int count = 0; // Counter to keep track of the word frequency char word[100]; // The target word to search for
  5. %}
  6.  
  7. %%
  8. {word} { count++; } // Increment the counter whenever the word is found
  9. .|\n ; // Ignore other characters
  10. %%
  11.  
  12. int main(int argc, char *argv[]) {
  13.  
  14.  
  15. if (argc != 3) {
  16. printf("Usage: %s <filename> <word_to_search>\n", argv[0]); return 1;
  17. }
  18.  
  19. FILE *file = fopen(argv[1], "r"); if (!file) {
  20. printf("Error: Could not open file %s\n", argv[1]); return 1;
  21. }
  22. strcpy(word, argv[2]); // Copy the target word to search into the global 'word' variable yyin = file; // Set the input stream to the file
  23. yylex(); // Start the lexical analysis
  24.  
  25. printf("The word '%s' appears %d times in the file %s.\n", word, count, argv[1]);
  26.  
  27. fclose(file); return 0;
  28. }
  29.  
Success #stdin #stdout #stderr 0.02s 6924KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/S1YwSg/prog:28:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit