Manual
Manual
Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 8: Debugging Your Parser 95<br />
’+’ shift, and go to state 4<br />
’-’ shift, and go to state 5<br />
’*’ shift, and go to state 6<br />
’/’ shift, and go to state 7<br />
’+’ [reduce using rule 4 (exp)]<br />
’-’ [reduce using rule 4 (exp)]<br />
’*’ [reduce using rule 4 (exp)]<br />
’/’ [reduce using rule 4 (exp)]<br />
$default reduce using rule 4 (exp)<br />
Observe that state 11 contains conflicts not only due to the lack of precedence of ‘/’ with<br />
respect to ‘+’, ‘-’, and ‘*’, but also because the associativity of ‘/’ is not specified.<br />
8.2 Tracing Your Parser<br />
If a Bison grammar compiles properly but doesn’t do what you want when it runs, the<br />
yydebug parser-trace feature can help you figure out why.<br />
There are several means to enable compilation of trace facilities:<br />
the macro YYDEBUG<br />
Define the macro YYDEBUG to a nonzero value when you compile the parser.<br />
This is compliant with POSIX Yacc. You could use ‘-DYYDEBUG=1’ as a compiler<br />
option or you could put ‘#define YYDEBUG 1’ in the prologue of the grammar<br />
file (see Section 3.1.1 [The Prologue], page 41).<br />
the option ‘-t’, ‘--debug’<br />
Use the ‘-t’ option when you run Bison (see Chapter 9 [Invoking Bison],<br />
page 97). This is POSIX compliant too.<br />
the directive ‘%debug’<br />
Add the %debug directive (see Section 3.7.11 [Bison Declaration Summary],<br />
page 59). This is a Bison extension, which will prove useful when Bison will<br />
output parsers for languages that don’t use a preprocessor. Unless POSIX and<br />
Yacc portability matter to you, this is the preferred solution.<br />
We suggest that you always enable the debug option so that debugging is always possible.<br />
The trace facility outputs messages with macro calls of the form YYFPRINTF (stderr,<br />
format, args) where format and args are the usual printf format and arguments. If you<br />
define YYDEBUG to a nonzero value but do not define YYFPRINTF, is automatically<br />
included and YYPRINTF is defined to fprintf.<br />
Once you have compiled the program with trace facilities, the way to request a trace is<br />
to store a nonzero value in the variable yydebug. You can do this by making the C code do<br />
it (in main, perhaps), or you can alter the value with a C debugger.<br />
Each step taken by the parser when yydebug is nonzero produces a line or two of trace<br />
information, written on stderr. The trace messages tell you these things:<br />
• Each time the parser calls yylex, what kind of token was read.<br />
• Each time a token is shifted, the depth and complete contents of the state stack (see<br />
Section 5.5 [Parser States], page 75).