English 中文(简体)
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()




相关问题
How to turn this into a parser

If I just add on to the following yacc file, will it turn into a parser? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %token VOID %token WHILE %token ID %token NUM %...

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. ...

What s wrong with this yacc file?

When I run yacc -d parser.y on the following file I get the following errors: parser.y:23.3-24.4: warning: unused value: $4 15 rules never reduced parser.y: warning: 7 useless nonterminals and 15 ...

How to find shift/reduce conflict in this yacc file?

When I try to use yacc on the following file I get the error conflicts: 1 shift/reduce How can I find and fix the conflict? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %...

Deriving a state machine from a BNF grammar

I am trying to put together a proof of concept of an XSS-safe string interpolation scheme. Given a string with substitutions, "Hello <b>$planetoid</b>!" I want break it into literal ...

热门标签