калькулятор полиномов начал работать

master
serr 2025-05-12 20:44:46 +03:00
parent 93df059f44
commit d09a25663a
3 changed files with 90 additions and 14 deletions

View File

@ -1,13 +1,27 @@
%{ %{
#include "C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\poly_calc\poly_calc.h"
#include "polynomials.tab.h" #include "polynomials.tab.h"
extern FILE *yyin;
%} %}
%% %%
[0-9]+ { yylval = atoi(yytext); return NUMBER; } [0-9]+ {
init_polynomial(&yylval.poly);
add_term(&yylval.poly, atoi(yytext), 0);
return NUMBER;
}
"x" {
init_polynomial(&yylval.poly);
add_term(&yylval.poly, 1, 1);
return VARIABLE;
}
[-+*^()] { return yytext[0]; } [-+*^()] { return yytext[0]; }
[ \t] ; [ \t] ;
\n { return 0; } \n { return 0; }
. { printf("unknown: %s\n", yytext); exit(0); } . { printf("unknown: %s\n", yytext); exit(0); }
%% %%
int yywrap() { return 1; } int yywrap() {
return 1;
}

View File

@ -1,12 +1,22 @@
%{ %{
#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 <math.h>
void yyerror(const char *s); void yyerror(const char *s);
int yylex(); int yylex();
// Объявляем внешнюю переменную для файла
extern FILE *yyin;
%} %}
%token NUMBER %union {
Polynomial poly;
}
%token <poly> NUMBER VARIABLE
%type <poly> expr
%left '+' '-' %left '+' '-'
%left '*' '/' %left '*' '/'
%left UMINUS %left UMINUS
@ -14,16 +24,52 @@ int yylex();
%% %%
input: input:
| input expr { printf("= %d\n", $2); } | input expr {
printf("Result: ");
sort_polynomial(&$2);
print_polynomial(&$2);
free_polynomial(&$2);
}
; ;
expr: NUMBER { $$ = $1; } expr: NUMBER { $$ = $1; }
| '-' expr %prec UMINUS { $$ = -$2; } | VARIABLE { $$ = $1; }
| expr '+' expr { $$ = $1 + $3; } | '-' expr %prec UMINUS {
| expr '-' expr { $$ = $1 - $3; } $$ = copy_poly(&$2);
| expr '*' expr { $$ = $1 * $3; } for (int i = 0; i < $$.size; i++) {
| expr '/' expr { $$ = $1 / $3; } $$.terms[i].coefficient *= -1;
| expr '^' expr { $$ = pow($1, $3); } }
free_polynomial(&$2);
}
| expr '+' expr {
$$ = add_polynomials(&$1, &$3);
free_polynomial(&$1);
free_polynomial(&$3);
}
| expr '-' expr {
$$ = sub_polynomials(&$1, &$3);
free_polynomial(&$1);
free_polynomial(&$3);
}
| expr '*' expr {
$$ = mul_polynomials(&$1, &$3);
free_polynomial(&$1);
free_polynomial(&$3);
}
| expr '^' expr {
if ($3.size != 1 || $3.terms[0].exponent != 0) {
yyerror("Exponent must be constant");
YYERROR;
}
int degree = $3.terms[0].coefficient;
if (degree < 0) {
yyerror("Negative exponents not supported");
YYERROR;
}
$$ = deg_poly(&$1, degree);
free_polynomial(&$1);
free_polynomial(&$3);
}
| '(' expr ')' { $$ = $2; } | '(' expr ')' { $$ = $2; }
; ;
%% %%
@ -32,7 +78,23 @@ void yyerror(const char *s) {
fprintf(stderr, "ERROR: %s\n", s); fprintf(stderr, "ERROR: %s\n", s);
} }
int main() { int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input_file\n", argv[0]);
return 1;
}
FILE *input_file = fopen(argv[1], "r");
if (!input_file) {
perror("Error opening file");
return 1;
}
// Устанавливаем файл как вход для лексера
yyin = input_file;
yyparse(); yyparse();
fclose(input_file);
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@ ANALYZERS_DIR = r'C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОР
FLEX_EXE_PATH = r"C:\tools\win_flex_bison\win_flex.exe" FLEX_EXE_PATH = r"C:\tools\win_flex_bison\win_flex.exe"
BISON_EXE_PATH = r"C:\tools\win_flex_bison\win_bison.exe" BISON_EXE_PATH = r"C:\tools\win_flex_bison\win_bison.exe"
DEPENDENCY_LIST = [ DEPENDENCY_LIST = [
# r"C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\stack\stack.c" r"C:\Users\user\Desktop\УЧЕБА\6_СЕМ\КОМПИЛЯТОРЫ\flex_bison_test\analyzers\polynomials\poly_calc\poly_calc.c"
] ]
def main(): def main():