fork download
  1. %{
  2. #include <stdio.h>
  3. #include <ctype.h>
  4.  
  5. int valid = 1; // flag to check validity
  6. %}
  7.  
  8. %%
  9. [0-9]+ ; // valid number
  10. [ \t\n]+ ; // ignore spaces, tabs, newlines
  11. [\+\-\*/\(\)] ; // valid operators and parentheses
  12. . { valid = 0; } // invalid character
  13. %%
  14.  
  15. int main() {
  16. printf("Enter an arithmetic expression: ");
  17. yylex();
  18.  
  19. if (valid)
  20. printf("✅ Valid arithmetic expression\n");
  21. else
  22. printf("❌ Invalid arithmetic expression\n");
  23.  
  24. return 0;
  25. }
  26.  
  27. int yywrap() {
  28. return 1;
  29. }
Success #stdin #stdout #stderr 0.03s 6912KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/qYy2eI/prog:2:1: Syntax error: Operator expected
ERROR: /home/qYy2eI/prog:29:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit