解析器中的yacc shift / reduce

时间:2015-04-28 17:29:14

标签: parsing bison yacc

我在一个作业中编写一个编译器解析器,当我运行命令时

$ bison --yacc -v --defines -o parser.c parser.y
parser.y: warning: 8 shift/reduce conflicts [-Wconflicts-sr]
$

除了if / else shift / reduce冲突之外,我在以下状态中遇到parser.output文件冲突,

国家34

 35 term: lvalue . PLUSPLUS
 37     | lvalue . MINUSMINUS
 39 assignexpr: lvalue . ASSIGN expr
 40 primary: lvalue .
 49 member: lvalue . FULLSTOP IDENTIFIER
 50       | lvalue . LEFTSQUARE expr RIGHTSQUARE
 54 call: lvalue . callsuffix

ASSIGN      shift, and go to state 88
PLUSPLUS    shift, and go to state 89
MINUSMINUS  shift, and go to state 90
LEFTSQUARE  shift, and go to state 91
FULLSTOP    shift, and go to state 92
LEFTPAR     shift, and go to state 93

PLUSPLUS    [reduce using rule 40 (primary)]
MINUSMINUS  [reduce using rule 40 (primary)]
LEFTSQUARE  [reduce using rule 40 (primary)]
LEFTPAR     [reduce using rule 40 (primary)]
$default    reduce using rule 40 (primary)

callsuffix  go to state 94
normcall    go to state 95
methodcall  go to state 96

国家36

 41 primary: call .
 51 member: call . FULLSTOP IDENTIFIER
 52       | call . LEFTSQUARE expr RIGHTSQUARE
 53 call: call . LEFTPAR elist RIGHTPAR

LEFTSQUARE  shift, and go to state 97
FULLSTOP    shift, and go to state 98
LEFTPAR     shift, and go to state 99

LEFTSQUARE  [reduce using rule 41 (primary)]
LEFTPAR     [reduce using rule 41 (primary)]
$default    reduce using rule 41 (primary)

国家52

 16 expr: expr . PLUS expr
 17     | expr . MINUS expr
 18     | expr . MUL expr
 19     | expr . DIV expr
 20     | expr . MOD expr
 21     | expr . GREATER expr
 22     | expr . GREATER_EQUAL expr
 23     | expr . LESS expr
 24     | expr . LESS_EQUAL expr
 25     | expr . EQUAL expr
 26     | expr . NOTEQUAL expr
 27     | expr . AND expr
 28     | expr . OR expr
 95 returnstmt: RETURN expr .

PLUS           shift, and go to state 74
MINUS          shift, and go to state 75
MUL            shift, and go to state 76
DIV            shift, and go to state 77
MOD            shift, and go to state 78
EQUAL          shift, and go to state 79
NOTEQUAL       shift, and go to state 80
OR             shift, and go to state 81
AND            shift, and go to state 82
GREATER        shift, and go to state 83
LESS           shift, and go to state 84
GREATER_EQUAL  shift, and go to state 85
LESS_EQUAL     shift, and go to state 86

MINUS     [reduce using rule 95 (returnstmt)]
$default  reduce using rule 95 (returnstmt)

知道怎么解决吗?

1 个答案:

答案 0 :(得分:2)

很难说出问题是什么,因为你没有显示完整的语法。通常情况下,冲突是由于语境中的其他规则(不是具有冲突的状态中显示的规则)引起的,或者是由于上下文,或者规则是如何组合的。

  • 州34/36: 您似乎在primarylvaluecall的规则之间存在某种圆形歧义。这些(完整)规则是什么?如何知道左值和初级之间的区别?

  • 状态52:这看起来像returnstmt和以MINUS开头的后续表达式之间的歧义。看起来你没有语句终止符/分隔符?

这些都可能是同一个底层问题 - 解析器无法弄清楚一个语句的结束位置和下一个语句的开始......