fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int COMMENT = 0;
  5. %}
  6.  
  7. identifier [a-zA-Z_][a-zA-Z0-9_]*
  8.  
  9. %%
  10. "#".* { printf("\n%s : Preprocessor Directive", yytext); }
  11.  
  12. "int"|"float"|"char"|"double"|"while"|"for"|"struct"|"typedef"|
  13. "do"|"if"|"break"|"continue"|"void"|"switch"|"return"|
  14. "else"|"goto" { printf("\n%s : Keyword", yytext); }
  15.  
  16. "/*" { COMMENT = 1; printf("\nComment Start"); }
  17. "*/" { COMMENT = 0; printf("\nComment End"); }
  18.  
  19. {identifier}"(" { if(!COMMENT) printf("\n%s : Function", yytext); }
  20.  
  21. "{" { if(!COMMENT) printf("\nBlock Begins"); }
  22. "}" { if(!COMMENT) printf("\nBlock Ends"); }
  23.  
  24. {identifier}(
  25.  
  26. \[[0-9]*\]
  27.  
  28. )? { if(!COMMENT) printf("\n%s : Identifier", yytext); }
  29.  
  30. \".*\" { if(!COMMENT) printf("\n%s : String", yytext); }
  31.  
  32. [0-9]+ { if(!COMMENT) printf("\n%s : Number", yytext); }
  33.  
  34. "=" { if(!COMMENT) printf("\n= : Assignment Operator"); }
  35.  
  36. "<="|">="|"=="|"<"|">" { if(!COMMENT) printf("\n%s : Relational Operator", yytext); }
  37.  
  38. ";" { if(!COMMENT) printf("\n; : Statement Terminator"); }
  39.  
  40. [ \t\n]+ ; /* Ignore whitespace */
  41.  
  42. . ; /* Ignore other characters */
  43. %%
  44.  
  45. int main(void)
  46. {
  47. yyin = fopen("var.c", "r");
  48. if (!yyin)
  49. {
  50. printf("Error: Cannot open file\n");
  51. return 1;
  52. }
  53. yylex();
  54. fclose(yyin);
  55. return 0;
  56. }
  57.  
  58. int yywrap(void)
  59. {
  60. return 1;
  61. }
  62.  
Success #stdin #stdout #stderr 0.02s 6940KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/CMPOg1/prog:2:1: Syntax error: Operator expected
ERROR: /home/CMPOg1/prog:61:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit