%{
#include <stdio.h> #include <string.h>

int count = 0; // Counter to keep track of the word frequency char word[100]; // The target word to search for
%}

%%
{word} { count++; } // Increment the counter whenever the word is found
.|\n ;	// Ignore other characters
%%

int main(int argc, char *argv[]) {
 

if (argc != 3) {
printf("Usage: %s <filename> <word_to_search>\n", argv[0]); return 1;
}

FILE *file = fopen(argv[1], "r"); if (!file) {
printf("Error: Could not open file %s\n", argv[1]); return 1;
}
strcpy(word, argv[2]); // Copy the target word to search into the global 'word' variable yyin = file; // Set the input stream to the file
yylex();	// Start the lexical analysis

printf("The word '%s' appears %d times in the file %s.\n", word, count, argv[1]);

fclose(file); return 0;
}
