How can I print whatever I see in Yacc/Bison?
<p>I have a complicated Yacc file with a bunch of rules, some of them complicated, for example:</p>

<pre><code>start: program
program: extern_list class
class: T_CLASS T_ID T_LCB field_dec_list method_dec_list T_RCB
</code></pre>

<p>The exact rules and the actions I take on them are not important, because what I want to do seems fairly simple: just print out the program as it appears in the source file, using the rules I define for other purposes. But I'm surprised at how difficult doing so is.</p>

<p>First I tried adding <code>printf("%s%s", $1, $2)</code> to the second rule above. This produced "��@P�@". From what I understand, the parsed text is also available as a variable, <code>yytext</code>. I added <code>printf("%s", yytext)</code> to every rule in the file and added <code>extern char* yytext;</code> to the top of the file. This produced <code>(null){void)1133331122222210101010--552222202020202222;;;;||||&amp;&amp;&amp;&amp;;;;;;;;;;;}}}}}}}}</code> from a valid file according to the language's syntax. Finally, I changed <code>extern char* yytext;</code> to <code>extern char yytext[]</code>, thinking it would not make a difference. The difference in output it made is best shown as a screenshot<a href="https://i.stack.imgur.com/saDHR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/saDHR.png" alt="Gibberish"></a></p>

<p>I am using Bison 3.0.2 on Xubuntu 14.04.</p>