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

71 lines
1.4 KiB
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);
}
%}
%%
"{" { return LBRACE; }
"}" { return RBRACE; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMICOLON; }
"=" { return ASSIGN; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"%" { return MOD; }
"&&" { return AND; }
"||" { return OR; }
"!" { return NOT; }
"<" { return LT; }
">" { return GT; }
"return" { return RET; }
"print" { return PRINT; }
"while" { return WHILE; }
"do" { return DO; }
"if" { return IF; }
"else" { return ELSE; }
"func" { return FUNC; }
"," { return COMMA; }
"//" {
int c;
while ((c = input()) != '\n' && c != 0);
if (c == '\n') line_number++;
}
"/*" {
int c, prev = 0;
while ((c = input()) != 0) {
if (c == '\n') line_number++;
if (prev == '*' && c == '/') break;
prev = c;
}
if (c == 0) yyerror("Unterminated comment");
}
[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;
}