fork download
  1.  
  2. %{
  3. /* Program to recognize a C program */
  4. int COMMENT = 0;
  5. %}
  6.  
  7. identifier [a-zA-Z][a-zA-Z0-9]*
  8.  
  9. %%
  10.  
  11. #.* { printf("\n%s is a PREPROCESSOR DIRECTIVE", yytext); }
  12. int|float|char|double|while|for|do|if|break|continue|void|switch|case|long|struct|const|typedef|return|else|goto { printf("\n\t%s is a KEYWORD", yytext); }
  13. "/*" { COMMENT = 1; }
  14. "*/" { COMMENT = 0; }
  15. {identifier}\( { if (!COMMENT) printf("\n\nFUNCTION\n\t%s", yytext); }
  16. \{ { if (!COMMENT) printf("\n BLOCK BEGINS"); }
  17. \} { if (!COMMENT) printf("\n BLOCK ENDS"); }
  18. {identifier}(\[[0-9]*\])? { if (!COMMENT) printf("\n %s IDENTIFIER", yytext); }
  19. \".*\" { if (!COMMENT) printf("\n\t%s is a STRING", yytext); }
  20. [0-9]+ { if (!COMMENT) printf("\n\t%s is a NUMBER", yytext); }
  21. \)(\;)? { if (!COMMENT) printf("\n\t"); ECHO; printf("\n"); }
  22. \( ECHO;
  23. = { if (!COMMENT) printf("\n\t%s is an ASSIGNMENT OPERATOR", yytext); }
  24. \<=|>=|\<|==|\> { if (!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR", yytext); }
  25.  
  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\n");
  40.  
  41. return 0;
  42. }
  43.  
  44. int yywrap() {
  45. return 0;
  46. }
  47.  
Success #stdin #stdout #stderr 0.02s 6984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Rhs66j/prog:46:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit