yacc/bison tokens error. '>>>' and '>>

2019-08-03 06: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.



标签: bison