Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with yylval and yylloc after switching to bison 3.0

Tags:

c++

bison

after upgrading from bison 2.7 to 3.0 I changed the following two lines in my parser definition file (.yy)

-------old--------
%define parser_class_name smathparser
%name-prefix = "imath"
-------new--------
%define api.prefix {imath}
%define parser_class_name {smathparser}
-------------------

and the result is that I get the following compiler errors when compiling the parser (!) file (compiling the lexer file gives no errors even though it uses yylval heavily):

error: ‘yylval’ was not declared in this scope
error: ‘yylloc’ was not declared in this scope

I searched everywhere I could think of but I couldn't see where yylval and yylloc are being defined. Have they been renamed? Deprecated? Why do they work in the lexer but not in the parser????

For clarification: I am actually using yylval and yylloc in the code of the parser.

like image 835
jrheinlaender Avatar asked Mar 20 '23 14:03

jrheinlaender


2 Answers

yylval and yylloc are set in the lexer, not in the parser. Traditionally, these are global variables, but in a "pure" (re-entrant) parser, they are internal to the parser, and their addresses are passed to the scanner as arguments. C++ parsers are always pure.

Since 3.0, these values are no longer independent variables in the parser; rather, they are part of a class representing a symbol. That change doesn't affect the scanner, since it still receives the same pointers as arguments, but it makes the values inaccessible by name to parser actions.

Clearly, that incompatibility should be documented somewhere, but I haven't found where.

Nonetheless, it's worth noting that you rarely need to refer to the semantic value and location of the lookahead token in a parser action, although doing so has been supported since time immemorial and continues to work in C parsers, and even in GLR parsers. I'm sure you have your reasons, and while I'm curious as to what they are, you are under no obligation to reveal them. Examination of the C++ skeleton code suggests that you should be able to use yyla.value and yyla.location instead of yylval and yylloc, but that's not documented so there are no guarantees.

If we're lucky, Akim Demaille will come by and answer this question properly. Personally, I use the C interface, even with C++; old habits die hard.

like image 152
rici Avatar answered Mar 22 '23 23:03

rici


In a .yy file of ours that was similarly affected by the upgrade to bison 3.0, I replaced yylloc with @$.

like image 31
Fabio Somenzi Avatar answered Mar 23 '23 00:03

Fabio Somenzi