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

int num_questions = 0;
int num_words = 0;
int num_lines = 0;

void count_words(const char *line) {
    char *token = strtok(line, " \t\n");
    while (token != NULL) {
        num_words++;
        token = strtok(NULL, " \t\n");
    }
}

%}

%%

// Match department line
"DEpartment[ ]*[:-][ ]*[A-Za-z ]+" {
    printf("Department: %s\n", yytext);
}

// Match semester line
"SEM[ ]*:[ ]*[a-zA-Z0-9]+" {
    printf("Semester: %s\n", yytext);
}

// Match questions
"question[ ]*[0-9]+[:-][ ]*.*" {
    printf("Found Question: %s\n", yytext);
    num_questions++;
    count_words(yytext); // Count words in the question line
}

// Count lines
\n {
    num_lines++;
}

// Ignore comments and other irrelevant text
.|\n { /* ignore */ }

%%

int main() {
    // Simulated input text
    const char *input = "DEpartment:- AIML\nSEM: v\nquestion 1:- What is compiler?\nquestion 2:- What is lexical analysis?\nquestion 3:- What is token?\nquestion 4:- Define regular expression?\n";
    
    // Use fmemopen to simulate file input
    FILE *input_stream = fmemopen((void *)input, strlen(input), "r");
    if (!input_stream) {
        perror("fmemopen failed");
        return 1;
    }
    
    yyin = input_stream;
    yylex();

    fclose(input_stream);

    // Print summary statistics
    printf("Total Lines: %d\n", num_lines);
    printf("Total Words: %d\n", num_words);
    printf("Total Questions: %d\n", num_questions);

    return 0;
}
