Ich habe einen Taschenrechner in Flex und Bison erstellt. Ich muss in der Lage sein, die Präfix-Notation von jedem Ausdruck zu drucken, den ich von der Standardeingabe erhalte. Ich konnte die Postfix-Notation einfach ausdrucken, aber ich habe Schwierigkeiten, die Präfix-Notation zu machen. Hier ist, was ich in Bison erreicht habe bisher:Bison - Drucken Sie die Präfix-Notation in einem einfachen Taschenrechner
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(char *);
int sym[26];
%}
%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'
%left POWER
%right '!'
%%
program:
program statement '\n'
|
;
statement:
expr { printf("\n%d \n", $1); }
|
VARIABLE '=' expr { sym[$1] = $3;printf("\n"); }
;
expr:
INTEGER { /*printf("%d",yylval);*/}
| VARIABLE { $$ = sym[$1]; printf("%d",sym[$1]);}
| expr '+' expr { $$ = $1 + $3; printf("+");}
| expr '-' expr { $$ = $1 - $3; printf("-");}
| expr '*' expr { $$ = $1 * $3; printf("*");}
| expr '/' expr { $$ = $1/$3; printf("/");}
| expr POWER expr { $$ = pow($1, $3);printf("pow");}
| '(' expr ')' { $$ = $2;}
| '!' expr { $$ = !$2; printf("!");}
;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
int main(void) {
yyparse();
return 0;
}
Dies ist Ihr Präfix? Es sieht wie Postfix aus. – user3344003