fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /* Variables to store counts */
  5. int char_count = 0;
  6. int word_count = 0;
  7. %}
  8. %%
  9. [a-zA-Z]+ {word_count++; char_count += yyleng;} /* Match words (sequence of alphabetic characters) and count them */
  10. . {char_count++;} /* Match any single character (including spaces, punctuation) and count them */
  11. %%
  12. int main(){
  13. printf("Enter input text (Ctrl+D to end):\n");
  14. yylex();
  15. printf("\nTotal characters: %d\n", char_count);
  16. printf("Total words: %d\n", word_count);
  17. return 0;
  18. }
  19. int yywrap() {
  20. return 1;
  21. }
  22.  
Success #stdin #stdout #stderr 0.02s 6988KB
stdin
Enter Text
stdout
Standard output is empty
stderr
ERROR: /home/JOQuh5/prog:2:5: Syntax error: Operator expected
ERROR: /home/JOQuh5/prog:21:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit