flex-bison-in-action/analyzers/c_analyzer/c_analyzer.l

41 lines
876 B
Plaintext

%{
#include "c_analyzer.tab.h"
int line_number = 1;
void yyerror(const char *s) {
fprintf(stderr,
"\033[91m\nError at line %i: %s near '%s'\033[0m\n",
line_number,
s,
yytext);
exit(1);
}
%}
%%
"{" { return LBRACE; }
"}" { return RBRACE; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMICOLON; }
"=" { return ASSIGN; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"%" { return MOD; }
"return" { return RET; }
"print" { return PRINT; }
[0-9]+ { yylval.str = strdup(yytext); return NUMBER; }
[a-zA-Z_][a-zA-Z0-9_]* { yylval.str = strdup(yytext); return IDENTIFIER; }
[ \t] ;
\n { line_number++; }
. { yyerror("Invalid character"); }
%%
int yywrap() {
return 1;
}