fork download
  1. lex
  2. %{
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #define TOKEN_ID 1
  6. #define TOKEN_NUMBER 2
  7. #define TOKEN_OPERATOR 3
  8. #define TOKEN_KEYWORD 4
  9. %}
  10. id[a-zA-Z][a-zA-Z0-9]*number[0-9]+operator[+*/-]
  11. keyword(if|else|while|for)
  12. %%
  13. {id}{printf("TOKEN_ID:%s\n",yytext);return TOKEN_ID;}
  14. {number}{printf("TOKEN_NUMBER:%s\n",yytext);return TOKEN_NUMBER;}
  15. {operator}{printf("TOKEN_OPERATOR:%s\n",yytext);return TOKEN_OPERATOR;}
  16. {keyword}{printf("TOKEN_KEYWORD:%s\n",yytext);return TOKEN_KEYWORD;}
  17. [\t\n];
  18. printf("error:invalid character'%c'\n",*yytext);
  19. %%
  20. int main()
  21. {
  22. yylex();
  23. return 0;
  24. }
  25. int yywrap();
  26. {
  27. return 1;
  28. }
Success #stdin #stdout #stderr 0.03s 6888KB
stdin
if x=5+3*2
stdout
Standard output is empty
stderr
ERROR: /home/5AI1fi/prog:28:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit