fork download
  1. %{
  2. int COMMENT=0;
  3. int cnt=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. do |
  14. if |
  15. break |
  16. continue |
  17. void |
  18. switch |
  19. case |
  20. long |
  21. struct |
  22. const |
  23. typedef |
  24. return |
  25. else |
  26. goto {printf("\n\t%s is a KEYWORD",yytext);}
  27. "/*" {COMMENT = 1;}
  28. "*/" {COMMENT = 0; cnt++;}
  29. {identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
  30. \{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
  31. \} {if(!COMMENT) printf("\n BLOCK ENDS");}
  32. {identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
  33. \".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
  34. [0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
  35. \)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
  36. \( ECHO;
  37. = {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
  38. \<= |
  39. \>= |
  40. \< |
  41. == |
  42. \> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
  43. %%
  44. int main(int argc,char **argv)
  45. {
  46. if (argc > 1)
  47. {
  48. FILE *file;
  49. file = fopen(argv[1],"r");
  50. if(!file)
  51. {
  52. printf("could not open %s \n",argv[1]);
  53. exit(0);
  54. }
  55. yyin = file;
  56. }
  57. yylex();
  58. printf("\n\n Total No.Of comments are %d",cnt);
  59. return 0;
  60. }
  61. int yywrap()
  62. {
  63. return 1;
  64.  
  65. }
Success #stdin #stdout #stderr 0.02s 6972KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/BZb20l/prog:66:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit