fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int i = 0; // Word count
  6. %}
  7.  
  8. /* Rules Section */
  9. %%
  10. [a-zA-Z0-9]+ { i++; } // Increment word count for each sequence of alphanumeric characters
  11.  
  12. \n { // On a newline, print the word count and reset
  13. printf("%d\n", i);
  14. i = 0;
  15. }
  16.  
  17. .|\n { /* Match any other character or newline (optional) */
  18. /* This is to consume characters that are not words. */
  19. }
  20.  
  21. /* Additional rule to catch end of input */
  22. %%
  23.  
  24. int yywrap(void) {
  25. return 1; // Indicate end of input
  26. }
  27.  
  28. int main() {
  29. // The function that starts the analysis
  30. yylex();
  31. // Print final count if the last line did not end with a newline
  32. if (i > 0) {
  33. printf("%d\n", i);
  34. }
  35. return 0;
  36. }
  37.  
Success #stdin #stdout #stderr 0.02s 6972KB
stdin
Automata and compiler theory lab
stdout
Standard output is empty
stderr
ERROR: /home/3hBjVj/prog:36:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit