Bison 抛出语法错误,但我看不到我的错误
Bison throwing syntax error but I can't see my error
我写了一个简单的程序,希望它是中缀计算器,可以输出后缀表示及其值。我不知道为什么,但是当我 运行 它并传递给它简单数据时,我得到语法错误。这是我传递的内容,以及我与 src 文件一起返回的内容。
执行:
2+2+2
2 2 +
4
syntax error
野牛文件:
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
extern int yylex();
extern int yyparse();
void yyerror(const char *msg);
%}
%union {
int ival;
}
%token <ival> NUM
%type <ival> N M P A
%%
S : A {printf("\n%d\n", );}
;
A : P '-' P {printf("- "); $$ = (int)( - );}
| P '+' P {printf("+ "); $$ = (int)( + );}
| P {$$ = ;}
;
P : M '/' M {printf("/ "); $$ = (int)( / );}
| M '*' M {printf("* "); $$ = (int)( * );}
| M '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
M : N '^' N {printf("^ "); $$ = (int)pow(, );}
| N {$$ = ;}
;
N : '(' A ')' {$$ = ;}
| '-' N {printf("-%d ", ); (int)($$ = -);}
| NUM {printf("%d ", ); (int)($$ = );}
;
%%
void main() {
yyparse();
return;
}
void yyerror(const char *msg) {
fprintf(stderr, "%s\n", msg);
}
弹性文件:
%{
#include <stdio.h>
#include <stdlib.h>
#include "bison.tab.h"
%}
%%
#.*\n
"\"\n
[0-9]+ {
yylval.ival = atoi(yytext);
return NUM;
}
[-+*/^%()] {
return yytext[0];
}
[ \t\n]
%%
你能看出我的错误吗? Maby 我只是没有充分阅读 Bison 文档。很难找到任何关于它的教程。
好的,我找到了解决方案,但我不知道它为什么有效如果有人想澄清我将不胜感激我不擅长正式语言。我改变的是:
P : M '/' M {printf("/ "); $$ = (int)( / );}
| M '*' M {printf("* "); $$ = (int)( * );}
| M '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
改为:
P : P '/' M {printf("/ "); $$ = (int)( / );}
| P '*' M {printf("* "); $$ = (int)( * );}
| P '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
并对每个非终结符进行相应处理。
我写了一个简单的程序,希望它是中缀计算器,可以输出后缀表示及其值。我不知道为什么,但是当我 运行 它并传递给它简单数据时,我得到语法错误。这是我传递的内容,以及我与 src 文件一起返回的内容。
执行:
2+2+2
2 2 +
4
syntax error
野牛文件:
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
extern int yylex();
extern int yyparse();
void yyerror(const char *msg);
%}
%union {
int ival;
}
%token <ival> NUM
%type <ival> N M P A
%%
S : A {printf("\n%d\n", );}
;
A : P '-' P {printf("- "); $$ = (int)( - );}
| P '+' P {printf("+ "); $$ = (int)( + );}
| P {$$ = ;}
;
P : M '/' M {printf("/ "); $$ = (int)( / );}
| M '*' M {printf("* "); $$ = (int)( * );}
| M '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
M : N '^' N {printf("^ "); $$ = (int)pow(, );}
| N {$$ = ;}
;
N : '(' A ')' {$$ = ;}
| '-' N {printf("-%d ", ); (int)($$ = -);}
| NUM {printf("%d ", ); (int)($$ = );}
;
%%
void main() {
yyparse();
return;
}
void yyerror(const char *msg) {
fprintf(stderr, "%s\n", msg);
}
弹性文件:
%{
#include <stdio.h>
#include <stdlib.h>
#include "bison.tab.h"
%}
%%
#.*\n
"\"\n
[0-9]+ {
yylval.ival = atoi(yytext);
return NUM;
}
[-+*/^%()] {
return yytext[0];
}
[ \t\n]
%%
你能看出我的错误吗? Maby 我只是没有充分阅读 Bison 文档。很难找到任何关于它的教程。
好的,我找到了解决方案,但我不知道它为什么有效如果有人想澄清我将不胜感激我不擅长正式语言。我改变的是:
P : M '/' M {printf("/ "); $$ = (int)( / );}
| M '*' M {printf("* "); $$ = (int)( * );}
| M '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
改为:
P : P '/' M {printf("/ "); $$ = (int)( / );}
| P '*' M {printf("* "); $$ = (int)( * );}
| P '%' M {printf("% "); $$ = (int)( % );}
| M {$$ = ;}
;
并对每个非终结符进行相应处理。