цепочки условий добавил

master
serr 2025-03-30 22:01:07 +03:00
parent 8080e13a2e
commit 6b954531b3
1 changed files with 11 additions and 4 deletions

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
extern int yylineno; extern int yylineno;
extern char *last_lexeme;
void yyerror(const char *s); void yyerror(const char *s);
int yylex(); int yylex();
@ -20,10 +21,12 @@ extern FILE *yyin;
%token IF ELSE WHILE RETURN PRINT %token IF ELSE WHILE RETURN PRINT
%token AND OR NOT GE LE EQ NE %token AND OR NOT GE LE EQ NE
%nonassoc '>' '<' GE LE EQ NE %right '='
%left OR %left OR
%left AND %left AND
%left '=' '+' '-' %left EQ NE
%left '<' '>' GE LE
%left '+' '-'
%left '*' '/' '%' %left '*' '/' '%'
%right NOT UMINUS %right NOT UMINUS
@ -74,13 +77,17 @@ expr:
%% %%
void yyerror(const char *s) { void yyerror(const char *s) {
fprintf(stderr, "\033[91mError at line %d: %s\n\033[0m", yylineno, s); if (last_lexeme) {
fprintf(stderr, "\033[91mError at line %d: %s (near '%s')\n\033[0m", yylineno, s, last_lexeme);
} else {
fprintf(stderr, "\033[91mError at line %d: %s\n\033[0m", yylineno, s);
}
exit(1); exit(1);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "\033[91mUsage: <...>.exe <input_file>\n\033[0m", argv[0]); fprintf(stderr, "\033[91mUsage: %s <input_file>\n\033[0m", argv[0]);
return 1; return 1;
} }