2505 |
useful semantic value associated with the @samp{+} token, it could be |
useful semantic value associated with the @samp{+} token, it could be |
2506 |
referred to as @code{$2}. |
referred to as @code{$2}. |
2507 |
|
|
2508 |
|
Note that the vertical-bar character @samp{|} is really a rule |
2509 |
|
separator, and actions are attached to a single rule. This is a |
2510 |
|
difference with tools like Flex, for which @samp{|} stands for either |
2511 |
|
``or'', or ``the same action as that of the next rule''. In the |
2512 |
|
following example, the action is triggered only when @samp{b} is found: |
2513 |
|
|
2514 |
|
@example |
2515 |
|
@group |
2516 |
|
a-or-b: 'a'|'b' @{ a_or_b_found = 1; @}; |
2517 |
|
@end group |
2518 |
|
@end example |
2519 |
|
|
2520 |
@cindex default action |
@cindex default action |
2521 |
If you don't specify an action for a rule, Bison supplies a default: |
If you don't specify an action for a rule, Bison supplies a default: |
2522 |
@w{@code{$$ = $1}.} Thus, the value of the first symbol in the rule becomes |
@w{@code{$$ = $1}.} Thus, the value of the first symbol in the rule becomes |
4960 |
|
|
4961 |
@node Debugging |
@node Debugging |
4962 |
@chapter Debugging Your Parser |
@chapter Debugging Your Parser |
|
@findex YYDEBUG |
|
4963 |
@findex yydebug |
@findex yydebug |
4964 |
@cindex debugging |
@cindex debugging |
4965 |
@cindex tracing the parser |
@cindex tracing the parser |
4967 |
If a Bison grammar compiles properly but doesn't do what you want when it |
If a Bison grammar compiles properly but doesn't do what you want when it |
4968 |
runs, the @code{yydebug} parser-trace feature can help you figure out why. |
runs, the @code{yydebug} parser-trace feature can help you figure out why. |
4969 |
|
|
4970 |
To enable compilation of trace facilities, you must define the macro |
There are several means to enable compilation of trace facilities: |
4971 |
@code{YYDEBUG} to a nonzero value when you compile the parser. You |
|
4972 |
could use @samp{-DYYDEBUG=1} as a compiler option or you could put |
@table @asis |
4973 |
@samp{#define YYDEBUG 1} in the prologue of the grammar file |
@item the macro @code{YYDEBUG} |
4974 |
(@pxref{Prologue, , The Prologue}). Alternatively, use the @samp{-t} |
@findex YYDEBUG |
4975 |
option when you run Bison (@pxref{Invocation, ,Invoking Bison}) or the |
Define the macro @code{YYDEBUG} to a nonzero value when you compile the |
4976 |
@code{%debug} declaration (@pxref{Decl Summary, ,Bison Declaration |
parser. This is compliant with POSIX Yacc. You could use |
4977 |
Summary}). We suggest that you always define @code{YYDEBUG} so that |
@samp{-DYYDEBUG=1} as a compiler option or you could put @samp{#define |
4978 |
debugging is always possible. |
YYDEBUG 1} in the prologue of the grammar file (@pxref{Prologue, , The |
4979 |
|
Prologue}). |
4980 |
|
|
4981 |
|
@item the option @option{-t}, @option{--debug} |
4982 |
|
Use the @samp{-t} option when you run Bison (@pxref{Invocation, |
4983 |
|
,Invoking Bison}). This is POSIX compliant too. |
4984 |
|
|
4985 |
|
@item the directive @samp{%debug} |
4986 |
|
@findex %debug |
4987 |
|
Add the @code{%debug} directive (@pxref{Decl Summary, ,Bison |
4988 |
|
Declaration Summary}). This is a Bison extension, which will prove |
4989 |
|
useful when Bison will output parsers for languages that don't use a |
4990 |
|
preprocessor. Useless POSIX and Yacc portability matter to you, this is |
4991 |
|
the preferred solution. |
4992 |
|
@end table |
4993 |
|
|
4994 |
|
We suggest that you always enable the debug option so that debugging is |
4995 |
|
always possible. |
4996 |
|
|
4997 |
The trace facility outputs messages with macro calls of the form |
The trace facility outputs messages with macro calls of the form |
4998 |
@code{YYFPRINTF (stderr, @var{format}, @var{args})} where |
@code{YYFPRINTF (stderr, @var{format}, @var{args})} where |
5335 |
@cindex symbols in Bison, table of |
@cindex symbols in Bison, table of |
5336 |
|
|
5337 |
@table @code |
@table @code |
5338 |
|
@item @@$ |
5339 |
|
In an action, the location of the left-hand side of the rule. |
5340 |
|
@xref{Locations, , Locations Overview}. |
5341 |
|
|
5342 |
|
@item @@@var{n} |
5343 |
|
In an action, the location of the @var{n}-th symbol of the right-hand |
5344 |
|
side of the rule. @xref{Locations, , Locations Overview}. |
5345 |
|
|
5346 |
|
@item $$ |
5347 |
|
In an action, the semantic value of the left-hand side of the rule. |
5348 |
|
@xref{Actions}. |
5349 |
|
|
5350 |
|
@item $@var{n} |
5351 |
|
In an action, the semantic value of the @var{n}-th symbol of the |
5352 |
|
right-hand side of the rule. @xref{Actions}. |
5353 |
|
|
5354 |
@item error |
@item error |
5355 |
A token name reserved for error recovery. This token may be used in |
A token name reserved for error recovery. This token may be used in |
5356 |
grammar rules so as to allow the Bison parser to recognize an error in |
grammar rules so as to allow the Bison parser to recognize an error in |
5376 |
Macro to discard a value from the parser stack and fake a look-ahead |
Macro to discard a value from the parser stack and fake a look-ahead |
5377 |
token. @xref{Action Features, ,Special Features for Use in Actions}. |
token. @xref{Action Features, ,Special Features for Use in Actions}. |
5378 |
|
|
5379 |
|
@item YYDEBUG |
5380 |
|
Macro to define to equip the parser with tracing code. @xref{Debugging, |
5381 |
|
,Debugging Your Parser}. |
5382 |
|
|
5383 |
@item YYERROR |
@item YYERROR |
5384 |
Macro to pretend that a syntax error has just been detected: call |
Macro to pretend that a syntax error has just been detected: call |
5385 |
@code{yyerror} and then perform normal error recovery if possible |
@code{yyerror} and then perform normal error recovery if possible |
5553 |
values. @xref{Union Decl, ,The Collection of Value Types}. |
values. @xref{Union Decl, ,The Collection of Value Types}. |
5554 |
@end table |
@end table |
5555 |
|
|
5556 |
|
@sp 1 |
5557 |
|
|
5558 |
These are the punctuation and delimiters used in Bison input: |
These are the punctuation and delimiters used in Bison input: |
5559 |
|
|
5560 |
@table @samp |
@table @samp |