From 1a77b9cbd6e6470c885330076acce58200b90cf5 Mon Sep 17 00:00:00 2001 From: serr Date: Mon, 12 May 2025 23:15:03 +0300 Subject: [PATCH] add errors --- analyzers/polynomials/polynomials.l | 16 ++++++++++++---- analyzers/polynomials/polynomials.y | 13 +++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/analyzers/polynomials/polynomials.l b/analyzers/polynomials/polynomials.l index 184e197..5622847 100644 --- a/analyzers/polynomials/polynomials.l +++ b/analyzers/polynomials/polynomials.l @@ -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() { diff --git a/analyzers/polynomials/polynomials.y b/analyzers/polynomials/polynomials.y index 7ddf92c..b71670f 100644 --- a/analyzers/polynomials/polynomials.y +++ b/analyzers/polynomials/polynomials.y @@ -4,11 +4,12 @@ #include #include -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]);