这是为柠檬解析器生成器编写的语法:
%left PostDecrementation.
%right PreDecrementation.
program ::= expression.
expression ::= Terminal.
expression ::= unaryoperation.
unaryoperation ::= Decrementation expression. [PreDecrementation]
unaryoperation ::= expression Decrementation. [PostDecrementation]
尝试编译它时,生成器会产生以下冲突:
State 0:
program ::= * expression
expression ::= * Null
expression ::= * unaryoperation
unaryoperation ::= * Decrementation expression
unaryoperation ::= * expression Decrementation
Null shift 5
Decrementation shift 1
program accept
expression shift 2
unaryoperation shift 4
State 1:
expression ::= * Null
expression ::= * unaryoperation
unaryoperation ::= * Decrementation expression
unaryoperation ::= Decrementation * expression
unaryoperation ::= * expression Decrementation
Null shift 5
Decrementation shift 1
expression shift 3
unaryoperation shift 4
State 2:
(0) program ::= expression *
unaryoperation ::= expression * Decrementation
$ reduce 0
Decrementation shift 6
State 3:
(3) unaryoperation ::= Decrementation expression *
unaryoperation ::= expression * Decrementation
Decrementation shift 6
Decrementation reduce 3 ** Parsing conflict **
{default} reduce 3
State 4:
(2) expression ::= unaryoperation *
{default} reduce 2
State 5:
(1) expression ::= Null *
{default} reduce 1
State 6:
(4) unaryoperation ::= expression Decrementation *
{default} reduce 4
为什么会出现冲突,因为 PreIncrementation 和 PostDecrementation 的优先级和关联性已在语法中明确指定?