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. if((fp=fopen(srgv[1],"r"))==NULL)
  28. {printf("file does not exist");}
  29. yyin=fp;
  30. yylex();
  31. return 0;
  32. }
Success #stdin #stdout #stderr 0.02s 6932KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ocighU/prog:3:9: Syntax error: Operator expected
ERROR: /home/ocighU/prog:32:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit