代码生成的 Bison 推导问题
您好,我正在使用 bison 作为编译器用于研究目的,我得到了下一个代码:
if :
if2
|
if1;
if2:
SE expressao {$1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc(); $1->label = label; label+=2;} ENTAO
comandos
SENAO {$1->for_goto = reserve_loc(); back_patch($1->for_jmp_false, JMP_FALSE, $1->label);}
comandos
FIMSE {back_patch($1->for_goto, GOTO, $1->label+1);}
|
SE expressao error {yyerrok; errors++; yyerror("Entao nao encontrado no se");}
comandos
SENAO
comandos
FIMSE ;
if1:
SE expressao {$1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc(); $1->label = label++;} ENTAO
comandos
FIMSE {back_patch($1->for_jmp_false, JMP_FALSE, $1->label); gen_code(LABEL,$1->label);}
|
SE expressao error {yyerrok; errors++; yyerror("Entao nao encontrado no se");}
comandos
FIMSE;
此代码仅在“”之前找到“FIMSE”(这表明该命令是一个简单的 if)时生成规则“if2” SENAO”(在本例中是 if else 命令)它会引发错误,只有当我输入 C 代码来生成中间代码时才会发生这种情况。我的问题是:为什么?我该如何解决这个问题?
Hello I'm using bison for a compiler for study purposes, i got the next code:
if :
if2
|
if1;
if2:
SE expressao {$1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc(); $1->label = label; label+=2;} ENTAO
comandos
SENAO {$1->for_goto = reserve_loc(); back_patch($1->for_jmp_false, JMP_FALSE, $1->label);}
comandos
FIMSE {back_patch($1->for_goto, GOTO, $1->label+1);}
|
SE expressao error {yyerrok; errors++; yyerror("Entao nao encontrado no se");}
comandos
SENAO
comandos
FIMSE ;
if1:
SE expressao {$1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc(); $1->label = label++;} ENTAO
comandos
FIMSE {back_patch($1->for_jmp_false, JMP_FALSE, $1->label); gen_code(LABEL,$1->label);}
|
SE expressao error {yyerrok; errors++; yyerror("Entao nao encontrado no se");}
comandos
FIMSE;
This code only generates the rule 'if2', when it found the "FIMSE" (this indicates that the command is a simple if) before the "SENAO" (in this case is a if else command) it's raises an error, this only happen when i put the C code to generate the intermediate code. My question is: Why? How can i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当解析器以增强形式接收
SE
和expressao
时,它立即需要选择要执行的操作(因为您要求该操作在该点发生)如果没有注解,它可以先推送ENTAO
和commandos
,然后做出减少的决定。本质上,RHS内部的一个动作被翻译成具有以下内容的动作 。动作放在最后,与另一个非终结符 So
被翻译成
结果,这些动作在你的语法中产生了减少-减少冲突。
有两种方法:
语法使得动作是
实际上两者都是同一个
替代方案。
When the parser receives
SE
andexpressao
, in your augmented form, it then immediately needs to make a choice which action to perform (because you asked that the action happens at that point. Without the annotation, it can first pushENTAO
andcommandos
, and then make a decision to reduce.In essence, an action inside the RHS is translated into one that has the action at the end, with another non-terminal. So
is translated into
As a consequence, these actions produce a reduce-reduce conflict in your grammar.
There are two approaches:
the grammar so that the action is
actually the same one in both
alternatives.