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"
  15. { printf("\n%s : Keyword", yytext); }
  16.  
  17. "/*" { COMMENT = 1; printf("\nComment Start"); }
  18. "*/" { COMMENT = 0; printf("\nComment End"); }
  19.  
  20. {identifier}"(" { if(!COMMENT) printf("\n%s : Function", yytext); }
  21.  
  22. "{" { if(!COMMENT) printf("\nBlock Begins"); }
  23. "}" { if(!COMMENT) printf("\nBlock Ends"); }
  24.  
  25. {identifier}(\[[0-9]*\])?
  26. { if(!COMMENT) printf("\n%s : Identifier", yytext); }
  27.  
  28. \".*\" { if(!COMMENT) printf("\n%s : String", yytext); }
  29.  
  30. [0-9]+ { if(!COMMENT) printf("\n%s : Number", yytext); }
  31.  
  32. "=" { if(!COMMENT) printf("\n= : Assignment Operator"); }
  33.  
  34. "<="|">="|"=="|"<"|">"
  35. { if(!COMMENT) printf("\n%s : Relational Operator", yytext); }
  36.  
  37. ";" { if(!COMMENT) printf("\n; : Statement Terminator"); }
  38.  
  39. [ \t\n]+ ; /* Ignore whitespace */
  40.  
  41. . ; /* Ignore other characters */
  42. %%
  43.  
  44. int main()
  45. {
  46. yyin = fopen("var.c", "r");
  47. if (!yyin)
  48. {
  49. printf("Error: Cannot open file");
  50. return 1;
  51. }
  52. yylex();
  53. fclose(yyin);
  54. return 0;
  55. }
  56.  
  57. int yywrap()
  58. {
  59. return 1;
  60. }
  61.  
Success #stdin #stdout #stderr 0.02s 6916KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/64mLmo/prog:2:1: Syntax error: Operator expected
ERROR: /home/64mLmo/prog:60:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit