如何解决后增量运算符的YACC移位/减少冲突? [英] How to fix YACC shift/reduce conflicts from post-increment operator?

查看:122
本文介绍了如何解决后增量运算符的YACC移位/减少冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用YACC(实际上是Bison)编写语法,但遇到了移位/归约问题.它是由于包含后缀递增和递减运算符而产生的.这是语法的精简版:

I'm writing a grammar in YACC (actually Bison), and I'm having a shift/reduce problem. It results from including the postfix increment and decrement operators. Here is a trimmed down version of the grammar:

%token NUMBER ID INC DEC

%left      '+' '-'
%left      '*' '/'
%right     PREINC
%left      POSTINC

%%

expr: NUMBER
|     ID
|     expr '+' expr
|     expr '-' expr
|     expr '*' expr
|     expr '/' expr
|     INC expr %prec PREINC
|     DEC expr %prec PREINC
|     expr INC %prec POSTINC
|     expr DEC %prec POSTINC
|     '(' expr ')'
;

%%

野牛告诉我,有12个移位/减少冲突,但是如果我注释掉后缀递增和递减的行,则可以正常工作.有人知道如何解决此冲突吗?在这一点上,我正在考虑使用LL(k)解析器生成器,这使它变得更加容易,但是LALR语法似乎总是更加自然.我也在考虑使用GLR,但是我不知道有什么好的C/C ++ GLR解析器生成器.

Bison tells me there are 12 shift/reduce conflicts, but if I comment out the lines for the postfix increment and decrement, it works fine. Does anyone know how to fix this conflict? At this point, I'm considering moving to an LL(k) parser generator, which makes it much easier, but LALR grammars have always seemed much more natural to write. I'm also considering GLR, but I don't know of any good C/C++ GLR parser generators.

推荐答案

如果在选项部分中指定%glr-parser,Bison/Yacc可以生成GLR解析器.

Bison/Yacc can generate a GLR parser if you specify %glr-parser in the option section.

这篇关于如何解决后增量运算符的YACC移位/减少冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆