PEREMENNOY PRISVOENO EXPR
parent
4990c0b6a3
commit
ff57526744
|
@ -9,13 +9,16 @@ char allowed_variable = 0;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
"print" { return PRINT; }
|
||||||
|
";" { return SEMICOLON; }
|
||||||
|
"=" { return EQUAL; }
|
||||||
[0-9]+ {
|
[0-9]+ {
|
||||||
init_polynomial(&yylval.poly);
|
init_polynomial(&yylval.poly);
|
||||||
add_term(&yylval.poly, atoi(yytext), 0);
|
add_term(&yylval.poly, atoi(yytext), 0);
|
||||||
return NUMBER;
|
return NUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
[a-zA-Z] {
|
[a-z] {
|
||||||
// Если переменная еще не задана, запоминаем первую встреченную
|
// Если переменная еще не задана, запоминаем первую встреченную
|
||||||
if (allowed_variable == 0) {
|
if (allowed_variable == 0) {
|
||||||
allowed_variable = yytext[0];
|
allowed_variable = yytext[0];
|
||||||
|
@ -31,9 +34,13 @@ char allowed_variable = 0;
|
||||||
return VARIABLE;
|
return VARIABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[A-Z] {
|
||||||
|
return VAR_POLY;
|
||||||
|
}
|
||||||
|
|
||||||
[-+*^()] { return yytext[0]; }
|
[-+*^()] { return yytext[0]; }
|
||||||
[ \t] ;
|
[ \t] ;
|
||||||
\n { return 0; }
|
\n { allowed_variable = 0; }
|
||||||
. { printf("unknown: %s\n", yytext); exit(0); }
|
. { printf("unknown: %s\n", yytext); exit(0); }
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ extern char allowed_variable;
|
||||||
Polynomial poly;
|
Polynomial poly;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%token SEMICOLON PRINT VAR_POLY EQUAL
|
||||||
%token <poly> NUMBER VARIABLE
|
%token <poly> NUMBER VARIABLE
|
||||||
%type <poly> expr
|
%type <poly> expr
|
||||||
|
|
||||||
|
@ -25,12 +26,24 @@ extern char allowed_variable;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
input:
|
input:
|
||||||
| input expr {
|
| input line
|
||||||
|
;
|
||||||
|
|
||||||
|
line: expr SEMICOLON {
|
||||||
|
sort_polynomial(&$1);
|
||||||
|
free_polynomial(&$1);
|
||||||
|
}
|
||||||
|
| PRINT expr SEMICOLON {
|
||||||
printf("Result: ");
|
printf("Result: ");
|
||||||
sort_polynomial(&$2);
|
sort_polynomial(&$2);
|
||||||
print_polynomial(&$2, allowed_variable);
|
print_polynomial(&$2, allowed_variable);
|
||||||
free_polynomial(&$2);
|
free_polynomial(&$2);
|
||||||
}
|
}
|
||||||
|
| VAR_POLY EQUAL expr SEMICOLON {
|
||||||
|
printf("PEREMENNOY PRISVOENO EXPR: ");
|
||||||
|
print_polynomial(&$3, allowed_variable);
|
||||||
|
free_polynomial(&$3);
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
expr: NUMBER { $$ = $1; }
|
expr: NUMBER { $$ = $1; }
|
||||||
|
|
Loading…
Reference in New Issue