所以我的语法出现了可怕的移位/减少错误。这是一个最小的测试用例:
%token PLUS MINUS TIMES DIVIDE NUMBER
%token EQUAL NEQUAL GREATER LESS NOT
%left EQUAL NEQUAL
%left GREATER LESS
%left PLUS MINUS
%left TIMES DIVIDE
%left UMINUS NOT
%%
exp : exp binop exp
| unop exp
| NUMBER
;
binop : MINUS
| PLUS
| TIMES
| DIVIDE
| EQUAL
| NEQUAL
| GREATER
| LESS
;
unop : MINUS %prec UMINUS
| NOT
;
%%
但是,通过实验,我设法使问题消失了:
%token PLUS MINUS TIMES DIVIDE NUMBER
%token EQUAL NEQUAL GREATER LESS NOT
%left EQUAL NEQUAL
%left GREATER LESS
%left PLUS MINUS
%left TIMES DIVIDE
%left UMINUS NOT
%%
exp : binops
| unops
| NUMBER
;
unops : MINUS exp %prec UMINUS
| NOT exp
;
binops : exp MINUS exp
| exp PLUS exp
| exp TIMES exp
| exp DIVIDE exp
| exp EQUAL exp
| exp NEQUAL exp
| exp GREATER exp
| exp LESS exp
;
%%
谁能解释为什么我首先会出现移位/减少错误以及为什么会这样?这甚至是一个适当的解决方案吗?如果不是,那是什么?