fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAXWORDS 1000
  6. #define MAXLEN 50
  7.  
  8. char words[MAXWORDS][MAXLEN];
  9. int count[MAXWORDS];
  10. int nwords = 0;
  11.  
  12. void addword(char *w) {
  13. for (int i = 0; i < nwords; i++) {
  14. if (strcmp(words[i], w) == 0) {
  15. count[i]++;
  16. return;
  17. }
  18. }
  19. strcpy(words[nwords], w);
  20. count[nwords++] = 1;
  21. }
  22. %}
  23.  
  24. %%
  25. [a-zA-Z]+ { addword(yytext); }
  26. [ \t\n]+ { /* ignore whitespace */ }
  27. . { /* ignore other chars */ }
  28. %%
  29.  
  30. int main() {
  31. yylex();
  32. printf("\n--- Histogram of Words ---\n");
  33. for (int i = 0; i < nwords; i++) {
  34. printf("%s : ", words[i]);
  35. for (int j = 0; j < count[i]; j++) printf("*");
  36. printf(" (%d)\n", count[i]);
  37. }
  38. return 0;
  39. }
  40.  
  41. int yywrap() { return 1; }
  42.  
Success #stdin #stdout #stderr 0.03s 6924KB
stdin
Hello world hello test test test hello
stdout
Standard output is empty
stderr
ERROR: /home/wZNYDQ/prog:2:1: Syntax error: Operator expected
ERROR: /home/wZNYDQ/prog:41:26: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit