5 |
@settitle Bison @value{VERSION} |
@settitle Bison @value{VERSION} |
6 |
@setchapternewpage odd |
@setchapternewpage odd |
7 |
|
|
|
@iftex |
|
8 |
@finalout |
@finalout |
|
@end iftex |
|
9 |
|
|
10 |
@c SMALL BOOK version |
@c SMALL BOOK version |
11 |
@c This edition has been formatted so that you can format and print it in |
@c This edition has been formatted so that you can format and print it in |
21 |
@c Check COPYRIGHT dates. should be updated in the titlepage, ifinfo |
@c Check COPYRIGHT dates. should be updated in the titlepage, ifinfo |
22 |
@c titlepage; should NOT be changed in the GPL. --mew |
@c titlepage; should NOT be changed in the GPL. --mew |
23 |
|
|
24 |
|
@c FIXME: I don't understand this `iftex'. Obsolete? --akim. |
25 |
@iftex |
@iftex |
26 |
@syncodeindex fn cp |
@syncodeindex fn cp |
27 |
@syncodeindex vr cp |
@syncodeindex vr cp |
153 |
* Error Recovery:: Writing rules for error recovery. |
* Error Recovery:: Writing rules for error recovery. |
154 |
* Context Dependency:: What to do if your language syntax is too |
* Context Dependency:: What to do if your language syntax is too |
155 |
messy for Bison to handle straightforwardly. |
messy for Bison to handle straightforwardly. |
156 |
* Debugging:: Debugging Bison parsers that parse wrong. |
* Debugging:: Understanding or debugging Bison parsers. |
157 |
* Invocation:: How to run Bison (to produce the parser source file). |
* Invocation:: How to run Bison (to produce the parser source file). |
158 |
* Table of Symbols:: All the keywords of the Bison language are explained. |
* Table of Symbols:: All the keywords of the Bison language are explained. |
159 |
* Glossary:: Basic concepts are explained. |
* Glossary:: Basic concepts are explained. |
298 |
* Tie-in Recovery:: Lexical tie-ins have implications for how |
* Tie-in Recovery:: Lexical tie-ins have implications for how |
299 |
error recovery rules must be written. |
error recovery rules must be written. |
300 |
|
|
301 |
|
Understanding or Debugging Your Parser |
302 |
|
|
303 |
|
* Understanding:: Understanding the structure of your parser. |
304 |
|
* Tracing:: Tracing the execution of your parser. |
305 |
|
|
306 |
Invoking Bison |
Invoking Bison |
307 |
|
|
308 |
* Bison Options:: All the options described in detail, |
* Bison Options:: All the options described in detail, |
711 |
those cases your code should respect the identifiers reserved by those |
those cases your code should respect the identifiers reserved by those |
712 |
headers. On some non-@sc{gnu} hosts, @code{<alloca.h>}, |
headers. On some non-@sc{gnu} hosts, @code{<alloca.h>}, |
713 |
@code{<stddef.h>}, and @code{<stdlib.h>} are included as needed to |
@code{<stddef.h>}, and @code{<stdlib.h>} are included as needed to |
714 |
declare memory allocators and related types. |
declare memory allocators and related types. Other system headers may |
715 |
Other system headers may be included if you define @code{YYDEBUG} to a |
be included if you define @code{YYDEBUG} to a nonzero value |
716 |
nonzero value (@pxref{Debugging, ,Debugging Your Parser}). |
(@pxref{Tracing, ,Tracing Your Parser}). |
717 |
|
|
718 |
@node Stages |
@node Stages |
719 |
@section Stages in Using Bison |
@section Stages in Using Bison |
2355 |
@end example |
@end example |
2356 |
|
|
2357 |
@noindent |
@noindent |
2358 |
Any kind of sequence can be defined using either left recursion or |
Any kind of sequence can be defined using either left recursion or right |
2359 |
right recursion, but you should always use left recursion, because it |
recursion, but you should always use left recursion, because it can |
2360 |
can parse a sequence of any number of elements with bounded stack |
parse a sequence of any number of elements with bounded stack space. |
2361 |
space. Right recursion uses up space on the Bison stack in proportion |
Right recursion uses up space on the Bison stack in proportion to the |
2362 |
to the number of elements in the sequence, because all the elements |
number of elements in the sequence, because all the elements must be |
2363 |
must be shifted onto the stack before the rule can be applied even |
shifted onto the stack before the rule can be applied even once. |
2364 |
once. @xref{Algorithm, ,The Bison Parser Algorithm }, for |
@xref{Algorithm, ,The Bison Parser Algorithm}, for further explanation |
2365 |
further explanation of this. |
of this. |
2366 |
|
|
2367 |
@cindex mutual recursion |
@cindex mutual recursion |
2368 |
@dfn{Indirect} or @dfn{mutual} recursion occurs when the result of the |
@dfn{Indirect} or @dfn{mutual} recursion occurs when the result of the |
3280 |
@item %debug |
@item %debug |
3281 |
In the parser file, define the macro @code{YYDEBUG} to 1 if it is not |
In the parser file, define the macro @code{YYDEBUG} to 1 if it is not |
3282 |
already defined, so that the debugging facilities are compiled. |
already defined, so that the debugging facilities are compiled. |
3283 |
@xref{Debugging, ,Debugging Your Parser}. |
@xref{Tracing, ,Tracing Your Parser}. |
3284 |
|
|
3285 |
@item %defines |
@item %defines |
3286 |
Write an extra output file containing macro definitions for the token |
Write an extra output file containing macro definitions for the token |
3390 |
@item %verbose |
@item %verbose |
3391 |
Write an extra output file containing verbose descriptions of the |
Write an extra output file containing verbose descriptions of the |
3392 |
parser states and what is done for each type of look-ahead token in |
parser states and what is done for each type of look-ahead token in |
3393 |
that state. |
that state. @xref{Understanding, , Understanding Your Parser}, for more |
3394 |
|
information. |
|
This file also describes all the conflicts, both those resolved by |
|
|
operator precedence and the unresolved ones. |
|
3395 |
|
|
|
The file's name is made by removing @samp{.tab.c} or @samp{.c} from |
|
|
the parser output file name, and adding @samp{.output} instead. |
|
3396 |
|
|
|
Therefore, if the input file is @file{foo.y}, then the parser file is |
|
|
called @file{foo.tab.c} by default. As a consequence, the verbose |
|
|
output file is called @file{foo.output}. |
|
3397 |
|
|
3398 |
@item %yacc |
@item %yacc |
3399 |
Pretend the option @option{--yacc} was given, i.e., imitate Yacc, |
Pretend the option @option{--yacc} was given, i.e., imitate Yacc, |
4951 |
be such that you can be sure that it always will, or always won't, have to |
be such that you can be sure that it always will, or always won't, have to |
4952 |
clear the flag. |
clear the flag. |
4953 |
|
|
4954 |
|
@c ================================================== Debugging Your Parser |
4955 |
|
|
4956 |
@node Debugging |
@node Debugging |
4957 |
@chapter Debugging Your Parser |
@chapter Debugging Your Parser |
4958 |
|
|
4959 |
|
Developing a parser can be a challenge, especially if you don't |
4960 |
|
understand the algorithm (@pxref{Algorithm, ,The Bison Parser |
4961 |
|
Algorithm}). Even so, sometimes a detailed description of the automaton |
4962 |
|
can help (@pxref{Understanding, , Understanding Your Parser}), or |
4963 |
|
tracing the execution of the parser can give some insight on why it |
4964 |
|
behaves improperly (@pxref{Tracing, , Tracing Your Parser}). |
4965 |
|
|
4966 |
|
@menu |
4967 |
|
* Understanding:: Understanding the structure of your parser. |
4968 |
|
* Tracing:: Tracing the execution of your parser. |
4969 |
|
@end menu |
4970 |
|
|
4971 |
|
@node Understanding |
4972 |
|
@section Understanding Your Parser |
4973 |
|
|
4974 |
|
As documented elsewhere (@pxref{Algorithm, ,The Bison Parser Algorithm}) |
4975 |
|
Bison parsers are @dfn{shift/reduce automata}. In some cases (much more |
4976 |
|
frequent than one would hope), looking at this automaton is required to |
4977 |
|
tune or simply fix a parser. Bison provides two different |
4978 |
|
representation of it, either textually or graphically (as a @sc{vcg} |
4979 |
|
file). |
4980 |
|
|
4981 |
|
The textual file is generated when the options @option{--report} or |
4982 |
|
@option{--verbose} are specified, see @xref{Invocation, , Invoking |
4983 |
|
Bison}. Its name is made by removing @samp{.tab.c} or @samp{.c} from |
4984 |
|
the parser output file name, and adding @samp{.output} instead. |
4985 |
|
Therefore, if the input file is @file{foo.y}, then the parser file is |
4986 |
|
called @file{foo.tab.c} by default. As a consequence, the verbose |
4987 |
|
output file is called @file{foo.output}. |
4988 |
|
|
4989 |
|
The following grammar file, @file{calc.y}, will be used in the sequel: |
4990 |
|
|
4991 |
|
@example |
4992 |
|
%token NUM STR |
4993 |
|
%left '+' '-' |
4994 |
|
%left '*' |
4995 |
|
%% |
4996 |
|
exp: exp '+' exp |
4997 |
|
| exp '-' exp |
4998 |
|
| exp '*' exp |
4999 |
|
| exp '/' exp |
5000 |
|
| NUM |
5001 |
|
; |
5002 |
|
useless: STR; |
5003 |
|
%% |
5004 |
|
@end example |
5005 |
|
|
5006 |
|
@command{bison} reports that @samp{calc.y contains 1 useless nonterminal |
5007 |
|
and 1 useless rule} and that @samp{calc.y contains 7 shift/reduce |
5008 |
|
conflicts}. When given @option{--report=state}, in addition to |
5009 |
|
@file{calc.tab.c}, it creates a file @file{calc.output} with contents |
5010 |
|
detailed below. The order of the output and the exact presentation |
5011 |
|
might vary, but the interpretation is the same. |
5012 |
|
|
5013 |
|
The first section includes details on conflicts that were solved thanks |
5014 |
|
to precedence and/or associativity: |
5015 |
|
|
5016 |
|
@example |
5017 |
|
Conflict in state 8 between rule 2 and token '+' resolved as reduce. |
5018 |
|
Conflict in state 8 between rule 2 and token '-' resolved as reduce. |
5019 |
|
Conflict in state 8 between rule 2 and token '*' resolved as shift. |
5020 |
|
@exdent @dots{} |
5021 |
|
@end example |
5022 |
|
|
5023 |
|
@noindent |
5024 |
|
The next section lists states that still have conflicts. |
5025 |
|
|
5026 |
|
@example |
5027 |
|
State 8 contains 1 shift/reduce conflict. |
5028 |
|
State 9 contains 1 shift/reduce conflict. |
5029 |
|
State 10 contains 1 shift/reduce conflict. |
5030 |
|
State 11 contains 4 shift/reduce conflicts. |
5031 |
|
@end example |
5032 |
|
|
5033 |
|
@noindent |
5034 |
|
@cindex token, useless |
5035 |
|
@cindex useless token |
5036 |
|
@cindex nonterminal, useless |
5037 |
|
@cindex useless nonterminal |
5038 |
|
@cindex rule, useless |
5039 |
|
@cindex useless rule |
5040 |
|
The next section reports useless tokens, nonterminal and rules. Useless |
5041 |
|
nonterminals and rules are removed in order to produce a smaller parser, |
5042 |
|
but useless tokens are preserved, since they might be used by the |
5043 |
|
scanner (note the difference between ``useless'' and ``not used'' |
5044 |
|
below): |
5045 |
|
|
5046 |
|
@example |
5047 |
|
Useless nonterminals: |
5048 |
|
useless |
5049 |
|
|
5050 |
|
Terminals which are not used: |
5051 |
|
STR |
5052 |
|
|
5053 |
|
Useless rules: |
5054 |
|
#6 useless: STR; |
5055 |
|
@end example |
5056 |
|
|
5057 |
|
@noindent |
5058 |
|
The next section reproduces the exact grammar that Bison used: |
5059 |
|
|
5060 |
|
@example |
5061 |
|
Grammar |
5062 |
|
|
5063 |
|
Number, Line, Rule |
5064 |
|
0 5 $axiom -> exp $ |
5065 |
|
1 5 exp -> exp '+' exp |
5066 |
|
2 6 exp -> exp '-' exp |
5067 |
|
3 7 exp -> exp '*' exp |
5068 |
|
4 8 exp -> exp '/' exp |
5069 |
|
5 9 exp -> NUM |
5070 |
|
@end example |
5071 |
|
|
5072 |
|
@noindent |
5073 |
|
and reports the uses of the symbols: |
5074 |
|
|
5075 |
|
@example |
5076 |
|
Terminals, with rules where they appear |
5077 |
|
|
5078 |
|
$ (0) 0 |
5079 |
|
'*' (42) 3 |
5080 |
|
'+' (43) 1 |
5081 |
|
'-' (45) 2 |
5082 |
|
'/' (47) 4 |
5083 |
|
error (256) |
5084 |
|
NUM (258) 5 |
5085 |
|
|
5086 |
|
Nonterminals, with rules where they appear |
5087 |
|
|
5088 |
|
$axiom (8) |
5089 |
|
on left: 0 |
5090 |
|
exp (9) |
5091 |
|
on left: 1 2 3 4 5, on right: 0 1 2 3 4 |
5092 |
|
@end example |
5093 |
|
|
5094 |
|
@noindent |
5095 |
|
@cindex item |
5096 |
|
@cindex pointed rule |
5097 |
|
@cindex rule, pointed |
5098 |
|
Bison then proceeds onto the automaton itself, describing each state |
5099 |
|
with it set of @dfn{items}, also known as @dfn{pointed rules}. Each |
5100 |
|
item is a production rule together with a point (marked by @samp{.}) |
5101 |
|
that the input cursor. |
5102 |
|
|
5103 |
|
@example |
5104 |
|
state 0 |
5105 |
|
|
5106 |
|
$axiom -> . exp $ (rule 0) |
5107 |
|
|
5108 |
|
NUM shift, and go to state 1 |
5109 |
|
|
5110 |
|
exp go to state 2 |
5111 |
|
@end example |
5112 |
|
|
5113 |
|
This reads as follows: ``state 0 corresponds to being at the very |
5114 |
|
beginning of the parsing, in the initial rule, right before the start |
5115 |
|
symbol (here, @code{exp}). When the parser returns to this state right |
5116 |
|
after having reduced a rule that produced an @code{exp}, the control |
5117 |
|
flow jumps to state 2. If there is no such transition on a nonterminal |
5118 |
|
symbol, and the lookahead is a @code{NUM}, then this token is shifted on |
5119 |
|
the parse stack, and the control flow jumps to state 1. Any other |
5120 |
|
lookahead triggers a parse error.'' |
5121 |
|
|
5122 |
|
@cindex core, item set |
5123 |
|
@cindex item set core |
5124 |
|
@cindex kernel, item set |
5125 |
|
@cindex item set core |
5126 |
|
Even though the only active rule in state 0 seems to be rule 0, the |
5127 |
|
report lists @code{NUM} as a lookahead symbol because @code{NUM} can be |
5128 |
|
at the beginning of any rule deriving an @code{exp}. By default Bison |
5129 |
|
reports the so-called @dfn{core} or @dfn{kernel} of the item set, but if |
5130 |
|
you want to see more detail you can invoke @command{bison} with |
5131 |
|
@option{--report=itemset} to list all the items, include those that can |
5132 |
|
be derived: |
5133 |
|
|
5134 |
|
@example |
5135 |
|
state 0 |
5136 |
|
|
5137 |
|
$axiom -> . exp $ (rule 0) |
5138 |
|
exp -> . exp '+' exp (rule 1) |
5139 |
|
exp -> . exp '-' exp (rule 2) |
5140 |
|
exp -> . exp '*' exp (rule 3) |
5141 |
|
exp -> . exp '/' exp (rule 4) |
5142 |
|
exp -> . NUM (rule 5) |
5143 |
|
|
5144 |
|
NUM shift, and go to state 1 |
5145 |
|
|
5146 |
|
exp go to state 2 |
5147 |
|
@end example |
5148 |
|
|
5149 |
|
@noindent |
5150 |
|
In the state 1... |
5151 |
|
|
5152 |
|
@example |
5153 |
|
state 1 |
5154 |
|
|
5155 |
|
exp -> NUM . (rule 5) |
5156 |
|
|
5157 |
|
$default reduce using rule 5 (exp) |
5158 |
|
@end example |
5159 |
|
|
5160 |
|
@noindent |
5161 |
|
the rule 5, @samp{exp: NUM;}, is completed. Whatever the lookahead |
5162 |
|
(@samp{$default}), the parser will reduce it. If it was coming from |
5163 |
|
state 0, then, after this reduction it will return to state 0, and will |
5164 |
|
jump to state 2 (@samp{exp: go to state 2}). |
5165 |
|
|
5166 |
|
@example |
5167 |
|
state 2 |
5168 |
|
|
5169 |
|
$axiom -> exp . $ (rule 0) |
5170 |
|
exp -> exp . '+' exp (rule 1) |
5171 |
|
exp -> exp . '-' exp (rule 2) |
5172 |
|
exp -> exp . '*' exp (rule 3) |
5173 |
|
exp -> exp . '/' exp (rule 4) |
5174 |
|
|
5175 |
|
$ shift, and go to state 3 |
5176 |
|
'+' shift, and go to state 4 |
5177 |
|
'-' shift, and go to state 5 |
5178 |
|
'*' shift, and go to state 6 |
5179 |
|
'/' shift, and go to state 7 |
5180 |
|
@end example |
5181 |
|
|
5182 |
|
@noindent |
5183 |
|
In state 2, the automaton can only shift a symbol. For instance, |
5184 |
|
because of the item @samp{exp -> exp . '+' exp}, if the lookahead if |
5185 |
|
@samp{+}, it will be shifted on the parse stack, and the automaton |
5186 |
|
control will jump to state 4, corresponding to the item @samp{exp -> exp |
5187 |
|
'+' . exp}. Since there is no default action, any other token than |
5188 |
|
those listed above will trigger a parse error. |
5189 |
|
|
5190 |
|
The state 3 is named the @dfn{final state}, or the @dfn{accepting |
5191 |
|
state}: |
5192 |
|
|
5193 |
|
@example |
5194 |
|
state 3 |
5195 |
|
|
5196 |
|
$axiom -> exp $ . (rule 0) |
5197 |
|
|
5198 |
|
$default accept |
5199 |
|
@end example |
5200 |
|
|
5201 |
|
@noindent |
5202 |
|
the initial rule is completed (the start symbol and the end |
5203 |
|
of input were read), the parsing exits successfully. |
5204 |
|
|
5205 |
|
The interpretation of states 4 to 7 is straightforward, and is left to |
5206 |
|
the reader. |
5207 |
|
|
5208 |
|
@example |
5209 |
|
state 4 |
5210 |
|
|
5211 |
|
exp -> exp '+' . exp (rule 1) |
5212 |
|
|
5213 |
|
NUM shift, and go to state 1 |
5214 |
|
|
5215 |
|
exp go to state 8 |
5216 |
|
|
5217 |
|
state 5 |
5218 |
|
|
5219 |
|
exp -> exp '-' . exp (rule 2) |
5220 |
|
|
5221 |
|
NUM shift, and go to state 1 |
5222 |
|
|
5223 |
|
exp go to state 9 |
5224 |
|
|
5225 |
|
state 6 |
5226 |
|
|
5227 |
|
exp -> exp '*' . exp (rule 3) |
5228 |
|
|
5229 |
|
NUM shift, and go to state 1 |
5230 |
|
|
5231 |
|
exp go to state 10 |
5232 |
|
|
5233 |
|
state 7 |
5234 |
|
|
5235 |
|
exp -> exp '/' . exp (rule 4) |
5236 |
|
|
5237 |
|
NUM shift, and go to state 1 |
5238 |
|
|
5239 |
|
exp go to state 11 |
5240 |
|
@end example |
5241 |
|
|
5242 |
|
As was announced in beginning of the report, @samp{State 8 contains 1 |
5243 |
|
shift/reduce conflict}: |
5244 |
|
|
5245 |
|
@example |
5246 |
|
state 8 |
5247 |
|
|
5248 |
|
exp -> exp . '+' exp (rule 1) |
5249 |
|
exp -> exp '+' exp . (rule 1) |
5250 |
|
exp -> exp . '-' exp (rule 2) |
5251 |
|
exp -> exp . '*' exp (rule 3) |
5252 |
|
exp -> exp . '/' exp (rule 4) |
5253 |
|
|
5254 |
|
'*' shift, and go to state 6 |
5255 |
|
'/' shift, and go to state 7 |
5256 |
|
|
5257 |
|
'/' [reduce using rule 1 (exp)] |
5258 |
|
$default reduce using rule 1 (exp) |
5259 |
|
@end example |
5260 |
|
|
5261 |
|
Indeed, there are two actions associated to the lookahead @samp{/}: |
5262 |
|
either shifting (and going to state 7), or reducing rule 1. The |
5263 |
|
conflict means that either the grammar is ambiguous, or the parser lacks |
5264 |
|
information to make the right decision. Indeed the grammar is |
5265 |
|
ambiguous, as, since we did not specify the precedence of @samp{/}, the |
5266 |
|
sentence @samp{NUM + NUM / NUM} can be parsed as @samp{NUM + (NUM / |
5267 |
|
NUM)}, which corresponds to shifting @samp{/}, or as @samp{(NUM + NUM) / |
5268 |
|
NUM}, which corresponds to reducing rule 1. |
5269 |
|
|
5270 |
|
Because in LALR(1) parsing a single decision can be made, Bison |
5271 |
|
arbitrarily chose to disable the reduction, see @ref{Shift/Reduce, , |
5272 |
|
Shift/Reduce Conflicts}. Discarded actions are reported in between |
5273 |
|
square brackets. |
5274 |
|
|
5275 |
|
Note that all the previous states had a single possible action: either |
5276 |
|
shifting the next token and going to the corresponding state, or |
5277 |
|
reducing a single rule. In the other cases, i.e., when shifting |
5278 |
|
@emph{and} reducing is possible or when @emph{several} reductions are |
5279 |
|
possible, the lookahead is required to select the action. State 8 is |
5280 |
|
one such state: if the lookahead is @samp{*} or @samp{/} then the action |
5281 |
|
is shifting, otherwise the action is reducing rule 1. In other words, |
5282 |
|
the first two items, corresponding to rule 1, are not eligible when the |
5283 |
|
lookahead is @samp{*}, since we specified that @samp{*} has higher |
5284 |
|
precedence that @samp{+}. More generally, some items are eligible only |
5285 |
|
with some set of possible lookaheads. When run with |
5286 |
|
@option{--report=lookahead}, Bison specifies these lookaheads: |
5287 |
|
|
5288 |
|
@example |
5289 |
|
state 8 |
5290 |
|
|
5291 |
|
exp -> exp . '+' exp [$, '+', '-', '/'] (rule 1) |
5292 |
|
exp -> exp '+' exp . [$, '+', '-', '/'] (rule 1) |
5293 |
|
exp -> exp . '-' exp (rule 2) |
5294 |
|
exp -> exp . '*' exp (rule 3) |
5295 |
|
exp -> exp . '/' exp (rule 4) |
5296 |
|
|
5297 |
|
'*' shift, and go to state 6 |
5298 |
|
'/' shift, and go to state 7 |
5299 |
|
|
5300 |
|
'/' [reduce using rule 1 (exp)] |
5301 |
|
$default reduce using rule 1 (exp) |
5302 |
|
@end example |
5303 |
|
|
5304 |
|
The remaining states are similar: |
5305 |
|
|
5306 |
|
@example |
5307 |
|
state 9 |
5308 |
|
|
5309 |
|
exp -> exp . '+' exp (rule 1) |
5310 |
|
exp -> exp . '-' exp (rule 2) |
5311 |
|
exp -> exp '-' exp . (rule 2) |
5312 |
|
exp -> exp . '*' exp (rule 3) |
5313 |
|
exp -> exp . '/' exp (rule 4) |
5314 |
|
|
5315 |
|
'*' shift, and go to state 6 |
5316 |
|
'/' shift, and go to state 7 |
5317 |
|
|
5318 |
|
'/' [reduce using rule 2 (exp)] |
5319 |
|
$default reduce using rule 2 (exp) |
5320 |
|
|
5321 |
|
state 10 |
5322 |
|
|
5323 |
|
exp -> exp . '+' exp (rule 1) |
5324 |
|
exp -> exp . '-' exp (rule 2) |
5325 |
|
exp -> exp . '*' exp (rule 3) |
5326 |
|
exp -> exp '*' exp . (rule 3) |
5327 |
|
exp -> exp . '/' exp (rule 4) |
5328 |
|
|
5329 |
|
'/' shift, and go to state 7 |
5330 |
|
|
5331 |
|
'/' [reduce using rule 3 (exp)] |
5332 |
|
$default reduce using rule 3 (exp) |
5333 |
|
|
5334 |
|
state 11 |
5335 |
|
|
5336 |
|
exp -> exp . '+' exp (rule 1) |
5337 |
|
exp -> exp . '-' exp (rule 2) |
5338 |
|
exp -> exp . '*' exp (rule 3) |
5339 |
|
exp -> exp . '/' exp (rule 4) |
5340 |
|
exp -> exp '/' exp . (rule 4) |
5341 |
|
|
5342 |
|
'+' shift, and go to state 4 |
5343 |
|
'-' shift, and go to state 5 |
5344 |
|
'*' shift, and go to state 6 |
5345 |
|
'/' shift, and go to state 7 |
5346 |
|
|
5347 |
|
'+' [reduce using rule 4 (exp)] |
5348 |
|
'-' [reduce using rule 4 (exp)] |
5349 |
|
'*' [reduce using rule 4 (exp)] |
5350 |
|
'/' [reduce using rule 4 (exp)] |
5351 |
|
$default reduce using rule 4 (exp) |
5352 |
|
@end example |
5353 |
|
|
5354 |
|
@noindent |
5355 |
|
Observe that state 11 contains conflicts due to the lack of precedence |
5356 |
|
of @samp{/} wrt @samp{+}, @samp{-}, and @samp{*}, but also because the |
5357 |
|
associativity of @samp{/} is not specified. |
5358 |
|
|
5359 |
|
|
5360 |
|
@node Tracing |
5361 |
|
@section Tracing Your Parser |
5362 |
@findex yydebug |
@findex yydebug |
5363 |
@cindex debugging |
@cindex debugging |
5364 |
@cindex tracing the parser |
@cindex tracing the parser |
5462 |
@} |
@} |
5463 |
@end smallexample |
@end smallexample |
5464 |
|
|
5465 |
|
@c ================================================= Invoking Bison |
5466 |
|
|
5467 |
@node Invocation |
@node Invocation |
5468 |
@chapter Invoking Bison |
@chapter Invoking Bison |
5469 |
@cindex invoking Bison |
@cindex invoking Bison |
5563 |
@itemx --debug |
@itemx --debug |
5564 |
In the parser file, define the macro @code{YYDEBUG} to 1 if it is not |
In the parser file, define the macro @code{YYDEBUG} to 1 if it is not |
5565 |
already defined, so that the debugging facilities are compiled. |
already defined, so that the debugging facilities are compiled. |
5566 |
@xref{Debugging, ,Debugging Your Parser}. |
@xref{Tracing, ,Tracing Your Parser}. |
5567 |
|
|
5568 |
@item --locations |
@item --locations |
5569 |
Pretend that @code{%locations} was specified. @xref{Decl Summary}. |
Pretend that @code{%locations} was specified. @xref{Decl Summary}. |
5609 |
Pretend that @code{%verbose} was specified, i.e, specify prefix to use |
Pretend that @code{%verbose} was specified, i.e, specify prefix to use |
5610 |
for all Bison output file names. @xref{Decl Summary}. |
for all Bison output file names. @xref{Decl Summary}. |
5611 |
|
|
5612 |
|
@item -r @var{things} |
5613 |
|
@itemx --report=@var{things} |
5614 |
|
Write an extra output file containing verbose description of the comma |
5615 |
|
separated list of @var{things} among: |
5616 |
|
|
5617 |
|
@table @code |
5618 |
|
@item state |
5619 |
|
Description of the grammar, conflicts (resolved and unresolved), and |
5620 |
|
LALR automaton. |
5621 |
|
|
5622 |
|
@item lookahead |
5623 |
|
Implies @code{state} and augments the description of the automaton with |
5624 |
|
each rule's lookahead set. |
5625 |
|
|
5626 |
|
@item itemset |
5627 |
|
Implies @code{state} and augments the description of the automaton with |
5628 |
|
the full set of items for each state, instead of its core only. |
5629 |
|
@end table |
5630 |
|
|
5631 |
|
For instance, on the following grammar |
5632 |
|
|
5633 |
@item -v |
@item -v |
5634 |
@itemx --verbose |
@itemx --verbose |
5635 |
Pretend that @code{%verbose} was specified, i.e, write an extra output |
Pretend that @code{%verbose} was specified, i.e, write an extra output |
5791 |
token. @xref{Action Features, ,Special Features for Use in Actions}. |
token. @xref{Action Features, ,Special Features for Use in Actions}. |
5792 |
|
|
5793 |
@item YYDEBUG |
@item YYDEBUG |
5794 |
Macro to define to equip the parser with tracing code. @xref{Debugging, |
Macro to define to equip the parser with tracing code. @xref{Tracing, |
5795 |
,Debugging Your Parser}. |
,Tracing Your Parser}. |
5796 |
|
|
5797 |
@item YYERROR |
@item YYERROR |
5798 |
Macro to pretend that a syntax error has just been detected: call |
Macro to pretend that a syntax error has just been detected: call |
5856 |
@item yydebug |
@item yydebug |
5857 |
External integer variable set to zero by default. If @code{yydebug} |
External integer variable set to zero by default. If @code{yydebug} |
5858 |
is given a nonzero value, the parser will output information on input |
is given a nonzero value, the parser will output information on input |
5859 |
symbols and parser action. @xref{Debugging, ,Debugging Your Parser}. |
symbols and parser action. @xref{Tracing, ,Tracing Your Parser}. |
5860 |
|
|
5861 |
@item yyerrok |
@item yyerrok |
5862 |
Macro to cause parser to recover immediately to its normal mode |
Macro to cause parser to recover immediately to its normal mode |