bison.info-2 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. Info file bison.info, produced by Makeinfo, -*- Text -*- from input
  2. file bison.texinfo.
  3. This file documents the Bison parser generator.
  4. Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  5. Permission is granted to make and distribute verbatim copies of
  6. this manual provided the copyright notice and this permission notice
  7. are preserved on all copies.
  8. Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the sections entitled "GNU General Public License" and
  11. "Conditions for Using Bison" are included exactly as in the original,
  12. and provided that the entire resulting derived work is distributed
  13. under the terms of a permission notice identical to this one.
  14. Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the sections entitled "GNU General Public
  17. License", "Conditions for Using Bison" and this permission notice may
  18. be included in translations approved by the Free Software Foundation
  19. instead of in the original English.
  20. 
  21. File: bison.info, Node: Infix Calc, Next: Simple Error Recovery, Prev: RPN Calc, Up: Examples
  22. Infix Notation Calculator: `calc'
  23. =================================
  24. We now modify rpcalc to handle infix operators instead of postfix.
  25. Infix notation involves the concept of operator precedence and the
  26. need for parentheses nested to arbitrary depth. Here is the Bison
  27. code for `calc.y', an infix desk-top calculator.
  28. /* Infix notation calculator--calc */
  29. %{
  30. #define YYSTYPE double
  31. #include <math.h>
  32. %}
  33. /* BISON Declarations */
  34. %token NUM
  35. %left '-' '+'
  36. %left '*' '/'
  37. %left NEG /* negation--unary minus */
  38. %right '^' /* exponentiation */
  39. /* Grammar follows */
  40. %%
  41. input: /* empty string */
  42. | input line
  43. ;
  44. line: '\n'
  45. | exp '\n' { printf ("\t%.10g\n", $1); }
  46. ;
  47. exp: NUM { $$ = $1; }
  48. | exp '+' exp { $$ = $1 + $3; }
  49. | exp '-' exp { $$ = $1 - $3; }
  50. | exp '*' exp { $$ = $1 * $3; }
  51. | exp '/' exp { $$ = $1 / $3; }
  52. | '-' exp %prec NEG { $$ = -$2; }
  53. | exp '^' exp { $$ = pow ($1, $3); }
  54. | '(' exp ')' { $$ = $2; }
  55. ;
  56. %%
  57. The functions `yylex', `yyerror' and `main' can be the same as before.
  58. There are two important new features shown in this code.
  59. In the second section (Bison declarations), `%left' declares token
  60. types and says they are left-associative operators. The declarations
  61. `%left' and `%right' (right associativity) take the place of `%token'
  62. which is used to declare a token type name without associativity.
  63. (These tokens are single-character literals, which ordinarily don't
  64. need to be declared. We declare them here to specify the
  65. associativity.)
  66. Operator precedence is determined by the line ordering of the
  67. declarations; the higher the line number of the declaration (lower on
  68. the page or screen), the higher the precedence. Hence, exponentiation
  69. has the highest precedence, unary minus (`NEG') is next, followed by
  70. `*' and `/', and so on. *Note Precedence::.
  71. The other important new feature is the `%prec' in the grammar
  72. section for the unary minus operator. The `%prec' simply instructs
  73. Bison that the rule `| '-' exp' has the same precedence as `NEG'--in
  74. this case the next-to-highest. *Note Contextual Precedence::.
  75. Here is a sample run of `calc.y':
  76. % calc
  77. 4 + 4.5 - (34/(8*3+-3))
  78. 6.880952381
  79. -56 + 2
  80. -54
  81. 3 ^ 2
  82. 9
  83. 
  84. File: bison.info, Node: Simple Error Recovery, Next: Multi-function Calc, Prev: Infix Calc, Up: Examples
  85. Simple Error Recovery
  86. =====================
  87. Up to this point, this manual has not addressed the issue of "error
  88. recovery"--how to continue parsing after the parser detects a syntax
  89. error. All we have handled is error reporting with `yyerror'. Recall
  90. that by default `yyparse' returns after calling `yyerror'. This means
  91. that an erroneous input line causes the calculator program to exit.
  92. Now we show how to rectify this deficiency.
  93. The Bison language itself includes the reserved word `error', which
  94. may be included in the grammar rules. In the example below it has
  95. been added to one of the alternatives for `line':
  96. line: '\n'
  97. | exp '\n' { printf ("\t%.10g\n", $1); }
  98. | error '\n' { yyerrok; }
  99. ;
  100. This addition to the grammar allows for simple error recovery in
  101. the event of a parse error. If an expression that cannot be evaluated
  102. is read, the error will be recognized by the third rule for `line',
  103. and parsing will continue. (The `yyerror' function is still called
  104. upon to print its message as well.) The action executes the statement
  105. `yyerrok', a macro defined automatically by Bison; its meaning is that
  106. error recovery is complete (*note Error Recovery::.). Note the
  107. difference between `yyerrok' and `yyerror'; neither one is a misprint.
  108. This form of error recovery deals with syntax errors. There are
  109. other kinds of errors; for example, division by zero, which raises an
  110. exception signal that is normally fatal. A real calculator program
  111. must handle this signal and use `longjmp' to return to `main' and
  112. resume parsing input lines; it would also have to discard the rest of
  113. the current line of input. We won't discuss this issue further
  114. because it is not specific to Bison programs.
  115. 
  116. File: bison.info, Node: Multi-function Calc, Next: Exercises, Prev: Simple Error Recovery, Up: Examples
  117. Multi-Function Calculator: `mfcalc'
  118. ===================================
  119. Now that the basics of Bison have been discussed, it is time to
  120. move on to a more advanced problem. The above calculators provided
  121. only five functions, `+', `-', `*', `/' and `^'. It would be nice to
  122. have a calculator that provides other mathematical functions such as
  123. `sin', `cos', etc.
  124. It is easy to add new operators to the infix calculator as long as
  125. they are only single-character literals. The lexical analyzer `yylex'
  126. passes back all non-number characters as tokens, so new grammar rules
  127. suffice for adding a new operator. But we want something more
  128. flexible: built-in functions whose syntax has this form:
  129. FUNCTION_NAME (ARGUMENT)
  130. At the same time, we will add memory to the calculator, by allowing you
  131. to create named variables, store values in them, and use them later.
  132. Here is a sample session with the multi-function calculator:
  133. % acalc
  134. pi = 3.141592653589
  135. 3.1415926536
  136. sin(pi)
  137. 0.0000000000
  138. alpha = beta1 = 2.3
  139. 2.3000000000
  140. alpha
  141. 2.3000000000
  142. ln(alpha)
  143. 0.8329091229
  144. exp(ln(beta1))
  145. 2.3000000000
  146. %
  147. Note that multiple assignment and nested function calls are
  148. permitted.
  149. * Menu:
  150. * Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
  151. * Rules: Mfcalc Rules. Grammar rules for the calculator.
  152. * Symtab: Mfcalc Symtab. Symbol table management subroutines.
  153. 
  154. File: bison.info, Node: Mfcalc Decl, Next: Mfcalc Rules, Prev: Multi-function Calc, Up: Multi-function Calc
  155. Declarations for `mfcalc'
  156. -------------------------
  157. Here are the C and Bison declarations for the multi-function
  158. calculator.
  159. %{
  160. #include <math.h> /* For math functions, cos(), sin(), etc. */
  161. #include "calc.h" /* Contains definition of `symrec' */
  162. %}
  163. %union {
  164. double val; /* For returning numbers. */
  165. symrec *tptr; /* For returning symbol-table pointers */
  166. }
  167. %token <val> NUM /* Simple double precision number */
  168. %token <tptr> VAR FNCT /* Variable and Function */
  169. %type <val> exp
  170. %right '='
  171. %left '-' '+'
  172. %left '*' '/'
  173. %left NEG /* Negation--unary minus */
  174. %right '^' /* Exponentiation */
  175. /* Grammar follows */
  176. %%
  177. The above grammar introduces only two new features of the Bison
  178. language. These features allow semantic values to have various data
  179. types (*note Multiple Types::.).
  180. The `%union' declaration specifies the entire list of possible
  181. types; this is instead of defining `YYSTYPE'. The allowable types are
  182. now double-floats (for `exp' and `NUM') and pointers to entries in the
  183. symbol table. *Note Union Decl::.
  184. Since values can now have various types, it is necessary to
  185. associate a type with each grammar symbol whose semantic value is
  186. used. These symbols are `NUM', `VAR', `FNCT', and `exp'. Their
  187. declarations are augmented with information about their data type
  188. (placed between angle brackets).
  189. The Bison construct `%type' is used for declaring nonterminal
  190. symbols, just as `%token' is used for declaring token types. We have
  191. not used `%type' before because nonterminal symbols are normally
  192. declared implicitly by the rules that define them. But `exp' must be
  193. declared explicitly so we can specify its value type. *Note Type
  194. Decl::.
  195. 
  196. File: bison.info, Node: Mfcalc Rules, Next: Mfcalc Symtab, Prev: Mfcalc Decl, Up: Multi-function Calc
  197. Grammar Rules for `mfcalc'
  198. --------------------------
  199. Here are the grammar rules for the multi-function calculator. Most
  200. of them are copied directly from `calc'; three rules, those which
  201. mention `VAR' or `FNCT', are new.
  202. input: /* empty */
  203. | input line
  204. ;
  205. line:
  206. '\n'
  207. | exp '\n' { printf ("\t%.10g\n", $1); }
  208. | error '\n' { yyerrok; }
  209. ;
  210. exp: NUM { $$ = $1; }
  211. | VAR { $$ = $1->value.var; }
  212. | VAR '=' exp { $$ = $3; $1->value.var = $3; }
  213. | FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
  214. | exp '+' exp { $$ = $1 + $3; }
  215. | exp '-' exp { $$ = $1 - $3; }
  216. | exp '*' exp { $$ = $1 * $3; }
  217. | exp '/' exp { $$ = $1 / $3; }
  218. | '-' exp %prec NEG { $$ = -$2; }
  219. | exp '^' exp { $$ = pow ($1, $3); }
  220. | '(' exp ')' { $$ = $2; }
  221. ;
  222. /* End of grammar */
  223. %%
  224. 
  225. File: bison.info, Node: Mfcalc Symtab, Prev: Mfcalc Rules, Up: Multi-function Calc
  226. The `mfcalc' Symbol Table
  227. -------------------------
  228. The multi-function calculator requires a symbol table to keep track
  229. of the names and meanings of variables and functions. This doesn't
  230. affect the grammar rules (except for the actions) or the Bison
  231. declarations, but it requires some additional C functions for support.
  232. The symbol table itself consists of a linked list of records. Its
  233. definition, which is kept in the header `calc.h', is as follows. It
  234. provides for either functions or variables to be placed in the table.
  235. /* Data type for links in the chain of symbols. */
  236. struct symrec
  237. {
  238. char *name; /* name of symbol */
  239. int type; /* type of symbol: either VAR or FNCT */
  240. union {
  241. double var; /* value of a VAR */
  242. double (*fnctptr)(); /* value of a FNCT */
  243. } value;
  244. struct symrec *next; /* link field */
  245. };
  246. typedef struct symrec symrec;
  247. /* The symbol table: a chain of `struct symrec'. */
  248. extern symrec *sym_table;
  249. symrec *putsym ();
  250. symrec *getsym ();
  251. The new version of `main' includes a call to `init_table', a
  252. function that initializes the symbol table. Here it is, and
  253. `init_table' as well:
  254. #include <stdio.h>
  255. main ()
  256. {
  257. init_table ();
  258. yyparse ();
  259. }
  260. yyerror (s) /* Called by yyparse on error */
  261. char *s;
  262. {
  263. printf ("%s\n", s);
  264. }
  265. struct init
  266. {
  267. char *fname;
  268. double (*fnct)();
  269. };
  270. struct init arith_fncts[]
  271. = {
  272. "sin", sin,
  273. "cos", cos,
  274. "atan", atan,
  275. "ln", log,
  276. "exp", exp,
  277. "sqrt", sqrt,
  278. 0, 0
  279. };
  280. /* The symbol table: a chain of `struct symrec'. */
  281. symrec *sym_table = (symrec *)0;
  282. init_table () /* puts arithmetic functions in table. */
  283. {
  284. int i;
  285. symrec *ptr;
  286. for (i = 0; arith_fncts[i].fname != 0; i++)
  287. {
  288. ptr = putsym (arith_fncts[i].fname, FNCT);
  289. ptr->value.fnctptr = arith_fncts[i].fnct;
  290. }
  291. }
  292. By simply editing the initialization list and adding the necessary
  293. include files, you can add additional functions to the calculator.
  294. Two important functions allow look-up and installation of symbols
  295. in the symbol table. The function `putsym' is passed a name and the
  296. type (`VAR' or `FNCT') of the object to be installed. The object is
  297. linked to the front of the list, and a pointer to the object is
  298. returned. The function `getsym' is passed the name of the symbol to
  299. look up. If found, a pointer to that symbol is returned; otherwise
  300. zero is returned.
  301. symrec *
  302. putsym (sym_name,sym_type)
  303. char *sym_name;
  304. int sym_type;
  305. {
  306. symrec *ptr;
  307. ptr = (symrec *) malloc (sizeof (symrec));
  308. ptr->name = (char *) malloc (strlen (sym_name) + 1);
  309. strcpy (ptr->name,sym_name);
  310. ptr->type = sym_type;
  311. ptr->value.var = 0; /* set value to 0 even if fctn. */
  312. ptr->next = (struct symrec *)sym_table;
  313. sym_table = ptr;
  314. return ptr;
  315. }
  316. symrec *
  317. getsym (sym_name)
  318. char *sym_name;
  319. {
  320. symrec *ptr;
  321. for (ptr = sym_table; ptr != (symrec *) 0;
  322. ptr = (symrec *)ptr->next)
  323. if (strcmp (ptr->name,sym_name) == 0)
  324. return ptr;
  325. return 0;
  326. }
  327. The function `yylex' must now recognize variables, numeric values,
  328. and the single-character arithmetic operators. Strings of alphanumeric
  329. characters with a leading nondigit are recognized as either variables
  330. or functions depending on what the symbol table says about them.
  331. The string is passed to `getsym' for look up in the symbol table.
  332. If the name appears in the table, a pointer to its location and its
  333. type (`VAR' or `FNCT') is returned to `yyparse'. If it is not already
  334. in the table, then it is installed as a `VAR' using `putsym'. Again,
  335. a pointer and its type (which must be `VAR') is returned to `yyparse'.
  336. No change is needed in the handling of numeric values and arithmetic
  337. operators in `yylex'.
  338. #include <ctype.h>
  339. yylex ()
  340. {
  341. int c;
  342. /* Ignore whitespace, get first nonwhite character. */
  343. while ((c = getchar ()) == ' ' || c == '\t');
  344. if (c == EOF)
  345. return 0;
  346. /* Char starts a number => parse the number. */
  347. if (c == '.' || isdigit (c))
  348. {
  349. ungetc (c, stdin);
  350. scanf ("%lf", &yylval.val);
  351. return NUM;
  352. }
  353. /* Char starts an identifier => read the name. */
  354. if (isalpha (c))
  355. {
  356. symrec *s;
  357. static char *symbuf = 0;
  358. static int length = 0;
  359. int i;
  360. /* Initially make the buffer long enough
  361. for a 40-character symbol name. */
  362. if (length == 0)
  363. length = 40, symbuf = (char *)malloc (length + 1);
  364. i = 0;
  365. do
  366. {
  367. /* If buffer is full, make it bigger. */
  368. if (i == length)
  369. {
  370. length *= 2;
  371. symbuf = (char *)realloc (symbuf, length + 1);
  372. }
  373. /* Add this character to the buffer. */
  374. symbuf[i++] = c;
  375. /* Get another character. */
  376. c = getchar ();
  377. }
  378. while (c != EOF && isalnum (c));
  379. ungetc (c, stdin);
  380. symbuf[i] = '\0';
  381. s = getsym (symbuf);
  382. if (s == 0)
  383. s = putsym (symbuf, VAR);
  384. yylval.tptr = s;
  385. return s->type;
  386. }
  387. /* Any other character is a token by itself. */
  388. return c;
  389. }
  390. This program is both powerful and flexible. You may easily add new
  391. functions, and it is a simple job to modify this code to install
  392. predefined variables such as `pi' or `e' as well.
  393. 
  394. File: bison.info, Node: Exercises, Prev: Multi-function calc, Up: Examples
  395. Exercises
  396. =========
  397. 1. Add some new functions from `math.h' to the initialization list.
  398. 2. Add another array that contains constants and their values. Then
  399. modify `init_table' to add these constants to the symbol table.
  400. It will be easiest to give the constants type `VAR'.
  401. 3. Make the program report an error if the user refers to an
  402. uninitialized variable in any way except to store a value in it.
  403. 
  404. File: bison.info, Node: Grammar File, Next: Interface, Prev: Examples, Up: Top
  405. Bison Grammar Files
  406. *******************
  407. Bison takes as input a context-free grammar specification and
  408. produces a C-language function that recognizes correct instances of
  409. the grammar.
  410. The Bison grammar input file conventionally has a name ending in
  411. `.y'.
  412. * Menu:
  413. * Grammar Outline:: Overall layout of the grammar file.
  414. * Symbols:: Terminal and nonterminal symbols.
  415. * Rules:: How to write grammar rules.
  416. * Recursion:: Writing recursive rules.
  417. * Semantics:: Semantic values and actions.
  418. * Declarations:: All kinds of Bison declarations are described here.
  419. * Multiple Parsers:: Putting more than one Bison parser in one program.
  420. 
  421. File: bison.info, Node: Grammar Outline, Next: Symbols, Prev: Grammar File, Up: Grammar File
  422. Outline of a Bison Grammar
  423. ==========================
  424. A Bison grammar file has four main sections, shown here with the
  425. appropriate delimiters:
  426. %{
  427. C DECLARATIONS
  428. %}
  429. BISON DECLARATIONS
  430. %%
  431. GRAMMAR RULES
  432. %%
  433. ADDITIONAL C CODE
  434. Comments enclosed in `/* ... */' may appear in any of the sections.
  435. * Menu:
  436. * C Declarations:: Syntax and usage of the C declarations section.
  437. * Bison Declarations:: Syntax and usage of the Bison declarations section.
  438. * Grammar Rules:: Syntax and usage of the grammar rules section.
  439. * C Code:: Syntax and usage of the additional C code section.
  440. 
  441. File: bison.info, Node: C Declarations, Next: Bison Declarations, Prev: Grammar Outline, Up: Grammar Outline
  442. The C Declarations Section
  443. --------------------------
  444. The C DECLARATIONS section contains macro definitions and
  445. declarations of functions and variables that are used in the actions
  446. in the grammar rules. These are copied to the beginning of the parser
  447. file so that they precede the definition of `yylex'. You can use
  448. `#include' to get the declarations from a header file. If you don't
  449. need any C declarations, you may omit the `%{' and `%}' delimiters
  450. that bracket this section.
  451. 
  452. File: bison.info, Node: Bison Declarations, Next: Grammar Rules, Prev: C Declarations, Up: Grammar Outline
  453. The Bison Declarations Section
  454. ------------------------------
  455. The BISON DECLARATIONS section contains declarations that define
  456. terminal and nonterminal symbols, specify precedence, and so on. In
  457. some simple grammars you may not need any declarations. *Note
  458. Declarations::.
  459. 
  460. File: bison.info, Node: Grammar Rules, Next: C Code, Prev: Bison Declarations, Up: Grammar Outline
  461. The Grammar Rules Section
  462. -------------------------
  463. The "grammar rules" section contains one or more Bison grammar
  464. rules, and nothing else. *Note Rules::.
  465. There must always be at least one grammar rule, and the first `%%'
  466. (which precedes the grammar rules) may never be omitted even if it is
  467. the first thing in the file.
  468. 
  469. File: bison.info, Node: C Code, Prev: Grammar Rules, Up: Grammar Outline
  470. The Additional C Code Section
  471. -----------------------------
  472. The ADDITIONAL C CODE section is copied verbatim to the end of the
  473. parser file, just as the C DECLARATIONS section is copied to the
  474. beginning. This is the most convenient place to put anything that you
  475. want to have in the parser file but which need not come before the
  476. definition of `yylex'. For example, the definitions of `yylex' and
  477. `yyerror' often go here. *Note Interface::.
  478. If the last section is empty, you may omit the `%%' that separates
  479. it from the grammar rules.
  480. The Bison parser itself contains many static variables whose names
  481. start with `yy' and many macros whose names start with `YY'. It is a
  482. good idea to avoid using any such names (except those documented in
  483. this manual) in the additional C code section of the grammar file.
  484. 
  485. File: bison.info, Node: Symbols, Next: Rules, Prev: Grammar Outline, Up: Grammar File
  486. Symbols, Terminal and Nonterminal
  487. =================================
  488. "Symbols" in Bison grammars represent the grammatical
  489. classifications of the language.
  490. A "terminal symbol" (also known as a "token type") represents a
  491. class of syntactically equivalent tokens. You use the symbol in
  492. grammar rules to mean that a token in that class is allowed. The
  493. symbol is represented in the Bison parser by a numeric code, and the
  494. `yylex' function returns a token type code to indicate what kind of
  495. token has been read. You don't need to know what the code value is;
  496. you can use the symbol to stand for it.
  497. A "nonterminal symbol" stands for a class of syntactically
  498. equivalent groupings. The symbol name is used in writing grammar
  499. rules. By convention, it should be all lower case.
  500. Symbol names can contain letters, digits (not at the beginning),
  501. underscores and periods. Periods make sense only in nonterminals.
  502. There are two ways of writing terminal symbols in the grammar:
  503. * A "named token type" is written with an identifier, like an
  504. identifier in C. By convention, it should be all upper case.
  505. Each such name must be defined with a Bison declaration such as
  506. `%token'. *Note Token Decl::.
  507. * A "character token type" (or "literal token") is written in the
  508. grammar using the same syntax used in C for character constants;
  509. for example, `'+'' is a character token type. A character token
  510. type doesn't need to be declared unless you need to specify its
  511. semantic value data type (*note Value Type::.), associativity, or
  512. precedence (*note Precedence::.).
  513. By convention, a character token type is used only to represent a
  514. token that consists of that particular character. Thus, the token
  515. type `'+'' is used to represent the character `+' as a token.
  516. Nothing enforces this convention, but if you depart from it, your
  517. program will confuse other readers.
  518. All the usual escape sequences used in character literals in C
  519. can be used in Bison as well, but you must not use the null
  520. character as a character literal because its ASCII code, zero, is
  521. the code `yylex' returns for end-of-input (*note Calling
  522. Convention::.).
  523. How you choose to write a terminal symbol has no effect on its
  524. grammatical meaning. That depends only on where it appears in rules
  525. and on when the parser function returns that symbol.
  526. The value returned by `yylex' is always one of the terminal symbols
  527. (or 0 for end-of-input). Whichever way you write the token type in the
  528. grammar rules, you write it the same way in the definition of `yylex'.
  529. The numeric code for a character token type is simply the ASCII code
  530. for the character, so `yylex' can use the identical character constant
  531. to generate the requisite code. Each named token type becomes a C
  532. macro in the parser file, so `yylex' can use the name to stand for the
  533. code. (This is why periods don't make sense in terminal symbols.)
  534. *Note Calling Convention::.
  535. If `yylex' is defined in a separate file, you need to arrange for
  536. the token-type macro definitions to be available there. Use the `-d'
  537. option when you run Bison, so that it will write these macro
  538. definitions into a separate header file `NAME.tab.h' which you can
  539. include in the other source files that need it. *Note Invocation::.
  540. The symbol `error' is a terminal symbol reserved for error recovery
  541. (*note Error Recovery::.); you shouldn't use it for any other purpose.
  542. In particular, `yylex' should never return this value.
  543. 
  544. File: bison.info, Node: Rules, Next: Recursion, Prev: Symbols, Up: Grammar File
  545. Syntax of Grammar Rules
  546. =======================
  547. A Bison grammar rule has the following general form:
  548. RESULT: COMPONENTS...
  549. ;
  550. where RESULT is the nonterminal symbol that this rule describes and
  551. COMPONENTS are various terminal and nonterminal symbols that are put
  552. together by this rule (*note Symbols::.). For example,
  553. exp: exp '+' exp
  554. ;
  555. says that two groupings of type `exp', with a `+' token in between,
  556. can be combined into a larger grouping of type `exp'.
  557. Whitespace in rules is significant only to separate symbols. You
  558. can add extra whitespace as you wish.
  559. Scattered among the components can be ACTIONS that determine the
  560. semantics of the rule. An action looks like this:
  561. {C STATEMENTS}
  562. Usually there is only one action and it follows the components. *Note
  563. Actions::.
  564. Multiple rules for the same RESULT can be written separately or can
  565. be joined with the vertical-bar character `|' as follows:
  566. RESULT: RULE1-COMPONENTS...
  567. | RULE2-COMPONENTS...
  568. ...
  569. ;
  570. They are still considered distinct rules even when joined in this way.
  571. If COMPONENTS in a rule is empty, it means that RESULT can match
  572. the empty string. For example, here is how to define a
  573. comma-separated sequence of zero or more `exp' groupings:
  574. expseq: /* empty */
  575. | expseq1
  576. ;
  577. expseq1: exp
  578. | expseq1 ',' exp
  579. ;
  580. It is customary to write a comment `/* empty */' in each rule with no
  581. components.
  582. 
  583. File: bison.info, Node: Recursion, Next: Semantics, Prev: Rules, Up: Grammar File
  584. Recursive Rules
  585. ===============
  586. A rule is called "recursive" when its RESULT nonterminal appears
  587. also on its right hand side. Nearly all Bison grammars need to use
  588. recursion, because that is the only way to define a sequence of any
  589. number of somethings. Consider this recursive definition of a
  590. comma-separated sequence of one or more expressions:
  591. expseq1: exp
  592. | expseq1 ',' exp
  593. ;
  594. Since the recursive use of `expseq1' is the leftmost symbol in the
  595. right hand side, we call this "left recursion". By contrast, here the
  596. same construct is defined using "right recursion":
  597. expseq1: exp
  598. | exp ',' expseq1
  599. ;
  600. Any kind of sequence can be defined using either left recursion or
  601. right recursion, but you should always use left recursion, because it
  602. can parse a sequence of any number of elements with bounded stack
  603. space. Right recursion uses up space on the Bison stack in proportion
  604. to the number of elements in the sequence, because all the elements
  605. must be shifted onto the stack before the rule can be applied even
  606. once. *Note The Algorithm of the Bison Parser: Algorithm, for further
  607. explanation of this.
  608. "Indirect" or "mutual" recursion occurs when the result of the rule
  609. does not appear directly on its right hand side, but does appear in
  610. rules for other nonterminals which do appear on its right hand side.
  611. For example:
  612. expr: primary
  613. | primary '+' primary
  614. ;
  615. primary: constant
  616. | '(' expr ')'
  617. ;
  618. defines two mutually-recursive nonterminals, since each refers to the
  619. other.
  620. 
  621. File: bison.info, Node: Semantics, Next: Declarations, Prev: Recursion, Up: Grammar File
  622. Defining Language Semantics
  623. ===========================
  624. The grammar rules for a language determine only the syntax. The
  625. semantics are determined by the semantic values associated with
  626. various tokens and groupings, and by the actions taken when various
  627. groupings are recognized.
  628. For example, the calculator calculates properly because the value
  629. associated with each expression is the proper number; it adds properly
  630. because the action for the grouping `X + Y' is to add the numbers
  631. associated with X and Y.
  632. * Menu:
  633. * Value Type:: Specifying one data type for all semantic values.
  634. * Multiple Types:: Specifying several alternative data types.
  635. * Actions:: An action is the semantic definition of a grammar rule.
  636. * Action Types:: Specifying data types for actions to operate on.
  637. * Mid-Rule Actions:: Most actions go at the end of a rule.
  638. This says when, why and how to use the exceptional
  639. action in the middle of a rule.
  640. 
  641. File: bison.info, Node: Value Type, Next: Multiple Types, Prev: Semantics, Up: Semantics
  642. Data Types of Semantic Values
  643. -----------------------------
  644. In a simple program it may be sufficient to use the same data type
  645. for the semantic values of all language constructs. This was true in
  646. the RPN and infix calculator examples (*note RPN Calc::.).
  647. Bison's default is to use type `int' for all semantic values. To
  648. specify some other type, define `YYSTYPE' as a macro, like this:
  649. #define YYSTYPE double
  650. This macro definition must go in the C declarations section of the
  651. grammar file (*note Grammar Outline::.).
  652. 
  653. File: bison.info, Node: Multiple Types, Next: Actions, Prev: Value Type, Up: Semantics
  654. More Than One Value Type
  655. ------------------------
  656. In most programs, you will need different data types for different
  657. kinds of tokens and groupings. For example, a numeric constant may
  658. need type `int' or `long', while a string constant needs type `char *',
  659. and an identifier might need a pointer to an entry in the symbol table.
  660. To use more than one data type for semantic values in one parser,
  661. Bison requires you to do two things:
  662. * Specify the entire collection of possible data types, with the
  663. `%union' Bison declaration (*note Union Decl::.).
  664. * Choose one of those types for each symbol (terminal or
  665. nonterminal) for which semantic values are used. This is done
  666. for tokens with the `%token' Bison declaration (*note Token
  667. Decl::.) and for groupings with the `%type' Bison declaration
  668. (*note Type Decl::.).
  669. 
  670. File: bison.info, Node: Actions, Next: Action Types, Prev: Multiple Types, Up: Semantics
  671. Actions
  672. -------
  673. An action accompanies a syntactic rule and contains C code to be
  674. executed each time an instance of that rule is recognized. The task
  675. of most actions is to compute a semantic value for the grouping built
  676. by the rule from the semantic values associated with tokens or smaller
  677. groupings.
  678. An action consists of C statements surrounded by braces, much like a
  679. compound statement in C. It can be placed at any position in the
  680. rule; it is executed at that position. Most rules have just one
  681. action at the end of the rule, following all the components. Actions
  682. in the middle of a rule are tricky and used only for special purposes
  683. (*note Mid-Rule Actions::.).
  684. The C code in an action can refer to the semantic values of the
  685. components matched by the rule with the construct `$N', which stands
  686. for the value of the Nth component. The semantic value for the
  687. grouping being constructed is `$$'. (Bison translates both of these
  688. constructs into array element references when it copies the actions
  689. into the parser file.)
  690. Here is a typical example:
  691. exp: ...
  692. | exp '+' exp
  693. { $$ = $1 + $3; }
  694. This rule constructs an `exp' from two smaller `exp' groupings
  695. connected by a plus-sign token. In the action, `$1' and `$3' refer to
  696. the semantic values of the two component `exp' groupings, which are
  697. the first and third symbols on the right hand side of the rule. The
  698. sum is stored into `$$' so that it becomes the semantic value of the
  699. addition-expression just recognized by the rule. If there were a
  700. useful semantic value associated with the `+' token, it could be
  701. referred to as `$2'.
  702. `$N' with N zero or negative is allowed for reference to tokens and
  703. groupings on the stack *before* those that match the current rule.
  704. This is a very risky practice, and to use it reliably you must be
  705. certain of the context in which the rule is applied. Here is a case
  706. in which you can use this reliably:
  707. foo: expr bar '+' expr { ... }
  708. | expr bar '-' expr { ... }
  709. ;
  710. bar: /* empty */
  711. { previous_expr = $0; }
  712. ;
  713. As long as `bar' is used only in the fashion shown here, `$0'
  714. always refers to the `expr' which precedes `bar' in the definition of
  715. `foo'.
  716. 
  717. File: bison.info, Node: Action Types, Next: Mid-Rule Actions, Prev: Actions, Up: Semantics
  718. Data Types of Values in Actions
  719. -------------------------------
  720. If you have chosen a single data type for semantic values, the `$$'
  721. and `$N' constructs always have that data type.
  722. If you have used `%union' to specify a variety of data types, then
  723. you must declare a choice among these types for each terminal or
  724. nonterminal symbol that can have a semantic value. Then each time you
  725. use `$$' or `$N', its data type is determined by which symbol it
  726. refers to in the rule. In this example,
  727. exp: ...
  728. | exp '+' exp
  729. { $$ = $1 + $3; }
  730. `$1' and `$3' refer to instances of `exp', so they all have the data
  731. type declared for the nonterminal symbol `exp'. If `$2' were used, it
  732. would have the data type declared for the terminal symbol `'+'',
  733. whatever that might be.
  734. Alternatively, you can specify the data type when you refer to the
  735. value, by inserting `<TYPE>' after the `$' at the beginning of the
  736. reference. For example, if you have defined types as shown here:
  737. %union {
  738. int itype;
  739. double dtype;
  740. }
  741. then you can write `$<itype>1' to refer to the first subunit of the
  742. rule as an integer, or `$<dtype>1' to refer to it as a double.
  743. 
  744. File: bison.info, Node: Mid-Rule Actions, Prev: Action Types, Up: Semantics
  745. Actions in Mid-Rule
  746. -------------------
  747. Occasionally it is useful to put an action in the middle of a rule.
  748. These actions are written just like usual end-of-rule actions, but they
  749. are executed before the parser even recognizes the following
  750. components.
  751. A mid-rule action may refer to the components preceding it using
  752. `$N', but it may not refer to subsequent components because it is run
  753. before they are parsed.
  754. The mid-rule action itself counts as one of the components of the
  755. rule. This makes a difference when there is another action later in
  756. the same rule (and usually there is another at the end): you have to
  757. count the actions along with the symbols when working out which number
  758. N to use in `$N'.
  759. The mid-rule action can also have a semantic value. This can be set
  760. within that action by an assignment to `$$', and can referred to by
  761. actions later in the rule using `$N'. Since there is no symbol to
  762. name the action, there is no way to declare a data type for the value
  763. in advance, so you must use the `$<...>' construct to specify a data
  764. type each time you refer to this value.
  765. There is no way to set the value of the entire rule with a mid-rule
  766. action, because assignments to `$$' do not have that effect. The only
  767. way to set the value for the entire rule is with an ordinary action at
  768. the end of the rule.
  769. Here is an example from a hypothetical compiler, handling a `let'
  770. statement that looks like `let (VARIABLE) STATEMENT' and serves to
  771. create a variable named VARIABLE temporarily for the duration of
  772. STATEMENT. To parse this construct, we must put VARIABLE into the
  773. symbol table while STATEMENT is parsed, then remove it afterward.
  774. Here is how it is done:
  775. stmt: LET '(' var ')'
  776. { $<context>$ = push_context ();
  777. declare_variable ($3); }
  778. stmt { $$ = $6;
  779. pop_context ($<context>5); }
  780. As soon as `let (VARIABLE)' has been recognized, the first action is
  781. run. It saves a copy of the current semantic context (the list of
  782. accessible variables) as its semantic value, using alternative
  783. `context' in the data-type union. Then it calls `declare_variable' to
  784. add the new variable to that list. Once the first action is finished,
  785. the embedded statement `stmt' can be parsed. Note that the mid-rule
  786. action is component number 5, so the `stmt' is component number 6.
  787. After the embedded statement is parsed, its semantic value becomes
  788. the value of the entire `let'-statement. Then the semantic value from
  789. the earlier action is used to restore the prior list of variables.
  790. This removes the temporary `let'-variable from the list so that it
  791. won't appear to exist while the rest of the program is parsed.
  792. Taking action before a rule is completely recognized often leads to
  793. conflicts since the parser must commit to a parse in order to execute
  794. the action. For example, the following two rules, without mid-rule
  795. actions, can coexist in a working parser because the parser can shift
  796. the open-brace token and look at what follows before deciding whether
  797. there is a declaration or not:
  798. compound: '{' declarations statements '}'
  799. | '{' statements '}'
  800. ;
  801. But when we add a mid-rule action as follows, the rules become
  802. nonfunctional:
  803. compound: { prepare_for_local_variables (); }
  804. '{' declarations statements '}'
  805. | '{' statements '}'
  806. ;
  807. Now the parser is forced to decide whether to run the mid-rule action
  808. when it has read no farther than the open-brace. In other words, it
  809. must commit to using one rule or the other, without sufficient
  810. information to do it correctly. (The open-brace token is what is
  811. called the "look-ahead" token at this time, since the parser is still
  812. deciding what to do about it. *Note Look-Ahead::.)
  813. You might think that you could correct the problem by putting
  814. identical actions into the two rules, like this:
  815. compound: { prepare_for_local_variables (); }
  816. '{' declarations statements '}'
  817. | { prepare_for_local_variables (); }
  818. '{' statements '}'
  819. ;
  820. But this does not help, because Bison does not realize that the two
  821. actions are identical. (Bison never tries to understand the C code in
  822. an action.)
  823. If the grammar is such that a declaration can be distinguished from
  824. a statement by the first token (which is true in C), then one solution
  825. which does work is to put the action after the open-brace, like this:
  826. compound: '{' { prepare_for_local_variables (); }
  827. declarations statements '}'
  828. | '{' statements '}'
  829. ;
  830. Now the first token of the following declaration or statement, which
  831. would in any case tell Bison which rule to use, can still do so.
  832. Another solution is to bury the action inside a nonterminal symbol
  833. which serves as a subroutine:
  834. subroutine: /* empty */
  835. { prepare_for_local_variables (); }
  836. ;
  837. compound: subroutine
  838. '{' declarations statements '}'
  839. | subroutine
  840. '{' statements '}'
  841. ;
  842. Now Bison can execute the action in the rule for `subroutine' without
  843. deciding which rule for `compound' it will eventually use. Note that
  844. the action is now at the end of its rule. Any mid-rule action can be
  845. converted to an end-of-rule action in this way, and this is what Bison
  846. actually does to implement mid-rule actions.
  847. 
  848. File: bison.info, Node: Declarations, Next: Multiple Parsers, Prev: Semantics, Up: Grammar File
  849. Bison Declarations
  850. ==================
  851. The "Bison declarations" section of a Bison grammar defines the
  852. symbols used in formulating the grammar and the data types of semantic
  853. values. *Note Symbols::.
  854. All token type names (but not single-character literal tokens such
  855. as `'+'' and `'*'') must be declared. Nonterminal symbols must be
  856. declared if you need to specify which data type to use for the semantic
  857. value (*note Multiple Types::.).
  858. The first rule in the file also specifies the start symbol, by
  859. default. If you want some other symbol to be the start symbol, you
  860. must declare it explicitly (*note Language and Grammar::.).
  861. * Menu:
  862. * Token Decl:: Declaring terminal symbols.
  863. * Precedence Decl:: Declaring terminals with precedence and associativity.
  864. * Union Decl:: Declaring the set of all semantic value types.
  865. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
  866. * Expect Decl:: Suppressing warnings about shift/reduce conflicts.
  867. * Start Decl:: Specifying the start symbol.
  868. * Pure Decl:: Requesting a reentrant parser.
  869. * Decl Summary:: Table of all Bison declarations.
  870. 
  871. File: bison.info, Node: Token Decl, Next: Precedence Decl, Prev: Declarations, Up: Declarations
  872. Token Type Names
  873. ----------------
  874. The basic way to declare a token type name (terminal symbol) is as
  875. follows:
  876. %token NAME
  877. Bison will convert this into a `#define' directive in the parser,
  878. so that the function `yylex' (if it is in this file) can use the name
  879. NAME to stand for this token type's code.
  880. Alternatively you can use `%left', `%right', or `%nonassoc' instead
  881. of `%token', if you wish to specify precedence. *Note Precedence
  882. Decl::.
  883. You can explicitly specify the numeric code for a token type by
  884. appending an integer value in the field immediately following the
  885. token name:
  886. %token NUM 300
  887. It is generally best, however, to let Bison choose the numeric codes
  888. for all token types. Bison will automatically select codes that don't
  889. conflict with each other or with ASCII characters.
  890. In the event that the stack type is a union, you must augment the
  891. `%token' or other token declaration to include the data type
  892. alternative delimited by angle-brackets (*note Multiple Types::.). For
  893. example:
  894. %union { /* define stack type */
  895. double val;
  896. symrec *tptr;
  897. }
  898. %token <val> NUM /* define token NUM and its type */
  899. 
  900. File: bison.info, Node: Precedence Decl, Next: Union Decl, Prev: Token Decl, Up: Declarations
  901. Operator Precedence
  902. -------------------
  903. Use the `%left', `%right' or `%nonassoc' declaration to declare a
  904. token and specify its precedence and associativity, all at once.
  905. These are called "precedence declarations". *Note Precedence::, for
  906. general information on operator precedence.
  907. The syntax of a precedence declaration is the same as that of
  908. `%token': either
  909. %left SYMBOLS...
  910. or
  911. %left <TYPE> SYMBOLS...
  912. And indeed any of these declarations serves the purposes of
  913. `%token'. But in addition, they specify the associativity and
  914. relative precedence for all the SYMBOLS:
  915. * The associativity of an operator OP determines how repeated uses
  916. of the operator nest: whether `X OP Y OP Z' is parsed by grouping
  917. X with Y first or by grouping Y with Z first. `%left' specifies
  918. left-associativity (grouping X with Y first) and `%right'
  919. specifies right-associativity (grouping Y with Z first).
  920. `%nonassoc' specifies no associativity, which means that `X OP Y
  921. OP Z' is considered a syntax error.
  922. * The precedence of an operator determines how it nests with other
  923. operators. All the tokens declared in a single precedence
  924. declaration have equal precedence and nest together according to
  925. their associativity. When two tokens declared in different
  926. precedence declarations associate, the one declared later has the
  927. higher precedence and is grouped first.
  928. 
  929. File: bison.info, Node: Union Decl, Next: Type Decl, Prev: Precedence Decl, Up: Declarations
  930. The Collection of Value Types
  931. -----------------------------
  932. The `%union' declaration specifies the entire collection of possible
  933. data types for semantic values. The keyword `%union' is followed by a
  934. pair of braces containing the same thing that goes inside a `union' in
  935. C. For example:
  936. %union {
  937. double val;
  938. symrec *tptr;
  939. }
  940. This says that the two alternative types are `double' and `symrec *'.
  941. They are given names `val' and `tptr'; these names are used in the
  942. `%token' and `%type' declarations to pick one of the types for a
  943. terminal or nonterminal symbol (*note Type Decl::.).
  944. Note that, unlike making a `union' declaration in C, you do not
  945. write a semicolon after the closing brace.
  946. 
  947. File: bison.info, Node: Type Decl, Next: Expect Decl, Prev: Union Decl, Up: Declarations
  948. Nonterminal Symbols
  949. -------------------
  950. When you use `%union' to specify multiple value types, you must
  951. declare the value type of each nonterminal symbol for which values are
  952. used. This is done with a `%type' declaration, like this:
  953. %type <TYPE> NONTERMINAL...
  954. Here NONTERMINAL is the name of a nonterminal symbol, and TYPE is the
  955. name given in the `%union' to the alternative that you want (*note
  956. Union Decl::.). You can give any number of nonterminal symbols in the
  957. same `%type' declaration, if they have the same value type. Use
  958. spaces to separate the symbol names.
  959. 
  960. File: bison.info, Node: Expect Decl, Next: Start Decl, Prev: Type Decl, Up: Declarations
  961. Suppressing Conflict Warnings
  962. -----------------------------
  963. Bison normally warns if there are any conflicts in the grammar
  964. (*note Shift/Reduce::.), but most real grammars have harmless
  965. shift/reduce conflicts which are resolved in a predictable way and
  966. would be difficult to eliminate. It is desirable to suppress the
  967. warning about these conflicts unless the number of conflicts changes.
  968. You can do this with the `%expect' declaration.
  969. The declaration looks like this:
  970. %expect N
  971. Here N is a decimal integer. The declaration says there should be
  972. no warning if there are N shift/reduce conflicts and no reduce/reduce
  973. conflicts. The usual warning is given if there are either more or
  974. fewer conflicts, or if there are any reduce/reduce conflicts.
  975. In general, using `%expect' involves these steps:
  976. * Compile your grammar without `%expect'. Use the `-v' option to
  977. get a verbose list of where the conflicts occur. Bison will also
  978. print the number of conflicts.
  979. * Check each of the conflicts to make sure that Bison's default
  980. resolution is what you really want. If not, rewrite the grammar
  981. and go back to the beginning.
  982. * Add an `%expect' declaration, copying the number N from the
  983. number which Bison printed.
  984. Now Bison will stop annoying you about the conflicts you have
  985. checked, but it will warn you again if changes in the grammer result
  986. in additional conflicts.
  987. 
  988. File: bison.info, Node: Start Decl, Next: Pure Decl, Prev: Expect Decl, Up: Declarations
  989. The Start-Symbol
  990. ----------------
  991. Bison assumes by default that the start symbol for the grammar is
  992. the first nonterminal specified in the grammar specification section.
  993. The programmer may override this restriction with the `%start'
  994. declaration as follows:
  995. %start SYMBOL
  996. 
  997. File: bison.info, Node: Pure Decl, Next: Decl Summary, Prev: Start Decl, Up: Declarations
  998. A Pure (Reentrant) Parser
  999. -------------------------
  1000. A "reentrant" program is one which does not alter in the course of
  1001. execution; in other words, it consists entirely of "pure" (read-only)
  1002. code. Reentrancy is important whenever asynchronous execution is
  1003. possible; for example, a nonreentrant program may not be safe to call
  1004. from a signal handler. In systems with multiple threads of control, a
  1005. nonreentrant program must be called only within interlocks.
  1006. The Bison parser is not normally a reentrant program, because it
  1007. uses statically allocated variables for communication with `yylex'.
  1008. These variables include `yylval' and `yylloc'.
  1009. The Bison declaration `%pure_parser' says that you want the parser
  1010. to be reentrant. It looks like this:
  1011. %pure_parser
  1012. The effect is that the two communication variables become local
  1013. variables in `yyparse', and a different calling convention is used for
  1014. the lexical analyzer function `yylex'. *Note Pure Calling::, for the
  1015. details of this. The variable `yynerrs' also becomes local in
  1016. `yyparse' (*note Error Reporting::.). The convention for calling
  1017. `yyparse' itself is unchanged.
  1018. 
  1019. File: bison.info, Node: Decl Summary, Prev: Pure Decl, Up: Declarations
  1020. Bison Declaration Summary
  1021. -------------------------
  1022. Here is a summary of all Bison declarations:
  1023. `%union'
  1024. Declare the collection of data types that semantic values may have
  1025. (*note Union Decl::.).
  1026. `%token'
  1027. Declare a terminal symbol (token type name) with no precedence or
  1028. associativity specified (*note Token Decl::.).
  1029. `%right'
  1030. Declare a terminal symbol (token type name) that is
  1031. right-associative (*note Precedence Decl::.).
  1032. `%left'
  1033. Declare a terminal symbol (token type name) that is
  1034. left-associative (*note Precedence Decl::.).
  1035. `%nonassoc'
  1036. Declare a terminal symbol (token type name) that is nonassociative
  1037. (using it in a way that would be associative is a syntax error)
  1038. (*note Precedence Decl::.).
  1039. `%type'
  1040. Declare the type of semantic values for a nonterminal symbol
  1041. (*note Type Decl::.).
  1042. `%start'
  1043. Specify the grammar's start symbol (*note Start Decl::.).
  1044. `%expect'
  1045. Declare the expected number of shift-reduce conflicts (*note
  1046. Expect Decl::.).
  1047. `%pure_parser'
  1048. Request a pure (reentrant) parser program (*note Pure Decl::.).
  1049.