fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int num_questions = 0;
  7. int num_words = 0;
  8. int num_lines = 0;
  9.  
  10. void count_words(const char *line) {
  11. char *token = strtok(line, " \t\n");
  12. while (token != NULL) {
  13. num_words++;
  14. token = strtok(NULL, " \t\n");
  15. }
  16. }
  17.  
  18. %}
  19.  
  20. %%
  21.  
  22. // Match department line
  23. "DEpartment[ ]*[:-][ ]*[A-Za-z ]+" {
  24. printf("Department: %s\n", yytext);
  25. }
  26.  
  27. // Match semester line
  28. "SEM[ ]*:[ ]*[a-zA-Z0-9]+" {
  29. printf("Semester: %s\n", yytext);
  30. }
  31.  
  32. // Match questions
  33. "question[ ]*[0-9]+[:-][ ]*.*" {
  34. printf("Found Question: %s\n", yytext);
  35. num_questions++;
  36. count_words(yytext); // Count words in the question line
  37. }
  38.  
  39. // Count lines
  40. \n {
  41. num_lines++;
  42. }
  43.  
  44. // Ignore comments and other irrelevant text
  45. .|\n { /* ignore */ }
  46.  
  47. %%
  48.  
  49. int main() {
  50. // Simulated input text
  51. 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";
  52.  
  53. // Use fmemopen to simulate file input
  54. FILE *input_stream = fmemopen((void *)input, strlen(input), "r");
  55. if (!input_stream) {
  56. perror("fmemopen failed");
  57. return 1;
  58. }
  59.  
  60. yyin = input_stream;
  61. yylex();
  62.  
  63. fclose(input_stream);
  64.  
  65. // Print summary statistics
  66. printf("Total Lines: %d\n", num_lines);
  67. printf("Total Words: %d\n", num_words);
  68. printf("Total Questions: %d\n", num_questions);
  69.  
  70. return 0;
  71. }
  72.  
Success #stdin #stdout #stderr 0.03s 6972KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/xBaR8H/prog:71:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit