fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define MAX_WORDS 1000
  5. #define WORD_LEN 50
  6.  
  7. char words[MAX_WORDS][WORD_LEN];
  8. int counts[MAX_WORDS];
  9. int wordIndex = 0;
  10.  
  11. int search_word(const char *word) {
  12. for (int i = 0; i < wordIndex; ++i)
  13. if (strcmp(words[i], word) == 0)
  14. return i;
  15. return -1;
  16. }
  17. %}
  18.  
  19. %%
  20. [ \n\t]+ ;
  21. [a-zA-Z0-9_]+ {
  22. int idx = search_word(yytext);
  23. if (idx >= 0)
  24. counts[idx]++;
  25. else {
  26. strcpy(words[wordIndex], yytext);
  27. counts[wordIndex++] = 1;
  28. }
  29. }
  30. . ;
  31. %%
  32.  
  33. int main() {
  34. yylex();
  35. printf("\nHistogram of words:\n");
  36. for (int i = 0; i < wordIndex; ++i)
  37. printf("%s: %d\n", words[i], counts[i]);
  38. return 0;
  39. }
  40.  
Success #stdin #stdout #stderr 0.03s 6800KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/iassOk/prog:2:1: Syntax error: Operator expected
ERROR: /home/iassOk/prog:39:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit