flex-bison-in-action/analyzers/polynomials/polynomials.l

50 lines
1.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

%{
#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;
%}
%%
"print" { return PRINT; }
";" { return SEMICOLON; }
"=" { return EQUAL; }
[0-9]+ {
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) {
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;
}
[A-Z] {
yylval.var_name = yytext[0];
return VAR_POLY;
}
[-+*^()] { return yytext[0]; }
[ \t] ;
\n { allowed_variable = 0; }
. { printf("unknown: %s\n", yytext); exit(0); }
%%
int yywrap() {
return 1;
}