fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. int token_count = 0; // Variable to hold the token count
  5. %}
  6.  
  7. %%
  8.  
  9. [ \t\n]+ { /* Ignore whitespace (spaces, tabs, and newlines) */ }
  10. [0-9]+ { token_count++; } // Match numbers
  11. [A-Za-z_][A-Za-z0-9_]* { token_count++; } // Match identifiers (words)
  12. [+\-*/=<>] { token_count++; } // Match operators (+, -, *, /, =, <, >)
  13. . { token_count++; } // Match any other single character (like punctuation)
  14.  
  15. %%
  16.  
  17. int main()
  18. {
  19. yylex(); // Start the lexical analysis
  20. printf("Total number of tokens: %d\n", token_count); // Print the token count
  21. return 0;
  22. }
  23.  
Success #stdin #stdout #stderr 0.03s 6972KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/oe3C9G/prog:2:1: Syntax error: Operator expected
ERROR: /home/oe3C9G/prog:22:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit