fork download
  1. /* lexical_analyzer.l */
  2. %{
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. %}
  6.  
  7. %%
  8.  
  9. "int" { printf("Keyword: int\n"); }
  10. "float" { printf("Keyword: float\n"); }
  11. "if" { printf("Keyword: if\n"); }
  12. "else" { printf("Keyword: else\n"); }
  13.  
  14. [0-9]+ { printf("Integer: %s\n", yytext); }
  15. [0-9]+\.[0-9]+ { printf("Float: %s\n", yytext); }
  16.  
  17. "=" { printf("Assignment Operator: =\n"); }
  18. "==" { printf("Equality Operator: ==\n"); }
  19. "+" { printf("Arithmetic Operator: +\n"); }
  20. "-" { printf("Arithmetic Operator: -\n"); }
  21. "*" { printf("Arithmetic Operator: *\n"); }
  22. "/" { printf("Arithmetic Operator: /\n"); }
  23.  
  24. [ \t\n\r]+ { /* Skip whitespace */ }
  25.  
  26. . { printf("Unknown character: %s\n", yytext); }
  27.  
  28. %%
  29.  
  30. int main(int argc, char **argv) {
  31. if (argc > 1) {
  32. FILE *fp = fopen(argv[1], "r");
  33. if (!fp) {
  34. perror("Error opening file");
  35. return 1;
  36. }
  37. yyin = fp;
  38. }
  39. yylex(); // Start lexical analysis
  40. return 0;
  41. }
  42.  
Success #stdin #stdout #stderr 0.02s 6948KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/9hgiIu/prog:3:1: Syntax error: Operator expected
ERROR: /home/9hgiIu/prog:41:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit