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