#define INT 1 #define FLOAT 2 %x SLCOMMENT MLCOMMENT const char *token_value; %% .*[\n] { BEGIN INITIAL; } (.|[\n])*"*/" { printf("There was a comment.\n"); BEGIN INITIAL; } "//" { BEGIN SLCOMMENT; } "/*" { BEGIN MLCOMMENT; } [+-]?[xX][0-9a-fA-F]* { token_value = strdup(yytext); return INT; } [+-]?[0][0-7]* { token_value = strdup(yytext); return INT; } [+-]?[0-9]* { token_value = strdup(yytext); return INT; } [+-]?[0-9]+\.[0-9]+ { token_value = strdup(yytext); return FLOAT; } [+-]?[0-9]+\.[0-9]+[eE][+-]?[0-9]+ { token_value = strdup(yytext); return FLOAT; } %% int main() { yyin = stdin; int token; while((token = yylex()) > 0) { if(token == INT) printf("INT: %s\n", token_value); else if(token == FLOAT) printf("FLOAT: %s\n", token_value); } }