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