1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html40/loose.dtd">
<HTML>
<!-- Created on January, 28 2005 by texi2html 1.66 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people <dev@texi2html.cvshome.org>
Send bugs and suggestions to <users@texi2html.cvshome.org>
-->
<HEAD>
<TITLE>Bison 2.21.5: Debugging</TITLE>
<META NAME="description" CONTENT="Bison 2.21.5: Debugging">
<META NAME="keywords" CONTENT="Bison 2.21.5: Debugging">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.66">
</HEAD>
<BODY LANG="en" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<A NAME="SEC85"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_10.html#SEC84"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_12.html#SEC86"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_10.html#SEC81"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_12.html#SEC86"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 8. Debugging Your Parser </H1>
<!--docid::SEC85::-->
<P>
If a Bison grammar compiles properly but doesn't do what you want when it
runs, the <CODE>yydebug</CODE> parser-trace feature can help you figure out why.
</P>
<P>
To enable compilation of trace facilities, you must define the macro
<CODE>YYDEBUG</CODE> when you compile the parser. You could use
`<SAMP>-DYYDEBUG=1</SAMP>' as a compiler option or you could put `<SAMP>#define
YYDEBUG 1</SAMP>' in the C declarations section of the grammar file
(see section <A HREF="bison_6.html#SEC35">The C Declarations Section</A>). Alternatively, use the `<SAMP>-t</SAMP>' option when
you run Bison (see section <A HREF="bison_12.html#SEC86">Invoking Bison</A>). We always define <CODE>YYDEBUG</CODE> so that
debugging is always possible.
</P>
<P>
The trace facility uses <CODE>stderr</CODE>, so you must add <CODE>#include
<stdio.h></CODE> to the C declarations section unless it is already there.
</P>
<P>
Once you have compiled the program with trace facilities, the way to
request a trace is to store a nonzero value in the variable <CODE>yydebug</CODE>.
You can do this by making the C code do it (in <CODE>main</CODE>, perhaps), or
you can alter the value with a C debugger.
</P>
<P>
Each step taken by the parser when <CODE>yydebug</CODE> is nonzero produces a
line or two of trace information, written on <CODE>stderr</CODE>. The trace
messages tell you these things:
</P>
<P>
<UL>
<LI>
Each time the parser calls <CODE>yylex</CODE>, what kind of token was read.
<P>
</P>
<LI>
Each time a token is shifted, the depth and complete contents of the
state stack (see section <A HREF="bison_8.html#SEC76">5.5 Parser States</A>).
<P>
</P>
<LI>
Each time a rule is reduced, which rule it is, and the complete contents
of the state stack afterward.
</UL>
<P>
To make sense of this information, it helps to refer to the listing file
produced by the Bison `<SAMP>-v</SAMP>' option (see section <A HREF="bison_12.html#SEC86">Invoking Bison</A>). This file
shows the meaning of each state in terms of positions in various rules, and
also what each state will do with each possible input token. As you read
the successive trace messages, you can see that the parser is functioning
according to its specification in the listing file. Eventually you will
arrive at the place where something undesirable happens, and you will see
which parts of the grammar are to blame.
</P>
<P>
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.
</P>
<P>
<A NAME="IDX45"></A>
The debugging information normally gives the token type of each token
read, but not its semantic value. You can optionally define a macro
named <CODE>YYPRINT</CODE> to provide a way to print the value. If you define
<CODE>YYPRINT</CODE>, it should take three arguments. The parser will pass a
standard I/O stream, the numeric code for the token type, and the token
value (from <CODE>yylval</CODE>).
</P>
<P>
Here is an example of <CODE>YYPRINT</CODE> suitable for the multi-function
calculator (see section <A HREF="bison_5.html#SEC29">Declarations for <CODE>mfcalc</CODE></A>):
</P>
<P>
<TABLE><tr><td> </td><td class=smallexample><pre><FONT SIZE=-1>#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);
}
</FONT></pre></td></tr></table><P>
<A NAME="Invocation"></A>
<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_10.html#SEC81"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_12.html#SEC86"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
by <I>Frank B. Brokken</I> on <I>January, 28 2005</I>
using <A HREF="http://texi2html.cvshome.org"><I>texi2html</I></A>
</FONT>
</BODY>
</HTML>
|