add errors

master
serr 2025-05-12 23:15:03 +03:00
parent 7802545f53
commit 1a77b9cbd6
2 changed files with 17 additions and 12 deletions

View File

@ -4,6 +4,12 @@
extern FILE *yyin;
char allowed_variable = 0;
int current_line_number = 1;
void yyerror(const char *s) {
fprintf(stderr, "Error at line %i: %s\n", current_line_number, s);
}
%}
%%
@ -21,7 +27,7 @@ char allowed_variable = 0;
allowed_variable = yytext[0];
}
else if (yytext[0] != allowed_variable) {
fprintf(stderr, "Error: Only variable '%c' is allowed in this expression\n", allowed_variable);
yyerror("Bad variable in expression");
exit(1);
}
@ -36,9 +42,11 @@ char allowed_variable = 0;
}
[-+*^()] { return yytext[0]; }
[ \t\n] ;
\n { }
. { printf("unknown: %s\n", yytext); exit(0); }
[ \t] ;
\n { current_line_number++; }
. { yyerror("Unknown symbol");
exit(1);
}
%%
int yywrap() {

View File

@ -4,11 +4,12 @@
#include <stdlib.h>
#include <string.h>
void yyerror(const char *s);
extern void yyerror(const char *s);
int yylex();
extern FILE *yyin;
extern char allowed_variable;
extern int current_line_number;
typedef struct {
char name;
@ -86,7 +87,7 @@ expr: NUMBER { $$ = $1; }
$$ = copy_poly(var_poly);
} else {
yyerror("Undefined variable");
YYERROR;
exit(1);
}
}
| '-' expr %prec UMINUS {
@ -114,12 +115,12 @@ expr: NUMBER { $$ = $1; }
| expr '^' expr {
if ($3.size != 1 || $3.terms[0].exponent != 0) {
yyerror("Exponent must be constant");
YYERROR;
exit(1);
}
int degree = $3.terms[0].coefficient;
if (degree < 0) {
yyerror("Negative exponents not supported");
YYERROR;
exit(1);
}
$$ = deg_poly(&$1, degree);
free_polynomial(&$1);
@ -129,10 +130,6 @@ expr: NUMBER { $$ = $1; }
;
%%
void yyerror(const char *s) {
fprintf(stderr, "ERROR: %s\n", s);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input_file\n", argv[0]);