Articles
Software
Gallery
Other works
Contacts

#Having a std::string (or any arbitrary type) in Bisons yylval.html



        >the key is knowing that %code takes an optional location qualifier
        >"requires" will place our definition providing headers high enough,
           that they are above the yylval union, unlike standard code blocks
           and default %code directives
        >no, "top" would not work; how did you know?
        >this DOES mean that those includes are going in your bison generated header,
          whether you want them or not; if it bothers you, good luck macro-juggling
        {
            /* @BAKE
                bison -o string.tab.c $@
                g++ string.tab.c -o string
                ./string
               @STOP
            */
            %code requires {
                #include 
                #include 
                void yyerror(...) { ; }
            }
            %union {
                std::string * strval;
            }
            %{
                int yylex() {
                    yylval.strval = new std::string("There are only two hard problems in CS: cache invalidation, naming things, and off-by-one errors.");
                    return STRING;
                }
            %}
            %token STRING
            %%
            string: STRING { std::cout << *(yylval.strval) << std::endl; }
            %%
            signed main() { yyparse(); }
        }
        >NOTE: that a string* must be used because, behold: %union creates an union,
                meaning we can only have trivial constructors