当解析器试图判断某些东西是一元运算符还是二元运算符时,我不断得到转换/减少冲突。
%token <intconst> tHEX tOCT tDEC tRUNE
%token <stringconst> tBOOL INTERPRETEDSTRING RAWSTRING tIDENTIFIER
%token <floatconst> tFLOAT
%token <charconst> tRUNES
%token TRUE FALSE BREAK CASE CHAN CONST CONTINUE DEFAULT DEFER ELSE FALLTHROUGH FOR FUNC GO GOTO IF IMPORT INTERFACE MAP PACKAGE RANGE RETURN SELECT STRUCT SWITCH TYPE VAR INT PRINT FLOAT PRINTLN BOOL APPEND RUNE STRING SEMICOLON NEWLINE PLUS MINUS TIMES DIV MOD AMP PIPE CARAT COUT CIN AMPCARAT SELFPLUS SELFMINUS SELFTIMES SELFDIV SELFMOD AMPEQUALS PIPEEQUALS CARATEQUALS COUTEQUALS CINEQUALS WTF AND OR REDIRECT INCREMENT DECREMENT DOESEQUALS LT GT EQUALS NOT NEQ LE GE COMPAT ELLIPSIS LEFTPAREN RIGHTPAREN LEFTSQUARE RIGHTSQUARE LEFTBRACE RIGHTBRACE COMMA PERIOD FULLCOLON ESCAPEA ESCAPEB ESCAPEF ESCAPEV ESCAPESLASH ESCAPEAPOSTROPHE INVALID
/*%token unary*/
/*%token binary*/
%left OR
%left AND
%left DOESEQUALS NEQ GT GE LT LE
%left PLUS MINUS PIPE CARAT
%left TIMES DIV MOD COUT CIN AMP AMPCARAT
/*%left binary*/
%left UPLUS UMINUS UNOT UCARAT UTIMES UAMP UPAREN
%start expList
%%
expList: exp expList {}
| /*empty*/
;
exp: exp OR addOp {}
| exp AND addOp {}
| exp NEQ addOp {}
| exp GT addOp {}
| exp GE addOp {}
| exp LT addOp {}
| exp LE addOp {}
| addOp {}
;
addOp: addOp PLUS mulOp {}
| addOp MINUS mulOp {}
| addOp PIPE mulOp {}
| addOp CARAT mulOp {}
| mulOp {}
;
mulOp: mulOp TIMES factor {}
| mulOp DIV factor {}
| mulOp MOD factor {}
| mulOp COUT factor {}
| mulOp CIN factor {}
| mulOp AMP factor {}
| mulOp AMPCARAT factor {}
| factor {}
;
factor: LEFTPAREN exp RIGHTPAREN %prec UPAREN {}
| PLUS factor %prec UPLUS {}
| MINUS factor %prec UMINUS {}
| NOT factor %prec UNOT {}
| CARAT factor %prec UCARAT {}
| TIMES factor %prec UTIMES {}
| AMP factor %prec UAMP {}
| tIDENTIFIER {}
| tDEC {}
| tFLOAT {}
| tOCT {}
| tHEX {}
| tRUNES {}
| INTERPRETEDSTRING {}
| RAWSTRING {}
;
%%
我知道我有很多代币,我最终会使用它们。我只是想让表达式的语法运转起来。 以下是我得到的转移/减少错误
State 18
10 exp: addOp .
11 addOp: addOp . PLUS mulOp
12 | addOp . MINUS mulOp
13 | addOp . PIPE mulOp
14 | addOp . CARAT mulOp
PLUS shift, and go to state 37
MINUS shift, and go to state 38
PIPE shift, and go to state 39
CARAT shift, and go to state 40
PLUS [reduce using rule 10 (exp)]
MINUS [reduce using rule 10 (exp)]
CARAT [reduce using rule 10 (exp)]
$default reduce using rule 10 (exp)
State 19
15 addOp: mulOp .
16 mulOp: mulOp . TIMES factor
17 | mulOp . DIV factor
18 | mulOp . MOD factor
19 | mulOp . COUT factor
20 | mulOp . CIN factor
21 | mulOp . AMP factor
22 | mulOp . AMPCARAT factor
TIMES shift, and go to state 41
DIV shift, and go to state 42
MOD shift, and go to state 43
AMP shift, and go to state 44
COUT shift, and go to state 45
CIN shift, and go to state 46
AMPCARAT shift, and go to state 47
TIMES [reduce using rule 15 (addOp)]
AMP [reduce using rule 15 (addOp)]
$default reduce using rule 15 (addOp)
我已经没有了想法,也会感激我能得到的任何帮助。
答案 0 :(得分:-1)
使用优先级和关联性指令,您应该首先简化语法,让它们工作,并检查仍然出现的冲突......
exp: exp OR exp {}
| exp AND exp {}
| exp NEQ exp {}
| ...
| exp PLUS exp {}
| ...
| exp '*' exp {}
| '(' exp ')' {}
| MINUS exp %prec UMINUS {}
| ...
| tDEC {}
| ...
;