fork download
  1. %option noyywrap
  2. %{
  3. #include<stdio.h>
  4. void yyerror(char *);
  5. %}
  6. letter [a-zA-Z]
  7. digit [0-9]
  8. op [-+*/]
  9. punct [,.;"]
  10. %%
  11. else|if|void|int {printf("%s is a keyword",yytext);}
  12. {digit}+ {printf("%s is a number",yytext);}
  13. {letter}({letter}|{digit})* {printf("%s is an identifier",yytext);}
  14. {op} {printf("%s is an operator",yytext);}
  15. [ ] ;
  16. \) {printf("%s is close parenthesis",yytext);}
  17. {punct} {printf("%s is a punctuation",yytext);}
  18. . yyerror("error");
  19. %%
  20. void yyerror(char *s)
  21. {
  22. fprintf(stderr,"%s\n",s);
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. FILE *fp;
  27.  
  28. if((fp=fopen(argv[1],"r"))==NULL)
  29. {printf("file does not exist");}
  30. yyin=fp;
  31. yylex();
  32. return 0;
  33. }
Success #stdin #stdout #stderr 0.02s 6928KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/3fDDfJ/prog:3:9: Syntax error: Operator expected
ERROR: /home/3fDDfJ/prog:33:16: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit