add errors
parent
7802545f53
commit
1a77b9cbd6
|
@ -4,6 +4,12 @@
|
||||||
|
|
||||||
extern FILE *yyin;
|
extern FILE *yyin;
|
||||||
char allowed_variable = 0;
|
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];
|
allowed_variable = yytext[0];
|
||||||
}
|
}
|
||||||
else if (yytext[0] != allowed_variable) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +42,11 @@ char allowed_variable = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
[-+*^()] { return yytext[0]; }
|
[-+*^()] { return yytext[0]; }
|
||||||
[ \t\n] ;
|
[ \t] ;
|
||||||
\n { }
|
\n { current_line_number++; }
|
||||||
. { printf("unknown: %s\n", yytext); exit(0); }
|
. { yyerror("Unknown symbol");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
%%
|
%%
|
||||||
|
|
||||||
int yywrap() {
|
int yywrap() {
|
||||||
|
|
|
@ -4,11 +4,12 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void yyerror(const char *s);
|
extern void yyerror(const char *s);
|
||||||
int yylex();
|
int yylex();
|
||||||
|
|
||||||
extern FILE *yyin;
|
extern FILE *yyin;
|
||||||
extern char allowed_variable;
|
extern char allowed_variable;
|
||||||
|
extern int current_line_number;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name;
|
char name;
|
||||||
|
@ -86,7 +87,7 @@ expr: NUMBER { $$ = $1; }
|
||||||
$$ = copy_poly(var_poly);
|
$$ = copy_poly(var_poly);
|
||||||
} else {
|
} else {
|
||||||
yyerror("Undefined variable");
|
yyerror("Undefined variable");
|
||||||
YYERROR;
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
| '-' expr %prec UMINUS {
|
| '-' expr %prec UMINUS {
|
||||||
|
@ -114,12 +115,12 @@ expr: NUMBER { $$ = $1; }
|
||||||
| expr '^' expr {
|
| expr '^' expr {
|
||||||
if ($3.size != 1 || $3.terms[0].exponent != 0) {
|
if ($3.size != 1 || $3.terms[0].exponent != 0) {
|
||||||
yyerror("Exponent must be constant");
|
yyerror("Exponent must be constant");
|
||||||
YYERROR;
|
exit(1);
|
||||||
}
|
}
|
||||||
int degree = $3.terms[0].coefficient;
|
int degree = $3.terms[0].coefficient;
|
||||||
if (degree < 0) {
|
if (degree < 0) {
|
||||||
yyerror("Negative exponents not supported");
|
yyerror("Negative exponents not supported");
|
||||||
YYERROR;
|
exit(1);
|
||||||
}
|
}
|
||||||
$$ = deg_poly(&$1, degree);
|
$$ = deg_poly(&$1, degree);
|
||||||
free_polynomial(&$1);
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stderr, "Usage: %s input_file\n", argv[0]);
|
fprintf(stderr, "Usage: %s input_file\n", argv[0]);
|
||||||
|
|
Loading…
Reference in New Issue