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

int blank_spaces = 0;  // Counter for blank spaces
int word_count = 0;    // Counter for words
int line_count = 0;    // Counter for lines
%}

%%

\n             { line_count++; }                 // Increment line count on encountering newline
[ \t]+         { blank_spaces++; }               // Increment blank spaces count for spaces and tabs
[A-Za-z0-9]+   { word_count++; }                 // Increment word count for words (alphanumeric)
.              { }                                // Ignore all other characters

%%

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s <filename>\n", argv[0]);
        return 1;
    }

    FILE *file = fopen(argv[1], "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    yyin = file;  // Set the input file for lex to read from
    yylex();      // Call the lexer to start scanning
    
    printf("Lines: %d\n", line_count);
    printf("Words: %d\n", word_count);
    printf("Blank Spaces: %d\n", blank_spaces);

    fclose(file);
    return 0;
}
