野牛:如何解决减少/减少冲突 [英] Bison: how to fix reduce/reduce conflict
问题描述
下面是一个Bison语法,它说明了我的问题.我正在使用的实际语法更加复杂.
Below is a a Bison grammar which illustrates my problem. The actual grammar that I'm using is more complicated.
%glr-parser
%%
s : e | p '=' s;
p : fp | p ',' fp;
fp : 'x';
e : te | e ';' te;
te : fe | te ',' fe;
fe : 'x';
一些输入示例为:
x
x = x
x,x = x,x
x,x = x;x
x,x,x = x,x;x,x
x = x,x = x;x
我要寻找的是将'='左侧的x解析为与右侧的x不同.但是,可能在"="符号右侧出现的一组合法表达式"要比在左侧的合法表达式"大(因为有;").
What I'm after is for the x's on the left side of an '=' to be parsed differently than those on the right. However, the set of legal "expressions" which may appear on the right of an '='-sign is larger than those on the left (because of the ';').
Bison打印消息(输入文件为test.y):
Bison prints the message (input file was test.y):
test.y: conflicts: 1 reduce/reduce.
必须有一些解决此问题的方法.在C中,您有类似的情况.下面的程序顺利通过了gcc.
There must be some way around this problem. In C, you have a similar situation. The program below passes through gcc with no errors.
int main(void) {
int x;
int *px;
x;
*px;
*px = x = 1;
}
在这种情况下,'px'和'x'的处理方式有所不同,具体取决于它们是出现在'='符号的左侧还是右侧.
In this case, the 'px' and 'x' get treated differently depending on whether they appear to the left or right of an '='-sign.
推荐答案
您正在使用%glr-parser
,因此无需修复"减少/减少冲突.野牛只告诉您一个,所以您知道语法可能是模棱两可的,因此您可能需要使用%dprec
或%merge
指令添加歧义度.但是,对于您而言,语法不是模棱两可的,因此您无需执行任何操作.
You're using %glr-parser
, so there's no need to "fix" the reduce/reduce conflict. Bison just tells you there is one, so that you know you grammar might be ambiguous, so you might need to add ambiguity resolution with %dprec
or %merge
directives. But in your case, the grammar is not ambiguous, so you don't need to do anything.
冲突不是错误,仅表示您的语法不是LALR(1).
A conflict is NOT an error, its just an indication that your grammar is not LALR(1).
这篇关于野牛:如何解决减少/减少冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!