c-Bison中的一元优先级



当解析器试图区分某个运算符是一元运算符还是二元运算符时,我不断地得到移位/减少冲突。

%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 {}
   | ...
   ;

相关内容

  • 没有找到相关文章

最新更新