fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int lines = 1, tokens = 0, errors = 0;
  6. FILE *cleaned;
  7.  
  8. /* Helper: print token info */
  9. void print_token(const char *type, const char *lexeme) {
  10. printf("Line %-4d %-15s %s\n", lines, type, lexeme);
  11. tokens++;
  12. }
  13.  
  14. /* Helper: report error */
  15. void report_error(const char *msg, const char *lexeme) {
  16. fprintf(stderr, "Error (Line %d): %s -> %s\n", lines, msg, lexeme);
  17. errors++;
  18. }
  19. %}
  20.  
  21. /* Definitions */
  22. KEYWORD (auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|inline|int|long|register|restrict|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while|_Bool|_Complex|_Imaginary)
  23. IDENT [A-Za-z_][A-Za-z0-9_]*
  24. NUM ([0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?)
  25. STR \"([^\"\\\n]|\\.)*\"
  26. CHAR \'([^\'\\\n]|\\.)\'
  27. INV_ID [0-9]+[A-Za-z_][A-Za-z0-9_]*
  28. STR_BAD \"([^\"\\\n]|\\.)*\n
  29. CHAR_BAD \'([^\'\\\n]|\\.)*\n
  30. OP (\+\+|--|==|!=|<=|>=|&&|\|\||\+=|-=|\*=|/=|%=|&=|\|=|\^=|->|<<|>>|[+\-*/%&|^~!=<>?:;,.\[\]\(\)\{\}])
  31. WS [ \t]+
  32. COMMENT (//[^\n]*|/\*([^*]|\*+[^*/])*\*+/)
  33.  
  34. %%
  35.  
  36. {WS} { fputc(' ', cleaned); }
  37. {COMMENT} { /* skip comments in cleaned file */ }
  38. {KEYWORD} { print_token("KEYWORD", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  39. {IDENT} { print_token("IDENTIFIER", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  40. {NUM} { print_token("NUMBER", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  41. {STR} { print_token("STRING", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  42. {CHAR} { print_token("CHAR", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  43. {INV_ID} { print_token("INVALID_ID", yytext); report_error("Invalid identifier", yytext); }
  44. {STR_BAD} { print_token("BAD_STRING", yytext); report_error("Unterminated string", yytext); lines++; }
  45. {CHAR_BAD} { print_token("BAD_CHAR", yytext); report_error("Unterminated char", yytext); lines++; }
  46. {OP} { print_token("OPERATOR", yytext); fputs(yytext, cleaned); fputc(' ', cleaned); }
  47. \n { lines++; fputc('\n', cleaned); }
  48. . { print_token("UNKNOWN", yytext); report_error("Unknown character", yytext); fputs(yytext, cleaned); }
  49.  
  50. %%
  51.  
  52. int main() {
  53. cleaned = fopen("cleaned.c", "w");
  54. if (!cleaned) {
  55. perror("cleaned.c");
  56. return 1;
  57. }
  58.  
  59. yylex();
  60.  
  61. printf("\n=== SUMMARY ===\n");
  62. printf("Lines : %d\n", lines);
  63. printf("Tokens : %d\n", tokens);
  64. printf("Errors : %d\n", errors);
  65.  
  66. fclose(cleaned);
  67. return 0;
  68. }
  69.  
  70. int yywrap() { return 1; }
  71.  
Success #stdin #stdout #stderr 0.04s 6872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gDMoss/prog:70:26: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit