]> git.saurik.com Git - bison.git/blob - doc/bison.info-2
Version 1.30d.
[bison.git] / doc / bison.info-2
1 Ceci est le fichier Info bison.info, produit par Makeinfo version 4.0b
2 à partir bison.texinfo.
3
4 START-INFO-DIR-ENTRY
5 * bison: (bison). GNU Project parser generator (yacc replacement).
6 END-INFO-DIR-ENTRY
7
8 This file documents the Bison parser generator.
9
10 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, 1999,
11 2000, 2001 Free Software Foundation, Inc.
12
13 Permission is granted to make and distribute verbatim copies of this
14 manual provided the copyright notice and this permission notice are
15 preserved on all copies.
16
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
24 Permission is granted to copy and distribute translations of this
25 manual into another language, under the above conditions for modified
26 versions, except that the sections entitled "GNU General Public
27 License", "Conditions for Using Bison" and this permission notice may be
28 included in translations approved by the Free Software Foundation
29 instead of in the original English.
30
31 \1f
32 File: bison.info, Node: Rpcalc Decls, Next: Rpcalc Rules, Up: RPN Calc
33
34 Declarations for `rpcalc'
35 -------------------------
36
37 Here are the C and Bison declarations for the reverse polish notation
38 calculator. As in C, comments are placed between `/*...*/'.
39
40 /* Reverse polish notation calculator. */
41
42 %{
43 #define YYSTYPE double
44 #include <math.h>
45 %}
46
47 %token NUM
48
49 %% /* Grammar rules and actions follow */
50
51 The C declarations section (*note The C Declarations Section: C
52 Declarations.) contains two preprocessor directives.
53
54 The `#define' directive defines the macro `YYSTYPE', thus specifying
55 the C data type for semantic values of both tokens and groupings (*note
56 Data Types of Semantic Values: Value Type.). The Bison parser will use
57 whatever type `YYSTYPE' is defined as; if you don't define it, `int' is
58 the default. Because we specify `double', each token and each
59 expression has an associated value, which is a floating point number.
60
61 The `#include' directive is used to declare the exponentiation
62 function `pow'.
63
64 The second section, Bison declarations, provides information to
65 Bison about the token types (*note The Bison Declarations Section:
66 Bison Declarations.). Each terminal symbol that is not a
67 single-character literal must be declared here. (Single-character
68 literals normally don't need to be declared.) In this example, all the
69 arithmetic operators are designated by single-character literals, so the
70 only terminal symbol that needs to be declared is `NUM', the token type
71 for numeric constants.
72
73 \1f
74 File: bison.info, Node: Rpcalc Rules, Next: Rpcalc Lexer, Prev: Rpcalc Decls, Up: RPN Calc
75
76 Grammar Rules for `rpcalc'
77 --------------------------
78
79 Here are the grammar rules for the reverse polish notation
80 calculator.
81
82 input: /* empty */
83 | input line
84 ;
85
86 line: '\n'
87 | exp '\n' { printf ("\t%.10g\n", $1); }
88 ;
89
90 exp: NUM { $$ = $1; }
91 | exp exp '+' { $$ = $1 + $2; }
92 | exp exp '-' { $$ = $1 - $2; }
93 | exp exp '*' { $$ = $1 * $2; }
94 | exp exp '/' { $$ = $1 / $2; }
95 /* Exponentiation */
96 | exp exp '^' { $$ = pow ($1, $2); }
97 /* Unary minus */
98 | exp 'n' { $$ = -$1; }
99 ;
100 %%
101
102 The groupings of the rpcalc "language" defined here are the
103 expression (given the name `exp'), the line of input (`line'), and the
104 complete input transcript (`input'). Each of these nonterminal symbols
105 has several alternate rules, joined by the `|' punctuator which is read
106 as "or". The following sections explain what these rules mean.
107
108 The semantics of the language is determined by the actions taken
109 when a grouping is recognized. The actions are the C code that appears
110 inside braces. *Note Actions::.
111
112 You must specify these actions in C, but Bison provides the means for
113 passing semantic values between the rules. In each action, the
114 pseudo-variable `$$' stands for the semantic value for the grouping
115 that the rule is going to construct. Assigning a value to `$$' is the
116 main job of most actions. The semantic values of the components of the
117 rule are referred to as `$1', `$2', and so on.
118
119 * Menu:
120
121 * Rpcalc Input::
122 * Rpcalc Line::
123 * Rpcalc Expr::
124
125 \1f
126 File: bison.info, Node: Rpcalc Input, Next: Rpcalc Line, Up: Rpcalc Rules
127
128 Explanation of `input'
129 ......................
130
131 Consider the definition of `input':
132
133 input: /* empty */
134 | input line
135 ;
136
137 This definition reads as follows: "A complete input is either an
138 empty string, or a complete input followed by an input line". Notice
139 that "complete input" is defined in terms of itself. This definition
140 is said to be "left recursive" since `input' appears always as the
141 leftmost symbol in the sequence. *Note Recursive Rules: Recursion.
142
143 The first alternative is empty because there are no symbols between
144 the colon and the first `|'; this means that `input' can match an empty
145 string of input (no tokens). We write the rules this way because it is
146 legitimate to type `Ctrl-d' right after you start the calculator. It's
147 conventional to put an empty alternative first and write the comment
148 `/* empty */' in it.
149
150 The second alternate rule (`input line') handles all nontrivial
151 input. It means, "After reading any number of lines, read one more
152 line if possible." The left recursion makes this rule into a loop.
153 Since the first alternative matches empty input, the loop can be
154 executed zero or more times.
155
156 The parser function `yyparse' continues to process input until a
157 grammatical error is seen or the lexical analyzer says there are no more
158 input tokens; we will arrange for the latter to happen at end of file.
159
160 \1f
161 File: bison.info, Node: Rpcalc Line, Next: Rpcalc Expr, Prev: Rpcalc Input, Up: Rpcalc Rules
162
163 Explanation of `line'
164 .....................
165
166 Now consider the definition of `line':
167
168 line: '\n'
169 | exp '\n' { printf ("\t%.10g\n", $1); }
170 ;
171
172 The first alternative is a token which is a newline character; this
173 means that rpcalc accepts a blank line (and ignores it, since there is
174 no action). The second alternative is an expression followed by a
175 newline. This is the alternative that makes rpcalc useful. The
176 semantic value of the `exp' grouping is the value of `$1' because the
177 `exp' in question is the first symbol in the alternative. The action
178 prints this value, which is the result of the computation the user
179 asked for.
180
181 This action is unusual because it does not assign a value to `$$'.
182 As a consequence, the semantic value associated with the `line' is
183 uninitialized (its value will be unpredictable). This would be a bug if
184 that value were ever used, but we don't use it: once rpcalc has printed
185 the value of the user's input line, that value is no longer needed.
186
187 \1f
188 File: bison.info, Node: Rpcalc Expr, Prev: Rpcalc Line, Up: Rpcalc Rules
189
190 Explanation of `expr'
191 .....................
192
193 The `exp' grouping has several rules, one for each kind of
194 expression. The first rule handles the simplest expressions: those
195 that are just numbers. The second handles an addition-expression,
196 which looks like two expressions followed by a plus-sign. The third
197 handles subtraction, and so on.
198
199 exp: NUM
200 | exp exp '+' { $$ = $1 + $2; }
201 | exp exp '-' { $$ = $1 - $2; }
202 ...
203 ;
204
205 We have used `|' to join all the rules for `exp', but we could
206 equally well have written them separately:
207
208 exp: NUM ;
209 exp: exp exp '+' { $$ = $1 + $2; } ;
210 exp: exp exp '-' { $$ = $1 - $2; } ;
211 ...
212
213 Most of the rules have actions that compute the value of the
214 expression in terms of the value of its parts. For example, in the
215 rule for addition, `$1' refers to the first component `exp' and `$2'
216 refers to the second one. The third component, `'+'', has no meaningful
217 associated semantic value, but if it had one you could refer to it as
218 `$3'. When `yyparse' recognizes a sum expression using this rule, the
219 sum of the two subexpressions' values is produced as the value of the
220 entire expression. *Note Actions::.
221
222 You don't have to give an action for every rule. When a rule has no
223 action, Bison by default copies the value of `$1' into `$$'. This is
224 what happens in the first rule (the one that uses `NUM').
225
226 The formatting shown here is the recommended convention, but Bison
227 does not require it. You can add or change whitespace as much as you
228 wish. For example, this:
229
230 exp : NUM | exp exp '+' {$$ = $1 + $2; } | ...
231
232 means the same thing as this:
233
234 exp: NUM
235 | exp exp '+' { $$ = $1 + $2; }
236 | ...
237
238 The latter, however, is much more readable.
239
240 \1f
241 File: bison.info, Node: Rpcalc Lexer, Next: Rpcalc Main, Prev: Rpcalc Rules, Up: RPN Calc
242
243 The `rpcalc' Lexical Analyzer
244 -----------------------------
245
246 The lexical analyzer's job is low-level parsing: converting
247 characters or sequences of characters into tokens. The Bison parser
248 gets its tokens by calling the lexical analyzer. *Note The Lexical
249 Analyzer Function `yylex': Lexical.
250
251 Only a simple lexical analyzer is needed for the RPN calculator.
252 This lexical analyzer skips blanks and tabs, then reads in numbers as
253 `double' and returns them as `NUM' tokens. Any other character that
254 isn't part of a number is a separate token. Note that the token-code
255 for such a single-character token is the character itself.
256
257 The return value of the lexical analyzer function is a numeric code
258 which represents a token type. The same text used in Bison rules to
259 stand for this token type is also a C expression for the numeric code
260 for the type. This works in two ways. If the token type is a
261 character literal, then its numeric code is the ASCII code for that
262 character; you can use the same character literal in the lexical
263 analyzer to express the number. If the token type is an identifier,
264 that identifier is defined by Bison as a C macro whose definition is
265 the appropriate number. In this example, therefore, `NUM' becomes a
266 macro for `yylex' to use.
267
268 The semantic value of the token (if it has one) is stored into the
269 global variable `yylval', which is where the Bison parser will look for
270 it. (The C data type of `yylval' is `YYSTYPE', which was defined at
271 the beginning of the grammar; *note Declarations for `rpcalc': Rpcalc
272 Decls..)
273
274 A token type code of zero is returned if the end-of-file is
275 encountered. (Bison recognizes any nonpositive value as indicating the
276 end of the input.)
277
278 Here is the code for the lexical analyzer:
279
280 /* Lexical analyzer returns a double floating point
281 number on the stack and the token NUM, or the ASCII
282 character read if not a number. Skips all blanks
283 and tabs, returns 0 for EOF. */
284
285 #include <ctype.h>
286
287 int
288 yylex (void)
289 {
290 int c;
291
292 /* skip white space */
293 while ((c = getchar ()) == ' ' || c == '\t')
294 ;
295 /* process numbers */
296 if (c == '.' || isdigit (c))
297 {
298 ungetc (c, stdin);
299 scanf ("%lf", &yylval);
300 return NUM;
301 }
302 /* return end-of-file */
303 if (c == EOF)
304 return 0;
305 /* return single chars */
306 return c;
307 }
308
309 \1f
310 File: bison.info, Node: Rpcalc Main, Next: Rpcalc Error, Prev: Rpcalc Lexer, Up: RPN Calc
311
312 The Controlling Function
313 ------------------------
314
315 In keeping with the spirit of this example, the controlling function
316 is kept to the bare minimum. The only requirement is that it call
317 `yyparse' to start the process of parsing.
318
319 int
320 main (void)
321 {
322 return yyparse ();
323 }
324
325 \1f
326 File: bison.info, Node: Rpcalc Error, Next: Rpcalc Gen, Prev: Rpcalc Main, Up: RPN Calc
327
328 The Error Reporting Routine
329 ---------------------------
330
331 When `yyparse' detects a syntax error, it calls the error reporting
332 function `yyerror' to print an error message (usually but not always
333 `"parse error"'). It is up to the programmer to supply `yyerror'
334 (*note Parser C-Language Interface: Interface.), so here is the
335 definition we will use:
336
337 #include <stdio.h>
338
339 void
340 yyerror (const char *s) /* Called by yyparse on error */
341 {
342 printf ("%s\n", s);
343 }
344
345 After `yyerror' returns, the Bison parser may recover from the error
346 and continue parsing if the grammar contains a suitable error rule
347 (*note Error Recovery::). Otherwise, `yyparse' returns nonzero. We
348 have not written any error rules in this example, so any invalid input
349 will cause the calculator program to exit. This is not clean behavior
350 for a real calculator, but it is adequate for the first example.
351
352 \1f
353 File: bison.info, Node: Rpcalc Gen, Next: Rpcalc Compile, Prev: Rpcalc Error, Up: RPN Calc
354
355 Running Bison to Make the Parser
356 --------------------------------
357
358 Before running Bison to produce a parser, we need to decide how to
359 arrange all the source code in one or more source files. For such a
360 simple example, the easiest thing is to put everything in one file. The
361 definitions of `yylex', `yyerror' and `main' go at the end, in the
362 "additional C code" section of the file (*note The Overall Layout of a
363 Bison Grammar: Grammar Layout.).
364
365 For a large project, you would probably have several source files,
366 and use `make' to arrange to recompile them.
367
368 With all the source in a single file, you use the following command
369 to convert it into a parser file:
370
371 bison FILE_NAME.y
372
373 In this example the file was called `rpcalc.y' (for "Reverse Polish
374 CALCulator"). Bison produces a file named `FILE_NAME.tab.c', removing
375 the `.y' from the original file name. The file output by Bison contains
376 the source code for `yyparse'. The additional functions in the input
377 file (`yylex', `yyerror' and `main') are copied verbatim to the output.
378
379 \1f
380 File: bison.info, Node: Rpcalc Compile, Prev: Rpcalc Gen, Up: RPN Calc
381
382 Compiling the Parser File
383 -------------------------
384
385 Here is how to compile and run the parser file:
386
387 # List files in current directory.
388 $ ls
389 rpcalc.tab.c rpcalc.y
390
391 # Compile the Bison parser.
392 # `-lm' tells compiler to search math library for `pow'.
393 $ cc rpcalc.tab.c -lm -o rpcalc
394
395 # List files again.
396 $ ls
397 rpcalc rpcalc.tab.c rpcalc.y
398
399 The file `rpcalc' now contains the executable code. Here is an
400 example session using `rpcalc'.
401
402 $ rpcalc
403 4 9 +
404 13
405 3 7 + 3 4 5 *+-
406 -13
407 3 7 + 3 4 5 * + - n Note the unary minus, `n'
408 13
409 5 6 / 4 n +
410 -3.166666667
411 3 4 ^ Exponentiation
412 81
413 ^D End-of-file indicator
414 $
415
416 \1f
417 File: bison.info, Node: Infix Calc, Next: Simple Error Recovery, Prev: RPN Calc, Up: Examples
418
419 Infix Notation Calculator: `calc'
420 =================================
421
422 We now modify rpcalc to handle infix operators instead of postfix.
423 Infix notation involves the concept of operator precedence and the need
424 for parentheses nested to arbitrary depth. Here is the Bison code for
425 `calc.y', an infix desk-top calculator.
426
427 /* Infix notation calculator--calc */
428
429 %{
430 #define YYSTYPE double
431 #include <math.h>
432 %}
433
434 /* BISON Declarations */
435 %token NUM
436 %left '-' '+'
437 %left '*' '/'
438 %left NEG /* negation--unary minus */
439 %right '^' /* exponentiation */
440
441 /* Grammar follows */
442 %%
443 input: /* empty string */
444 | input line
445 ;
446
447 line: '\n'
448 | exp '\n' { printf ("\t%.10g\n", $1); }
449 ;
450
451 exp: NUM { $$ = $1; }
452 | exp '+' exp { $$ = $1 + $3; }
453 | exp '-' exp { $$ = $1 - $3; }
454 | exp '*' exp { $$ = $1 * $3; }
455 | exp '/' exp { $$ = $1 / $3; }
456 | '-' exp %prec NEG { $$ = -$2; }
457 | exp '^' exp { $$ = pow ($1, $3); }
458 | '(' exp ')' { $$ = $2; }
459 ;
460 %%
461
462 The functions `yylex', `yyerror' and `main' can be the same as before.
463
464 There are two important new features shown in this code.
465
466 In the second section (Bison declarations), `%left' declares token
467 types and says they are left-associative operators. The declarations
468 `%left' and `%right' (right associativity) take the place of `%token'
469 which is used to declare a token type name without associativity.
470 (These tokens are single-character literals, which ordinarily don't
471 need to be declared. We declare them here to specify the
472 associativity.)
473
474 Operator precedence is determined by the line ordering of the
475 declarations; the higher the line number of the declaration (lower on
476 the page or screen), the higher the precedence. Hence, exponentiation
477 has the highest precedence, unary minus (`NEG') is next, followed by
478 `*' and `/', and so on. *Note Operator Precedence: Precedence.
479
480 The other important new feature is the `%prec' in the grammar section
481 for the unary minus operator. The `%prec' simply instructs Bison that
482 the rule `| '-' exp' has the same precedence as `NEG'--in this case the
483 next-to-highest. *Note Context-Dependent Precedence: Contextual
484 Precedence.
485
486 Here is a sample run of `calc.y':
487
488 $ calc
489 4 + 4.5 - (34/(8*3+-3))
490 6.880952381
491 -56 + 2
492 -54
493 3 ^ 2
494 9
495
496 \1f
497 File: bison.info, Node: Simple Error Recovery, Next: Location Tracking Calc, Prev: Infix Calc, Up: Examples
498
499 Simple Error Recovery
500 =====================
501
502 Up to this point, this manual has not addressed the issue of "error
503 recovery"--how to continue parsing after the parser detects a syntax
504 error. All we have handled is error reporting with `yyerror'. Recall
505 that by default `yyparse' returns after calling `yyerror'. This means
506 that an erroneous input line causes the calculator program to exit.
507 Now we show how to rectify this deficiency.
508
509 The Bison language itself includes the reserved word `error', which
510 may be included in the grammar rules. In the example below it has been
511 added to one of the alternatives for `line':
512
513 line: '\n'
514 | exp '\n' { printf ("\t%.10g\n", $1); }
515 | error '\n' { yyerrok; }
516 ;
517
518 This addition to the grammar allows for simple error recovery in the
519 event of a parse error. If an expression that cannot be evaluated is
520 read, the error will be recognized by the third rule for `line', and
521 parsing will continue. (The `yyerror' function is still called upon to
522 print its message as well.) The action executes the statement
523 `yyerrok', a macro defined automatically by Bison; its meaning is that
524 error recovery is complete (*note Error Recovery::). Note the
525 difference between `yyerrok' and `yyerror'; neither one is a misprint.
526
527 This form of error recovery deals with syntax errors. There are
528 other kinds of errors; for example, division by zero, which raises an
529 exception signal that is normally fatal. A real calculator program
530 must handle this signal and use `longjmp' to return to `main' and
531 resume parsing input lines; it would also have to discard the rest of
532 the current line of input. We won't discuss this issue further because
533 it is not specific to Bison programs.
534
535 \1f
536 File: bison.info, Node: Location Tracking Calc, Next: Multi-function Calc, Prev: Simple Error Recovery, Up: Examples
537
538 Location Tracking Calculator: `ltcalc'
539 ======================================
540
541 This example extends the infix notation calculator with location
542 tracking. This feature will be used to improve the error messages. For
543 the sake of clarity, this example is a simple integer calculator, since
544 most of the work needed to use locations will be done in the lexical
545 analyser.
546
547 * Menu:
548
549 * Decls: Ltcalc Decls. Bison and C declarations for ltcalc.
550 * Rules: Ltcalc Rules. Grammar rules for ltcalc, with explanations.
551 * Lexer: Ltcalc Lexer. The lexical analyzer.
552
553 \1f
554 File: bison.info, Node: Ltcalc Decls, Next: Ltcalc Rules, Up: Location Tracking Calc
555
556 Declarations for `ltcalc'
557 -------------------------
558
559 The C and Bison declarations for the location tracking calculator are
560 the same as the declarations for the infix notation calculator.
561
562 /* Location tracking calculator. */
563
564 %{
565 #define YYSTYPE int
566 #include <math.h>
567 %}
568
569 /* Bison declarations. */
570 %token NUM
571
572 %left '-' '+'
573 %left '*' '/'
574 %left NEG
575 %right '^'
576
577 %% /* Grammar follows */
578
579 Note there are no declarations specific to locations. Defining a data
580 type for storing locations is not needed: we will use the type provided
581 by default (*note Data Types of Locations: Location Type.), which is a
582 four member structure with the following integer fields: `first_line',
583 `first_column', `last_line' and `last_column'.
584
585 \1f
586 File: bison.info, Node: Ltcalc Rules, Next: Ltcalc Lexer, Prev: Ltcalc Decls, Up: Location Tracking Calc
587
588 Grammar Rules for `ltcalc'
589 --------------------------
590
591 Whether handling locations or not has no effect on the syntax of your
592 language. Therefore, grammar rules for this example will be very close
593 to those of the previous example: we will only modify them to benefit
594 from the new information.
595
596 Here, we will use locations to report divisions by zero, and locate
597 the wrong expressions or subexpressions.
598
599 input : /* empty */
600 | input line
601 ;
602
603 line : '\n'
604 | exp '\n' { printf ("%d\n", $1); }
605 ;
606
607 exp : NUM { $$ = $1; }
608 | exp '+' exp { $$ = $1 + $3; }
609 | exp '-' exp { $$ = $1 - $3; }
610 | exp '*' exp { $$ = $1 * $3; }
611 | exp '/' exp
612 {
613 if ($3)
614 $$ = $1 / $3;
615 else
616 {
617 $$ = 1;
618 fprintf (stderr, "%d.%d-%d.%d: division by zero",
619 @3.first_line, @3.first_column,
620 @3.last_line, @3.last_column);
621 }
622 }
623 | '-' exp %preg NEG { $$ = -$2; }
624 | exp '^' exp { $$ = pow ($1, $3); }
625 | '(' exp ')' { $$ = $2; }
626
627 This code shows how to reach locations inside of semantic actions, by
628 using the pseudo-variables `@N' for rule components, and the
629 pseudo-variable `@$' for groupings.
630
631 We don't need to assign a value to `@$': the output parser does it
632 automatically. By default, before executing the C code of each action,
633 `@$' is set to range from the beginning of `@1' to the end of `@N', for
634 a rule with N components. This behavior can be redefined (*note
635 Default Action for Locations: Location Default Action.), and for very
636 specific rules, `@$' can be computed by hand.
637
638 \1f
639 File: bison.info, Node: Ltcalc Lexer, Prev: Ltcalc Rules, Up: Location Tracking Calc
640
641 The `ltcalc' Lexical Analyzer.
642 ------------------------------
643
644 Until now, we relied on Bison's defaults to enable location
645 tracking. The next step is to rewrite the lexical analyser, and make it
646 able to feed the parser with the token locations, as it already does for
647 semantic values.
648
649 To this end, we must take into account every single character of the
650 input text, to avoid the computed locations of being fuzzy or wrong:
651
652 int
653 yylex (void)
654 {
655 int c;
656
657 /* skip white space */
658 while ((c = getchar ()) == ' ' || c == '\t')
659 ++yylloc.last_column;
660
661 /* step */
662 yylloc.first_line = yylloc.last_line;
663 yylloc.first_column = yylloc.last_column;
664
665 /* process numbers */
666 if (isdigit (c))
667 {
668 yylval = c - '0';
669 ++yylloc.last_column;
670 while (isdigit (c = getchar ()))
671 {
672 ++yylloc.last_column;
673 yylval = yylval * 10 + c - '0';
674 }
675 ungetc (c, stdin);
676 return NUM;
677 }
678
679 /* return end-of-file */
680 if (c == EOF)
681 return 0;
682
683 /* return single chars and update location */
684 if (c == '\n')
685 {
686 ++yylloc.last_line;
687 yylloc.last_column = 0;
688 }
689 else
690 ++yylloc.last_column;
691 return c;
692 }
693
694 Basically, the lexical analyzer performs the same processing as
695 before: it skips blanks and tabs, and reads numbers or single-character
696 tokens. In addition, it updates `yylloc', the global variable (of type
697 `YYLTYPE') containing the token's location.
698
699 Now, each time this function returns a token, the parser has its
700 number as well as its semantic value, and its location in the text. The
701 last needed change is to initialize `yylloc', for example in the
702 controlling function:
703
704 int
705 main (void)
706 {
707 yylloc.first_line = yylloc.last_line = 1;
708 yylloc.first_column = yylloc.last_column = 0;
709 return yyparse ();
710 }
711
712 Remember that computing locations is not a matter of syntax. Every
713 character must be associated to a location update, whether it is in
714 valid input, in comments, in literal strings, and so on.
715
716 \1f
717 File: bison.info, Node: Multi-function Calc, Next: Exercises, Prev: Location Tracking Calc, Up: Examples
718
719 Multi-Function Calculator: `mfcalc'
720 ===================================
721
722 Now that the basics of Bison have been discussed, it is time to move
723 on to a more advanced problem. The above calculators provided only five
724 functions, `+', `-', `*', `/' and `^'. It would be nice to have a
725 calculator that provides other mathematical functions such as `sin',
726 `cos', etc.
727
728 It is easy to add new operators to the infix calculator as long as
729 they are only single-character literals. The lexical analyzer `yylex'
730 passes back all nonnumber characters as tokens, so new grammar rules
731 suffice for adding a new operator. But we want something more
732 flexible: built-in functions whose syntax has this form:
733
734 FUNCTION_NAME (ARGUMENT)
735
736 At the same time, we will add memory to the calculator, by allowing you
737 to create named variables, store values in them, and use them later.
738 Here is a sample session with the multi-function calculator:
739
740 $ mfcalc
741 pi = 3.141592653589
742 3.1415926536
743 sin(pi)
744 0.0000000000
745 alpha = beta1 = 2.3
746 2.3000000000
747 alpha
748 2.3000000000
749 ln(alpha)
750 0.8329091229
751 exp(ln(beta1))
752 2.3000000000
753 $
754
755 Note that multiple assignment and nested function calls are
756 permitted.
757
758 * Menu:
759
760 * Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
761 * Rules: Mfcalc Rules. Grammar rules for the calculator.
762 * Symtab: Mfcalc Symtab. Symbol table management subroutines.
763
764 \1f
765 File: bison.info, Node: Mfcalc Decl, Next: Mfcalc Rules, Up: Multi-function Calc
766
767 Declarations for `mfcalc'
768 -------------------------
769
770 Here are the C and Bison declarations for the multi-function
771 calculator.
772
773 %{
774 #include <math.h> /* For math functions, cos(), sin(), etc. */
775 #include "calc.h" /* Contains definition of `symrec' */
776 %}
777 %union {
778 double val; /* For returning numbers. */
779 symrec *tptr; /* For returning symbol-table pointers */
780 }
781
782 %token <val> NUM /* Simple double precision number */
783 %token <tptr> VAR FNCT /* Variable and Function */
784 %type <val> exp
785
786 %right '='
787 %left '-' '+'
788 %left '*' '/'
789 %left NEG /* Negation--unary minus */
790 %right '^' /* Exponentiation */
791
792 /* Grammar follows */
793
794 %%
795
796 The above grammar introduces only two new features of the Bison
797 language. These features allow semantic values to have various data
798 types (*note More Than One Value Type: Multiple Types.).
799
800 The `%union' declaration specifies the entire list of possible types;
801 this is instead of defining `YYSTYPE'. The allowable types are now
802 double-floats (for `exp' and `NUM') and pointers to entries in the
803 symbol table. *Note The Collection of Value Types: Union Decl.
804
805 Since values can now have various types, it is necessary to
806 associate a type with each grammar symbol whose semantic value is used.
807 These symbols are `NUM', `VAR', `FNCT', and `exp'. Their declarations
808 are augmented with information about their data type (placed between
809 angle brackets).
810
811 The Bison construct `%type' is used for declaring nonterminal
812 symbols, just as `%token' is used for declaring token types. We have
813 not used `%type' before because nonterminal symbols are normally
814 declared implicitly by the rules that define them. But `exp' must be
815 declared explicitly so we can specify its value type. *Note
816 Nonterminal Symbols: Type Decl.
817
818 \1f
819 File: bison.info, Node: Mfcalc Rules, Next: Mfcalc Symtab, Prev: Mfcalc Decl, Up: Multi-function Calc
820
821 Grammar Rules for `mfcalc'
822 --------------------------
823
824 Here are the grammar rules for the multi-function calculator. Most
825 of them are copied directly from `calc'; three rules, those which
826 mention `VAR' or `FNCT', are new.
827
828 input: /* empty */
829 | input line
830 ;
831
832 line:
833 '\n'
834 | exp '\n' { printf ("\t%.10g\n", $1); }
835 | error '\n' { yyerrok; }
836 ;
837
838 exp: NUM { $$ = $1; }
839 | VAR { $$ = $1->value.var; }
840 | VAR '=' exp { $$ = $3; $1->value.var = $3; }
841 | FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
842 | exp '+' exp { $$ = $1 + $3; }
843 | exp '-' exp { $$ = $1 - $3; }
844 | exp '*' exp { $$ = $1 * $3; }
845 | exp '/' exp { $$ = $1 / $3; }
846 | '-' exp %prec NEG { $$ = -$2; }
847 | exp '^' exp { $$ = pow ($1, $3); }
848 | '(' exp ')' { $$ = $2; }
849 ;
850 /* End of grammar */
851 %%
852
853 \1f
854 File: bison.info, Node: Mfcalc Symtab, Prev: Mfcalc Rules, Up: Multi-function Calc
855
856 The `mfcalc' Symbol Table
857 -------------------------
858
859 The multi-function calculator requires a symbol table to keep track
860 of the names and meanings of variables and functions. This doesn't
861 affect the grammar rules (except for the actions) or the Bison
862 declarations, but it requires some additional C functions for support.
863
864 The symbol table itself consists of a linked list of records. Its
865 definition, which is kept in the header `calc.h', is as follows. It
866 provides for either functions or variables to be placed in the table.
867
868 /* Fonctions type. */
869 typedef double (*func_t) (double);
870
871 /* Data type for links in the chain of symbols. */
872 struct symrec
873 {
874 char *name; /* name of symbol */
875 int type; /* type of symbol: either VAR or FNCT */
876 union
877 {
878 double var; /* value of a VAR */
879 func_t fnctptr; /* value of a FNCT */
880 } value;
881 struct symrec *next; /* link field */
882 };
883
884 typedef struct symrec symrec;
885
886 /* The symbol table: a chain of `struct symrec'. */
887 extern symrec *sym_table;
888
889 symrec *putsym (const char *, func_t);
890 symrec *getsym (const char *);
891
892 The new version of `main' includes a call to `init_table', a
893 function that initializes the symbol table. Here it is, and
894 `init_table' as well:
895
896 #include <stdio.h>
897
898 int
899 main (void)
900 {
901 init_table ();
902 return yyparse ();
903 }
904
905 void
906 yyerror (const char *s) /* Called by yyparse on error */
907 {
908 printf ("%s\n", s);
909 }
910
911 struct init
912 {
913 char *fname;
914 double (*fnct)(double);
915 };
916
917 struct init arith_fncts[] =
918 {
919 "sin", sin,
920 "cos", cos,
921 "atan", atan,
922 "ln", log,
923 "exp", exp,
924 "sqrt", sqrt,
925 0, 0
926 };
927
928 /* The symbol table: a chain of `struct symrec'. */
929 symrec *sym_table = (symrec *) 0;
930
931 /* Put arithmetic functions in table. */
932 void
933 init_table (void)
934 {
935 int i;
936 symrec *ptr;
937 for (i = 0; arith_fncts[i].fname != 0; i++)
938 {
939 ptr = putsym (arith_fncts[i].fname, FNCT);
940 ptr->value.fnctptr = arith_fncts[i].fnct;
941 }
942 }
943
944 By simply editing the initialization list and adding the necessary
945 include files, you can add additional functions to the calculator.
946
947 Two important functions allow look-up and installation of symbols in
948 the symbol table. The function `putsym' is passed a name and the type
949 (`VAR' or `FNCT') of the object to be installed. The object is linked
950 to the front of the list, and a pointer to the object is returned. The
951 function `getsym' is passed the name of the symbol to look up. If
952 found, a pointer to that symbol is returned; otherwise zero is returned.
953
954 symrec *
955 putsym (char *sym_name, int sym_type)
956 {
957 symrec *ptr;
958 ptr = (symrec *) malloc (sizeof (symrec));
959 ptr->name = (char *) malloc (strlen (sym_name) + 1);
960 strcpy (ptr->name,sym_name);
961 ptr->type = sym_type;
962 ptr->value.var = 0; /* set value to 0 even if fctn. */
963 ptr->next = (struct symrec *)sym_table;
964 sym_table = ptr;
965 return ptr;
966 }
967
968 symrec *
969 getsym (const char *sym_name)
970 {
971 symrec *ptr;
972 for (ptr = sym_table; ptr != (symrec *) 0;
973 ptr = (symrec *)ptr->next)
974 if (strcmp (ptr->name,sym_name) == 0)
975 return ptr;
976 return 0;
977 }
978
979 The function `yylex' must now recognize variables, numeric values,
980 and the single-character arithmetic operators. Strings of alphanumeric
981 characters with a leading non-digit are recognized as either variables
982 or functions depending on what the symbol table says about them.
983
984 The string is passed to `getsym' for look up in the symbol table. If
985 the name appears in the table, a pointer to its location and its type
986 (`VAR' or `FNCT') is returned to `yyparse'. If it is not already in
987 the table, then it is installed as a `VAR' using `putsym'. Again, a
988 pointer and its type (which must be `VAR') is returned to `yyparse'.
989
990 No change is needed in the handling of numeric values and arithmetic
991 operators in `yylex'.
992
993 #include <ctype.h>
994
995 int
996 yylex (void)
997 {
998 int c;
999
1000 /* Ignore whitespace, get first nonwhite character. */
1001 while ((c = getchar ()) == ' ' || c == '\t');
1002
1003 if (c == EOF)
1004 return 0;
1005
1006 /* Char starts a number => parse the number. */
1007 if (c == '.' || isdigit (c))
1008 {
1009 ungetc (c, stdin);
1010 scanf ("%lf", &yylval.val);
1011 return NUM;
1012 }
1013
1014 /* Char starts an identifier => read the name. */
1015 if (isalpha (c))
1016 {
1017 symrec *s;
1018 static char *symbuf = 0;
1019 static int length = 0;
1020 int i;
1021
1022 /* Initially make the buffer long enough
1023 for a 40-character symbol name. */
1024 if (length == 0)
1025 length = 40, symbuf = (char *)malloc (length + 1);
1026
1027 i = 0;
1028 do
1029 {
1030 /* If buffer is full, make it bigger. */
1031 if (i == length)
1032 {
1033 length *= 2;
1034 symbuf = (char *)realloc (symbuf, length + 1);
1035 }
1036 /* Add this character to the buffer. */
1037 symbuf[i++] = c;
1038 /* Get another character. */
1039 c = getchar ();
1040 }
1041 while (c != EOF && isalnum (c));
1042
1043 ungetc (c, stdin);
1044 symbuf[i] = '\0';
1045
1046 s = getsym (symbuf);
1047 if (s == 0)
1048 s = putsym (symbuf, VAR);
1049 yylval.tptr = s;
1050 return s->type;
1051 }
1052
1053 /* Any other character is a token by itself. */
1054 return c;
1055 }
1056
1057 This program is both powerful and flexible. You may easily add new
1058 functions, and it is a simple job to modify this code to install
1059 predefined variables such as `pi' or `e' as well.
1060
1061 \1f
1062 File: bison.info, Node: Exercises, Prev: Multi-function Calc, Up: Examples
1063
1064 Exercises
1065 =========
1066
1067 1. Add some new functions from `math.h' to the initialization list.
1068
1069 2. Add another array that contains constants and their values. Then
1070 modify `init_table' to add these constants to the symbol table.
1071 It will be easiest to give the constants type `VAR'.
1072
1073 3. Make the program report an error if the user refers to an
1074 uninitialized variable in any way except to store a value in it.
1075
1076 \1f
1077 File: bison.info, Node: Grammar File, Next: Interface, Prev: Examples, Up: Top
1078
1079 Bison Grammar Files
1080 *******************
1081
1082 Bison takes as input a context-free grammar specification and
1083 produces a C-language function that recognizes correct instances of the
1084 grammar.
1085
1086 The Bison grammar input file conventionally has a name ending in
1087 `.y'. *Note Invoking Bison: Invocation.
1088
1089 * Menu:
1090
1091 * Grammar Outline:: Overall layout of the grammar file.
1092 * Symbols:: Terminal and nonterminal symbols.
1093 * Rules:: How to write grammar rules.
1094 * Recursion:: Writing recursive rules.
1095 * Semantics:: Semantic values and actions.
1096 * Locations:: Locations and actions.
1097 * Declarations:: All kinds of Bison declarations are described here.
1098 * Multiple Parsers:: Putting more than one Bison parser in one program.
1099
1100 \1f
1101 File: bison.info, Node: Grammar Outline, Next: Symbols, Up: Grammar File
1102
1103 Outline of a Bison Grammar
1104 ==========================
1105
1106 A Bison grammar file has four main sections, shown here with the
1107 appropriate delimiters:
1108
1109 %{
1110 C DECLARATIONS
1111 %}
1112
1113 BISON DECLARATIONS
1114
1115 %%
1116 GRAMMAR RULES
1117 %%
1118
1119 ADDITIONAL C CODE
1120
1121 Comments enclosed in `/* ... */' may appear in any of the sections.
1122
1123 * Menu:
1124
1125 * C Declarations:: Syntax and usage of the C declarations section.
1126 * Bison Declarations:: Syntax and usage of the Bison declarations section.
1127 * Grammar Rules:: Syntax and usage of the grammar rules section.
1128 * C Code:: Syntax and usage of the additional C code section.
1129
1130 \1f
1131 File: bison.info, Node: C Declarations, Next: Bison Declarations, Up: Grammar Outline
1132
1133 The C Declarations Section
1134 --------------------------
1135
1136 The C DECLARATIONS section contains macro definitions and
1137 declarations of functions and variables that are used in the actions in
1138 the grammar rules. These are copied to the beginning of the parser
1139 file so that they precede the definition of `yyparse'. You can use
1140 `#include' to get the declarations from a header file. If you don't
1141 need any C declarations, you may omit the `%{' and `%}' delimiters that
1142 bracket this section.
1143
1144 \1f
1145 File: bison.info, Node: Bison Declarations, Next: Grammar Rules, Prev: C Declarations, Up: Grammar Outline
1146
1147 The Bison Declarations Section
1148 ------------------------------
1149
1150 The BISON DECLARATIONS section contains declarations that define
1151 terminal and nonterminal symbols, specify precedence, and so on. In
1152 some simple grammars you may not need any declarations. *Note Bison
1153 Declarations: Declarations.
1154
1155 \1f
1156 File: bison.info, Node: Grammar Rules, Next: C Code, Prev: Bison Declarations, Up: Grammar Outline
1157
1158 The Grammar Rules Section
1159 -------------------------
1160
1161 The "grammar rules" section contains one or more Bison grammar
1162 rules, and nothing else. *Note Syntax of Grammar Rules: Rules.
1163
1164 There must always be at least one grammar rule, and the first `%%'
1165 (which precedes the grammar rules) may never be omitted even if it is
1166 the first thing in the file.
1167
1168 \1f
1169 File: bison.info, Node: C Code, Prev: Grammar Rules, Up: Grammar Outline
1170
1171 The Additional C Code Section
1172 -----------------------------
1173
1174 The ADDITIONAL C CODE section is copied verbatim to the end of the
1175 parser file, just as the C DECLARATIONS section is copied to the
1176 beginning. This is the most convenient place to put anything that you
1177 want to have in the parser file but which need not come before the
1178 definition of `yyparse'. For example, the definitions of `yylex' and
1179 `yyerror' often go here. *Note Parser C-Language Interface: Interface.
1180
1181 If the last section is empty, you may omit the `%%' that separates it
1182 from the grammar rules.
1183
1184 The Bison parser itself contains many static variables whose names
1185 start with `yy' and many macros whose names start with `YY'. It is a
1186 good idea to avoid using any such names (except those documented in this
1187 manual) in the additional C code section of the grammar file.
1188
1189 \1f
1190 File: bison.info, Node: Symbols, Next: Rules, Prev: Grammar Outline, Up: Grammar File
1191
1192 Symbols, Terminal and Nonterminal
1193 =================================
1194
1195 "Symbols" in Bison grammars represent the grammatical classifications
1196 of the language.
1197
1198 A "terminal symbol" (also known as a "token type") represents a
1199 class of syntactically equivalent tokens. You use the symbol in grammar
1200 rules to mean that a token in that class is allowed. The symbol is
1201 represented in the Bison parser by a numeric code, and the `yylex'
1202 function returns a token type code to indicate what kind of token has
1203 been read. You don't need to know what the code value is; you can use
1204 the symbol to stand for it.
1205
1206 A "nonterminal symbol" stands for a class of syntactically equivalent
1207 groupings. The symbol name is used in writing grammar rules. By
1208 convention, it should be all lower case.
1209
1210 Symbol names can contain letters, digits (not at the beginning),
1211 underscores and periods. Periods make sense only in nonterminals.
1212
1213 There are three ways of writing terminal symbols in the grammar:
1214
1215 * A "named token type" is written with an identifier, like an
1216 identifier in C. By convention, it should be all upper case. Each
1217 such name must be defined with a Bison declaration such as
1218 `%token'. *Note Token Type Names: Token Decl.
1219
1220 * A "character token type" (or "literal character token") is written
1221 in the grammar using the same syntax used in C for character
1222 constants; for example, `'+'' is a character token type. A
1223 character token type doesn't need to be declared unless you need to
1224 specify its semantic value data type (*note Data Types of Semantic
1225 Values: Value Type.), associativity, or precedence (*note Operator
1226 Precedence: Precedence.).
1227
1228 By convention, a character token type is used only to represent a
1229 token that consists of that particular character. Thus, the token
1230 type `'+'' is used to represent the character `+' as a token.
1231 Nothing enforces this convention, but if you depart from it, your
1232 program will confuse other readers.
1233
1234 All the usual escape sequences used in character literals in C can
1235 be used in Bison as well, but you must not use the null character
1236 as a character literal because its ASCII code, zero, is the code
1237 `yylex' returns for end-of-input (*note Calling Convention for
1238 `yylex': Calling Convention.).
1239
1240 * A "literal string token" is written like a C string constant; for
1241 example, `"<="' is a literal string token. A literal string token
1242 doesn't need to be declared unless you need to specify its semantic
1243 value data type (*note Value Type::), associativity, or precedence
1244 (*note Precedence::).
1245
1246 You can associate the literal string token with a symbolic name as
1247 an alias, using the `%token' declaration (*note Token
1248 Declarations: Token Decl.). If you don't do that, the lexical
1249 analyzer has to retrieve the token number for the literal string
1250 token from the `yytname' table (*note Calling Convention::).
1251
1252 *WARNING*: literal string tokens do not work in Yacc.
1253
1254 By convention, a literal string token is used only to represent a
1255 token that consists of that particular string. Thus, you should
1256 use the token type `"<="' to represent the string `<=' as a token.
1257 Bison does not enforce this convention, but if you depart from
1258 it, people who read your program will be confused.
1259
1260 All the escape sequences used in string literals in C can be used
1261 in Bison as well. A literal string token must contain two or more
1262 characters; for a token containing just one character, use a
1263 character token (see above).
1264
1265 How you choose to write a terminal symbol has no effect on its
1266 grammatical meaning. That depends only on where it appears in rules and
1267 on when the parser function returns that symbol.
1268
1269 The value returned by `yylex' is always one of the terminal symbols
1270 (or 0 for end-of-input). Whichever way you write the token type in the
1271 grammar rules, you write it the same way in the definition of `yylex'.
1272 The numeric code for a character token type is simply the ASCII code for
1273 the character, so `yylex' can use the identical character constant to
1274 generate the requisite code. Each named token type becomes a C macro in
1275 the parser file, so `yylex' can use the name to stand for the code.
1276 (This is why periods don't make sense in terminal symbols.) *Note
1277 Calling Convention for `yylex': Calling Convention.
1278
1279 If `yylex' is defined in a separate file, you need to arrange for the
1280 token-type macro definitions to be available there. Use the `-d'
1281 option when you run Bison, so that it will write these macro definitions
1282 into a separate header file `NAME.tab.h' which you can include in the
1283 other source files that need it. *Note Invoking Bison: Invocation.
1284
1285 The symbol `error' is a terminal symbol reserved for error recovery
1286 (*note Error Recovery::); you shouldn't use it for any other purpose.
1287 In particular, `yylex' should never return this value.
1288
1289 \1f
1290 File: bison.info, Node: Rules, Next: Recursion, Prev: Symbols, Up: Grammar File
1291
1292 Syntax of Grammar Rules
1293 =======================
1294
1295 A Bison grammar rule has the following general form:
1296
1297 RESULT: COMPONENTS...
1298 ;
1299
1300 where RESULT is the nonterminal symbol that this rule describes, and
1301 COMPONENTS are various terminal and nonterminal symbols that are put
1302 together by this rule (*note Symbols::).
1303
1304 For example,
1305
1306 exp: exp '+' exp
1307 ;
1308
1309 says that two groupings of type `exp', with a `+' token in between, can
1310 be combined into a larger grouping of type `exp'.
1311
1312 Whitespace in rules is significant only to separate symbols. You
1313 can add extra whitespace as you wish.
1314
1315 Scattered among the components can be ACTIONS that determine the
1316 semantics of the rule. An action looks like this:
1317
1318 {C STATEMENTS}
1319
1320 Usually there is only one action and it follows the components. *Note
1321 Actions::.
1322
1323 Multiple rules for the same RESULT can be written separately or can
1324 be joined with the vertical-bar character `|' as follows:
1325
1326 RESULT: RULE1-COMPONENTS...
1327 | RULE2-COMPONENTS...
1328 ...
1329 ;
1330
1331 They are still considered distinct rules even when joined in this way.
1332
1333 If COMPONENTS in a rule is empty, it means that RESULT can match the
1334 empty string. For example, here is how to define a comma-separated
1335 sequence of zero or more `exp' groupings:
1336
1337 expseq: /* empty */
1338 | expseq1
1339 ;
1340
1341 expseq1: exp
1342 | expseq1 ',' exp
1343 ;
1344
1345 It is customary to write a comment `/* empty */' in each rule with no
1346 components.
1347
1348 \1f
1349 File: bison.info, Node: Recursion, Next: Semantics, Prev: Rules, Up: Grammar File
1350
1351 Recursive Rules
1352 ===============
1353
1354 A rule is called "recursive" when its RESULT nonterminal appears
1355 also on its right hand side. Nearly all Bison grammars need to use
1356 recursion, because that is the only way to define a sequence of any
1357 number of a particular thing. Consider this recursive definition of a
1358 comma-separated sequence of one or more expressions:
1359
1360 expseq1: exp
1361 | expseq1 ',' exp
1362 ;
1363
1364 Since the recursive use of `expseq1' is the leftmost symbol in the
1365 right hand side, we call this "left recursion". By contrast, here the
1366 same construct is defined using "right recursion":
1367
1368 expseq1: exp
1369 | exp ',' expseq1
1370 ;
1371
1372 Any kind of sequence can be defined using either left recursion or
1373 right recursion, but you should always use left recursion, because it
1374 can parse a sequence of any number of elements with bounded stack
1375 space. Right recursion uses up space on the Bison stack in proportion
1376 to the number of elements in the sequence, because all the elements
1377 must be shifted onto the stack before the rule can be applied even
1378 once. *Note The Bison Parser Algorithm: Algorithm, for further
1379 explanation of this.
1380
1381 "Indirect" or "mutual" recursion occurs when the result of the rule
1382 does not appear directly on its right hand side, but does appear in
1383 rules for other nonterminals which do appear on its right hand side.
1384
1385 For example:
1386
1387 expr: primary
1388 | primary '+' primary
1389 ;
1390
1391 primary: constant
1392 | '(' expr ')'
1393 ;
1394
1395 defines two mutually-recursive nonterminals, since each refers to the
1396 other.
1397
1398 \1f
1399 File: bison.info, Node: Semantics, Next: Locations, Prev: Recursion, Up: Grammar File
1400
1401 Defining Language Semantics
1402 ===========================
1403
1404 The grammar rules for a language determine only the syntax. The
1405 semantics are determined by the semantic values associated with various
1406 tokens and groupings, and by the actions taken when various groupings
1407 are recognized.
1408
1409 For example, the calculator calculates properly because the value
1410 associated with each expression is the proper number; it adds properly
1411 because the action for the grouping `X + Y' is to add the numbers
1412 associated with X and Y.
1413
1414 * Menu:
1415
1416 * Value Type:: Specifying one data type for all semantic values.
1417 * Multiple Types:: Specifying several alternative data types.
1418 * Actions:: An action is the semantic definition of a grammar rule.
1419 * Action Types:: Specifying data types for actions to operate on.
1420 * Mid-Rule Actions:: Most actions go at the end of a rule.
1421 This says when, why and how to use the exceptional
1422 action in the middle of a rule.
1423