bison.info-4 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. This is Info file bison.info, produced by Makeinfo-1.55 from the input
  2. file bison.texinfo.
  3. This file documents the Bison parser generator.
  4. Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
  5. Inc. Modified (1993) from bison-1.22 by Wilfred J. Hansen
  6. (wjh+@cmu.edu), Andrew Consortium, Carnegie Mellon University
  7. CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  8. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. FITNESS. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
  10. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  11. RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  12. CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17. Permission is granted to copy and distribute modified versions of
  18. this manual under the conditions for verbatim copying, provided also
  19. that the sections entitled "GNU General Public License" and "Conditions
  20. for Using Bison" are included exactly as in the original, and provided
  21. that the entire resulting derived work is distributed under the terms
  22. of a permission notice identical to this one.
  23. Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that the sections entitled "GNU General Public
  26. License", "Conditions for Using Bison" and this permission notice may be
  27. included in translations approved by the Free Software Foundation
  28. instead of in the original English.
  29. 
  30. File: bison.info, Node: Reduce/Reduce, Next: Mystery Conflicts, Prev: Parser States, Up: Algorithm
  31. Reduce/Reduce Conflicts
  32. =======================
  33. A reduce/reduce conflict occurs if there are two or more rules that
  34. apply to the same sequence of input. This usually indicates a serious
  35. error in the grammar.
  36. For example, here is an erroneous attempt to define a sequence of
  37. zero or more `word' groupings.
  38. sequence: /* empty */
  39. { printf ("empty sequence\n"); }
  40. | maybeword
  41. | sequence word
  42. { printf ("added word %s\n", $2); }
  43. ;
  44. maybeword: /* empty */
  45. { printf ("empty maybeword\n"); }
  46. | word
  47. { printf ("single word %s\n", $1); }
  48. ;
  49. The error is an ambiguity: there is more than one way to parse a single
  50. `word' into a `sequence'. It could be reduced to a `maybeword' and
  51. then into a `sequence' via the second rule. Alternatively,
  52. nothing-at-all could be reduced into a `sequence' via the first rule,
  53. and this could be combined with the `word' using the third rule for
  54. `sequence'.
  55. There is also more than one way to reduce nothing-at-all into a
  56. `sequence'. This can be done directly via the first rule, or
  57. indirectly via `maybeword' and then the second rule.
  58. You might think that this is a distinction without a difference,
  59. because it does not change whether any particular input is valid or
  60. not. But it does affect which actions are run. One parsing order runs
  61. the second rule's action; the other runs the first rule's action and
  62. the third rule's action. In this example, the output of the program
  63. changes.
  64. Bison resolves a reduce/reduce conflict by choosing to use the rule
  65. that appears first in the grammar, but it is very risky to rely on
  66. this. Every reduce/reduce conflict must be studied and usually
  67. eliminated. Here is the proper way to define `sequence':
  68. sequence: /* empty */
  69. { printf ("empty sequence\n"); }
  70. | sequence word
  71. { printf ("added word %s\n", $2); }
  72. ;
  73. Here is another common error that yields a reduce/reduce conflict:
  74. sequence: /* empty */
  75. | sequence words
  76. | sequence redirects
  77. ;
  78. words: /* empty */
  79. | words word
  80. ;
  81. redirects:/* empty */
  82. | redirects redirect
  83. ;
  84. The intention here is to define a sequence which can contain either
  85. `word' or `redirect' groupings. The individual definitions of
  86. `sequence', `words' and `redirects' are error-free, but the three
  87. together make a subtle ambiguity: even an empty input can be parsed in
  88. infinitely many ways!
  89. Consider: nothing-at-all could be a `words'. Or it could be two
  90. `words' in a row, or three, or any number. It could equally well be a
  91. `redirects', or two, or any number. Or it could be a `words' followed
  92. by three `redirects' and another `words'. And so on.
  93. Here are two ways to correct these rules. First, to make it a
  94. single level of sequence:
  95. sequence: /* empty */
  96. | sequence word
  97. | sequence redirect
  98. ;
  99. Second, to prevent either a `words' or a `redirects' from being
  100. empty:
  101. sequence: /* empty */
  102. | sequence words
  103. | sequence redirects
  104. ;
  105. words: word
  106. | words word
  107. ;
  108. redirects:redirect
  109. | redirects redirect
  110. ;
  111. 
  112. File: bison.info, Node: Mystery Conflicts, Next: Stack Overflow, Prev: Reduce/Reduce, Up: Algorithm
  113. Mysterious Reduce/Reduce Conflicts
  114. ==================================
  115. Sometimes reduce/reduce conflicts can occur that don't look
  116. warranted. Here is an example:
  117. %token ID
  118. %%
  119. def: param_spec return_spec ','
  120. ;
  121. param_spec:
  122. type
  123. | name_list ':' type
  124. ;
  125. return_spec:
  126. type
  127. | name ':' type
  128. ;
  129. type: ID
  130. ;
  131. name: ID
  132. ;
  133. name_list:
  134. name
  135. | name ',' name_list
  136. ;
  137. It would seem that this grammar can be parsed with only a single
  138. token of look-ahead: when a `param_spec' is being read, an `ID' is a
  139. `name' if a comma or colon follows, or a `type' if another `ID'
  140. follows. In other words, this grammar is LR(1).
  141. However, Bison, like most parser generators, cannot actually handle
  142. all LR(1) grammars. In this grammar, two contexts, that after an `ID'
  143. at the beginning of a `param_spec' and likewise at the beginning of a
  144. `return_spec', are similar enough that Bison assumes they are the same.
  145. They appear similar because the same set of rules would be active--the
  146. rule for reducing to a `name' and that for reducing to a `type'. Bison
  147. is unable to determine at that stage of processing that the rules would
  148. require different look-ahead tokens in the two contexts, so it makes a
  149. single parser state for them both. Combining the two contexts causes a
  150. conflict later. In parser terminology, this occurrence means that the
  151. grammar is not LALR(1).
  152. In general, it is better to fix deficiencies than to document them.
  153. But this particular deficiency is intrinsically hard to fix; parser
  154. generators that can handle LR(1) grammars are hard to write and tend to
  155. produce parsers that are very large. In practice, Bison is more useful
  156. as it is now.
  157. When the problem arises, you can often fix it by identifying the two
  158. parser states that are being confused, and adding something to make them
  159. look distinct. In the above example, adding one rule to `return_spec'
  160. as follows makes the problem go away:
  161. %token BOGUS
  162. ...
  163. %%
  164. ...
  165. return_spec:
  166. type
  167. | name ':' type
  168. /* This rule is never used. */
  169. | ID BOGUS
  170. ;
  171. This corrects the problem because it introduces the possibility of an
  172. additional active rule in the context after the `ID' at the beginning of
  173. `return_spec'. This rule is not active in the corresponding context in
  174. a `param_spec', so the two contexts receive distinct parser states. As
  175. long as the token `BOGUS' is never generated by `yylex', the added rule
  176. cannot alter the way actual input is parsed.
  177. In this particular example, there is another way to solve the
  178. problem: rewrite the rule for `return_spec' to use `ID' directly
  179. instead of via `name'. This also causes the two confusing contexts to
  180. have different sets of active rules, because the one for `return_spec'
  181. activates the altered rule for `return_spec' rather than the one for
  182. `name'.
  183. param_spec:
  184. type
  185. | name_list ':' type
  186. ;
  187. return_spec:
  188. type
  189. | ID ':' type
  190. ;
  191. 
  192. File: bison.info, Node: Stack Overflow, Prev: Mystery Conflicts, Up: Algorithm
  193. Stack Overflow, and How to Avoid It
  194. ===================================
  195. The Bison parser stack can overflow if too many tokens are shifted
  196. and not reduced. When this happens, the parser function `yyparse'
  197. returns a nonzero value, pausing only to call `yyerror' to report the
  198. overflow.
  199. By defining the macro `YYMAXDEPTH', you can control how deep the
  200. parser stack can become before a stack overflow occurs. Define the
  201. macro with a value that is an integer. This value is the maximum number
  202. of tokens that can be shifted (and not reduced) before overflow. It
  203. must be a constant expression whose value is known at compile time.
  204. The stack space allowed is not necessarily allocated. If you
  205. specify a large value for `YYMAXDEPTH', the parser actually allocates a
  206. small stack at first, and then makes it bigger by stages as needed.
  207. This increasing allocation happens automatically and silently.
  208. Therefore, you do not need to make `YYMAXDEPTH' painfully small merely
  209. to save space for ordinary inputs that do not need much stack.
  210. The default value of `YYMAXDEPTH', if you do not define it, is 10000.
  211. You can control how much stack is allocated initially by defining the
  212. macro `YYINITDEPTH'. This value too must be a compile-time constant
  213. integer. The default is 200.
  214. 
  215. File: bison.info, Node: Error Recovery, Next: Context Dependency, Prev: Algorithm, Up: Top
  216. Error Recovery
  217. **************
  218. It is not usually acceptable to have a program terminate on a parse
  219. error. For example, a compiler should recover sufficiently to parse the
  220. rest of the input file and check it for errors; a calculator should
  221. accept another expression.
  222. In a simple interactive command parser where each input is one line,
  223. it may be sufficient to allow `yyparse' to return 1 on error and have
  224. the caller ignore the rest of the input line when that happens (and
  225. then call `yyparse' again). But this is inadequate for a compiler,
  226. because it forgets all the syntactic context leading up to the error.
  227. A syntax error deep within a function in the compiler input should not
  228. cause the compiler to treat the following line like the beginning of a
  229. source file.
  230. You can define how to recover from a syntax error by writing rules to
  231. recognize the special token `error'. This is a terminal symbol that is
  232. always defined (you need not declare it) and reserved for error
  233. handling. The Bison parser generates an `error' token whenever a
  234. syntax error happens; if you have provided a rule to recognize this
  235. token in the current context, the parse can continue.
  236. For example:
  237. stmnts: /* empty string */
  238. | stmnts '\n'
  239. | stmnts exp '\n'
  240. | stmnts error '\n'
  241. The fourth rule in this example says that an error followed by a
  242. newline makes a valid addition to any `stmnts'.
  243. What happens if a syntax error occurs in the middle of an `exp'? The
  244. error recovery rule, interpreted strictly, applies to the precise
  245. sequence of a `stmnts', an `error' and a newline. If an error occurs in
  246. the middle of an `exp', there will probably be some additional tokens
  247. and subexpressions on the stack after the last `stmnts', and there will
  248. be tokens to read before the next newline. So the rule is not
  249. applicable in the ordinary way.
  250. But Bison can force the situation to fit the rule, by discarding
  251. part of the semantic context and part of the input. First it discards
  252. states and objects from the stack until it gets back to a state in
  253. which the `error' token is acceptable. (This means that the
  254. subexpressions already parsed are discarded, back to the last complete
  255. `stmnts'.) At this point the `error' token can be shifted. Then, if
  256. the old look-ahead token is not acceptable to be shifted next, the
  257. parser reads tokens and discards them until it finds a token which is
  258. acceptable. In this example, Bison reads and discards input until the
  259. next newline so that the fourth rule can apply.
  260. The choice of error rules in the grammar is a choice of strategies
  261. for error recovery. A simple and useful strategy is simply to skip the
  262. rest of the current input line or current statement if an error is
  263. detected:
  264. stmnt: error ';' /* on error, skip until ';' is read */
  265. It is also useful to recover to the matching close-delimiter of an
  266. opening-delimiter that has already been parsed. Otherwise the
  267. close-delimiter will probably appear to be unmatched, and generate
  268. another, spurious error message:
  269. primary: '(' expr ')'
  270. | '(' error ')'
  271. ...
  272. ;
  273. Error recovery strategies are necessarily guesses. When they guess
  274. wrong, one syntax error often leads to another. In the above example,
  275. the error recovery rule guesses that an error is due to bad input
  276. within one `stmnt'. Suppose that instead a spurious semicolon is
  277. inserted in the middle of a valid `stmnt'. After the error recovery
  278. rule recovers from the first error, another syntax error will be found
  279. straightaway, since the text following the spurious semicolon is also
  280. an invalid `stmnt'.
  281. To prevent an outpouring of error messages, the parser will output
  282. no error message for another syntax error that happens shortly after
  283. the first; only after three consecutive input tokens have been
  284. successfully shifted will error messages resume.
  285. Note that rules which accept the `error' token may have actions, just
  286. as any other rules can.
  287. You can make error messages resume immediately by using the macro
  288. `yyerrok' in an action. If you do this in the error rule's action, no
  289. error messages will be suppressed. This macro requires no arguments;
  290. `yyerrok;' is a valid C statement.
  291. The previous look-ahead token is reanalyzed immediately after an
  292. error. If this is unacceptable, then the macro `yyclearin' may be used
  293. to clear this token. Write the statement `yyclearin;' in the error
  294. rule's action.
  295. For example, suppose that on a parse error, an error handling
  296. routine is called that advances the input stream to some point where
  297. parsing should once again commence. The next symbol returned by the
  298. lexical scanner is probably correct. The previous look-ahead token
  299. ought to be discarded with `yyclearin;'.
  300. The macro `YYRECOVERING' stands for an expression that has the value
  301. 1 when the parser is recovering from a syntax error, and 0 the rest of
  302. the time. A value of 1 indicates that error messages are currently
  303. suppressed for new syntax errors.
  304. 
  305. File: bison.info, Node: Context Dependency, Next: Debugging, Prev: Error Recovery, Up: Top
  306. Handling Context Dependencies
  307. *****************************
  308. The Bison paradigm is to parse tokens first, then group them into
  309. larger syntactic units. In many languages, the meaning of a token is
  310. affected by its context. Although this violates the Bison paradigm,
  311. certain techniques (known as "kludges") may enable you to write Bison
  312. parsers for such languages.
  313. * Menu:
  314. * Semantic Tokens:: Token parsing can depend on the semantic context.
  315. * Lexical Tie-ins:: Token parsing can depend on the syntactic context.
  316. * Tie-in Recovery:: Lexical tie-ins have implications for how
  317. error recovery rules must be written.
  318. (Actually, "kludge" means any technique that gets its job done but is
  319. neither clean nor robust.)
  320. 
  321. File: bison.info, Node: Semantic Tokens, Next: Lexical Tie-ins, Up: Context Dependency
  322. Semantic Info in Token Types
  323. ============================
  324. The C language has a context dependency: the way an identifier is
  325. used depends on what its current meaning is. For example, consider
  326. this:
  327. foo (x);
  328. This looks like a function call statement, but if `foo' is a typedef
  329. name, then this is actually a declaration of `x'. How can a Bison
  330. parser for C decide how to parse this input?
  331. The method used in GNU C is to have two different token types,
  332. `IDENTIFIER' and `TYPENAME'. When `yylex' finds an identifier, it
  333. looks up the current declaration of the identifier in order to decide
  334. which token type to return: `TYPENAME' if the identifier is declared as
  335. a typedef, `IDENTIFIER' otherwise.
  336. The grammar rules can then express the context dependency by the
  337. choice of token type to recognize. `IDENTIFIER' is accepted as an
  338. expression, but `TYPENAME' is not. `TYPENAME' can start a declaration,
  339. but `IDENTIFIER' cannot. In contexts where the meaning of the
  340. identifier is *not* significant, such as in declarations that can
  341. shadow a typedef name, either `TYPENAME' or `IDENTIFIER' is
  342. accepted--there is one rule for each of the two token types.
  343. This technique is simple to use if the decision of which kinds of
  344. identifiers to allow is made at a place close to where the identifier is
  345. parsed. But in C this is not always so: C allows a declaration to
  346. redeclare a typedef name provided an explicit type has been specified
  347. earlier:
  348. typedef int foo, bar, lose;
  349. static foo (bar); /* redeclare `bar' as static variable */
  350. static int foo (lose); /* redeclare `foo' as function */
  351. Unfortunately, the name being declared is separated from the
  352. declaration construct itself by a complicated syntactic structure--the
  353. "declarator".
  354. As a result, the part of Bison parser for C needs to be duplicated,
  355. with all the nonterminal names changed: once for parsing a declaration
  356. in which a typedef name can be redefined, and once for parsing a
  357. declaration in which that can't be done. Here is a part of the
  358. duplication, with actions omitted for brevity:
  359. initdcl:
  360. declarator maybeasm '='
  361. init
  362. | declarator maybeasm
  363. ;
  364. notype_initdcl:
  365. notype_declarator maybeasm '='
  366. init
  367. | notype_declarator maybeasm
  368. ;
  369. Here `initdcl' can redeclare a typedef name, but `notype_initdcl'
  370. cannot. The distinction between `declarator' and `notype_declarator'
  371. is the same sort of thing.
  372. There is some similarity between this technique and a lexical tie-in
  373. (described next), in that information which alters the lexical analysis
  374. is changed during parsing by other parts of the program. The
  375. difference is here the information is global, and is used for other
  376. purposes in the program. A true lexical tie-in has a special-purpose
  377. flag controlled by the syntactic context.
  378. 
  379. File: bison.info, Node: Lexical Tie-ins, Next: Tie-in Recovery, Prev: Semantic Tokens, Up: Context Dependency
  380. Lexical Tie-ins
  381. ===============
  382. One way to handle context-dependency is the "lexical tie-in": a flag
  383. which is set by Bison actions, whose purpose is to alter the way tokens
  384. are parsed.
  385. For example, suppose we have a language vaguely like C, but with a
  386. special construct `hex (HEX-EXPR)'. After the keyword `hex' comes an
  387. expression in parentheses in which all integers are hexadecimal. In
  388. particular, the token `a1b' must be treated as an integer rather than
  389. as an identifier if it appears in that context. Here is how you can do
  390. it:
  391. %{
  392. int hexflag;
  393. %}
  394. %%
  395. ...
  396. expr: IDENTIFIER
  397. | constant
  398. | HEX '('
  399. { hexflag = 1; }
  400. expr ')'
  401. { hexflag = 0;
  402. $$ = $4; }
  403. | expr '+' expr
  404. { $$ = make_sum ($1, $3); }
  405. ...
  406. ;
  407. constant:
  408. INTEGER
  409. | STRING
  410. ;
  411. Here we assume that `yylex' looks at the value of `hexflag'; when it is
  412. nonzero, all integers are parsed in hexadecimal, and tokens starting
  413. with letters are parsed as integers if possible.
  414. The declaration of `hexflag' shown in the C declarations section of
  415. the parser file is needed to make it accessible to the actions (*note
  416. The C Declarations Section: C Declarations.). You must also write the
  417. code in `yylex' to obey the flag.
  418. 
  419. File: bison.info, Node: Tie-in Recovery, Prev: Lexical Tie-ins, Up: Context Dependency
  420. Lexical Tie-ins and Error Recovery
  421. ==================================
  422. Lexical tie-ins make strict demands on any error recovery rules you
  423. have. *Note Error Recovery::.
  424. The reason for this is that the purpose of an error recovery rule is
  425. to abort the parsing of one construct and resume in some larger
  426. construct. For example, in C-like languages, a typical error recovery
  427. rule is to skip tokens until the next semicolon, and then start a new
  428. statement, like this:
  429. stmt: expr ';'
  430. | IF '(' expr ')' stmt { ... }
  431. ...
  432. error ';'
  433. { hexflag = 0; }
  434. ;
  435. If there is a syntax error in the middle of a `hex (EXPR)'
  436. construct, this error rule will apply, and then the action for the
  437. completed `hex (EXPR)' will never run. So `hexflag' would remain set
  438. for the entire rest of the input, or until the next `hex' keyword,
  439. causing identifiers to be misinterpreted as integers.
  440. To avoid this problem the error recovery rule itself clears
  441. `hexflag'.
  442. There may also be an error recovery rule that works within
  443. expressions. For example, there could be a rule which applies within
  444. parentheses and skips to the close-parenthesis:
  445. expr: ...
  446. | '(' expr ')'
  447. { $$ = $2; }
  448. | '(' error ')'
  449. ...
  450. If this rule acts within the `hex' construct, it is not going to
  451. abort that construct (since it applies to an inner level of parentheses
  452. within the construct). Therefore, it should not clear the flag: the
  453. rest of the `hex' construct should be parsed with the flag still in
  454. effect.
  455. What if there is an error recovery rule which might abort out of the
  456. `hex' construct or might not, depending on circumstances? There is no
  457. way you can write the action to determine whether a `hex' construct is
  458. being aborted or not. So if you are using a lexical tie-in, you had
  459. better make sure your error recovery rules are not of this kind. Each
  460. rule must be such that you can be sure that it always will, or always
  461. won't, have to clear the flag.
  462. 
  463. File: bison.info, Node: Debugging, Next: Invocation, Prev: Context Dependency, Up: Top
  464. Debugging Your Parser
  465. *********************
  466. If a Bison grammar compiles properly but doesn't do what you want
  467. when it runs, the `yydebug' parser-trace feature can help you figure
  468. out why.
  469. To enable compilation of trace facilities, you must define the macro
  470. `YYDEBUG' when you compile the parser. You could use `-DYYDEBUG=1' as
  471. a compiler option or you could put `#define YYDEBUG 1' in the C
  472. declarations section of the grammar file (*note The C Declarations
  473. Section: C Declarations.). Alternatively, use the `-t' option when you
  474. run Bison (*note Invoking Bison: Invocation.). We always define
  475. `YYDEBUG' so that debugging is always possible.
  476. The trace facility uses `stderr', so you must add
  477. `#include <stdio.h>' to the C declarations section unless it is already
  478. there.
  479. Once you have compiled the program with trace facilities, the way to
  480. request a trace is to store a nonzero value in the variable `yydebug'.
  481. You can do this by making the C code do it (in `main', perhaps), or you
  482. can alter the value with a C debugger.
  483. Each step taken by the parser when `yydebug' is nonzero produces a
  484. line or two of trace information, written on `stderr'. The trace
  485. messages tell you these things:
  486. * Each time the parser calls `yylex', what kind of token was read.
  487. * Each time a token is shifted, the depth and complete contents of
  488. the state stack (*note Parser States::.).
  489. * Each time a rule is reduced, which rule it is, and the complete
  490. contents of the state stack afterward.
  491. To make sense of this information, it helps to refer to the listing
  492. file produced by the Bison `-v' option (*note Invoking Bison:
  493. Invocation.). This file shows the meaning of each state in terms of
  494. positions in various rules, and also what each state will do with each
  495. possible input token. As you read the successive trace messages, you
  496. can see that the parser is functioning according to its specification
  497. in the listing file. Eventually you will arrive at the place where
  498. something undesirable happens, and you will see which parts of the
  499. grammar are to blame.
  500. The parser file is a C program and you can use C debuggers on it,
  501. but it's not easy to interpret what it is doing. The parser function
  502. is a finite-state machine interpreter, and aside from the actions it
  503. executes the same code over and over. Only the values of variables
  504. show where in the grammar it is working.
  505. The debugging information normally gives the token type of each token
  506. read, but not its semantic value. You can optionally define a macro
  507. named `YYPRINT' to provide a way to print the value. If you define
  508. `YYPRINT', it should take three arguments. The parser will pass a
  509. standard I/O stream, the numeric code for the token type, and the token
  510. value (from `yylval').
  511. Here is an example of `YYPRINT' suitable for the multi-function
  512. calculator (*note Declarations for `mfcalc': Mfcalc Decl.):
  513. #define YYPRINT(file, type, value) yyprint (file, type, value)
  514. static void
  515. yyprint (file, type, value)
  516. FILE *file;
  517. int type;
  518. YYSTYPE value;
  519. {
  520. if (type == VAR)
  521. fprintf (file, " %s", value.tptr->name);
  522. else if (type == NUM)
  523. fprintf (file, " %d", value.val);
  524. }
  525. 
  526. File: bison.info, Node: Invocation, Next: Table of Symbols, Prev: Debugging, Up: Top
  527. Invoking Bison
  528. **************
  529. The usual way to invoke Bison is as follows:
  530. bison INFILE
  531. Here INFILE is the grammar file name, which usually ends in `.y'.
  532. The parser file's name is made by replacing the `.y' with `.tab.c'.
  533. Thus, the `bison foo.y' filename yields `foo.tab.c', and the `bison
  534. hack/foo.y' filename yields `hack/foo.tab.c'.
  535. * Menu:
  536. * Bison Options:: All the options described in detail,
  537. in alphabetical order by short options.
  538. * Option Cross Key:: Alphabetical list of long options.
  539. * VMS Invocation:: Bison command syntax on VMS.
  540. 
  541. File: bison.info, Node: Bison Options, Next: Option Cross Key, Up: Invocation
  542. Bison Options
  543. =============
  544. Bison supports both traditional single-letter options and mnemonic
  545. long option names. Long option names are indicated with `--' instead of
  546. `-'. Abbreviations for option names are allowed as long as they are
  547. unique. When a long option takes an argument, like `--file-prefix',
  548. connect the option name and the argument with `='.
  549. Here is a list of options that can be used with Bison, alphabetized
  550. by short option. It is followed by a cross key alphabetized by long
  551. option.
  552. `-b FILE-PREFIX'
  553. `--file-prefix=PREFIX'
  554. Specify a prefix to use for all Bison output file names. The
  555. names are chosen as if the input file were named `PREFIX.c'.
  556. `-d'
  557. `--defines'
  558. Write an extra output file containing macro definitions for the
  559. token type names defined in the grammar and the semantic value type
  560. `YYSTYPE', as well as a few `extern' variable declarations.
  561. If the parser output file is named `NAME.c' then this file is
  562. named `NAME.h'.
  563. This output file is essential if you wish to put the definition of
  564. `yylex' in a separate source file, because `yylex' needs to be
  565. able to refer to token type codes and the variable `yylval'.
  566. *Note Semantic Values of Tokens: Token Values.
  567. `-k'
  568. `--token-table'
  569. This switch causes the .tab.c output to include a list of token
  570. names in order by their token numbers; this is defined in the
  571. array `yytname'. The first three elements are `"$"', `"error"',
  572. and `"$illegal"'; entries for single- and multiple-character
  573. symbols include their quotes: `"\'+\'"' and `"\"<=\""'. Also
  574. generated are #defines for `YYNTOKENS, YYNNTS, YYNRULES', and
  575. `YYNSTATES' giving, respectively, one more than the highest token
  576. number, the number of non-terminal symbols, the number of grammar
  577. rules, and the number of states.
  578. `-l'
  579. `--no-lines'
  580. Don't put any `#line' preprocessor commands in the parser file.
  581. Ordinarily Bison puts them in the parser file so that the C
  582. compiler and debuggers will associate errors with your source
  583. file, the grammar file. This option causes them to associate
  584. errors with the parser file, treating it an independent source
  585. file in its own right.
  586. `-n'
  587. `--no-parser'
  588. Do not generate the parser code into the output; generate only
  589. declarations. The generated `y.tab.c' file will have only
  590. constant declarations. In addition, a FILENAME.act file is
  591. generated containing a switch statement body containing all the
  592. translated actions. (The FILENAME is taken from the input file or
  593. set in accordance with the -o switch.) The declarations in the
  594. FILENAME`.tab.c' file will all be static or #defined. Some
  595. symbols only appear if appropriate options are selected. These
  596. are #defined: YYLTYPE, YYFINAL, YYFLAG, YYNTBASE, YYTRANSLATE,
  597. YYLAST, YYNTOKENS, YYNNTS, YYNRULES, YYNSTATES, YYMAXUTOK. These
  598. are declared and given values: yyltype, yytranslate, yyprhs,
  599. yyrhs, yystos, yyrline, yytname, yytoknum, yyr1, yyr2, yydefact,
  600. yydefgoto, yypact, yypgoto, yytable, and yycheck. See the source
  601. file output.c for definitions of these variables.
  602. `-o OUTFILE'
  603. `--output-file=OUTFILE'
  604. Specify the name OUTFILE for the parser file.
  605. The other output files' names are constructed from OUTFILE as
  606. described under the `-v' and `-d' options.
  607. `-p PREFIX'
  608. `--name-prefix=PREFIX'
  609. Rename the external symbols used in the parser so that they start
  610. with PREFIX instead of `yy'. The precise list of symbols renamed
  611. is `yyparse', `yylex', `yyerror', `yylval', `yychar' and `yydebug'.
  612. For example, if you use `-p c', the names become `cparse', `clex',
  613. and so on.
  614. *Note Multiple Parsers in the Same Program: Multiple Parsers.
  615. `-r'
  616. `--raw'
  617. In the output to `NAME.h' the tokens are usually defined with Yacc
  618. compatible token numbers. If this switch is specified, the Bison
  619. assigned numbers are output instead. (Yacc numbers start at 257
  620. except for single character tokens; Bison assigns token numbers
  621. sequentially for all tokens starting at 3.)
  622. `-t'
  623. `--debug'
  624. Output a definition of the macro `YYDEBUG' into the parser file,
  625. so that the debugging facilities are compiled. *Note Debugging
  626. Your Parser: Debugging.
  627. `-v'
  628. `--verbose'
  629. Write an extra output file containing verbose descriptions of the
  630. parser states and what is done for each type of look-ahead token in
  631. that state.
  632. This file also describes all the conflicts, both those resolved by
  633. operator precedence and the unresolved ones.
  634. The file's name is made by removing `.tab.c' or `.c' from the
  635. parser output file name, and adding `.output' instead.
  636. Therefore, if the input file is `foo.y', then the parser file is
  637. called `foo.tab.c' by default. As a consequence, the verbose
  638. output file is called `foo.output'.
  639. `-V'
  640. `--version'
  641. Print the version number of Bison and exit.
  642. `-h'
  643. `--help'
  644. Print a summary of the command-line options to Bison and exit.
  645. `-y'
  646. `--yacc'
  647. `--fixed-output-files'
  648. Equivalent to `-o y.tab.c'; the parser output file is called
  649. `y.tab.c', and the other outputs are called `y.output' and
  650. `y.tab.h'. The purpose of this switch is to imitate Yacc's output
  651. file name conventions. Thus, the following shell script can
  652. substitute for Yacc:
  653. bison -y $*
  654. 
  655. File: bison.info, Node: Option Cross Key, Next: VMS Invocation, Prev: Bison Options, Up: Invocation
  656. Option Cross Key
  657. ================
  658. Here is a list of options, alphabetized by long option, to help you
  659. find the corresponding short option.
  660. --debug -t
  661. --defines -d
  662. --file-prefix=PREFIX -b FILE-PREFIX
  663. --fixed-output-files --yacc -y
  664. --help -h
  665. --name-prefix -p
  666. --no-lines -l
  667. --no-parser -n
  668. --output-file=OUTFILE -o OUTFILE
  669. --raw -r
  670. --token-table -k
  671. --verbose -v
  672. --version -V
  673. 
  674. File: bison.info, Node: VMS Invocation, Prev: Option Cross Key, Up: Invocation
  675. Invoking Bison under VMS
  676. ========================
  677. The command line syntax for Bison on VMS is a variant of the usual
  678. Bison command syntax--adapted to fit VMS conventions.
  679. To find the VMS equivalent for any Bison option, start with the long
  680. option, and substitute a `/' for the leading `--', and substitute a `_'
  681. for each `-' in the name of the long option. For example, the
  682. following invocation under VMS:
  683. bison /debug/name_prefix=bar foo.y
  684. is equivalent to the following command under POSIX.
  685. bison --debug --name-prefix=bar foo.y
  686. The VMS file system does not permit filenames such as `foo.tab.c'.
  687. In the above example, the output file would instead be named
  688. `foo_tab.c'.
  689. 
  690. File: bison.info, Node: Table of Symbols, Next: Parser Symbols, Prev: Invocation, Up: Top
  691. Bison Symbols
  692. *************
  693. `error'
  694. A token name reserved for error recovery. This token may be used
  695. in grammar rules so as to allow the Bison parser to recognize an
  696. error in the grammar without halting the process. In effect, a
  697. sentence containing an error may be recognized as valid. On a
  698. parse error, the token `error' becomes the current look-ahead
  699. token. Actions corresponding to `error' are then executed, and
  700. the look-ahead token is reset to the token that originally caused
  701. the violation. *Note Error Recovery::.
  702. `YYABORT'
  703. Macro to pretend that an unrecoverable syntax error has occurred,
  704. by making `yyparse' return 1 immediately. The error reporting
  705. function `yyerror' is not called. *Note The Parser Function
  706. `yyparse': Parser Function.
  707. `YYACCEPT'
  708. Macro to pretend that a complete utterance of the language has been
  709. read, by making `yyparse' return 0 immediately. *Note The Parser
  710. Function `yyparse': Parser Function.
  711. `YYBACKUP'
  712. Macro to discard a value from the parser stack and fake a
  713. look-ahead token. *Note Special Features for Use in Actions:
  714. Action Features.
  715. `YYERROR'
  716. Macro to pretend that a syntax error has just been detected: call
  717. `yyerror' and then perform normal error recovery if possible
  718. (*note Error Recovery::.), or (if recovery is impossible) make
  719. `yyparse' return 1. *Note Error Recovery::.
  720. `YYERROR_VERBOSE'
  721. Macro that you define with `#define' in the Bison declarations
  722. section to request verbose, specific error message strings when
  723. `yyerror' is called.
  724. `YYINITDEPTH'
  725. Macro for specifying the initial size of the parser stack. *Note
  726. Stack Overflow::.
  727. `YYLTYPE'
  728. Macro for the data type of `yylloc'; a structure with four
  729. members. *Note Textual Positions of Tokens: Token Positions.
  730. `yyltype'
  731. Default value for YYLTYPE.
  732. `YYMAXDEPTH'
  733. Macro for specifying the maximum size of the parser stack. *Note
  734. Stack Overflow::.
  735. `YYRECOVERING'
  736. Macro whose value indicates whether the parser is recovering from a
  737. syntax error. *Note Special Features for Use in Actions: Action
  738. Features.
  739. `YYSTYPE'
  740. Macro for the data type of semantic values; `int' by default.
  741. *Note Data Types of Semantic Values: Value Type.
  742. `yychar'
  743. External integer variable that contains the integer value of the
  744. current look-ahead token. (In a pure parser, it is a local
  745. variable within `yyparse'.) Error-recovery rule actions may
  746. examine this variable. *Note Special Features for Use in Actions:
  747. Action Features.
  748. `yyclearin'
  749. Macro used in error-recovery rule actions. It clears the previous
  750. look-ahead token. *Note Error Recovery::.
  751. `yydebug'
  752. External integer variable set to zero by default. If `yydebug' is
  753. given a nonzero value, the parser will output information on input
  754. symbols and parser action. *Note Debugging Your Parser: Debugging.
  755. `yyerrok'
  756. Macro to cause parser to recover immediately to its normal mode
  757. after a parse error. *Note Error Recovery::.
  758. `yyerror'
  759. User-supplied function to be called by `yyparse' on error. The
  760. function receives one argument, a pointer to a character string
  761. containing an error message. *Note The Error Reporting Function
  762. `yyerror': Error Reporting.
  763. `yylex'
  764. User-supplied lexical analyzer function, called with no arguments
  765. to get the next token. *Note The Lexical Analyzer Function
  766. `yylex': Lexical.
  767. `yylval'
  768. External variable in which `yylex' should place the semantic value
  769. associated with a token. (In a pure parser, it is a local
  770. variable within `yyparse', and its address is passed to `yylex'.)
  771. *Note Semantic Values of Tokens: Token Values.
  772. `yylloc'
  773. External variable in which `yylex' should place the line and
  774. column numbers associated with a token. (In a pure parser, it is a
  775. local variable within `yyparse', and its address is passed to
  776. `yylex'.) You can ignore this variable if you don't use the `@'
  777. feature in the grammar actions. *Note Textual Positions of
  778. Tokens: Token Positions.
  779. `yynerrs'
  780. Global variable which Bison increments each time there is a parse
  781. error. (In a pure parser, it is a local variable within
  782. `yyparse'.) *Note The Error Reporting Function `yyerror': Error
  783. Reporting.
  784. `yyparse'
  785. The parser function produced by Bison; call this function to start
  786. parsing. *Note The Parser Function `yyparse': Parser Function.
  787. `%left'
  788. Bison declaration to assign left associativity to token(s). *Note
  789. Operator Precedence: Precedence Decl.
  790. `%nonassoc'
  791. Bison declaration to assign nonassociativity to token(s). *Note
  792. Operator Precedence: Precedence Decl.
  793. `%prec'
  794. Bison declaration to assign a precedence to a specific rule.
  795. *Note Context-Dependent Precedence: Contextual Precedence.
  796. `%pure_parser'
  797. Bison declaration to request a pure (reentrant) parser. *Note A
  798. Pure (Reentrant) Parser: Pure Decl.
  799. `%right'
  800. Bison declaration to assign right associativity to token(s).
  801. *Note Operator Precedence: Precedence Decl.
  802. `%start'
  803. Bison declaration to specify the start symbol. *Note The
  804. Start-Symbol: Start Decl.
  805. `%token'
  806. Bison declaration to declare token(s) without specifying
  807. precedence. *Note Token Type Names: Token Decl.
  808. `%type'
  809. Bison declaration to declare nonterminals. *Note Nonterminal
  810. Symbols: Type Decl.
  811. `%union'
  812. Bison declaration to specify several possible data types for
  813. semantic values. *Note The Collection of Value Types: Union Decl.
  814. These are the punctuation and delimiters used in Bison input:
  815. `%%'
  816. Delimiter used to separate the grammar rule section from the Bison
  817. declarations section or the additional C code section. *Note The
  818. Overall Layout of a Bison Grammar: Grammar Layout.
  819. `%{ %}'
  820. All code listed between `%{' and `%}' is copied directly to the
  821. output file uninterpreted. Such code forms the "C declarations"
  822. section of the input file. *Note Outline of a Bison Grammar:
  823. Grammar Outline.
  824. `/*...*/'
  825. Comment delimiters, as in C.
  826. `:'
  827. Separates a rule's result from its components. *Note Syntax of
  828. Grammar Rules: Rules.
  829. `;'
  830. Terminates a rule. *Note Syntax of Grammar Rules: Rules.
  831. `|'
  832. Separates alternate rules for the same result nonterminal. *Note
  833. Syntax of Grammar Rules: Rules.
  834. 
  835. File: bison.info, Node: Parser Symbols, Next: Glossary, Prev: Table of Symbols, Up: Top
  836. Parser Symbols
  837. ==============
  838. Each symbol (either token or variable) receives a symbol number.
  839. Numbers 0 to ntokens-1 are for tokens, and ntokens to nsyms-1 are for
  840. variables. Symbol number zero is the end-of-input token. This token
  841. is counted in ntokens.
  842. The rules receive rule numbers 1 to nrules in the order they are
  843. written. Actions and guards are accessed via the rule number.
  844. The rules themselves are described by three arrays: rrhs, rlhs and
  845. ritem. rlhs[R] is the symbol number of the left hand side of rule R.
  846. The right hand side is stored as symbol numbers in a portion of ritem.
  847. rrhs[R] contains the index in ritem of the beginning of the portion for
  848. rule R.
  849. If rlhs[R] is -1, the rule has been thrown out by reduce.c and
  850. should be ignored.
  851. The length of the portion is one greater than the number of symbols
  852. in the rule's right hand side. The last element in the portion
  853. contains minus R, which identifies it as the end of a portion and says
  854. which rule it is for.
  855. The portions of ritem come in order of increasing rule number and are
  856. followed by an element which is zero to mark the end.
  857. These symbols are #defined:
  858. YYFINAL = the state number of the termination state. YYFLAG = most
  859. negative short int. Used to flag ?? YYNTBASE = ntokens YYTRANSLATE =
  860. macro to translate token number from yacc to bison YYLAST = index of
  861. highest entry in yytable and yycheck YYNTOKENS = number of terminal
  862. symbols (same as YYNTBASE) YYNNTS = number of nonterminals YYNRULES =
  863. number of rules in the grammar YYNSTATES = number of states in parser
  864. YYMAXUTOK = highest user token number
  865. YYTRANSLATE(y) == if y <= YYMAXUTOK then yytranslate[y] else
  866. YYNTOKENS+YYNNTS
  867. The parser tables consist of the following tables. Starred ones
  868. needed only for the semantic parser. Double starred are output only if
  869. switches are set.
  870. yytranslate = vector mapping yylex's token numbers into bison's
  871. token numbers. The token numbers differ because (a) yacc/yylex
  872. utilize the first 256 token numbers to refer to individual
  873. characters and (b) the yacc grammar allows explicit assignment
  874. of token numbers. Bison token numbers are assigned
  875. sequentially from 0 to YYNTOKENS-1.
  876. ** yytname = vector of string-names indexed by bison token number
  877. (range 0...YYNTOKENS+YYNNTS, the last entry is "") user
  878. token names are in (3...YYNTOKENS-1) and may be identifier - all letters
  879. an identifier represents either a reserved word or a token class
  880. character literal - 'x' (the apostrophes appear in the string)
  881. string literal - "xxx" (the quotes appear in the string)
  882. non-terminal symbol names are in (YYNTOKENS...YYNTOKENS+YYNNTS-1)
  883. ** yytoknum = vector of yacc/yylex token numbers corresponding to
  884. entries in yytname (range 0...YYNTOKENS+YYNNTS, the last entry
  885. is 0)
  886. yyrline = vector of line-numbers of all rules. For yydebug
  887. printouts. (range 0...YYNRULES, the first entry is 0)
  888. ** yyrhs = vector of items of all rules. This is exactly
  889. what ritems contains. For yydebug and for semantic parser.
  890. ** yyprhs[r] = index in yyrhs of first item for rule r.
  891. (range 0...YYNRULES, the first entry is 0)
  892. yyr1[r] = symbol number of symbol that rule r derives.
  893. (range 0...YYNRULES, the first entry is 0)
  894. yyr2[r] = number of symbols composing right hand side of rule r.
  895. (range 0...YYNRULES, the first entry is 0)
  896. * yystos[s] = the symbol number of the symbol that leads to state s.
  897. (range 0...NSTATES-1)
  898. yydefact[s] = default rule to reduce with in state s,
  899. when yytable doesn't specify something else to do. Zero means
  900. the default is an error. (range 0...NSTATES-1)
  901. yydefgoto[i] = default state to go to after a reduction of a rule
  902. that generates variable ntokens + i, except when yytable
  903. specifies something else to do.
  904. yypact[s] = index in yytable of the portion describing state s.
  905. (range 0...YYNSTATES-1). The lookahead token's
  906. type is used to index that portion to find out what to do.
  907. If the value in yytable is positive, we shift the
  908. token and go to that state.
  909. If the value is negative, it is minus a rule number to reduce by.
  910. If the value is zero, the default action from yydefact[s] is
  911. used.
  912. yypgoto[i] = the index in yytable of the portion describing
  913. what to do after reducing a rule that derives variable i + ntokens.
  914. This portion is indexed by the parser state number as
  915. of before the text for this nonterminal was read. The value from
  916. yytable is the state to go to.
  917. yytable = a vector filled with portions for different uses,
  918. found via yypact and yypgoto. (range 0...YYLAST)
  919. yycheck = a vector indexed in parallel with yytable. It
  920. indicates, in a roundabout way, the bounds of the portion you are
  921. trying to examine.
  922. Suppose that the portion of yytable starts at index p
  923. and the index to be examined within the portion is i. Then if
  924. yycheck[p+i] != i, i is outside the bounds of what is
  925. actually allocated, and the default (from yydefact or yydefgoto)
  926. should be used. Otherwise, yytable[p+i] should be used. (range
  927. 0...YYLAST)