fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int token_count = 1; // Token number (for the order of tokens)
  7. int line_number = 1; // Line number (for counting lines in input)
  8.  
  9. void print_token(const char *token_type, const char *token_value) {
  10. // Print the token number, line number, token type, and lexeme
  11. printf("%-12d %-15d %-15s %s\n", token_count++, line_number, token_type, token_value);
  12. }
  13. %}
  14.  
  15. /* Definitions Section */
  16. %%
  17. "int"|"float"|"char"|"if"|"else" { print_token("KEYWORD", yytext); }
  18. "+"|"-"|"*"|"/"|"=" { print_token("OPERATOR", yytext); }
  19. [0-9]+ { print_token("NUMBER", yytext); }
  20. [a-zA-Z_][a-zA-Z0-9_]* { print_token("IDENTIFIER", yytext); }
  21. [ \t\r]+ ; /* Skip whitespace (spaces, tabs, carriage returns) */
  22. \n { line_number++; } /* Increment line number on newline */
  23. . { print_token("UNKNOWN", yytext); } /* Catch-all rule for unrecognized characters */
  24. %%
  25.  
  26. int yywrap() {
  27. return 1;
  28. }
  29.  
  30. int main() {
  31. printf("TOKEN NO LINE NO TOKEN LEXEME\n");
  32. yylex(); /* Call the lexer */
  33. return 0;
  34. }
Success #stdin #stdout #stderr 0.03s 6864KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/SDMZGa/prog:2:1: Syntax error: Operator expected
ERROR: /home/SDMZGa/prog:34:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit