C语言 野牛中的一元优先级

f3temu5u  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(99)

当解析器试图分辨某个东西是一元运算符还是二元运算符时,我不断地遇到移位/归约冲突。

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


我已经没有办法了,如果能得到任何帮助,我将不胜感激。

wr98u20j

wr98u20j1#

我不知道下面的内容会有多大帮助,但这样的东西对我很有用:

exp :
     '(' exp ')'
   | unary_exp
   | exp bin_op term
   | exp bin_op unary_exp
   | term

unary_exp : u_op exp %prec FIRST
term : CONSTANT | NUM

bin_op : '+' | '-' | '*' | '/'
u_op   : '-'

字符串
关于prec的更多信息

vh0rcniy

vh0rcniy2#

对于优先和结合指令,你应该从简化语法开始,让它们工作,并检查仍然出现的冲突。

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

字符串

相关问题