fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int keyword_count = 0, id_count = 0, num_count = 0;
  6.  
  7. char *keywords[] = {
  8. "int", "float", "char", "double", "while", "for", "if", "else", "return", "void"
  9. };
  10. int is_keyword(char *word) {
  11. for (int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
  12. if (strcmp(keywords[i], word) == 0)
  13. return 1;
  14. }
  15. return 0;
  16. }
  17. %}
  18.  
  19. IDENTIFIER [a-zA-Z_][a-zA-Z0-9_]*
  20. NUMBER [0-9]+(\.[0-9]+)?
  21.  
  22. %%
  23.  
  24. {IDENTIFIER} {
  25. if (is_keyword(yytext)) {
  26. printf("Keyword: %s\n", yytext);
  27. keyword_count++;
  28. } else {
  29. printf("Identifier: %s\n", yytext);
  30. id_count++;
  31. }
  32. }
  33.  
  34. {NUMBER} {
  35. printf("Number: %s\n", yytext);
  36. num_count++;
  37. }
  38.  
  39. [ \t\n]+ ; // Ignore whitespace
  40.  
  41. . ; // Ignore everything else
  42.  
  43. %%
  44.  
  45. int main() {
  46. printf("Enter your C-like code (press Ctrl+D to finish input):\n\n");
  47. yylex();
  48.  
  49. printf("\n--- Summary ---\n");
  50. printf("Keywords: %d\n", keyword_count);
  51. printf("Identifiers: %d\n", id_count);
  52. printf("Numbers: %d\n", num_count);
  53. return 0;
  54. }
  55.  
  56. int yywrap() {
  57. return 1;
  58. }
  59.  
Success #stdin #stdout #stderr 0.04s 6888KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Xr3ptP/prog:2:1: Syntax error: Operator expected
ERROR: /home/Xr3ptP/prog:58:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit