42 lines
1.4 KiB
Plaintext
42 lines
1.4 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;
|
||
%}
|
||
|
||
%%
|
||
[0-9]+ {
|
||
init_polynomial(&yylval.poly);
|
||
add_term(&yylval.poly, atoi(yytext), 0);
|
||
return NUMBER;
|
||
}
|
||
|
||
[a-zA-Z] {
|
||
// Если переменная еще не задана, запоминаем первую встреченную
|
||
if (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);
|
||
exit(1);
|
||
}
|
||
|
||
init_polynomial(&yylval.poly);
|
||
add_term(&yylval.poly, 1, 1);
|
||
return VARIABLE;
|
||
}
|
||
|
||
[-+*^()] { return yytext[0]; }
|
||
[ \t] ;
|
||
\n { return 0; }
|
||
. { printf("unknown: %s\n", yytext); exit(0); }
|
||
%%
|
||
|
||
int yywrap() {
|
||
return 1;
|
||
} |