开发者

Bison/Yacc, make literal token return its own value?

Below is my rule, when i replace $2 with '=' my code works. I know by default all literal tokens uses their ascii value (hence why multi character token require a definition)

The below doesnt work. The 开发者_高级运维function is called with 0 instead of '=' like i expect. is there an option i can set? (It doesn't appear so via man pages)

AssignExpr: var '=' rval            { $$ = func($1, $2, $3); } 

In another piece of code i have MathOp: '=' | '+' | '%' ... hence why i am interested.


The value for $2 in this context will be whatever the yylex function put into the global variable yylval before it returned the token '='. If the lexer doesn't put anything into yylval, it will probably still be 0, as you're seeing.


I think you are right, Bison just doesn't work that way.

You can easily fix it, of course:

  • just declare a token for =, recognize it in your lexer, and return its semantic value, or...
  • declare a production for it and return it with $$ or...
  • assign '=' to yylval in yylex()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜