yacc/bison tokens error. '>>>' and '>>' both assigned number 62

不打扰是莪最后的温柔 提交于 2019-12-11 07:30:07

问题


I do something like this in my code

CmpExpr:
      rval '<<' rval           { $$ = $1 << $3; }
    | rval '>>' rval           { $$ = $1 >> $3; }
    | rval '>>>' rval          { $$ = (unsigned)($1) >> ($3); }
    ;

the warning i get is

tokens '>>>' and '>>' both assigned number 62

How do i make it use different tokens?


回答1:


%TOKEN LSHIFT RSHIFT RRSHIFT

in lex write

"<<"        { return LSHIFT; }
">>"        { return RSHIFT; }
">>>"       { return RRSHIFT; }

then you can write

CmpExpr:
      rval LSHIFT  rval           { $$ = $1 << $3; }
    | rval RSHIFT  rval           { $$ = $1 >> $3; }
    | rval RRSHIFT rval           { $$ = (unsigned)($1) >> ($3); }

I think you can write "<<" instead of LSHIFT since it compiles but i have no idea if it runs differently




回答2:


You can only have a single character between the quotes in bison -- any multiple character token must be recognized by the lexer as such and returned as a single token, as described by acidzombie

When you put multiple characters in quotes in bison (as you have done), it essentially just ignores all except for the first, which means '>>' and '>>>' are really the same token (the same as '>'), giving the error you see. This is not terribly useful behavior, but was inherited from the original yacc program.



来源:https://stackoverflow.com/questions/1515294/yacc-bison-tokens-error-and-both-assigned-number-62

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!