fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int COMMENT = 0; // Flag to track multi-line comments
  6. %}
  7.  
  8. identifier [a-zA-Z][a-zA-Z0-9]*
  9.  
  10. %%
  11.  
  12. #.* { printf("\n%s is a PREPROCESSOR DIRECTIVE", yytext); }
  13.  
  14. (int|float|char|double|while|for|struct|typedef|do|if|break|continue|void|switch|return|else|goto)
  15. { printf("\n%s is a KEYWORD", yytext); }
  16.  
  17. "/*" { COMMENT = 1; printf("\n%s is a COMMENT", yytext); }
  18. "*/" { COMMENT = 0; printf("\n%s ends COMMENT", yytext); }
  19.  
  20. {identifier}\( { if(!COMMENT) printf("\n%s FUNCTION", yytext); }
  21.  
  22. {identifier}(\[[0-9]*\])? { if(!COMMENT) printf("\n%s IDENTIFIER", yytext); }
  23.  
  24. \{ { if(!COMMENT) printf("\n%s BLOCK BEGINS", yytext); }
  25. \} { if(!COMMENT) printf("\n%s BLOCK ENDS", yytext); }
  26.  
  27. \; { if(!COMMENT) printf("\n%s PUNCTUATOR", yytext); }
  28.  
  29. \".*\" { if(!COMMENT) printf("\n%s STRING", yytext); }
  30.  
  31. [0-9]+ { if(!COMMENT) printf("\n%s NUMBER", yytext); }
  32.  
  33. = { if(!COMMENT) printf("\n%s ASSIGNMENT OPERATOR", yytext); }
  34.  
  35. "=="|"<="|">="|"<"|">" { if(!COMMENT) printf("\n%s RELATIONAL OPERATOR", yytext); }
  36.  
  37. [ \t\n]+ { /* ignore whitespace */ }
  38.  
  39. . { /* ignore other characters */ }
  40.  
  41. %%
  42.  
  43. int main(int argc, char **argv) {
  44. FILE *file = fopen("input.c", "r"); // File to analyze
  45. if (!file) {
  46. printf("Could not open the file\n");
  47. exit(0);
  48. }
  49.  
  50. yyin = file; // Set input for lexer
  51. yylex(); // Start lexical analysis
  52. printf("\nLexical Analysis Complete.\n");
  53. return 0;
  54. }
  55.  
  56. int yywrap() {
  57. return 1;
  58. }
  59.  
Success #stdin #stdout #stderr 0.02s 6868KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/TmiWab/prog:2:1: Syntax error: Operator expected
ERROR: /home/TmiWab/prog:58:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit