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