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