Next: Invoking Bison Prev: Handling Context Dependencies
If a Bison grammar compiles properly but doesn't do what you want when it runs, the yydebug
parser-trace feature can help you figure out why.
To enable compilation of trace facilities, you must define the macro YYDEBUG
when you compile the parser. You could use `-DYYDEBUG=1' as a compiler option or you could put `#define YYDEBUG 1' in the C declarations section of the grammar file (See The C Declarations Section). Alternatively, use the `-t' option when you run Bison (See Invoking Bison). We always define YYDEBUG
so that debugging is always possible.
The trace facility uses stderr
, so you must add #include <stdio.h>
Once you have compiled the program with trace facilities, the way to request a trace is to store a nonzero value in the variable yydebug
. You can do this by making the C code do it (in main
, perhaps), or you can alter the value with a C debugger.
Each step taken by the parser when yydebug
is nonzero produces a line or two of trace information, written on stderr
. The trace messages tell you these things:
yylex
, what kind of token was read.
The parser file is a C program and you can use C debuggers on it, but it's not easy to interpret what it is doing. The parser function is a finite-state machine interpreter, and aside from the actions it executes the same code over and over. Only the values of variables show where in the grammar it is working.
Here is an example of YYPRINT
suitable for the multi-function calculator (See Declarations for mfcalc
):
#define YYPRINT(file, type, value) yyprint (file, type, value) static void yyprint (file, type, value) FILE *file; int type; YYSTYPE value; { if (type == VAR) fprintf (file, " %s", value.tptr->name); else if (type == NUM) fprintf (file, " %d", value.val); }