64 lines
1.4 KiB
Plaintext
64 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);
|
|
|
|
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 AND; }
|
|
"||" { return OR; }
|
|
"!" { return NOT; }
|
|
"return" { return RET; }
|
|
"print" { return PRINT; }
|
|
"while" { return WHILE; }
|
|
"if" { return IF; }
|
|
"else" { return ELSE; }
|
|
|
|
"//" {
|
|
int c;
|
|
while ((c = input()) != '\n' && c != 0); // Используем input() вместо yyinput()
|
|
if (c == '\n') line_number++;
|
|
}
|
|
|
|
"/*" {
|
|
int c, prev = 0;
|
|
while ((c = input()) != 0) { // Используем input() вместо yyinput()
|
|
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;
|
|
} |