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

100 lines
2.4 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 <stdio.h>
#include <stdlib.h>
void yyerror(const char *s);
int yylex();
// Объявляем внешнюю переменную для файла
extern FILE *yyin;
%}
%union {
Polynomial poly;
}
%token <poly> NUMBER VARIABLE
%type <poly> expr
%left '+' '-'
%left '*' '/'
%left UMINUS
%right '^'
%%
input:
| input expr {
printf("Result: ");
sort_polynomial(&$2);
print_polynomial(&$2);
free_polynomial(&$2);
}
;
expr: NUMBER { $$ = $1; }
| VARIABLE { $$ = $1; }
| '-' expr %prec UMINUS {
$$ = copy_poly(&$2);
for (int i = 0; i < $$.size; i++) {
$$.terms[i].coefficient *= -1;
}
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; }
;
%%
void yyerror(const char *s) {
fprintf(stderr, "ERROR: %s\n", s);
}
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();
fclose(input_file);
return 0;
}