fork download
  1. %{
  2. %}
  3. identifier [a-zA-Z]*
  4. digit [0-9]
  5. letter [a-zA-Z]
  6. int com = 0,com1 = 0;
  7. %%
  8. #.* { printf("\n%s is PRE PROCESSOR DIRECTIVE",yytext); }
  9. int|float|if { if(!com) printf("\n keyword %s ",yytext); }
  10. "/*" { com=1; printf("\n Comments"); }
  11. "*/" { com=0; }
  12. [a-z]+ { if(!com) printf("\n varibles %s ",yytext); }
  13. {identifier}\( { if(!com) printf("\n function %s",yytext); }
  14. \{ { if(!com) printf("\n block begines "); }
  15. \} { if(!com) printf("\n block ends "); }
  16. \".*\" { if(!com) printf("\n string is %s ",yytext); }
  17. [0-9]+ { if(!com) printf("\n number is %s ",yytext); }
  18. \\ { if(!com) printf("\n\t"); }
  19. = { if(!com) printf("\n assigment %s ",yytext); }
  20. \<=|\>=|\>|\<|\== { if(!com) printf("\n relational operators %s
  21. ",yytext); }
  22. %%
  23. main(argc,argv)
  24. int argc;
  25. char **argv;
  26. {
  27. if(argc>1)
  28. {
  29. FILE *file;
  30. file=fopen(argv[1],"r");
  31. if(!file)
  32. {
  33. printf("couldnot open %s",argv[1]);
  34. exit(0);
  35. }
  36. yyin=file;
  37. }
  38. yylex();
  39. }
  40. int yywrap()
  41. {
  42. return(0);
  43. }
  44. OUTPUT:
  45. $ lex cse.l
  46. $ cc cse.yy.c
  47. $/a.out temp.c
  48. temp.c
  49. #include<stdio.h>
  50. main()
  51. {
  52. int a,b,c;
  53. a=1;
  54. b=2;
  55. c=a+b;
  56. printf("Sum:%d",c);
  57. }
  58. #include<stdio.h>is PRE PROCESSOR DIRECTIVE
  59. function main
  60. block begines
  61. keyword int
  62. variables a b c
  63. assignment = = =
  64. function printf
  65. block ends
  66.  
Success #stdin #stdout #stderr 0.02s 6852KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/6vzCpF/prog:66:10: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit