закончил с переменными

master
serr 2025-05-12 22:47:16 +03:00
parent 6a7b3d2a99
commit 6da915f09a
4 changed files with 59 additions and 19 deletions

View File

@ -14,6 +14,11 @@ typedef struct {
int capacity; // вместимость массива int capacity; // вместимость массива
} Polynomial; } Polynomial;
typedef struct {
char name;
Polynomial poly;
} Variable;
// Инициализация полинома // Инициализация полинома
void init_polynomial(Polynomial *p); void init_polynomial(Polynomial *p);
// Добавление слагаемого в полином // Добавление слагаемого в полином

View File

@ -3,8 +3,6 @@
#include "polynomials.tab.h" #include "polynomials.tab.h"
extern FILE *yyin; extern FILE *yyin;
// Глобальная переменная для хранения разрешенной переменной
char allowed_variable = 0; char allowed_variable = 0;
%} %}
@ -19,11 +17,9 @@ char allowed_variable = 0;
} }
[a-z] { [a-z] {
// Если переменная еще не задана, запоминаем первую встреченную
if (allowed_variable == 0) { if (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); fprintf(stderr, "Error: Only variable '%c' is allowed in this expression\n", allowed_variable);
exit(1); exit(1);
@ -31,17 +27,17 @@ char allowed_variable = 0;
init_polynomial(&yylval.poly); init_polynomial(&yylval.poly);
add_term(&yylval.poly, 1, 1); add_term(&yylval.poly, 1, 1);
return VARIABLE; // переменная от которой полином return VARIABLE;
} }
[A-Z] { [A-Z] {
yylval.var_name = yytext[0]; yylval.var_name = yytext[0];
return VAR_POLY; // переменная которой присвоен полином return VAR_POLY;
} }
[-+*^()] { return yytext[0]; } [-+*^()] { return yytext[0]; }
[ \t] ; [ \t\n] ;
\n { allowed_variable = 0; } \n { }
. { printf("unknown: %s\n", yytext); exit(0); } . { printf("unknown: %s\n", yytext); exit(0); }
%% %%

View File

@ -2,13 +2,37 @@
#include "C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\poly_calc\poly_calc.h" #include "C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\poly_calc\poly_calc.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
void yyerror(const char *s); void yyerror(const char *s);
int yylex(); int yylex();
// Объявляем внешнюю переменную для файла
extern FILE *yyin; extern FILE *yyin;
extern char allowed_variable; extern char allowed_variable;
Variable variables[26];
int variables_count = 0;
Polynomial* find_variable(char name) {
for (int i = 0; i < variables_count; i++) {
if (variables[i].name == name) {
return &variables[i].poly;
}
}
return NULL;
}
void add_variable(char name, Polynomial poly) {
Polynomial* existing = find_variable(name);
if (existing) {
free_polynomial(existing);
*existing = poly;
} else {
variables[variables_count].name = name;
variables[variables_count].poly = poly;
variables_count++;
}
}
%} %}
%union { %union {
@ -42,14 +66,24 @@ line: expr SEMICOLON {
free_polynomial(&$2); free_polynomial(&$2);
} }
| VAR_POLY EQUAL expr SEMICOLON { | VAR_POLY EQUAL expr SEMICOLON {
printf("PEREMENNOY '%c' PRISVOENO EXPR: ", $1); //printf("Variable '%c' assigned: ", $1);
print_polynomial(&$3, allowed_variable); sort_polynomial(&$3);
free_polynomial(&$3); //print_polynomial(&$3, allowed_variable);
add_variable($1, $3);
} }
; ;
expr: NUMBER { $$ = $1; } expr: NUMBER { $$ = $1; }
| VARIABLE { $$ = $1; } | VARIABLE { $$ = $1; }
| VAR_POLY {
Polynomial* var_poly = find_variable($1);
if (var_poly) {
$$ = copy_poly(var_poly);
} else {
yyerror("Undefined variable");
YYERROR;
}
}
| '-' expr %prec UMINUS { | '-' expr %prec UMINUS {
$$ = copy_poly(&$2); $$ = copy_poly(&$2);
for (int i = 0; i < $$.size; i++) { for (int i = 0; i < $$.size; i++) {
@ -106,11 +140,13 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
// Устанавливаем файл как вход для лексера
yyin = input_file; yyin = input_file;
yyparse(); yyparse();
fclose(input_file); fclose(input_file);
// Освобождаем память переменных
for (int i = 0; i < variables_count; i++) {
free_polynomial(&variables[i].poly);
}
return 0; return 0;
} }

View File

@ -1,4 +1,7 @@
2+2; A = (x+2)^2;
Z = 2+2; A = A + A + 2;
print ((3*z^2 - 2*z + 1) * (z^3 - 4*z) + (5*z^4 - z^2)^2) - (z + 1)^4; print A;
print (x+2)^2 +1;
B = 1;
A = x + B;
print A;