Yacc/Bison, minimize amount by grouping math ops

后端 未结 2 1084
抹茶落季
抹茶落季 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:18

    You can do it in 2 ways:

    • At lex stage define recognition of operators and provide terminal symbol (in you syntax mathOp) with value of operator '+', '-' ...
    • Using mathOp as nonterminal you can return some associated value:

      mathOp : '+' { $$ = '+'; } | '-' { $$ = '-'; } ...

    Then usage will look like (pay attention to $2):

    | expr mathOp expr         { $$ = opr($2, 2, $1, $3); }
    

    may be you would like to define more complicated mathOp then use %type

提交回复
热议问题