fork download
  1. //Implementation of Lexical Analyzer using Lex tool
  2. %{
  3. int COMMENT=0;
  4. %}
  5. identifier [a-zA-Z][a-zA-Z0-9]*
  6. %%
  7. #.* {printf("\n%s is a preprocessor directive",yytext);}
  8. int |
  9. char |
  10. double |
  11. while |
  12. for |
  13. struct |
  14. typedef |
  15. do |
  16. if |
  17. break |
  18. continue |
  19. void |
  20. switch |
  21. return |
  22. else |
  23. goto {printf("\n\t%s is a keyword",yytext);}
  24. "/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}
  25. {identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}
  26. \{ {if(!COMMENT)printf("\n BLOCK BEGINS");}
  27. \} {if(!COMMENT)printf("BLOCK ENDS ");}
  28. {identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
  29. \".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
  30. [0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}
  31. \)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
  32. \( ECHO;
  33. = {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
  34. \<= |
  35. \>= |
  36. \< |
  37. == |
  38. \> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
  39. %%
  40. int main(int argc, char **argv)
  41. {
  42. FILE *file;
  43. file=fopen("var.c","r");
  44. if(!file)
  45. {
  46. printf("could not open the file");
  47. exit(0);
  48. }
  49. yyin=file;
  50. yylex();
  51. printf("\n");
  52. return(0);
  53. }
  54. int yywrap()
  55. {
  56. return(1);
  57. }
Success #stdin #stdout #stderr 0.02s 6956KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/6KcmVJ/prog:58:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit