fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Declare functions
  6. int yylex(void);
  7. void yyerror(const char *s);
  8. %}
  9.  
  10. %option noyywrap // Disable the default wrapping behavior for LEX
  11.  
  12. // Definitions section
  13. DIGIT [0-9]
  14. LETTER [a-zA-Z]
  15. NUMBER {DIGIT}+ // Matches any integer number
  16.  
  17. // Rules section
  18. %%
  19. "+" { printf("Operator: +\n"); }
  20. "-" { printf("Operator: -\n"); }
  21. "*" { printf("Operator: *\n"); }
  22. "/" { printf("Operator: /\n"); }
  23. "(" { printf("Parenthesis: (\n"); }
  24. ")" { printf("Parenthesis: )\n"); }
  25.  
  26. {NUMBER} { printf("Number: %s\n", yytext); }
  27.  
  28. [ \t\n]+ { /* Skip whitespace */ }
  29.  
  30. . { printf("Unknown: %s\n", yytext); }
  31.  
  32. %%
  33.  
  34. // User Code section
  35. int main() {
  36. yylex(); // Start lexical analysis
  37. return 0;
  38. }
  39.  
  40. void yyerror(const char *s) {
  41. fprintf(stderr, "Error: %s\n", s);
  42. }
Success #stdin #stdout #stderr 0.02s 6924KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/VjbFu1/prog:2:1: Syntax error: Operator expected
ERROR: /home/VjbFu1/prog:42:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit