Yacc Debugging |
Yacc has facilities that enable debugging. This feature may vary with different versions of
yacc so be sure to consult documentation for details. The code generated by yacc in file y.tab.c
includes debugging statements that are enabled by defining YYDEBUG
and setting it to a non-zero value. This may also be done by specifying
command-line option "-t
". With YYDEBUG
properly set, debug output may be
toggled on and off by setting yydebug
. Output includes tokens scanned and shift/reduce
actions.
%{ #define YYDEBUG 1 %} %% ... %% int main(void) { #if YYDEBUG yydebug = 1; #endif yylex(); }
In addition, you can dump the parse states by specifying command-line option "-v
".
States are dumped to file y.output
, and are often useful when debugging a grammar.
Alternatively you can write your own debug code by defining a TRACE
macro as
illustrated below. When DEBUG
is defined a trace of reductions, by line number, is
displayed.
%{ #ifdef DEBUG #define TRACE printf("reduce at line %d\n", __LINE__); #else #define TRACE #endif %} %% statement_list: statement { TRACE $$ = $1; } | statement_list statement { TRACE $$ = newNode(';', 2, $1, $2); } ;