title: Flex/Bison: cannot use semantic_type
body: <p>I try to create a c++ flex/bison parser. I used <a href="http://www.jonathanbeard.io/tutorials/FlexBisonC++" rel="nofollow noreferrer">this tutorial</a> as a starting point and did not change any bison/flex configurations. I am stuck now to the point of trying to unit test the lexer.</p>
  
  <p>I have a function in my unit tests that directly calls yylex, and checks the result of it:</p>
  
  <pre><code>private: static void checkIntToken(MyScanner &amp;scanner, Compiler *comp, unsigned long expected, unsigned char size, char isUnsigned, unsigned int line, const std::string &amp;label) {
      yy::MyParser::location_type loc;
      yy::MyParser::semantic_type semantic; // &lt;---- is seems like the destructor of this variable causes the crash
  
      int type = scanner.yylex(&amp;semantic, &amp;loc, comp);
      Assert::equals(yy::MyParser::token::INT, type, label + "__1");
  
      MyIntToken* token = semantic.as&lt;MyIntToken*&gt;();
      Assert::equals(expected, token-&gt;value, label + "__2");
      Assert::equals(size, token-&gt;size, label + "__3");
      Assert::equals(isUnsigned, token-&gt;isUnsigned, label + "__4");
      Assert::equals(line, loc.begin.line, label + "__5");
  
      //execution comes to this point, and then, program crashes
  }
  </code></pre>
  
  <p>The error message is:</p>
  
  <pre><code>program: ../src/__autoGenerated__/MyParser.tab.hh:190: yy::variant&lt;32&gt;::~variant() [S = 32]: Assertion `!yytypeid_' failed.
  </code></pre>
  
  <p>I have tried to follow the logic in the auto-generated bison files, and make some sense out of it. But I did not succeed on that and ultimately gave up. I searched then for any advice on the web about this error message but did not find any.</p>
  
  <p>The location indicated by the error has the following code:</p>
  
  <pre><code>~variant (){
    YYASSERT (!yytypeid_);
  }
  </code></pre>
  
  <p>EDIT: The problem disappears only if I remove the</p>
  
  <pre><code>%define parse.assert
  </code></pre>
  
  <p>option from the bison file. But I am not sure if this is a good idea...</p>
  
  <p>What is the proper way to obtain the value of the token generated by flex, for unit testing purposes?</p>