title: Yacc/Bison: The pseudo-variables ($$, $1, $2,..) and how to print them using printf
body: <p>I have a lexical analyser written in flex that passes tokens to my parser written in bison.</p>
  
  <p>The following is a small part of my lexer:</p>
  
  <pre><code>ID [a-z][a-z0-9]*
  
  %%
  
  rule {
      printf("A rule: %s\n", yytext);
      return RULE;
  }
  
  {ID} { 
      printf( "An identifier: %s\n", yytext );
      return ID;
  }
  
  "(" return LEFT;
  ")" return RIGHT;
  </code></pre>
  
  <p>There are other bits for parsing whitespace etc too.</p>
  
  <p>Then part of the parser looks like this:</p>
  
  <pre><code>%{
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define YYSTYPE char*
  %}
  
  %token ID RULE 
  %token LEFT RIGHT 
  
  %%
  
  rule_decl : 
      RULE LEFT ID RIGHT { printf("Parsing a rule, its identifier is: %s\n", $2); }
      ;
  
  %%
  </code></pre>
  
  <p>It's all working fine but I just want to print out the ID token using printf - that's all :).  I'm not writing a compiler.. it's just that flex/bison are good tools for my software.  How are you meant to print tokens?  I just get <code>(null)</code> when I print.</p>
  
  <p>Thank you.</p>