0
所以我用我的語法有可怕的移位/減少錯誤。這裏有一個最小的測試用例:野牛:固定移位/減少錯誤,不知道如何
%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
;
%%
任何人都可以解釋爲什麼我有移進/歸約擺在首位的錯誤和爲什麼這個工作?這甚至是一個合適的解決方案?如果不是,那是什麼?