fork download
  1. %{
  2. #include <stdio.h>
  3. %}
  4.  
  5. digit [0-9]
  6. letter [a-zA-Z]
  7. id {letter}({letter}|{digit})*
  8. numbers {digit}+(\.{digit}*)?(E[+-]?{digit}+)?
  9. %%
  10.  
  11. {id} { printf("%s is an identifier\n", yytext); }
  12. if|else|printf|#include|<stdio.h>|main|int|float|char|return { printf("%s is a keyword\n", yytext); }
  13. "<"|">"|"<="|">="|"=="|!=" { printf("%s is a relational operator\n", yytext); }
  14. {numbers} { printf("%s is a number\n", yytext); }
  15. "="|"+|"-"|"*"|"/"|"%" { printf("%s is an operator\n", yytext); }
  16. "("|")"|"{"|"}" { printf("%s is a separator\n", yytext); }
  17.  
  18. %%
  19.  
  20. int main() {
  21. FILE *file = fopen("program.c", "r");
  22. if (!file) {
  23. printf("Error: cannot open file program.c\n");
  24. return 1;
  25. }
  26. yyin = file;
  27. yylex();
  28. fclose(file);
  29. return 0;
  30. }
  31.  
  32. int yywrap() { return 1; }
  33.  
Success #stdin #stdout #stderr 0.02s 6880KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/WI8aus/prog:32:26: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit