%{
#include <stdio.h>
#include <string.h>

// Define token types for printing
void print_token(const char* type, const char* lexeme) {
    printf("Token: %-12s Lexeme: %s\n", type, lexeme);
}
%}

// Define keywords
KEYWORD     (if|else|while|for|return|int|float|char|double|void)

// Define identifier: starts with letter or _, followed by letters, digits, or _
ID          [a-zA-Z_][a-zA-Z0-9_]*

// Define number (integer only here)
NUMBER      [0-9]+

// Operators (including multi-char)
OPERATOR    (\+\+|--|==|!=|<=|>=|&&|\|\||[+\-*/%=<>!&|])

// Separators
SEPARATOR   [\(\)\{\}\[\];,\.]

// Ignore whitespace (spaces, tabs, newlines)
WHITESPACE  [ \t\n\r]+

%%

{WHITESPACE}    ;  // Ignore whitespace characters

{KEYWORD}       { print_token("Keyword", yytext); }
{ID}            { print_token("Identifier", yytext); }
{NUMBER}        { print_token("Number", yytext); }
{OPERATOR}      { print_token("Operator", yytext); }
{SEPARATOR}     { print_token("Separator", yytext); }

.               { printf("Unknown character: %s\n", yytext); }

%%

int main(int argc, char **argv)
{
    if (argc > 1) {
        FILE *file = fopen(argv[1], "r");
        if (!file) {
            perror("Cannot open file");
            return 1;
        }
        yyin = file;
    }
    yylex();
    return 0;
}
