fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int COMMENT = 0;
  6. %}
  7.  
  8. identifier [a-zA-Z][a-zA-Z0-9]*
  9.  
  10. %%
  11.  
  12. #.* { printf("\n%s is a Preprocessor Directive", yytext); }
  13. int|float|main|if|else|printf|scanf|for|char|getch|while
  14. { if (!COMMENT) printf("\n%s is a Keyword", yytext); }
  15.  
  16. "/*" { COMMENT = 1; }
  17. "*/" { COMMENT = 0; }
  18.  
  19. {identifier}\({ if (!COMMENT) printf("\nFunction:\t%s", yytext); }
  20. \{ { if (!COMMENT) printf("\nBlock Begins"); }
  21. \} { if (!COMMENT) printf("\nBlock Ends"); }
  22. {identifier}(\[[0-9]*\])? { if (!COMMENT) printf("\n%s is an Identifier", yytext); }
  23. \".*\" { if (!COMMENT) printf("\n%s is a String", yytext); }
  24. [0-9]+ { if (!COMMENT) printf("\n%s is a Number", yytext); }
  25. \)(\;)? { if (!COMMENT) { printf("\t"); ECHO; printf("\n"); } }
  26. \( { if (!COMMENT) { ECHO; printf("\n"); } }
  27. \= { if (!COMMENT) printf("\n%s is an Assignment Operator", yytext); }
  28. \<=|\>=|\<|\== { if (!COMMENT) printf("\n%s is a Relational Operator", yytext); }
  29. .|\n { /* Ignore other characters */ }
  30.  
  31. %%
  32.  
  33. int main(int argc, char **argv) {
  34. if (argc > 1) {
  35. FILE *file = fopen(argv[1], "r");
  36. if (!file) {
  37. printf("\nCould not open the file: %s", argv[1]);
  38. exit(0);
  39. }
  40. yyin = file;
  41. }
  42. yylex();
  43. printf("\n\n");
  44. return 0;
  45. }
  46.  
  47. int yywrap() {
  48. return 1;
  49. }
  50.  
Success #stdin #stdout #stderr 0.03s 6956KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/T4tRsM/prog:49:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit