66 lines
1.8 KiB
Plaintext
66 lines
1.8 KiB
Plaintext
%{
|
||
#include "C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\poly_calc\poly_calc.h"
|
||
#include "polynomials.tab.h"
|
||
|
||
extern FILE *yyin;
|
||
char allowed_variable = 0;
|
||
int current_line_number = 1;
|
||
|
||
const char* INT_MAX_STR = "2147483646";
|
||
|
||
void yyerror(const char *s) {
|
||
fprintf(stderr, "Error at line %i: %s\n", current_line_number, s);
|
||
}
|
||
|
||
%}
|
||
|
||
%%
|
||
"print" { return PRINT; }
|
||
";" { return SEMICOLON; }
|
||
"=" { return EQUAL; }
|
||
[0-9]+ {
|
||
size_t len = strlen(yytext);
|
||
if (len > strlen(INT_MAX_STR)) {
|
||
yyerror("Number too large (max 2147483646)");
|
||
exit(-1);
|
||
} else if (len == strlen(INT_MAX_STR)) {
|
||
if (strcmp(yytext, INT_MAX_STR) > 0) {
|
||
yyerror("Number too large (max 2147483646)");
|
||
exit(-1);
|
||
}
|
||
}
|
||
init_polynomial(&yylval.poly);
|
||
add_term(&yylval.poly, atoi(yytext), 0);
|
||
return NUMBER;
|
||
}
|
||
|
||
[a-z] {
|
||
if (allowed_variable == 0) {
|
||
allowed_variable = yytext[0];
|
||
}
|
||
else if (yytext[0] != allowed_variable) {
|
||
yyerror("Bad variable in expression");
|
||
exit(1);
|
||
}
|
||
|
||
init_polynomial(&yylval.poly);
|
||
add_term(&yylval.poly, 1, 1);
|
||
return VARIABLE;
|
||
}
|
||
|
||
[A-Z] {
|
||
yylval.var_name = yytext[0];
|
||
return VAR_POLY;
|
||
}
|
||
|
||
[-+*^()] { return yytext[0]; }
|
||
[ \t] ;
|
||
\n { current_line_number++; }
|
||
. { yyerror("Unknown symbol");
|
||
exit(1);
|
||
}
|
||
%%
|
||
|
||
int yywrap() {
|
||
return 1;
|
||
} |