fork download
  1. /* program name is lexp.l */
  2. %{
  3. /* program to recognize a c program */
  4. int COMMENT=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. /*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/
  30. "*/" {COMMENT = 0;}
  31.  
  32. /* printf("\n\n\t%s is a COMMENT\n",yytext);}*/
  33. {identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
  34. \{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
  35. \} {if(!COMMENT) printf("\n BLOCK ENDS");}
  36. {identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
  37. \".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
  38. [0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
  39. \)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
  40. \( ECHO;
  41. = {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
  42. \<= |
  43. \>= |
  44. \< |
  45. == |
  46. \> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
  47. %%
  48. int main(int argc,char **argv)
  49. {
  50. if (argc > 1)
  51. {
  52. FILE *file;
  53. file = fopen(argv[1],"r");
  54. if(!file)
  55. {
  56. printf("could not open %s \n",argv[1]);
  57. exit(0);
  58. }
  59. yyin = file;
  60. }
  61. yylex();
  62. printf("\n\n");
  63. return 0;
  64. } int yywrap()
  65.  
  66. {
  67. return 0;
  68.  
  69. }
Success #stdin #stdout #stderr 0.02s 6860KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/SPZHjS/prog:70:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit