47 lines
1010 B
Plaintext
47 lines
1010 B
Plaintext
%{
|
|
#include "test.tab.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
int CURRENT_LINE_NUMBER = 1;
|
|
|
|
void yyerror(const char *s) {
|
|
fprintf(stderr, "\033[91mError at line %i: %s\033[0m\n", CURRENT_LINE_NUMBER, s);
|
|
exit(1);
|
|
}
|
|
|
|
%}
|
|
|
|
%%
|
|
|
|
"int" { return INT; }
|
|
"var" { return VAR; }
|
|
|
|
":=" { return SHORT_DECLARATION; }
|
|
"{" { return LBRACE; }
|
|
"}" { return RBRACE; }
|
|
";" { return SEMICOLON; }
|
|
|
|
[a-zA-Z_][a-zA-Z0-9_]* {
|
|
yylval.str = strdup(yytext);
|
|
return IDENTIFIER;
|
|
}
|
|
[0-9]+ {
|
|
yylval.str = strdup(yytext);
|
|
return NUMBER;
|
|
}
|
|
|
|
[ \t]+ ; // Пропускаем пробелы и табы
|
|
\n { CURRENT_LINE_NUMBER++; }
|
|
. {
|
|
fprintf(stderr, "\033[91mUnexpected character at line %i: %c\033[0m\n",
|
|
CURRENT_LINE_NUMBER, yytext[0]);
|
|
exit(1);
|
|
}
|
|
|
|
%%
|
|
|
|
int yywrap() {
|
|
return 1;
|
|
} |