Vip*_*kat 0 expression yacc infix-notation bison
我一直试图使用YACC(Bison)将中缀表达式转换为后缀表达式但没有成功.我想知道怎么做?示例代码将是真棒:)
小智 6
这是我的解决方案:
%{
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUM;}
\n return 0;
. return *yytext;
%%
int yywrap(){
return 1;
}
Run Code Online (Sandbox Code Playgroud)
%{
#include<stdio.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%right NEGATIVE
%%
S: E {printf("\n");}
;
E: E '+' E {printf("+");}
| E '*' E {printf("*");}
| E '-' E {printf("-");}
| E '/' E {printf("/");}
| '(' E ')'
| '-' E %prec NEGATIVE {printf("-");}
| NUM {printf("%d", yylval);}
;
%%
int main(){
yyparse();
}
int yyerror (char *msg) {
return printf ("error YACC: %s\n", msg);
}
Run Code Online (Sandbox Code Playgroud)
yacc -d gram.y
flex gram.l
cc lex.yy.c y.tab.c
./a.out
Run Code Online (Sandbox Code Playgroud)
2+6*2-5/3
262*+53/-
Run Code Online (Sandbox Code Playgroud)