%{
#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 ;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKaW50IG51bV9xdWVzdGlvbnMgPSAwOwppbnQgbnVtX3dvcmRzID0gMDsKaW50IG51bV9saW5lcyA9IDA7Cgp2b2lkIGNvdW50X3dvcmRzKGNvbnN0IGNoYXIgKmxpbmUpIHsKICAgIGNoYXIgKnRva2VuID0gc3RydG9rKGxpbmUsICIgXHRcbiIpOwogICAgd2hpbGUgKHRva2VuICE9IE5VTEwpIHsKIC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