gpt4 book ai didi

c - Bison 中的一元优先级

转载 作者:行者123 更新时间:2023-11-30 18:12:59 24 4
gpt4 key购买 nike

当解析器尝试确定某物是一元运算符还是二元运算符时,我不断遇到移位/归约冲突。

%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)

我已经没有想法了,非常感谢我能得到的任何帮助。

最佳答案

对于优先级和关联性指令,您应该首先简化语法,让它们发挥作用,并检查仍然出现哪些冲突...

exp: exp OR exp {}
| exp AND exp {}
| exp NEQ exp {}
| ...
| exp PLUS exp {}
| ...
| exp '*' exp {}
| '(' exp ')' {}
| MINUS exp %prec UMINUS {}
| ...
| tDEC {}
| ...
;

关于c - Bison 中的一元优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28508115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com