fork download
  1. %{
  2. #include <stdio.h>
  3. int id=0, op=0, ob=0, cb=0;
  4. %}
  5.  
  6. %%
  7. [a-zA-Z0-9]+ { id++; printf("Identifier: %s\n", yytext); }
  8. [+\-*/] { op++; }
  9. "(" { ob++; }
  10. ")" { cb++; }
  11. .|\n ;
  12. %%
  13.  
  14. int yywrap() { return 1; }
  15.  
  16. int main() {
  17. printf("Enter expression: ");
  18. yylex();
  19. // Basic validation: Brackets must match, and usually identifiers = operators + 1
  20. if(ob == cb && id == op + 1)
  21. printf("Valid expression\n");
  22. else
  23. printf("Invalid expression\n");
  24. return 0;
  25. }
Success #stdin #stdout #stderr 0.02s 6952KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/klkbTh/prog:25:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit