Yacc/Bison, minimize amount by grouping math ops

后端 未结 2 1075
抹茶落季
抹茶落季 2020-12-21 04:24

I am looking at the calc source here http://epaperpress.com/lexandyacc/

I see theses lines in calc.y

| expr \'+\' expr         { $$ = opr(\'+\', 2,          


        
2条回答
  •  甜味超标
    2020-12-21 05:14

    The problem with grouping them like that is that you lose the precedences on the rules -- you only have one rule that has different precedence depending on which mathop it is, which bison/yacc cannot handle. That said, you CAN group ops of the same precedence level together

    expr: expr mulOp expr { $$ = opr($2, 2, $1, $3); } %prec '*'
        | expr addOp expr { $$ = opr($2, 2, $1, $3); } %prec '+'
        | expr relOp expr { $$ = opr($2, 2, $1, $3); } %prec '<'
                 :
    
    mulOp: '*' { $$ = '*'; }
         | '/' { $$ = '/'; }
    ;
    

提交回复
热议问题