Category : C Source Code
Archive   : BISON122.ZIP
Filename : BISON122.TAR

 
Output of file : BISON122.TAR contained in archive : BISON122.ZIP
bison-1.22/ 40777 21641 1 0 5442734476 11756 5ustar friedmandaemonbison-1.22/COPYING100666 21570 13 43076 5022247405 11645 0ustar djmuser GNU GENERAL PUBLIC LICENSE
Version 2, June 1991

Copyright (C) 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Preamble

The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.

When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.

We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

The precise terms and conditions for copying, distribution and
modification follow.

GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.

b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.

c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)

These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,

b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,

c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.

9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

NO WARRANTY

11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

END OF TERMS AND CONDITIONS

Appendix: How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.


Copyright (C) 19yy

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:

Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.

, 1 April 1989
Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
bison-1.22/ChangeLog100666 21641 1 111331 5442733441 13654 0ustar friedmandaemonMon Sep 6 15:32:32 1993 Noah Friedman ([email protected])

* Version 1.22 released.

* mkinstalldirs: New file.

* Makefile.in (dist): Use .gz for extension, not .z.
(DISTFILES): New variable.
(dist): Use it instead of explicit file list.
Try to link each file separately, then copy file if ln fails.
(installdirs): Use mkinstalldirs script.

Thu Jul 29 20:35:02 1993 David J. MacKenzie ([email protected])

* Makefile.in (config.status): Run config.status --recheck, not
configure, to get the right args passed.

Sat Jul 24 04:00:52 1993 Richard Stallman ([email protected])

* bison.simple (yyparse): Init yychar1 to avoid warning.

Sun Jul 4 16:05:58 1993 Richard Stallman ([email protected])

* bison.simple (yyparse): Don't set yyval when yylen is 0.

Sat Jun 26 15:54:04 1993 David J. MacKenzie ([email protected])

* getargs.c (getargs): Exit after printing the version number.
Add --help and -h options.
(usage): New function.

Fri Jun 25 15:11:25 1993 Richard Stallman ([email protected])

* getargs.c (longopts): Allow `output' as an alternative.

Wed Jun 16 17:02:37 1993 Richard Stallman ([email protected])

* bison.simple (yyparse): Conditionalize the entire call to yyoverflow,
not just two arguments in it.

Thu Jun 3 13:07:19 1993 Richard Stallman ([email protected])

* bison.simple [__hpux] (alloca): Don't specify arg types.

Fri May 7 05:53:17 1993 Noah Friedman ([email protected])

* Makefile.in (install): Depend on `uninstall' and `installdirs'.
(installdirs): New target.

Wed Apr 28 15:15:15 1993 Noah Friedman ([email protected])

* reader.c: Remove declaration of atoi.

Fri Apr 23 12:29:20 1993 Noah Friedman ([email protected])

* new.h [!__STDC__] (FREE): Check x != 0.
Make expr to call `free' evaluate to 0.

Tue Apr 20 01:43:44 1993 David J. MacKenzie ([email protected])

* files.c [MSDOS]: Use xmalloc, not malloc.
* allocate.c (xmalloc): Renamed from mallocate. Remove old wrapper.
* conflicts.c, symtab.c, files.c, LR0.c, new.h: Change callers.
* allocate.c (xrealloc): New function.
* new.h: Declare it.
* lex.c, reader.c: Use it.

Sun Apr 18 00:45:56 1993 Noah Friedman ([email protected])

* Version 1.21 released.

* reader.c : Don't declare `realloc'.

* Makefile.in (bison.s1): use `rm -f' since it's quieter.
(dist): make gzipped tar file.

Fri Apr 16 21:24:10 1993 Noah Friedman ([email protected])

* Makefile.in (Makefile, config.status, configure): New targets.

Thu Apr 15 15:37:28 1993 Richard Stallman ([email protected])

* main.c: Don't declare `abort'.

* files.c: Don't declare `exit'.

Thu Apr 15 02:42:38 1993 Noah Friedman ([email protected])

* configure.in: Add AC_CONST.

Wed Apr 14 00:51:17 1993 Richard Stallman ([email protected])

* Makefile.in (all): Depend on bison.s1.

Tue Apr 13 14:52:32 1993 Richard Stallman ([email protected])

* Version 1.20 released.

Wed Mar 24 21:45:47 1993 Richard Stallman ([email protected])

* output.c (output_headers): Rename yynerrs if -p.

Thu Mar 18 00:02:17 1993 Noah Friedman ([email protected])

* system.h: Don't try to include stdlib.h unless HAVE_STDLIB_H is
defined.

* configure.in: Check for stdlib.h.

Wed Mar 17 14:44:27 1993 Richard Stallman ([email protected])

* bison.simple [__hpux, not __GNUC__]: Declare alloca.
(yyparse): When printing the expected token types for an error,
Avoid negative indexes in yycheck and yytname.

Sat Mar 13 23:31:25 1993 Richard Stallman ([email protected])

* Makefile.in (files.o, .c.o): Put CPPFLAGS and CFLAGS last.

Mon Mar 1 17:49:08 1993 Richard Stallman ([email protected])

* bison.simple: Test __sgi like __sparc.

Wed Feb 17 00:04:13 1993 Richard Stallman ([email protected])

* conflicts.c (resolve_sr_conflict): Add extra parens in alloca call.

* bison.simple [__GNUC__] (yyparse): Declare with prototype.

Fri Jan 15 13:15:17 1993 Richard Stallman ([email protected])

* conflicts.c (print_reduction): Near end, increment fp2 when mask
recycles.

Wed Jan 13 04:15:03 1993 Richard Stallman ([email protected])

* Makefile.in (bison.s1): New target. Modifies bison.simple.
(install): Install bison.s1, without changing it.
(clean): Delete bison.s1.

Mon Jan 4 20:35:58 1993 Richard Stallman ([email protected])

* reader.c (reader): Put Bison version in comment in output file.

Tue Dec 22 19:00:58 1992 Richard Stallman ([email protected])

* files.c (openfiles): Use .output, not .out, for outfile,
regardless of spec_name_prefix.

Tue Dec 15 19:22:11 1992 Richard Stallman ([email protected])

* output.c (output_gram): Include yyrhs in the same #if as yyprhs.

Tue Dec 15 18:29:16 1992 Noah Friedman ([email protected])

* output.c (output): output directives checking for __cplusplus as
well as __STDC__ to determine when to define "const" as an empty
token. (Patch from Wolfgang Glunz )

Tue Dec 8 21:51:23 1992 David J. MacKenzie ([email protected])

* system.h, conflicts.c: Replace USG with HAVE_STRING_H and
HAVE_MEMORY_H.

Sat Nov 21 00:37:16 1992 David J. MacKenzie ([email protected])

* Makefile.in: Set and use $(MAKEINFO).

Fri Nov 20 20:45:57 1992 Richard Stallman ([email protected])

* files.c (done) [MSDOS]: Delete the tmpdefsfile with the rest.

Thu Oct 8 21:55:52 1992 Richard Stallman ([email protected])

* Makefile.in (dist): Put configure.bat in the distribution.

Thu Oct 1 09:16:24 1992 David J. MacKenzie ([email protected])

* Makefile.in (install): cd to $(srcdir) before installing info files.

Wed Sep 30 17:18:39 1992 Richard Stallman ([email protected])

* Makefile.in (files.o): Supply $(DEFS), and $(CPPFLAGS).

Fri Sep 25 18:06:28 1992 Richard Stallman ([email protected])

* Version 1.19 released.

* reader.c (parse_union_decl): Fix ending of C++ comment;
don't lose the char after the newline.

* configure.bat: New file.

Thu Sep 24 16:23:15 1992 Richard Stallman ([email protected])

* conflicts.c: Check for using alloca.h as getopt.c does.

Sun Sep 6 08:01:53 1992 Karl Berry (karl@hayley)

* files.c (openfiles): open `fdefines' after we have assigned a name
to `tmpdefsfile', and only if `definesflag' is set.
(done): only create the real .tab.h file if `definesflag' is set.
* reader.c (packsymbols): don't close `fdefines' here.

Sat Sep 5 15:02:11 1992 Richard Stallman ([email protected])

* files.c (openfiles): Open fdefines as temp file, like ftable.
(done): Copy temp defines file to real one, like main output file.

Fri Aug 21 12:47:48 1992 Richard Stallman ([email protected])

* Makefile.in (dist): Don't release mergedir.awk
(install): Use sed, not awk. Don't depend on mergedir.awk.
* mergedir.awk: File effectively deleted.

Wed Jul 29 00:53:25 1992 Richard Stallman ([email protected])

* bison.simple: Test __sparc along with __sparc__.

Sat Jul 11 14:08:33 1992 Richard Stallman ([email protected])

* lex.c (skip_white_space): Count \n just once at end of c++ comment.

Fri Jun 26 00:00:30 1992 Richard Stallman ([email protected])

* bison.simple: Comment fix; #line command updated.

Wed Jun 24 15:12:42 1992 Richard Stallman ([email protected])

* Makefile.in (install): Specify full new file name for the executable.

Mon Jun 22 16:38:24 1992 Richard Stallman ([email protected])

* Makefile.in (dist): Include bison.rnh in distribution.

Sun Jun 21 22:42:13 1992 Eric Youngdale ([email protected])

Clean up rough edges in VMS port of bison, add support for remaining
command line options.

* bison.cld: Add /version, /yacc, /file_prefix, and /name_prefix
switches.

* build.com: General cleanup: add logic to automatically sense
which C compiler is present; add code to cwd to the directory
that contains bison sources; do not define XPFILE, XPFILE1
(correct defaults are applied in file.c).

* files.c: Append _tab, not .tab when using /file_prefix under VMS.

* system.h: Include string.h instead of strings.h (a la USG).

* vmsgetargs.c: Add support for all switches added to bison.cld.

Sun Jun 21 15:53:26 1992 Richard Stallman ([email protected])

* Makefile.in (install): Always specify new file name for install.
Redirect awk output to temp file and install that.

Wed May 27 22:27:50 1992 Richard Stallman ([email protected])

* bison.simple (yyparse): Make yybackup and yyerrlab1 always be used.

Fri May 22 14:58:42 1992 Richard Stallman ([email protected])

* Makefile.in (dist): Depend on bison.info
(bison.info): Delete spurious <.

Sun May 17 21:48:55 1992 Richard Stallman ([email protected])

* Makefile.in (.c.o): New rule. Use $(DEFS) directly.
(CFLAGS): Use just -g by default.
(CDEBUG): Variable deleted.

Thu May 7 00:03:37 1992 Richard Stallman ([email protected])

* reader.c (copy_guard): Fix typo skipping comment.

Mon May 4 01:23:21 1992 Richard Stallman ([email protected])

* Version 1.18.

* getargs.c (getargs): Change '0' to 0 in case for long options.

Sun Apr 19 10:17:52 1992 Richard Stallman ([email protected])

* reader.c (packsymbols): Handle -p when declaring yylval.

Sat Apr 18 18:18:48 1992 Richard Stallman ([email protected])

* output.c (output_gram): Output #endif properly at end of decl.

Mon Mar 30 01:13:41 1992 Richard Stallman ([email protected])

* Version 1.17.

* Makefile.in (clean): Don't delete configuration files or TAGS.
(distclean): New target; do delete those.

Sat Mar 28 17:18:50 1992 Richard Stallman ([email protected])

* output.c (output_gram): Conditionalize yyprhs on YYDEBUG.

* LR0.c (augment_automaton): If copying sp->shifts to insert new
shift, handle case of inserting at end.

Sat Mar 21 23:25:47 1992 Richard Stallman ([email protected])

* lex.c (skip_white_space): Handle C++ comments.
* reader.c (copy_definition, parse_union_decl, copy_guard):
(copy_action): Likewise.

Sun Mar 8 01:22:21 1992 Richard Stallman ([email protected])

* bison.simple (YYPOPSTACK): Fix typo.

Sat Feb 29 03:53:06 1992 Richard Stallman ([email protected])

* Makefile.in (install): Install bison.info* files one by one.

Fri Feb 28 19:55:30 1992 David J. MacKenzie ([email protected])

* bison.1: Document long options as starting with `--', not `+'.

Sat Feb 1 00:08:09 1992 Richard Stallman ([email protected])

* getargs.c (getargs): Accept value 0 from getopt_long.

Thu Jan 30 23:39:15 1992 Richard Stallman ([email protected])

* Makefile.in (mostlyclean): Renamed from `clean'.
(clean): Renamed from 'distclean'. Dep on mostlyclean, not realclean.
(realclean): Dep on clean.

Mon Jan 27 21:59:19 1992 Richard Stallman ([email protected])

* bison.simple: Use malloc, not xmalloc, and handle failure explicitly.

Sun Jan 26 22:40:04 1992 Richard Stallman ([email protected])

* conflicts.c (total_conflicts): Delete unused arg to fprintf.

Tue Jan 21 23:17:44 1992 Richard Stallman ([email protected])

* Version 1.16.

Mon Jan 6 16:50:11 1992 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* Makefile (distclean): Depend on clean, not realclean. Don't rm TAGS.
(realclean): rm TAGS here.

* symtab.c (free_symtab): Don't free the type names.

Sun Dec 29 22:25:40 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* machine.h: MSDOS has 32-bit ints if __GO32__.

Wed Dec 25 22:09:07 1991 David J. MacKenzie (djm at wookumz.gnu.ai.mit.edu)

* bison.simple [_AIX]: Indent `#pragma alloca', so old C compilers
don't choke on it.

Mon Dec 23 02:10:16 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* getopt.c, getopt1.c, getopt.h: Link them to standard source location.
* alloca.c: Likewise.
* Makefile.in (dist): Copy those files from current dir.

* getargs.c: Update usage message.

* LR0.c (augment_automaton): Put new shift in proper order.

Fri Dec 20 18:39:20 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* conflicts.c: Use memcpy if ANSI C library.

* closure.c (set_fderives): Delete redundant assignment to vrow.

* closure.c (print_firsts): Fix bounds and offset checking tags.

* closure.c (tags): Declare just once at start of file.

* LR0.c (allocate_itemsets): Eliminate unused var max.
(augment_automaton): Test sp is non-null.

* lalr.c (initialize_LA): Make the vectors at least 1 element long.

* reader.c (readgram): Remove separate YYSTYPE default for MSDOS.

Wed Dec 18 02:40:32 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* print.c (print_grammar): Don't print disabled rules.

Tue Dec 17 03:48:07 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* lex.c (lex): Parse hex escapes properly.
Handle \v when filling token_buffer.

* lex.c: Include new.h.
(token_buffer): Change to a pointer.
(init_lex): Allocate initial buffer.
(grow_token_buffer): New function.
(lex, parse_percent_token): Use that.

* reader.c (read_declarations): Call open_extra_files just once.
(parse_token_decl): Don't free previous typename value.
Don't increment nvars if symbol is already a nonterminal.
(parse_union_decl): Catch unmatched close-brace.
(parse_expect_decl): Null-terminate buffer.
(copy_guard): Set brace_flag for {, not for }.

* reader.c: Fix %% in calls to fatal.

* reader.c (token_buffer): Just one extern decl, at top level.
Declare as pointer.

* symtab.c (free_symtab): Free type_name fields. Free symtab itself.

Mon Nov 25 23:04:31 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* bison.simple: Handle alloca for AIX.

* Makefile.in (mandir): Compute default using manext.

Sat Nov 2 21:39:32 1991 David J. MacKenzie (djm at wookumz.gnu.ai.mit.edu)

* Update all files to GPL version 2.

Fri Sep 6 01:51:36 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* bison.simple (__yy_bcopy): Use builtin if GCC version 2.

Mon Aug 26 22:09:12 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* reader.c (parse_assoc_decl): Error if same symbol gets two precs.

Mon Aug 26 16:42:09 1991 David J. MacKenzie (djm at pogo.gnu.ai.mit.edu)

* Makefile.in, configure: Only put $< in Makefile if using VPATH,
because older makes don't understand it.

Fri Aug 23 00:05:54 1991 David J. MacKenzie (djm at apple-gunkies)

* conflicts.c [_AIX]: #pragma alloca.
* reduce.c: Don't define TRUE and FALSE if already defined.

Mon Aug 12 22:49:58 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* Makefile.in: Add deps on system.h.
(install): Add some deps.

Fri Aug 2 12:19:20 1991 David J. MacKenzie (djm at apple-gunkies)

* Makefile.in (dist): Include texinfo.tex.

* configure: Create config.status. Remove it and Makefile if
interrupted while creating them.

Thu Aug 1 23:14:01 1991 David J. MacKenzie (djm at apple-gunkies)

* configure: Check for +srcdir etc. arg and look for
Makefile.in in that directory. Set VPATH if srcdir is not `.'.
* Makefile.in (prefix): Renamed from DESTDIR.

Wed Jul 31 21:29:47 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* print.c (print_grammar): Make output prettier. Break lines.

Tue Jul 30 22:38:01 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* print.c (print_grammar): New function.
(verbose): Call it instead of printing token names here.

Mon Jul 22 16:39:54 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* vmsgetargs.c (spec_name_prefix, spec_file_prefix): Define variables.

Wed Jul 10 01:38:25 1991 David J. MacKenzie (djm at wookumz.gnu.ai.mit.edu)

* configure, Makefile.in: $(INSTALLPROG) -> $(INSTALL),
$(INSTALLTEXT) -> $(INSTALLDATA).

Tue Jul 9 00:53:58 1991 David J. MacKenzie (djm at wookumz.gnu.ai.mit.edu)

* bison.simple: Don't include malloc.h if __TURBOC__.

Sat Jul 6 15:18:12 1991 David J. MacKenzie (djm at geech.gnu.ai.mit.edu)

* Replace Makefile with configure and Makefile.in.
Update README with current compilation instructions.

Mon Jul 1 23:12:20 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* reader.c (reader): Make the output define YYBISON.

Thu Jun 20 16:52:51 1991 David J. MacKenzie (djm at geech.gnu.ai.mit.edu)

* Makefile (MANDIR, MANEXT): Install man page in
/usr/local/man/man1/bison.1 by default, instead of
/usr/man/manl/bison.l, for consistency with other GNU programs.
* Makefile: Rename BINDIR et al. to lowercase to conform to
GNU coding standards.
(install): Make man page non-executable.

Fri May 31 23:22:13 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* Makefile (bison.info): New target.
(realclean): New target.

Thu May 2 16:36:19 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* bison.simple: Use YYPRINT to print a token, if it's defined.

Mon Apr 29 12:22:55 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* lalr.c (transpose): Rename R to R_arg.
(initialize_LA): Avoid shadowing variable j.

* reader.c (packsymbols): Avoid shadowing variable i.

* files.c: Declare exit and perror.

* machine.h: Define MAXSHORT and MINSHORT for the eta-10.

Tue Apr 2 20:49:12 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* allocate.c (mallocate): Always allocate at least one byte.

Tue Mar 19 22:17:19 1991 Richard Stallman (rms at mole.gnu.ai.mit.edu)

* Makefile (dist): Put alloca.c into distribution.

Wed Mar 6 17:45:42 1991 Richard Stallman (rms at mole.ai.mit.edu)

* print.c (print_actions): Nicer output for final states.

Thu Feb 21 20:39:53 1991 Richard Stallman (rms at mole.ai.mit.edu)

* output.c (output_rule_data): Break lines in yytline based on hpos.

Thu Feb 7 12:54:36 1991 Richard Stallman (rms at mole.ai.mit.edu)

* bison.simple (yyparse): Move decl of yylsa before use.

Tue Jan 15 23:41:33 1991 Richard Stallman (rms at mole.ai.mit.edu)

* Version 1.14.

* output.c (output_rule_data): Handle NULL in tags[i].

Fri Jan 11 17:27:24 1991 Richard Stallman (rms at mole.ai.mit.edu)

* bison.simple: On MSDOS, include malloc.h.

Sat Dec 29 19:59:55 1990 David J. MacKenzie (djm at wookumz.ai.mit.edu)

* files.c: Use `mallocate' instead of `xmalloc' so no extra decl is
needed.

Wed Dec 19 18:31:21 1990 Richard Stallman (rms at mole.ai.mit.edu)

* reader.c (readgram): Alternate YYSTYPE defn for MSDOS.
* files.c [MSDOS]: Declare xmalloc.

Thu Dec 13 12:45:54 1990 Richard Stallman (rms at mole.ai.mit.edu)

* output.c (output_rule_data): Put all symbols in yytname.

* bison.simple (yyparse): Delete extra fprintf arg
when printing a result of reduction.

Mon Dec 10 13:55:15 1990 Richard Stallman (rms at mole.ai.mit.edu)

* reader.c (packsymbols): Don't declare yylval if pure_parser.

Tue Oct 30 23:38:09 1990 Richard Stallman (rms at mole.ai.mit.edu)

* Version 1.12.

* LR0.c (augment_automaton): Fix bugs adding sp2 to chain of shifts.

Tue Oct 23 17:41:49 1990 Richard Stallman (rms at mole.ai.mit.edu)

* bison.simple: Don't define alloca if already defined.

Sun Oct 21 22:10:53 1990 Richard Stallman (rms at mole.ai.mit.edu)

* getopt.c: On VMS, use string.h.

* main.c (main): Return type int.

Mon Sep 10 16:59:01 1990 Richard Stallman (rms at mole.ai.mit.edu)

* output.c (output_headers): Output macro defs for -p.

* reader.c (readgram): Handle consecutive actions.

* getargs.c (getargs): Rename -a to -p.
* files.c (openfiles): Change names used for -b.

Mon Aug 27 00:30:15 1990 Richard Stallman (rms at mole.ai.mit.edu)

* reduce.c (reduce_grammar_tables): Don't map rlhs of disabled rule.

Sun Aug 26 13:43:32 1990 Richard Stallman (rms at mole.ai.mit.edu)

* closure.c (print_firsts, print_fderives): Use BITISSET to test bits.

Thu Aug 23 22:13:40 1990 Richard Stallman (rms at mole.ai.mit.edu)

* closure.c (print_firsts): vrowsize => varsetsize.
(print_fderives): rrowsize => rulesetsize.

Fri Aug 10 15:32:11 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple (alloca): Don't define if already defined.
(__yy_bcopy): Alternate definition for C++.

Wed Jul 11 00:46:03 1990 David J. MacKenzie (djm at albert.ai.mit.edu)

* getargs.c (getargs): Mention +yacc in usage message.

Tue Jul 10 17:29:08 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (parse_token_decl, copy_action):
Set value_components_used if appropriate.
(readgram): Inhibit output of YYSTYPE definition in that case.

Sat Jun 30 13:47:57 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* output.c (output_parser): Define YYPURE if pure, and not otherwise.
Don't define YYIMPURE.
* bison.simple: Adjust conditionals accordingly.
* bison.simple (YYLEX): If locations not in use, don't pass &yylloc.

Thu Jun 28 12:32:21 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* getargs.c (longopts): Add `yacc'.

Thu Jun 28 00:40:21 1990 David J. MacKenzie (djm at apple-gunkies)

* getargs.c (getargs): Add long options.
* Makefile: Link with getopt1.o and add getopt1.c and getopt.h to
dist.

* Move version number and description back into version.c from
Makefile and getargs.c.
* Makefile (dist): Extract version number from version.c.

Tue Jun 26 13:16:35 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* output.c (output): Always call output_gram.
* bison.simple (yyparse): Print rhs and lhs symbols of reduction rule.

Thu Jun 21 00:15:40 1990 David J. MacKenzie (djm at albert.ai.mit.edu)

* main.c: New global var `program_name' to hold argv[0] for error
messages.
* allocate.c, files.c, getargs.c, reader.c: Use `program_name'
in messages instead of hardcoded "bison".

Wed Jun 20 23:38:34 1990 David J. MacKenzie (djm at albert.ai.mit.edu)

* Makefile: Specify Bison version here. Add rule to pass it to
version.c. Encode it in distribution directory and tar file names.
* version.c: Use version number from Makefile.
* getargs.c (getargs): Print additional text that used to be part of
version_string in version.c. Use -V instead of -version to print
Bison version info. Print a usage message and exit if given an
invalid option.

Tue Jun 19 01:15:18 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple: Fix a #line.

* Makefile (INSTALL): New parameter.
(install): Use that.
(CFLAGS): Move definition to top.

Sun Jun 17 17:10:21 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (parse_type_decl): Ignore semicolon.
Remove excess % from error messages.

Sat Jun 16 19:15:48 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.11.

* Makefile (install): Ensure installed files readable.

Tue Jun 12 12:50:56 EDT 1990 Jay Fenlason ([email protected])

* getargs.c: Declare spec_file_prefix

* lex.c (lex): \a is '\007' instead of '007'

* reader.c: include machine.h

* files.h: Declare extern spec_name_prefix.

Trivial patch from Thorsten Ohl ([email protected])

Thu May 31 22:00:16 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.10.

* bison.simple (YYBACKUP, YYRECOVERING): New macros.
(YYINITDEPTH): This is what used to be YYMAXDEPTH.
(YYMAXDEPTH): This is what used to be YYMAXLIMIT.
If the value is 0, use the default instead.
(yyparse): Return 2 on stack overflow.

Wed May 30 21:09:07 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple (YYERROR): Jump to new label; don't print error message.
(yyparse): Define label yyerrlab1.

Wed May 16 13:23:58 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* files.c (openfiles): Support -b.
* getargs.c (getargs): Likewise.

* reader.c (readgram): Error if too many symbols.

* lex.c (lex): Handle \a. Make error msgs more reliable.
* reader.c (read_declarations): Make error msgs more reliable.

Sun May 13 15:03:37 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.09.

* reduce.c (reduce_grammar_tables): Fix backward test.

Sat May 12 21:05:34 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Makefile (bison-dist.*): Rename targets and files to bison.*.
(bison.tar): Make tar file to unpack into subdirectory named `bison'.

Mon Apr 30 03:46:58 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reduce.c (reduce_grammar_tables): Set rlhs to -1 for useless rules.
* nullable.c (set_nullable): Ignore those rules.
* derives.c (set_derives): Likewise.

Mon Apr 23 15:16:09 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple (yyparse): Mention rule number as well as line number.

Thu Mar 29 00:00:43 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple (__yy_bcopy): New function.
(yyparse): Use that, not bcopy.

Wed Mar 28 15:23:51 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* print.c (print_actions): Don't alter i and j spuriously when errp==0.

Mon Mar 12 16:22:18 1990 Jim Kingdon (kingdon at pogo.ai.mit.edu)

* bison.simple [__GNUC__]: Use builtin_alloca.

Wed Mar 7 21:11:36 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Makefile (install): Use mergedir.awk to process bison.simple
for installation.

* bison.simple (yyparse): New feature to include possible valid
tokens in parse error message.

Sat Mar 3 14:10:56 1990 Richard Stallman (rms at geech)

* Version 1.08.

Mon Feb 26 16:32:21 1990 Jim Kingdon (kingdon at pogo.ai.mit.edu)

* print.c (print_actions)
conflicts.c (print_reductions): Change "shift %d" to
"shift, and go to state %d" and "reduce %d" to "reduce using rule %d"
and "goto %d" to "go to state %d".
print.c (print_core): Change "(%d)" to "(rule %d)".

Tue Feb 20 14:22:47 EST 1990 Jay Fenlason (hack @ wookumz.ai.mit.edu)

* bison.simple: Comment out unused yyresume: label.

Fri Feb 9 16:14:34 EST 1990 Jay Fenlason (hack @ wookumz.ai.mit.edu)

* bison.simple : surround all declarations and (remaining) uses of
yyls* and yylloc with #ifdef YYLSP_NEEDED This will significantly
cut down on stack usage, and gets rid of unused-variable msgs from
GCC.

Wed Jan 31 13:06:08 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* files.c (done) [VMS]: Don't delete files that weren't used.
[VMS]: Let user override XPFILE and XPFILE1.

Wed Jan 3 15:52:28 1990 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.07.

Sat Dec 16 15:50:21 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* gram.c (dummy): New function.

* reader.c (readgram): Detect error if two consec actions.

Wed Nov 15 02:06:08 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reduce.c (reduce_grammar_tables): Update rline like other tables.

* Makefile (install): Install the man page.

Sat Nov 11 03:21:58 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* output.c (output_rule_data): Write #if YYDEBUG around yyrline.

Wed Oct 18 13:07:55 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.06.

* vmsgetargs.c (getargs): Downcase specified output file name.

Fri Oct 13 17:48:14 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (readgram): Warn if there is no default to use for $$
and one is needed.

Fri Sep 29 12:51:53 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.05.

* vmsgetargs.h (getargs): Process outfile option.

Fri Sep 8 03:05:14 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Version 1.04.

* reader.c (parse_union_decl): Count newlines even in comments.

Wed Sep 6 22:03:19 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* files.c (openfiles): short_base_length was always == base_length.

Thu Aug 24 16:55:06 1989 Richard Stallman (rms at apple-gunkies.ai.mit.edu)

* Version 1.03.

* files.c (openfiles): Write output into same dir as input, by default.

Wed Aug 23 15:03:07 1989 Jay Fenlason (hack at gnu)

* Makefile: Include system.h in bison-dist.tar

Tue Aug 15 22:30:42 1989 Richard Stallman (rms at hobbes.ai.mit.edu)

* version 1.03.

* reader.c (reader): Output LTYPESTR to fdefines
only after reading the grammar.

Sun Aug 6 16:55:23 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (read_declarations): Put space before comment
to avoid bug in Green Hills C compiler.

Mon Jun 19 20:14:01 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* allocate.c (xmalloc): New function.

Fri Jun 16 23:59:40 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* build.com: Compile and link reduce.c.

Fri Jun 9 23:00:54 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reduce.c (reduce_grammar_tables): Adjust start_symbol when #s change.

Sat May 27 17:57:29 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (copy_definition, copy_guard): Don't object to \-newline
inside strings.

Mon May 22 12:30:59 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* files.c (openfiles): Alternate file names for MSDOS.
(open_extra_files): Likewise.
(done): On MSDOS, unlink temp files here, not in openfiles.

* machine.h (BITS_PER_WORD): 16 on MSDOS.
(MAXTABLE): Now defined in this file.

* system.h: New file includes system-dependent headers.
All relevant .c files include it.

Thu Apr 27 17:00:47 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* version.c: Version 1.01.

Tue Apr 18 12:46:05 1989 Randall Smith (randy at apple-gunkies.ai.mit.edu)

* conflicts.c (total_conflicts): Fixed typo in yacc style output;
mention conflicts if > 0.

Sat Apr 15 17:36:18 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (packsymbols): Start new symbols after 256.

Wed Apr 12 14:09:09 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (reader): Always assign code 256 to `error' token.
Always set `translations' to 1 so this code gets handled.
* bison.simple (YYERRCODE): Define it.

Tue Apr 11 19:26:32 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* conflicts.c: If GNU C, use builtin alloca.

* Makefile (install): Delete parser files before copying them.

Thu Mar 30 13:51:17 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* getargs.c (getargs): Turn off checking of name Bison was invoked by.

* Makefile (dist): Include ChangeLog in distrib.

Thu Mar 23 15:19:41 1989 Jay Fenlason (hack at apple-gunkies.ai.mit.edu)

* LR0.c closure.c conflicts.c derives.c files.c getargs.c lalr.c
lex.c main.c nullable.c output.c print.c reader.c reduce.c
symtab.c warshall.c: A first pass at getting gcc -Wall to shut up.
Mostly declared functions as void, etc.

* reduce.c moved 'extern int fixed_outfiles;' into print_notices()
where it belongs.

Wed Mar 1 12:33:28 1989 Randall Smith (randy at apple-gunkies.ai.mit.edu)

* types.h, symtab.h, state.h, new.h, machine.h, lex.h, gram.h,
files.h, closure.c, vmsgetargs.c, warshall.c, symtab.c, reduce.c,
reader.c, print.c, output.c, nullable.c, main.c, lex.c, lalr.c,
gram.c, getargs.c, files.c, derives.c, conflicts.c, allocate.c,
LR0.c, Makefile, bison.simple: Changed copyright notices to be in
accord with the new General Public License.
* COPYING: Made a link to the new copying file.

Wed Feb 22 06:18:20 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* new.h (FREE): Alternate definition for __STDC__ avoids error
if `free' returns void.

Tue Feb 21 15:03:34 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (read_declarations): Double a `%' in a format string.
(copy_definition, parse_start_decl, parse_token_decl): Likewise.
(parse_type_decl, parse_union_decl, copy_guard, readgram, get_type).
(copy_action): change a `fatal' to `fatals'.

* lalr.c (map_goto): Initial high-end of binary search was off by 1.

Sat Feb 18 08:49:57 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple [sparc]: Include alloca.h.

Wed Feb 15 06:24:36 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (packsymbols): Write decl of yylval into .tab.h file.

Sat Jan 28 18:19:05 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple: Avoid comments on `#line' lines.

* reader.c (LTYPESTR): Rearrange to avoid whitespace after \-newline.

Mon Jan 9 18:43:08 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* conflicts.c (total_conflicts): if -y, use output syntax POSIX wants.
* reduce.c (print_notices): likewise.

* lex.c (lex): Handle \v, and \x hex escapes.

* reader.c (reader): Merge output_ltype into here.
Don't output YYLTYPE definition to .tab.h file
unless the @ construct is used.

* bison.simple: Define YYERROR, YYABORT, YYACCEPT here.
* reader.c (output_ltype): Don't output them here.

* bison.simple: YYDEBUG now should be 0 or 1.
* output.c (output): For YYDEBUG, output conditional to define it
only if not previously defined.

Mon Jan 2 11:29:55 1989 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple (yyparse) [YYPURE]: Add local yynerrs.
(yydebug): Declare global, but don't initialize, regardless of YYPURE.
(yyparse): Don't declare yydebug here.

Thu Dec 22 22:01:22 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reduce.c (print_notices): Typo in message.

Sun Dec 11 11:32:07 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* output.c (pack_table): Free only nonzero the elts of froms & tos.

Thu Dec 8 16:26:46 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* gram.c (rprecsym): New vector indicates the %prec symbol for a rule.
* reader.c (packgram): Allocate it and fill it in.
* reduce.c (inaccessable_symbols): Use it to set V1.
* reduce.c (print_results): Don't complain about useless token
if it's in V1.

Mon Dec 5 14:33:17 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* machine.h (RESETBIT, BITISSET): New macros.
(SETBIT, WORDSIZE): Change to use BITS_PER_WORD.

* reduce.c: New file, by David Bakin. Reduces the grammar.
* Makefile: Compile it, link it, put it in dist.

* main.c (main): Call reduce_grammar (in reduce.c).

Thu Nov 17 18:33:04 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* conflicts.c: Don't declare alloca if including alloca.h.

* bison.cld: Define qualifiers `nolines', `debug'.
* vmsgetargs.c (getargs): Handle them.

* output.c (output_program): Notice `nolinesflag'.

* output.c (output_parser): Simplify logic for -l and #line.
Avoid writing EOF char into output.

Wed Oct 12 18:00:03 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Implement `-l' option.
* getopt.c: Set flag `nolinesflag'.
* reader.c (copy_definition, parse_union_decl, copy_guard, copy_action)
Obey that flag; don't generate #line.
* output.c (output_parser): Discard #line's when copying the parser.

Mon Sep 12 16:33:17 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (copy_guard): Fix brace-counting for brace-surrounded guard.

Thu Sep 8 20:09:53 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* bison.simple: Correct number in #line command.
(yyparse): Call YYABORT instead of YYERROR, due to last change in
output_ltype.

Mon Sep 5 14:55:30 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* Makefile: New variable LIBS. Alternatives for USG.
* conflicts.c [USG]: Define bcopy.
* symtab.c [USG]: Include string.h instead of strings.h.

* conflicts.c [sparc]: Include alloca.h.

Tue Aug 2 08:38:38 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (parse_token_decl): Ignore commas.

Sat Jun 25 10:29:20 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* reader.c (output_ltype): Make YYERROR yacc-compatible (like YYFAIL).

Fri Jun 24 11:25:11 1988 Richard Stallman (rms at sugar-bombs.ai.mit.edu)

* getargs.c (getargs): -t sets debugflag.
Eliminate upper case duplicate options.
* output.c (output): If debugflag, output `#define YYDEBUG'.

Thu May 26 06:04:21 1988 Richard Stallman (rms at frosted-flakes.ai.mit.edu)

* allocate.c (mallocate): New name for `allocate' (which loses in VMS).
Calls changed in LR0.c, conflicts.c, symtab.c, new.h.

* getargs.c (getargs): If argv[0] is "yacc", set fixed_outfiles.

Tue May 17 12:15:30 1988 Richard Stallman (rms at frosted-flakes.ai.mit.edu)

* conflicts.c: Declare alloca.
* reader.c: Declare realloc.
* warshall.c (TC): Fix one arithmetic op that was omitted last time.

Thu May 5 14:36:03 1988 Richard Stallman (rms at frosted-flakes.ai.mit.edu)

* bison.simple: Conditionalize most refs to yylsp on YYLSP_NEEDED.
* reader.c (copy_guard, copy_action): Notice if `@' is used.
(reader): If it was, output `#define YYLSP_NEEDED'.

Mon Apr 18 04:54:32 1988 Richard Stallman (rms at rice-krispies.ai.mit.edu)

* bison.simple: New variable yynerr counts calls to yyerror.

* lex.c (lex, case '='): Update lineno when skipping a newline.

* reader.c (parse_expect_decl): ungetc the char that ends the number;
don't read any further. This handles multi-line comments right
and avoids incorrect lineno.

* reader.c: Delete duplicate decl of symval.

* warshall.c (RTC, TC): Cast ptrs to char *, not unsigned, for arith.

Local Variables:
mode: indented-text
left-margin: 8
fill-column: 76
version-control: never
End:
bison-1.22/Makefile.in100666 21641 1 13377 5442733547 14151 0ustar friedmandaemon# Makefile for bison
# Copyright (C) 1988, 1989, 1991, 1993 Bob Corbett and Free Software Foundation, Inc.
#
# This file is part of Bison, the GNU Compiler Compiler.
#
# Bison is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Bison is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bison; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

#### Start of system configuration section. ####

srcdir = @srcdir@
VPATH = @srcdir@

CC = @CC@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
MAKEINFO = makeinfo

# Things you might add to DEFS:
# -DSTDC_HEADERS If you have ANSI C headers and libraries.
# -DHAVE_STRING_H If you don't have ANSI C headers but have string.h.
# -DHAVE_MEMORY_H If you don't have ANSI C headers and have memory.h.
# -DHAVE_STRERROR If you have strerror function.
DEFS = @DEFS@

CFLAGS = -g
LDFLAGS = -g

LIBS = @LIBS@

# Some System V machines do not come with libPW. If this is true, use
# the GNU alloca.o here.
ALLOCA = @ALLOCA@

prefix = /usr/local
exec_prefix = $(prefix)

# where the installed binary goes
bindir = $(exec_prefix)/bin

# where the parsers go
datadir = $(prefix)/lib

# where the info files go
infodir = $(prefix)/info

# where manual pages go and what their extensions should be
mandir = $(prefix)/man/man$(manext)
manext = 1

#### End of system configuration section. ####

DISTFILES = COPYING ChangeLog Makefile.in configure configure.in \
REFERENCES bison.1 bison.rnh configure.bat \
bison.simple bison.hairy \
LR0.c allocate.c closure.c conflicts.c derives.c \
files.c getargs.c gram.c lalr.c lex.c main.c nullable.c \
output.c print.c reader.c reduce.c symtab.c version.c \
warshall.c files.h gram.h lex.h machine.h new.h state.h \
symtab.h system.h types.h bison.cld build.com vmsgetargs.c \
vmshlp.mar README INSTALL bison.texinfo bison.info* texinfo.tex \
getopt.c getopt.h getopt1.c alloca.c mkinstalldirs


SHELL = /bin/sh

# This rule allows us to supply the necessary -D options
# in addition to whatever the user asks for.
.c.o:
$(CC) -c $(DEFS) -I$(srcdir)/../include $(CPPFLAGS) $(CFLAGS) $<

# names of parser files
PFILE = bison.simple
PFILE1 = bison.hairy

PFILES = -DXPFILE=\"$(datadir)/$(PFILE)\" \
-DXPFILE1=\"$(datadir)/$(PFILE1)\"

OBJECTS = LR0.o allocate.o closure.o conflicts.o derives.o files.o \
getargs.o gram.o lalr.o lex.o \
main.o nullable.o output.o print.o reader.o reduce.o symtab.o \
warshall.o version.o \
getopt.o getopt1.o $(ALLOCA)

all: bison bison.info bison.s1

Makefile: Makefile.in config.status
./config.status

config.status: configure
./config.status --recheck

configure: configure.in
cd $(srcdir); autoconf

# Copy bison.simple, inserting directory name into the #line commands.
bison.s1: bison.simple
-rm -f bison.s1
sed -e "/^#line/ s|bison|$(datadir)/bison|" < $(srcdir)/$(PFILE) > bison.s1

clean:
rm -f *.o core bison bison.s1

mostlyclean: clean

distclean: clean
rm -f Makefile config.status

realclean: distclean
rm -f TAGS *.info*

# Most of these deps are in case using RCS.
install: all bison.1 $(srcdir)/$(PFILE) $(srcdir)/$(PFILE1) installdirs uninstall
$(INSTALL_PROGRAM) bison $(bindir)/bison
$(INSTALL_DATA) ./bison.s1 $(datadir)/$(PFILE)
$(INSTALL_DATA) $(srcdir)/$(PFILE1) $(datadir)/$(PFILE1)
-chmod a+r $(datadir)/$(PFILE) $(datadir)/$(PFILE1)
-$(INSTALL_DATA) $(srcdir)/bison.1 $(mandir)/bison.$(manext)
-chmod a+r $(mandir)/bison.$(manext)
cd $(srcdir); for f in bison.info*; \
do $(INSTALL_DATA) $$f $(infodir)/$$f; done

# Make sure all installation directories, e.g. $(bindir) actually exist by
# making them if necessary.
installdirs:
-sh $(srcdir)/mkinstalldirs $(bindir) $(datadir) $(libdir) $(infodir) $(mandir)

uninstall:
rm -f $(bindir)/bison
-cd $(datadir); rm -f $(PFILE) $(PFILE1)
rm -f $(mandir)/bison.$(manext) $(infodir)/bison.info*

bison: $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBS)

dist: bison.info
echo bison-`sed -e '/version_string/!d' -e 's/[^0-9.]*\([0-9.]*\).*/\1/' -e q version.c` > .fname
-rm -rf `cat .fname`
mkdir `cat .fname`
dst=`cat .fname`; for f in $(DISTFILES); do \
ln $(srcdir)/$$f $$dst/$$f || { echo copying $$f; cp -p $(srcdir)/$$f $$dst/$$f ; } \
done
tar --gzip -chf `cat .fname`.tar.gz `cat .fname`
-rm -rf `cat .fname` .fname

bison.info: bison.texinfo
$(MAKEINFO) $(srcdir)/bison.texinfo

TAGS: *.c *.h
etags *.c *.h

# This file is different to pass the parser file names to the compiler.
files.o: files.c
$(CC) -c $(PFILES) $(DEFS) $(CPPFLAGS) $(CFLAGS) \
$(srcdir)/files.c $(OUTPUT_OPTION)

LR0.o: system.h machine.h new.h gram.h state.h
closure.o: system.h machine.h new.h gram.h
conflicts.o: system.h machine.h new.h files.h gram.h state.h
derives.o: system.h new.h types.h gram.h
files.o: system.h files.h new.h gram.h
getargs.o: system.h files.h
lalr.o: system.h machine.h types.h state.h new.h gram.h
lex.o: system.h files.h symtab.h lex.h
main.o: system.h machine.h
nullable.o: system.h types.h gram.h new.h
output.o: system.h machine.h new.h files.h gram.h state.h
print.o: system.h machine.h new.h files.h gram.h state.h
reader.o: system.h files.h new.h symtab.h lex.h gram.h
reduce.o: system.h machine.h files.h new.h gram.h
symtab.o: system.h new.h symtab.h gram.h
warshall.o: system.h machine.h

# Prevent GNU make v3 from overflowing arg limit on SysV.
.NOEXPORT:
bison-1.22/configure100777 21641 1 44061 5440457066 14002 0ustar friedmandaemon#!/bin/sh
# Guess values for system-dependent variables and create Makefiles.
# Generated automatically using autoconf.
# Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Usage: configure [--srcdir=DIR] [--host=HOST] [--gas] [--nfp] [--no-create]
# [--prefix=PREFIX] [--exec-prefix=PREFIX] [--with-PACKAGE[=VALUE]]
# Ignores all args except --srcdir, --prefix, --exec-prefix, --no-create, and
# --with-PACKAGE[=VALUE] unless this script has special code to handle it.


for arg
do
# Handle --exec-prefix with a space before the argument.
if test x$next_exec_prefix = xyes; then exec_prefix=$arg; next_exec_prefix=
# Handle --host with a space before the argument.
elif test x$next_host = xyes; then next_host=
# Handle --prefix with a space before the argument.
elif test x$next_prefix = xyes; then prefix=$arg; next_prefix=
# Handle --srcdir with a space before the argument.
elif test x$next_srcdir = xyes; then srcdir=$arg; next_srcdir=
else
case $arg in
# For backward compatibility, also recognize exact -exec_prefix.
-exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* | --exec=* | --exe=* | --ex=* | --e=*)
exec_prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
-exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- | --exec | --exe | --ex | --e)
next_exec_prefix=yes ;;

-gas | --gas | --ga | --g) ;;

-host=* | --host=* | --hos=* | --ho=* | --h=*) ;;
-host | --host | --hos | --ho | --h)
next_host=yes ;;

-nfp | --nfp | --nf) ;;

-no-create | --no-create | --no-creat | --no-crea | --no-cre | --no-cr | --no-c | --no- | --no)
no_create=1 ;;

-prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
-prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
next_prefix=yes ;;

-srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=* | --s=*)
srcdir=`echo $arg | sed 's/[-a-z_]*=//'` ;;
-srcdir | --srcdir | --srcdi | --srcd | --src | --sr | --s)
next_srcdir=yes ;;

-with-* | --with-*)
package=`echo $arg|sed -e 's/-*with-//' -e 's/=.*//'`
# Reject names that aren't valid shell variable names.
if test -n "`echo $package| sed 's/[-a-zA-Z0-9_]//g'`"; then
echo "configure: $package: invalid package name" >&2; exit 1
fi
package=`echo $package| sed 's/-/_/g'`
case "$arg" in
*=*) val="`echo $arg|sed 's/[^=]*=//'`" ;;
*) val=1 ;;
esac
eval "with_$package='$val'" ;;

-v | -verbose | --verbose | --verbos | --verbo | --verb | --ver | --ve | --v)
verbose=yes ;;

*) ;;
esac
fi
done

trap 'rm -fr conftest* core; exit 1' 1 3 15

# NLS nuisances.
# These must not be set unconditionally because not all systems understand
# e.g. LANG=C (notably SCO).
if test "${LC_ALL+set}" = 'set' ; then LC_ALL=C; export LC_ALL; fi
if test "${LANG+set}" = 'set' ; then LANG=C; export LANG; fi

rm -f conftest*
compile='${CC-cc} $CFLAGS $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1'

# A filename unique to this package, relative to the directory that
# configure is in, which we can look for to find out if srcdir is correct.
unique_file=reduce.c

# Find the source files, if location was not specified.
if test -z "$srcdir"; then
srcdirdefaulted=yes
# Try the directory containing this script, then `..'.
prog=$0
confdir=`echo $prog|sed 's%/[^/][^/]*$%%'`
test "X$confdir" = "X$prog" && confdir=.
srcdir=$confdir
if test ! -r $srcdir/$unique_file; then
srcdir=..
fi
fi
if test ! -r $srcdir/$unique_file; then
if test x$srcdirdefaulted = xyes; then
echo "configure: Can not find sources in \`${confdir}' or \`..'." 1>&2
else
echo "configure: Can not find sources in \`${srcdir}'." 1>&2
fi
exit 1
fi
# Preserve a srcdir of `.' to avoid automounter screwups with pwd.
# But we can't avoid them for `..', to make subdirectories work.
case $srcdir in
.|/*|~*) ;;
*) srcdir=`cd $srcdir; pwd` ;; # Make relative path absolute.
esac

# Save the original args to write them into config.status later.
configure_args="$*"

if test -z "$CC"; then
# Extract the first word of `gcc', so it can be a program name with args.
set dummy gcc; word=$2
echo checking for $word
IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
test -z "$dir" && dir=.
if test -f $dir/$word; then
CC="gcc"
break
fi
done
IFS="$saveifs"
fi
test -z "$CC" && CC="cc"
test -n "$CC" -a -n "$verbose" && echo " setting CC to $CC"

# Find out if we are using GNU C, under whatever name.
cat > conftest.c < #ifdef __GNUC__
yes
#endif
EOF
${CC-cc} -E conftest.c > conftest.out 2>&1
if egrep yes conftest.out >/dev/null 2>&1; then
GCC=1 # For later tests.
fi
rm -f conftest*

echo checking how to run the C preprocessor
if test -z "$CPP"; then
# This must be in double quotes, not single quotes, because CPP may get
# substituted into the Makefile and ``${CC-cc}'' will simply confuse
# make. It must be expanded now.
CPP="${CC-cc} -E"
cat > conftest.c < #include
Syntax Error
EOF
err=`eval "($CPP \$DEFS conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
:
else
CPP=/lib/cpp
fi
rm -f conftest*
fi
test ".${verbose}" != "." && echo " setting CPP to $CPP"

# Make sure to not get the incompatible SysV /etc/install and
# /usr/sbin/install, which might be in PATH before a BSD-like install,
# or the SunOS /usr/etc/install directory, or the AIX /bin/install,
# or the AFS install, which mishandles nonexistent args, or
# /usr/ucb/install on SVR4, which tries to use the nonexistent group
# `staff'. On most BSDish systems install is in /usr/bin, not /usr/ucb
# anyway. Sigh.
if test "z${INSTALL}" = "z" ; then
echo checking for install
IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
test -z "$dir" && dir=.
case $dir in
/etc|/usr/sbin|/usr/etc|/usr/afsws/bin|/usr/ucb) ;;
*)
if test -f $dir/installbsd; then
INSTALL="$dir/installbsd -c" # OSF1
INSTALL_PROGRAM='$(INSTALL)'
INSTALL_DATA='$(INSTALL) -m 644'
break
fi
if test -f $dir/install; then
if grep dspmsg $dir/install >/dev/null 2>&1; then
: # AIX
else
INSTALL="$dir/install -c"
INSTALL_PROGRAM='$(INSTALL)'
INSTALL_DATA='$(INSTALL) -m 644'
break
fi
fi
;;
esac
done
IFS="$saveifs"
fi
INSTALL=${INSTALL-cp}
test -n "$verbose" && echo " setting INSTALL to $INSTALL"
INSTALL_PROGRAM=${INSTALL_PROGRAM-'$(INSTALL)'}
test -n "$verbose" && echo " setting INSTALL_PROGRAM to $INSTALL_PROGRAM"
INSTALL_DATA=${INSTALL_DATA-'$(INSTALL)'}
test -n "$verbose" && echo " setting INSTALL_DATA to $INSTALL_DATA"

echo checking for minix/config.h
cat > conftest.c < #include
EOF
err=`eval "($CPP \$DEFS conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
MINIX=1
fi
rm -f conftest*

# The Minix shell can't assign to the same variable on the same line!
if test -n "$MINIX"; then

{
test -n "$verbose" && \
echo " defining _POSIX_SOURCE"
DEFS="$DEFS -D_POSIX_SOURCE=1"
}


{
test -n "$verbose" && \
echo " defining _POSIX_1_SOURCE to be 2"
DEFS="$DEFS -D_POSIX_1_SOURCE=2"
}


{
test -n "$verbose" && \
echo " defining _MINIX"
DEFS="$DEFS -D_MINIX=1"
}

fi

echo checking for POSIXized ISC
if test -d /etc/conf/kconfig.d &&
grep _POSIX_VERSION /usr/include/sys/unistd.h >/dev/null 2>&1
then
ISC=1 # If later tests want to check for ISC.

{
test -n "$verbose" && \
echo " defining _POSIX_SOURCE"
DEFS="$DEFS -D_POSIX_SOURCE=1"
}

if test -n "$GCC"; then
CC="$CC -posix"
else
CC="$CC -Xp"
fi
fi

prog='/* Ultrix mips cc rejects this. */
typedef int charset[2]; const charset x;
/* SunOS 4.1.1 cc rejects this. */
char const *const *ccp;
char **p;
/* AIX XL C 1.02.0.0 rejects this.
It does not let you subtract one const X* pointer from another in an arm
of an if-expression whose if-part is not a constant expression */
const char *g = "string";
ccp = &g + (g ? g-g : 0);
/* HPUX 7.0 cc rejects these. */
++ccp;
p = (char**) ccp;
ccp = (char const *const *) p;
{ /* SCO 3.2v4 cc rejects this. */
char *t;
char const *s = 0 ? (char *) 0 : (char const *) 0;

*t++ = 0;
}
{ /* Someone thinks the Sun supposedly-ANSI compiler will reject this. */
int x[] = {25,17};
const int *foo = &x[0];
++foo;
}
{ /* Sun SC1.0 ANSI compiler rejects this -- but not the above. */
typedef const int *iptr;
iptr p = 0;
++p;
}
{ /* AIX XL C 1.02.0.0 rejects this saying
"k.c", line 2.27: 1506-025 (S) Operand must be a modifiable lvalue. */
struct s { int j; const int *ap[3]; };
struct s *b; b->j = 5;
}'
echo checking for working const
cat > conftest.c <
int main() { exit(0); }
int t() { $prog }
EOF
if eval $compile; then
:
else

{
test -n "$verbose" && \
echo " defining const to be empty"
DEFS="$DEFS -Dconst="
}

fi
rm -f conftest*

echo checking for ANSI C header files
cat > conftest.c < #include
#include
#include
#include
EOF
err=`eval "($CPP \$DEFS conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
# SunOS 4.x string.h does not declare mem*, contrary to ANSI.
echo '#include ' > conftest.c
eval "$CPP \$DEFS conftest.c > conftest.out 2>&1"
if egrep "memchr" conftest.out >/dev/null 2>&1; then
# SGI's /bin/cc from Irix-4.0.5 gets non-ANSI ctype macros unless using -ansi.
cat > conftest.c < #include
#define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
#define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
#define XOR(e,f) (((e) && !(f)) || (!(e) && (f)))
int main () { int i; for (i = 0; i < 256; i++)
if (XOR (islower (i), ISLOWER (i)) || toupper (i) != TOUPPER (i)) exit(2);
exit (0); }

EOF
eval $compile
if test -s conftest && (./conftest; exit) 2>/dev/null; then

{
test -n "$verbose" && \
echo " defining STDC_HEADERS"
DEFS="$DEFS -DSTDC_HEADERS=1"
}

fi
rm -f conftest*
fi
rm -f conftest*

fi
rm -f conftest*

for hdr in string.h stdlib.h memory.h
do
trhdr=HAVE_`echo $hdr | tr '[a-z]./' '[A-Z]__'`
echo checking for ${hdr}
cat > conftest.c < #include <${hdr}>
EOF
err=`eval "($CPP \$DEFS conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then

{
test -n "$verbose" && \
echo " defining ${trhdr}"
DEFS="$DEFS -D${trhdr}=1"
}

fi
rm -f conftest*
done

for func in strerror
do
trfunc=HAVE_`echo $func | tr '[a-z]' '[A-Z]'`
echo checking for ${func}
cat > conftest.c < #include
int main() { exit(0); }
int t() {
/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */
#if defined (__stub_${func}) || defined (__stub___${func})
choke me
#else
/* Override any gcc2 internal prototype to avoid an error. */
extern char ${func}(); ${func}();
#endif
}
EOF
if eval $compile; then
{
test -n "$verbose" && \
echo " defining ${trfunc}"
DEFS="$DEFS -D${trfunc}=1"
}

fi
rm -f conftest*
done

# The Ultrix 4.2 mips builtin alloca declared by alloca.h only works
# for constant arguments. Useless!
echo checking for working alloca.h
cat > conftest.c < #include
int main() { exit(0); }
int t() { char *p = alloca(2 * sizeof(int)); }
EOF
if eval $compile; then

{
test -n "$verbose" && \
echo " defining HAVE_ALLOCA_H"
DEFS="$DEFS -DHAVE_ALLOCA_H=1"
}

fi
rm -f conftest*

decl="#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#if HAVE_ALLOCA_H
#include
#else
#ifdef _AIX
#pragma alloca
#else
char *alloca ();
#endif
#endif
#endif
"
echo checking for alloca
cat > conftest.c < $decl
int main() { exit(0); }
int t() { char *p = (char *) alloca(1); }
EOF
if eval $compile; then
:
else
alloca_missing=1
cat > conftest.c <
#if defined(CRAY) && ! defined(CRAY2)
winnitude
#else
lossage
#endif

EOF
eval "$CPP \$DEFS conftest.c > conftest.out 2>&1"
if egrep "winnitude" conftest.out >/dev/null 2>&1; then
echo checking for _getb67
cat > conftest.c < #include
int main() { exit(0); }
int t() {
/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */
#if defined (__stub__getb67) || defined (__stub____getb67)
choke me
#else
/* Override any gcc2 internal prototype to avoid an error. */
extern char _getb67(); _getb67();
#endif
}
EOF
if eval $compile; then
{
test -n "$verbose" && \
echo " defining CRAY_STACKSEG_END to be _getb67"
DEFS="$DEFS -DCRAY_STACKSEG_END=_getb67"
}

else
echo checking for GETB67
cat > conftest.c < #include
int main() { exit(0); }
int t() {
/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */
#if defined (__stub_GETB67) || defined (__stub___GETB67)
choke me
#else
/* Override any gcc2 internal prototype to avoid an error. */
extern char GETB67(); GETB67();
#endif
}
EOF
if eval $compile; then
{
test -n "$verbose" && \
echo " defining CRAY_STACKSEG_END to be GETB67"
DEFS="$DEFS -DCRAY_STACKSEG_END=GETB67"
}

else
echo checking for getb67
cat > conftest.c < #include
int main() { exit(0); }
int t() {
/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */
#if defined (__stub_getb67) || defined (__stub___getb67)
choke me
#else
/* Override any gcc2 internal prototype to avoid an error. */
extern char getb67(); getb67();
#endif
}
EOF
if eval $compile; then
{
test -n "$verbose" && \
echo " defining CRAY_STACKSEG_END to be getb67"
DEFS="$DEFS -DCRAY_STACKSEG_END=getb67"
}

fi
rm -f conftest*

fi
rm -f conftest*

fi
rm -f conftest*

fi
rm -f conftest*


fi
rm -f conftest*

if test -n "$alloca_missing"; then
# The SVR3 libPW and SVR4 libucb both contain incompatible functions
# that cause trouble. Some versions do not even contain alloca or
# contain a buggy version. If you still want to use their alloca,
# use ar to extract alloca.o from them instead of compiling alloca.c.
ALLOCA=alloca.o

echo 'checking stack direction for C alloca'
echo checking whether cross-compiling
# If we cannot run a trivial program, we must be cross compiling.
cat > conftest.c < main(){exit(0);}
EOF
eval $compile
if test -s conftest && (./conftest; exit) 2>/dev/null; then
:
else
cross_compiling=1
fi
rm -f conftest*

if test -n "$cross_compiling"
then

{
test -n "$verbose" && \
echo " defining STACK_DIRECTION to be 0"
DEFS="$DEFS -DSTACK_DIRECTION=0"
}

else
cat > conftest.c < find_stack_direction ()
{
static char *addr = 0;
auto char dummy;
if (addr == 0)
{
addr = &dummy;
return find_stack_direction ();
}
else
return (&dummy > addr) ? 1 : -1;
}
main ()
{
exit (find_stack_direction() < 0);
}
EOF
eval $compile
if test -s conftest && (./conftest; exit) 2>/dev/null; then

{
test -n "$verbose" && \
echo " defining STACK_DIRECTION to be 1"
DEFS="$DEFS -DSTACK_DIRECTION=1"
}

else

{
test -n "$verbose" && \
echo " defining STACK_DIRECTION to be -1"
DEFS="$DEFS -DSTACK_DIRECTION=-1"
}

fi
fi
rm -f conftest*
fi

if test -n "$prefix"; then
test -z "$exec_prefix" && exec_prefix='${prefix}'
prsub="s%^prefix\\([ ]*\\)=\\([ ]*\\).*$%prefix\\1=\\2$prefix%"
fi
if test -n "$exec_prefix"; then
prsub="$prsub
s%^exec_prefix\\([ ]*\\)=\\([ ]*\\).*$%exec_prefix\\1=\\2$exec_prefix%"
fi
cat >conftest.def < $DEFS
EOF
escape_ampersand_and_backslash='s%[&\\]%\\&%g'
DEFS=`sed "$escape_ampersand_and_backslash" rm -f conftest.def

trap 'rm -f config.status; exit 1' 1 3 15
echo creating config.status
rm -f config.status
cat > config.status < #!/bin/sh
# Generated automatically by configure.
# Run this file to recreate the current configuration.
# This directory was configured as follows,
# on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
#
# $0 $configure_args

for arg
do
case "\$arg" in
-recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
echo running /bin/sh $0 $configure_args
exec /bin/sh $0 $configure_args ;;
*) echo "Usage: config.status --recheck" 2>&1; exit 1 ;;
esac
done

trap 'rm -f Makefile; exit 1' 1 3 15
CC='$CC'
CPP='$CPP'
INSTALL='$INSTALL'
INSTALL_PROGRAM='$INSTALL_PROGRAM'
INSTALL_DATA='$INSTALL_DATA'
ALLOCA='$ALLOCA'
LIBS='$LIBS'
srcdir='$srcdir'
DEFS='$DEFS'
prefix='$prefix'
exec_prefix='$exec_prefix'
prsub='$prsub'
EOF
cat >> config.status <<\EOF

top_srcdir=$srcdir

# Allow make-time overrides of the generated file list.
test -n "$gen_files" || gen_files="Makefile"

for file in .. $gen_files; do if [ "x$file" != "x.." ]; then
srcdir=$top_srcdir
# Remove last slash and all that follows it. Not all systems have dirname.
dir=`echo $file|sed 's%/[^/][^/]*$%%'`
if test "$dir" != "$file"; then
test "$top_srcdir" != . && srcdir=$top_srcdir/$dir
test ! -d $dir && mkdir $dir
fi
echo creating $file
rm -f $file
echo "# Generated automatically from `echo $file|sed 's|.*/||'`.in by configure." > $file
sed -e "
$prsub
s%@CC@%$CC%g
s%@CPP@%$CPP%g
s%@INSTALL@%$INSTALL%g
s%@INSTALL_PROGRAM@%$INSTALL_PROGRAM%g
s%@INSTALL_DATA@%$INSTALL_DATA%g
s%@ALLOCA@%$ALLOCA%g
s%@LIBS@%$LIBS%g
s%@srcdir@%$srcdir%g
s%@DEFS@%$DEFS%
" $top_srcdir/${file}.in >> $file
fi; done

exit 0
EOF
chmod +x config.status
test -n "$no_create" || ./config.status

bison-1.22/configure.in100666 21641 1 415 5363202036 14324 0ustar friedmandaemondnl Process this file with autoconf to produce a configure script.
AC_INIT(reduce.c)
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_MINIX
AC_ISC_POSIX
AC_CONST
AC_STDC_HEADERS
AC_HAVE_HEADERS(string.h stdlib.h memory.h)
AC_HAVE_FUNCS(strerror)
AC_ALLOCA
AC_OUTPUT(Makefile)
bison-1.22/REFERENCES100666 12120 1 2255 4161426723 12415 0ustar rmsdaemonFrom phr Tue Jul 8 10:36:19 1986
Date: Tue, 8 Jul 86 00:52:24 EDT
From: phr (Paul Rubin)
To: riferguson%[email protected], tower
Subject: Re: Bison documentation?

The main difference between Bison and Yacc that I know of is that
Bison supports the @N construction, which gives you access to
the starting and ending line number and character number associated
with any of the symbols in the current rule.

Also, Bison supports the command `%expect N' which says not to mention
the conflicts if there are N shift/reduce conflicts and no reduce/reduce
conflicts.

The differences in the algorithms stem mainly from the horrible
kludges that Johnson had to perpetrate to make Yacc fit in a PDP-11.

Also, Bison uses a faster but less space-efficient encoding for the
parse tables (see Corbett's PhD thesis from Berkeley, "Static
Semantics in Compiler Error Recovery", June 1985, Report No. UCB/CSD
85/251), and more modern technique for generating the lookahead sets.
(See "Efficient Construction of LALR(1) Lookahead Sets" by F. DeRemer
and A. Pennello, in ACM TOPLS Vol 4 No 4, October 1982. Their
technique is the standard one now.)

paul rubin
free software foundation


bison-1.22/bison.1100666 21570 1 11751 5413125332 12241 0ustar djmdaemon.TH BISON 1 local
.SH NAME
bison \- GNU Project parser generator (yacc replacement)
.SH SYNOPSIS
.B bison
[
.BI \-b " file-prefix"
] [
.BI \-\-file-prefix= file-prefix
] [
.B \-d
] [
.B \-\-defines
] [
.B \-l
] [
.B \-\-no-lines
] [
.BI \-o " outfile"
] [
.BI \-\-output-file= outfile
] [
.BI \-p " prefix"
] [
.BI \-\-name-prefix= prefix
] [
.B \-t
] [
.B \-\-debug
] [
.B \-v
] [
.B \-\-verbose
] [
.B \-V
] [
.B \-\-version
] [
.B \-y
] [
.B \-\-yacc
] [
.B \-h
] [
.B \-\-help
] [
.B \-\-fixed-output-files
]
file
.SH DESCRIPTION
.I Bison
is a parser generator in the style of
.IR yacc (1).
It should be upwardly compatible with input files designed
for
.IR yacc .
.PP
Input files should follow the
.I yacc
convention of ending in
.BR .y .
Unlike
.IR yacc ,
the generated files do not have fixed names, but instead use the prefix
of the input file.
For instance, a grammar description file named
.B parse.y
would produce the generated parser in a file named
.BR parse.tab.c ,
instead of
.IR yacc 's
.BR y.tab.c .
.PP
This description of the options that can be given to
.I bison
is adapted from the node
.B Invocation
in the
.B bison.texinfo
manual, which should be taken as authoritative.
.PP
.I Bison
supports both traditional single-letter options and mnemonic long
option names. Long option names are indicated with
.B \-\-
instead of
.BR \- .
Abbreviations for option names are allowed as long as they
are unique. When a long option takes an argument, like
.BR \-\-file-prefix ,
connect the option name and the argument with
.BR = .
.SS OPTIONS
.TP
.BI \-b " file-prefix"
.br
.ns
.TP
.BI \-\-file-prefix= file-prefix
Specify a prefix to use for all
.I bison
output file names. The names are
chosen as if the input file were named
\fIfile-prefix\fB.c\fR.
.TP
.B \-d
.br
.ns
.TP
.B \-\-defines
Write an extra output file containing macro definitions for the token
type names defined in the grammar and the semantic value type
.BR YYSTYPE ,
as well as a few
.B extern
variable declarations.
.sp
If the parser output file is named
\fIname\fB.c\fR
then this file
is named
\fIname\fB.h\fR.
.sp
This output file is essential if you wish to put the definition of
.B yylex
in a separate source file, because
.B yylex
needs to be able to refer to token type codes and the variable
.BR yylval .
.TP
.B \-l
.br
.ns
.TP
.B \-\-no-lines
Don't put any
.B #line
preprocessor commands in the parser file.
Ordinarily
.I bison
puts them in the parser file so that the C compiler
and debuggers will associate errors with your source file, the
grammar file. This option causes them to associate errors with the
parser file, treating it an independent source file in its own right.
.TP
.BI \-o " outfile"
.br
.ns
.TP
.BI \-\-output-file= outfile
Specify the name
.I outfile
for the parser file.
.sp
The other output files' names are constructed from
.I outfile
as described under the
.B \-v
and
.B \-d
switches.
.TP
.BI \-p " prefix"
.br
.ns
.TP
.BI \-\-name-prefix= prefix
Rename the external symbols used in the parser so that they start with
.I prefix
instead of
.BR yy .
The precise list of symbols renamed is
.BR yyparse ,
.BR yylex ,
.BR yyerror ,
.BR yylval ,
.BR yychar ,
and
.BR yydebug .
.sp
For example, if you use
.BR "\-p c" ,
the names become
.BR cparse ,
.BR clex ,
and so on.
.TP
.B \-t
.br
.ns
.TP
.B \-\-debug
Output a definition of the macro
.B YYDEBUG
into the parser file,
so that the debugging facilities are compiled.
.TP
.B \-v
.br
.ns
.TP
.B \-\-verbose
Write an extra output file containing verbose descriptions of the
parser states and what is done for each type of look-ahead token in
that state.
.sp
This file also describes all the conflicts, both those resolved by
operator precedence and the unresolved ones.
.sp
The file's name is made by removing
.B .tab.c
or
.B .c
from the parser output file name, and adding
.B .output
instead.
.sp
Therefore, if the input file is
.BR foo.y ,
then the parser file is called
.B foo.tab.c
by default. As a consequence, the verbose
output file is called
.BR foo.output .
.TP
.B \-V
.br
.ns
.TP
.B \-\-version
Print the version number of
.I bison
and exit.
.TP
.B \-h
.br
.ns
.B \-\-help
Print a summary of the options to
.I bison
and exit.
.TP
.B \-y
.br
.ns
.TP
.B \-\-yacc
.br
.ns
.TP
.B \-\-fixed-output-files
Equivalent to
.BR "\-o y.tab.c" ;
the parser output file is called
.BR y.tab.c ,
and the other outputs are called
.B y.output
and
.BR y.tab.h .
The purpose of this switch is to imitate
.IR yacc 's
output file name conventions.
Thus, the following shell script can substitute for
.IR yacc :
.sp
.RS
.ft B
bison \-y $*
.ft R
.sp
.RE
.PP
The long-named options can be introduced with `+' as well as `\-\-',
for compatibility with previous releases. Eventually support for `+'
will be removed, because it is incompatible with the POSIX.2 standard.
.SH FILES
/usr/local/lib/bison.simple simple parser
.br
/usr/local/lib/bison.hairy complicated parser
.SH SEE ALSO
.IR yacc (1)
.br
The
.IR "Bison Reference Manual" ,
included as the file
.B bison.texinfo
in the
.I bison
source distribution.
.SH DIAGNOSTICS
Self explanatory.

bison-1.22/bison.rnh100666 12120 1 7300 5221250774 12667 0ustar rmsdaemon.!
.! RUNOFF source file for BISON.HLP
.!
.! This is a RUNOFF input file which will produce a VMS help file
.! for the VMS HELP library.
.!
.! Date of last revision: June 21, 1992
.!
.!
.! Eric Youngdale
.!
.literal
.end literal
.no paging
.no flags all
.right margin 70
.left margin 1

.indent -1
1 BISON
.skip
The BISON command invokes the GNU BISON parser generator.
.skip
.literal
BISON file-spec
.end literal
.skip
.indent -1
2 Parameters
.skip
file-spec
.skip
Here file-spec is the grammar file name, which usually ends in
.y. The parser file's name is made by replacing the .y
with _tab.c. Thus, the command bison foo.y yields
foo_tab.c.

.skip
.indent -1
2 Qualifiers
.skip
The following is the list of available qualifiers for BISON:
.literal
/DEBUG
/DEFINES
/FILE_PREFIX=prefix
/FIXED_OUTFILES
/NAME_PREFIX=prefix
/NOLINES
/OUTPUT=outfilefile
/VERBOSE
/VERSION
/YACC
.end literal
.skip
.indent -1
2 /DEBUG
.skip
Output a definition of the macro YYDEBUG into the parser file,
so that the debugging facilities are compiled.
.skip
.indent -1
2 /DEFINES
.skip
Write an extra output file containing macro definitions for the token
type names defined in the grammar and the semantic value type
YYSTYPE, as well as a extern variable declarations.
.skip
If the parser output file is named "name.c" then this file
is named "name.h".
.skip
This output file is essential if you wish to put the definition of
yylex in a separate source file, because yylex needs to
be able to refer to token type codes and the variable
yylval.
.skip
.indent -1
2 /FILE_PREFIX
.skip
.literal
/FILIE_PREFIX=prefix
.end literal
.skip
Specify a prefix to use for all Bison output file names. The names are
chosen as if the input file were named prefix.c

.skip
.indent -1
2 /FIXED_OUTFILES
.skip
Equivalent to /OUTPUT=y_tab.c; the parser output file is called
y_tab.c, and the other outputs are called y.output and
y_tab.h. The purpose of this switch is to imitate Yacc's output
file name conventions. The /YACC qualifier is functionally equivalent
to /FIXED_OUTFILES. The following command definition will
work as a substitute for Yacc:

.literal
$YACC:==BISON/FIXED_OUTFILES
.end literal
.skip
.indent -1
2 /NAME_PREFIX
.skip
.literal
/NAME_PREFIX=prefix
.end literal
.skip
Rename the external symbols used in the parser so that they start with
"prefix" instead of "yy". The precise list of symbols renamed
is yyparse, yylex, yyerror, yylval, yychar and yydebug.

For example, if you use /NAME_PREFIX="c", the names become cparse,
clex, and so on.

.skip
.indent -1
2 /NOLINES
.skip
Don't put any "#line" preprocessor commands in the parser file.
Ordinarily Bison puts them in the parser file so that the C compiler
and debuggers will associate errors with your source file, the
grammar file. This option causes them to associate errors with the
parser file, treating it an independent source file in its own right.

.skip
.indent -1
2 /OUTPUT
.skip
.literal
/OUTPUT=outfile
.end literal
.skip
Specify the name "outfile" for the parser file.
.skip
.indent -1
2 /VERBOSE
.skip
Write an extra output file containing verbose descriptions of the
parser states and what is done for each type of look-ahead token in
that state.
.skip
This file also describes all the conflicts, both those resolved by
operator precedence and the unresolved ones.
.skip
The file's name is made by removing _tab.c or .c from
the parser output file name, and adding .output instead.
.skip
Therefore, if the input file is foo.y, then the parser file is
called foo_tab.c by default. As a consequence, the verbose
output file is called foo.output.
.skip
.indent -1
2 /VERSION
.skip
Print the version number of Bison.

.skip
.indent -1
2 /YACC
.skip
See /FIXED_OUTFILES.
.skip
.indent -1



bison-1.22/configure.bat100666 12120 1 1610 5260706540 13513 0ustar rmsdaemon@echo off
echo Configuring bison for go32
rem This batch file assumes a unix-type "sed" program

echo # Makefile generated by "configure.bat"> Makefile
echo all.dos : bison >> Makefile

if exist config.sed del config.sed

echo "s/@srcdir@/./g ">> config.sed
echo "s/@CC@/gcc/g ">> config.sed
echo "s/@INSTALL@//g ">> config.sed
echo "s/@INSTALL_PROGRAM@//g ">> config.sed
echo "s/@INSTALL_DATA@//g ">> config.sed
echo "s/@DEFS@/-DHAVE_STRERROR/g ">> config.sed
echo "s/@LIBS@//g ">> config.sed
echo "s/@ALLOCA@//g ">> config.sed

echo "/^bison[ ]*:/,/-o/ { ">> config.sed
echo " s/ \$(CC)/ >bison.rf/ ">> config.sed
echo " /-o/ a\ ">> config.sed
echo " $(CC) @bison.rf ">> config.sed
echo "} ">> config.sed

sed -e "s/^\"//" -e "s/\"$//" -e "s/[ ]*$//" config.sed > config2.sed
sed -f config2.sed Makefile.in >> Makefile
del config.sed
del config2.sed
bison-1.22/bison.simple100666 12120 1 37511 5424166054 13422 0ustar rmsdaemon/* -*-C-*- Note some compilers choke on comments on `#line' lines. */
#line 3 "bison.simple"

/* Skeleton output parser for bison,
Copyright (C) 1984, 1989, 1990 Bob Corbett and Richard Stallman

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */


#ifndef alloca
#ifdef __GNUC__
#define alloca __builtin_alloca
#else /* not GNU C. */
#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
#include
#else /* not sparc */
#if defined (MSDOS) && !defined (__TURBOC__)
#include
#else /* not MSDOS, or __TURBOC__ */
#if defined(_AIX)
#include
#pragma alloca
#else /* not MSDOS, __TURBOC__, or _AIX */
#ifdef __hpux
#ifdef __cplusplus
extern "C" {
void *alloca (unsigned int);
};
#else /* not __cplusplus */
void *alloca ();
#endif /* not __cplusplus */
#endif /* __hpux */
#endif /* not _AIX */
#endif /* not MSDOS, or __TURBOC__ */
#endif /* not sparc. */
#endif /* not GNU C. */
#endif /* alloca not defined. */

/* This is the parser code that is written into each bison parser
when the %semantic_parser declaration is not specified in the grammar.
It was written by Richard Stallman by simplifying the hairy parser
used when %semantic_parser is specified. */

/* Note: there must be only one dollar sign in this file.
It is replaced by the list of actions, each action
as one case of the switch. */

#define yyerrok (yyerrstatus = 0)
#define yyclearin (yychar = YYEMPTY)
#define YYEMPTY -2
#define YYEOF 0
#define YYACCEPT return(0)
#define YYABORT return(1)
#define YYERROR goto yyerrlab1
/* Like YYERROR except do call yyerror.
This remains here temporarily to ease the
transition to the new meaning of YYERROR, for GCC.
Once GCC version 2 has supplanted version 1, this can go. */
#define YYFAIL goto yyerrlab
#define YYRECOVERING() (!!yyerrstatus)
#define YYBACKUP(token, value) \
do \
if (yychar == YYEMPTY && yylen == 1) \
{ yychar = (token), yylval = (value); \
yychar1 = YYTRANSLATE (yychar); \
YYPOPSTACK; \
goto yybackup; \
} \
else \
{ yyerror ("syntax error: cannot back up"); YYERROR; } \
while (0)

#define YYTERROR 1
#define YYERRCODE 256

#ifndef YYPURE
#define YYLEX yylex()
#endif

#ifdef YYPURE
#ifdef YYLSP_NEEDED
#define YYLEX yylex(&yylval, &yylloc)
#else
#define YYLEX yylex(&yylval)
#endif
#endif

/* If nonreentrant, generate the variables here */

#ifndef YYPURE

int yychar; /* the lookahead symbol */
YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */

#ifdef YYLSP_NEEDED
YYLTYPE yylloc; /* location data for the lookahead */
/* symbol */
#endif

int yynerrs; /* number of parse errors so far */
#endif /* not YYPURE */

#if YYDEBUG != 0
int yydebug; /* nonzero means print parse trace */
/* Since this is uninitialized, it does not stop multiple parsers
from coexisting. */
#endif

/* YYINITDEPTH indicates the initial size of the parser's stacks */

#ifndef YYINITDEPTH
#define YYINITDEPTH 200
#endif

/* YYMAXDEPTH is the maximum size the stacks can grow to
(effective only if the built-in stack extension method is used). */

#if YYMAXDEPTH == 0
#undef YYMAXDEPTH
#endif

#ifndef YYMAXDEPTH
#define YYMAXDEPTH 10000
#endif

/* Prevent warning if -Wstrict-prototypes. */
#ifdef __GNUC__
int yyparse (void);
#endif

#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */
#define __yy_bcopy(FROM,TO,COUNT) __builtin_memcpy(TO,FROM,COUNT)
#else /* not GNU C or C++ */
#ifndef __cplusplus

/* This is the most reliable way to avoid incompatibilities
in available built-in functions on various systems. */
static void
__yy_bcopy (from, to, count)
char *from;
char *to;
int count;
{
register char *f = from;
register char *t = to;
register int i = count;

while (i-- > 0)
*t++ = *f++;
}

#else /* __cplusplus */

/* This is the most reliable way to avoid incompatibilities
in available built-in functions on various systems. */
static void
__yy_bcopy (char *from, char *to, int count)
{
register char *f = from;
register char *t = to;
register int i = count;

while (i-- > 0)
*t++ = *f++;
}

#endif
#endif

#line 184 "bison.simple"
int
yyparse()
{
register int yystate;
register int yyn;
register short *yyssp;
register YYSTYPE *yyvsp;
int yyerrstatus; /* number of tokens to shift before error messages enabled */
int yychar1 = 0; /* lookahead token as an internal (translated) token number */

short yyssa[YYINITDEPTH]; /* the state stack */
YYSTYPE yyvsa[YYINITDEPTH]; /* the semantic value stack */

short *yyss = yyssa; /* refer to the stacks thru separate pointers */
YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */

#ifdef YYLSP_NEEDED
YYLTYPE yylsa[YYINITDEPTH]; /* the location stack */
YYLTYPE *yyls = yylsa;
YYLTYPE *yylsp;

#define YYPOPSTACK (yyvsp--, yyssp--, yylsp--)
#else
#define YYPOPSTACK (yyvsp--, yyssp--)
#endif

int yystacksize = YYINITDEPTH;

#ifdef YYPURE
int yychar;
YYSTYPE yylval;
int yynerrs;
#ifdef YYLSP_NEEDED
YYLTYPE yylloc;
#endif
#endif

YYSTYPE yyval; /* the variable used to return */
/* semantic values from the action */
/* routines */

int yylen;

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Starting parse\n");
#endif

yystate = 0;
yyerrstatus = 0;
yynerrs = 0;
yychar = YYEMPTY; /* Cause a token to be read. */

/* Initialize stack pointers.
Waste one element of value and location stack
so that they stay on the same level as the state stack.
The wasted elements are never initialized. */

yyssp = yyss - 1;
yyvsp = yyvs;
#ifdef YYLSP_NEEDED
yylsp = yyls;
#endif

/* Push a new state, which is found in yystate . */
/* In all cases, when you get here, the value and location stacks
have just been pushed. so pushing a state here evens the stacks. */
yynewstate:

*++yyssp = yystate;

if (yyssp >= yyss + yystacksize - 1)
{
/* Give user a chance to reallocate the stack */
/* Use copies of these so that the &'s don't force the real ones into memory. */
YYSTYPE *yyvs1 = yyvs;
short *yyss1 = yyss;
#ifdef YYLSP_NEEDED
YYLTYPE *yyls1 = yyls;
#endif

/* Get the current used size of the three stacks, in elements. */
int size = yyssp - yyss + 1;

#ifdef yyoverflow
/* Each stack pointer address is followed by the size of
the data in use in that stack, in bytes. */
#ifdef YYLSP_NEEDED
/* This used to be a conditional around just the two extra args,
but that might be undefined if yyoverflow is a macro. */
yyoverflow("parser stack overflow",
&yyss1, size * sizeof (*yyssp),
&yyvs1, size * sizeof (*yyvsp),
&yyls1, size * sizeof (*yylsp),
&yystacksize);
#else
yyoverflow("parser stack overflow",
&yyss1, size * sizeof (*yyssp),
&yyvs1, size * sizeof (*yyvsp),
&yystacksize);
#endif

yyss = yyss1; yyvs = yyvs1;
#ifdef YYLSP_NEEDED
yyls = yyls1;
#endif
#else /* no yyoverflow */
/* Extend the stack our own way. */
if (yystacksize >= YYMAXDEPTH)
{
yyerror("parser stack overflow");
return 2;
}
yystacksize *= 2;
if (yystacksize > YYMAXDEPTH)
yystacksize = YYMAXDEPTH;
yyss = (short *) alloca (yystacksize * sizeof (*yyssp));
__yy_bcopy ((char *)yyss1, (char *)yyss, size * sizeof (*yyssp));
yyvs = (YYSTYPE *) alloca (yystacksize * sizeof (*yyvsp));
__yy_bcopy ((char *)yyvs1, (char *)yyvs, size * sizeof (*yyvsp));
#ifdef YYLSP_NEEDED
yyls = (YYLTYPE *) alloca (yystacksize * sizeof (*yylsp));
__yy_bcopy ((char *)yyls1, (char *)yyls, size * sizeof (*yylsp));
#endif
#endif /* no yyoverflow */

yyssp = yyss + size - 1;
yyvsp = yyvs + size - 1;
#ifdef YYLSP_NEEDED
yylsp = yyls + size - 1;
#endif

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Stack size increased to %d\n", yystacksize);
#endif

if (yyssp >= yyss + yystacksize - 1)
YYABORT;
}

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Entering state %d\n", yystate);
#endif

goto yybackup;
yybackup:

/* Do appropriate processing given the current state. */
/* Read a lookahead token if we need one and don't already have one. */
/* yyresume: */

/* First try to decide what to do without reference to lookahead token. */

yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yydefault;

/* Not known => get a lookahead token if don't already have one. */

/* yychar is either YYEMPTY or YYEOF
or a valid token in external form. */

if (yychar == YYEMPTY)
{
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Reading a token: ");
#endif
yychar = YYLEX;
}

/* Convert token to internal form (in yychar1) for indexing tables with */

if (yychar <= 0) /* This means end of input. */
{
yychar1 = 0;
yychar = YYEOF; /* Don't call YYLEX any more */

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Now at end of input.\n");
#endif
}
else
{
yychar1 = YYTRANSLATE(yychar);

#if YYDEBUG != 0
if (yydebug)
{
fprintf (stderr, "Next token is %d (%s", yychar, yytname[yychar1]);
/* Give the individual parser a way to print the precise meaning
of a token, for further debugging info. */
#ifdef YYPRINT
YYPRINT (stderr, yychar, yylval);
#endif
fprintf (stderr, ")\n");
}
#endif
}

yyn += yychar1;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
goto yydefault;

yyn = yytable[yyn];

/* yyn is what to do for this token type in this state.
Negative => reduce, -yyn is rule number.
Positive => shift, yyn is new state.
New state is final state => don't bother to shift,
just return success.
0, or most negative number => error. */

if (yyn < 0)
{
if (yyn == YYFLAG)
goto yyerrlab;
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrlab;

if (yyn == YYFINAL)
YYACCEPT;

/* Shift the lookahead token. */

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
#endif

/* Discard the token being shifted unless it is eof. */
if (yychar != YYEOF)
yychar = YYEMPTY;

*++yyvsp = yylval;
#ifdef YYLSP_NEEDED
*++yylsp = yylloc;
#endif

/* count tokens shifted since error; after three, turn off error status. */
if (yyerrstatus) yyerrstatus--;

yystate = yyn;
goto yynewstate;

/* Do the default action for the current state. */
yydefault:

yyn = yydefact[yystate];
if (yyn == 0)
goto yyerrlab;

/* Do a reduction. yyn is the number of a rule to reduce with. */
yyreduce:
yylen = yyr2[yyn];
if (yylen > 0)
yyval = yyvsp[1-yylen]; /* implement default value of the action */

#if YYDEBUG != 0
if (yydebug)
{
int i;

fprintf (stderr, "Reducing via rule %d (line %d), ",
yyn, yyrline[yyn]);

/* Print the symbols being reduced, and their result. */
for (i = yyprhs[yyn]; yyrhs[i] > 0; i++)
fprintf (stderr, "%s ", yytname[yyrhs[i]]);
fprintf (stderr, " -> %s\n", yytname[yyr1[yyn]]);
}
#endif

$ /* the action file gets copied in in place of this dollarsign */
#line 465 "bison.simple"

yyvsp -= yylen;
yyssp -= yylen;
#ifdef YYLSP_NEEDED
yylsp -= yylen;
#endif

#if YYDEBUG != 0
if (yydebug)
{
short *ssp1 = yyss - 1;
fprintf (stderr, "state stack now");
while (ssp1 != yyssp)
fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n");
}
#endif

*++yyvsp = yyval;

#ifdef YYLSP_NEEDED
yylsp++;
if (yylen == 0)
{
yylsp->first_line = yylloc.first_line;
yylsp->first_column = yylloc.first_column;
yylsp->last_line = (yylsp-1)->last_line;
yylsp->last_column = (yylsp-1)->last_column;
yylsp->text = 0;
}
else
{
yylsp->last_line = (yylsp+yylen-1)->last_line;
yylsp->last_column = (yylsp+yylen-1)->last_column;
}
#endif

/* Now "shift" the result of the reduction.
Determine what state that goes to,
based on the state we popped back to
and the rule number reduced by. */

yyn = yyr1[yyn];

yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
yystate = yytable[yystate];
else
yystate = yydefgoto[yyn - YYNTBASE];

goto yynewstate;

yyerrlab: /* here on detecting error */

if (! yyerrstatus)
/* If not already recovering from an error, report this error. */
{
++yynerrs;

#ifdef YYERROR_VERBOSE
yyn = yypact[yystate];

if (yyn > YYFLAG && yyn < YYLAST)
{
int size = 0;
char *msg;
int x, count;

count = 0;
/* Start X at -yyn if nec to avoid negative indexes in yycheck. */
for (x = (yyn < 0 ? -yyn : 0);
x < (sizeof(yytname) / sizeof(char *)); x++)
if (yycheck[x + yyn] == x)
size += strlen(yytname[x]) + 15, count++;
msg = (char *) malloc(size + 15);
if (msg != 0)
{
strcpy(msg, "parse error");

if (count < 5)
{
count = 0;
for (x = (yyn < 0 ? -yyn : 0);
x < (sizeof(yytname) / sizeof(char *)); x++)
if (yycheck[x + yyn] == x)
{
strcat(msg, count == 0 ? ", expecting `" : " or `");
strcat(msg, yytname[x]);
strcat(msg, "'");
count++;
}
}
yyerror(msg);
free(msg);
}
else
yyerror ("parse error; also virtual memory exceeded");
}
else
#endif /* YYERROR_VERBOSE */
yyerror("parse error");
}

goto yyerrlab1;
yyerrlab1: /* here on error raised explicitly by an action */

if (yyerrstatus == 3)
{
/* if just tried and failed to reuse lookahead token after an error, discard it. */

/* return failure if at end of input */
if (yychar == YYEOF)
YYABORT;

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
#endif

yychar = YYEMPTY;
}

/* Else will try to reuse lookahead token
after shifting the error token. */

yyerrstatus = 3; /* Each real token shifted decrements this */

goto yyerrhandle;

yyerrdefault: /* current state does not do anything special for the error token. */

#if 0
/* This is wrong; only states that explicitly want error tokens
should shift them. */
yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/
if (yyn) goto yydefault;
#endif

yyerrpop: /* pop the current state because it cannot handle the error token */

if (yyssp == yyss) YYABORT;
yyvsp--;
yystate = *--yyssp;
#ifdef YYLSP_NEEDED
yylsp--;
#endif

#if YYDEBUG != 0
if (yydebug)
{
short *ssp1 = yyss - 1;
fprintf (stderr, "Error: state stack now");
while (ssp1 != yyssp)
fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n");
}
#endif

yyerrhandle:

yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yyerrdefault;

yyn += YYTERROR;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
goto yyerrdefault;

yyn = yytable[yyn];
if (yyn < 0)
{
if (yyn == YYFLAG)
goto yyerrpop;
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrpop;

if (yyn == YYFINAL)
YYACCEPT;

#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Shifting error token, ");
#endif

*++yyvsp = yylval;
#ifdef YYLSP_NEEDED
*++yylsp = yylloc;
#endif

yystate = yyn;
goto yynewstate;
}
bison-1.22/bison.hairy100666 12120 1 14515 4311627021 13232 0ustar rmsdaemon
extern int timeclock;


int yyerror; /* Yyerror and yycost are set by guards. */
int yycost; /* If yyerror is set to a nonzero value by a */
/* guard, the reduction with which the guard */
/* is associated is not performed, and the */
/* error recovery mechanism is invoked. */
/* Yycost indicates the cost of performing */
/* the reduction given the attributes of the */
/* symbols. */


/* YYMAXDEPTH indicates the size of the parser's state and value */
/* stacks. */

#ifndef YYMAXDEPTH
#define YYMAXDEPTH 500
#endif

/* YYMAXRULES must be at least as large as the number of rules that */
/* could be placed in the rule queue. That number could be determined */
/* from the grammar and the size of the stack, but, as yet, it is not. */

#ifndef YYMAXRULES
#define YYMAXRULES 100
#endif

#ifndef YYMAXBACKUP
#define YYMAXBACKUP 100
#endif


short yyss[YYMAXDEPTH]; /* the state stack */
YYSTYPE yyvs[YYMAXDEPTH]; /* the semantic value stack */
YYLTYPE yyls[YYMAXDEPTH]; /* the location stack */
short yyrq[YYMAXRULES]; /* the rule queue */
int yychar; /* the lookahead symbol */

YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */

YYSTYPE yytval; /* the semantic value for the state */
/* at the top of the state stack. */

YYSTYPE yyval; /* the variable used to return */
/* semantic values from the action */
/* routines */

YYLTYPE yylloc; /* location data for the lookahead */
/* symbol */

YYLTYPE yytloc; /* location data for the state at the */
/* top of the state stack */


int yynunlexed;
short yyunchar[YYMAXBACKUP];
YYSTYPE yyunval[YYMAXBACKUP];
YYLTYPE yyunloc[YYMAXBACKUP];

short *yygssp; /* a pointer to the top of the state */
/* stack; only set during error */
/* recovery. */

YYSTYPE *yygvsp; /* a pointer to the top of the value */
/* stack; only set during error */
/* recovery. */

YYLTYPE *yyglsp; /* a pointer to the top of the */
/* location stack; only set during */
/* error recovery. */


/* Yyget is an interface between the parser and the lexical analyzer. */
/* It is costly to provide such an interface, but it avoids requiring */
/* the lexical analyzer to be able to back up the scan. */

yyget()
{
if (yynunlexed > 0)
{
yynunlexed--;
yychar = yyunchar[yynunlexed];
yylval = yyunval[yynunlexed];
yylloc = yyunloc[yynunlexed];
}
else if (yychar <= 0)
yychar = 0;
else
{
yychar = yylex();
if (yychar < 0)
yychar = 0;
else yychar = YYTRANSLATE(yychar);
}
}



yyunlex(chr, val, loc)
int chr;
YYSTYPE val;
YYLTYPE loc;
{
yyunchar[yynunlexed] = chr;
yyunval[yynunlexed] = val;
yyunloc[yynunlexed] = loc;
yynunlexed++;
}



yyrestore(first, last)
register short *first;
register short *last;
{
register short *ssp;
register short *rp;
register int symbol;
register int state;
register int tvalsaved;

ssp = yygssp;
yyunlex(yychar, yylval, yylloc);

tvalsaved = 0;
while (first != last)
{
symbol = yystos[*ssp];
if (symbol < YYNTBASE)
{
yyunlex(symbol, yytval, yytloc);
tvalsaved = 1;
ssp--;
}

ssp--;

if (first == yyrq)
first = yyrq + YYMAXRULES;

first--;

for (rp = yyrhs + yyprhs[*first]; symbol = *rp; rp++)
{
if (symbol < YYNTBASE)
state = yytable[yypact[*ssp] + symbol];
else
{
state = yypgoto[symbol - YYNTBASE] + *ssp;

if (state >= 0 && state <= YYLAST && yycheck[state] == *ssp)
state = yytable[state];
else
state = yydefgoto[symbol - YYNTBASE];
}

*++ssp = state;
}
}

if ( ! tvalsaved && ssp > yyss)
{
yyunlex(yystos[*ssp], yytval, yytloc);
ssp--;
}

yygssp = ssp;
}



int
yyparse()
{
register int yystate;
register int yyn;
register short *yyssp;
register short *yyrq0;
register short *yyptr;
register YYSTYPE *yyvsp;

int yylen;
YYLTYPE *yylsp;
short *yyrq1;
short *yyrq2;

yystate = 0;
yyssp = yyss - 1;
yyvsp = yyvs - 1;
yylsp = yyls - 1;
yyrq0 = yyrq;
yyrq1 = yyrq0;
yyrq2 = yyrq0;

yychar = yylex();
if (yychar < 0)
yychar = 0;
else yychar = YYTRANSLATE(yychar);

yynewstate:

if (yyssp >= yyss + YYMAXDEPTH - 1)
{
yyabort("Parser Stack Overflow");
YYABORT;
}

*++yyssp = yystate;

yyresume:

yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yydefault;

yyn += yychar;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar)
goto yydefault;

yyn = yytable[yyn];
if (yyn < 0)
{
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrlab;

yystate = yyn;

yyptr = yyrq2;
while (yyptr != yyrq1)
{
yyn = *yyptr++;
yylen = yyr2[yyn];
yyvsp -= yylen;
yylsp -= yylen;

yyguard(yyn, yyvsp, yylsp);
if (yyerror)
goto yysemerr;

yyaction(yyn, yyvsp, yylsp);
*++yyvsp = yyval;

yylsp++;
if (yylen == 0)
{
yylsp->timestamp = timeclock;
yylsp->first_line = yytloc.first_line;
yylsp->first_column = yytloc.first_column;
yylsp->last_line = (yylsp-1)->last_line;
yylsp->last_column = (yylsp-1)->last_column;
yylsp->text = 0;
}
else
{
yylsp->last_line = (yylsp+yylen-1)->last_line;
yylsp->last_column = (yylsp+yylen-1)->last_column;
}

if (yyptr == yyrq + YYMAXRULES)
yyptr = yyrq;
}

if (yystate == YYFINAL)
YYACCEPT;

yyrq2 = yyptr;
yyrq1 = yyrq0;

*++yyvsp = yytval;
*++yylsp = yytloc;
yytval = yylval;
yytloc = yylloc;
yyget();

goto yynewstate;

yydefault:

yyn = yydefact[yystate];
if (yyn == 0)
goto yyerrlab;

yyreduce:

*yyrq0++ = yyn;

if (yyrq0 == yyrq + YYMAXRULES)
yyrq0 = yyrq;

if (yyrq0 == yyrq2)
{
yyabort("Parser Rule Queue Overflow");
YYABORT;
}

yyssp -= yyr2[yyn];
yyn = yyr1[yyn];

yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
yystate = yytable[yystate];
else
yystate = yydefgoto[yyn - YYNTBASE];

goto yynewstate;

yysemerr:
*--yyptr = yyn;
yyrq2 = yyptr;
yyvsp += yyr2[yyn];

yyerrlab:

yygssp = yyssp;
yygvsp = yyvsp;
yyglsp = yylsp;
yyrestore(yyrq0, yyrq2);
yyrecover();
yystate = *yygssp;
yyssp = yygssp;
yyvsp = yygvsp;
yyrq0 = yyrq;
yyrq1 = yyrq0;
yyrq2 = yyrq0;
goto yyresume;
}

$
bison-1.22/LR0.c100666 21570 13 36252 5364706445 11366 0ustar djmuser/* Generate the nondeterministic finite state machine for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* See comments in state.h for the data structures that represent it.
The entry point is generate_states. */

#include
#include "system.h"
#include "machine.h"
#include "new.h"
#include "gram.h"
#include "state.h"


extern char *nullable;
extern short *itemset;
extern short *itemsetend;


int nstates;
int final_state;
core *first_state;
shifts *first_shift;
reductions *first_reduction;

int get_state();
core *new_state();

void new_itemsets();
void append_states();
void initialize_states();
void save_shifts();
void save_reductions();
void augment_automaton();
void insert_start_shift();
extern void initialize_closure();
extern void closure();
extern void finalize_closure();
extern void toomany();

static core *this_state;
static core *last_state;
static shifts *last_shift;
static reductions *last_reduction;

static int nshifts;
static short *shift_symbol;

static short *redset;
static short *shiftset;

static short **kernel_base;
static short **kernel_end;
static short *kernel_items;

/* hash table for states, to recognize equivalent ones. */

#define STATE_TABLE_SIZE 1009
static core **state_table;



void
allocate_itemsets()
{
register short *itemp;
register int symbol;
register int i;
register int count;
register short *symbol_count;

count = 0;
symbol_count = NEW2(nsyms, short);

itemp = ritem;
symbol = *itemp++;
while (symbol)
{
if (symbol > 0)
{
count++;
symbol_count[symbol]++;
}
symbol = *itemp++;
}

/* see comments before new_itemsets. All the vectors of items
live inside kernel_items. The number of active items after
some symbol cannot be more than the number of times that symbol
appears as an item, which is symbol_count[symbol].
We allocate that much space for each symbol. */

kernel_base = NEW2(nsyms, short *);
kernel_items = NEW2(count, short);

count = 0;
for (i = 0; i < nsyms; i++)
{
kernel_base[i] = kernel_items + count;
count += symbol_count[i];
}

shift_symbol = symbol_count;
kernel_end = NEW2(nsyms, short *);
}


void
allocate_storage()
{
allocate_itemsets();

shiftset = NEW2(nsyms, short);
redset = NEW2(nrules + 1, short);
state_table = NEW2(STATE_TABLE_SIZE, core *);
}


void
free_storage()
{
FREE(shift_symbol);
FREE(redset);
FREE(shiftset);
FREE(kernel_base);
FREE(kernel_end);
FREE(kernel_items);
FREE(state_table);
}



/* compute the nondeterministic finite state machine (see state.h for details)
from the grammar. */
void
generate_states()
{
allocate_storage();
initialize_closure(nitems);
initialize_states();

while (this_state)
{
/* Set up ruleset and itemset for the transitions out of this state.
ruleset gets a 1 bit for each rule that could reduce now.
itemset gets a vector of all the items that could be accepted next. */
closure(this_state->items, this_state->nitems);
/* record the reductions allowed out of this state */
save_reductions();
/* find the itemsets of the states that shifts can reach */
new_itemsets();
/* find or create the core structures for those states */
append_states();

/* create the shifts structures for the shifts to those states,
now that the state numbers transitioning to are known */
if (nshifts > 0)
save_shifts();

/* states are queued when they are created; process them all */
this_state = this_state->next;
}

/* discard various storage */
finalize_closure();
free_storage();

/* set up initial and final states as parser wants them */
augment_automaton();
}



/* Find which symbols can be shifted in the current state,
and for each one record which items would be active after that shift.
Uses the contents of itemset.
shift_symbol is set to a vector of the symbols that can be shifted.
For each symbol in the grammar, kernel_base[symbol] points to
a vector of item numbers activated if that symbol is shifted,
and kernel_end[symbol] points after the end of that vector. */
void
new_itemsets()
{
register int i;
register int shiftcount;
register short *isp;
register short *ksp;
register int symbol;

#ifdef TRACE
fprintf(stderr, "Entering new_itemsets\n");
#endif

for (i = 0; i < nsyms; i++)
kernel_end[i] = NULL;

shiftcount = 0;

isp = itemset;

while (isp < itemsetend)
{
i = *isp++;
symbol = ritem[i];
if (symbol > 0)
{
ksp = kernel_end[symbol];

if (!ksp)
{
shift_symbol[shiftcount++] = symbol;
ksp = kernel_base[symbol];
}

*ksp++ = i + 1;
kernel_end[symbol] = ksp;
}
}

nshifts = shiftcount;
}



/* Use the information computed by new_itemsets to find the state numbers
reached by each shift transition from the current state.

shiftset is set up as a vector of state numbers of those states. */
void
append_states()
{
register int i;
register int j;
register int symbol;

#ifdef TRACE
fprintf(stderr, "Entering append_states\n");
#endif

/* first sort shift_symbol into increasing order */

for (i = 1; i < nshifts; i++)
{
symbol = shift_symbol[i];
j = i;
while (j > 0 && shift_symbol[j - 1] > symbol)
{
shift_symbol[j] = shift_symbol[j - 1];
j--;
}
shift_symbol[j] = symbol;
}

for (i = 0; i < nshifts; i++)
{
symbol = shift_symbol[i];
shiftset[i] = get_state(symbol);
}
}



/* find the state number for the state we would get to
(from the current state) by shifting symbol.
Create a new state if no equivalent one exists already.
Used by append_states */

int
get_state(symbol)
int symbol;
{
register int key;
register short *isp1;
register short *isp2;
register short *iend;
register core *sp;
register int found;

int n;

#ifdef TRACE
fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
#endif

isp1 = kernel_base[symbol];
iend = kernel_end[symbol];
n = iend - isp1;

/* add up the target state's active item numbers to get a hash key */
key = 0;
while (isp1 < iend)
key += *isp1++;

key = key % STATE_TABLE_SIZE;

sp = state_table[key];

if (sp)
{
found = 0;
while (!found)
{
if (sp->nitems == n)
{
found = 1;
isp1 = kernel_base[symbol];
isp2 = sp->items;

while (found && isp1 < iend)
{
if (*isp1++ != *isp2++)
found = 0;
}
}

if (!found)
{
if (sp->link)
{
sp = sp->link;
}
else /* bucket exhausted and no match */
{
sp = sp->link = new_state(symbol);
found = 1;
}
}
}
}
else /* bucket is empty */
{
state_table[key] = sp = new_state(symbol);
}

return (sp->number);
}



/* subroutine of get_state. create a new state for those items, if necessary. */

core *
new_state(symbol)
int symbol;
{
register int n;
register core *p;
register short *isp1;
register short *isp2;
register short *iend;

#ifdef TRACE
fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
#endif

if (nstates >= MAXSHORT)
toomany("states");

isp1 = kernel_base[symbol];
iend = kernel_end[symbol];
n = iend - isp1;

p = (core *) xmalloc((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
p->accessing_symbol = symbol;
p->number = nstates;
p->nitems = n;

isp2 = p->items;
while (isp1 < iend)
*isp2++ = *isp1++;

last_state->next = p;
last_state = p;

nstates++;

return (p);
}


void
initialize_states()
{
register core *p;
/* register unsigned *rp1; JF unused */
/* register unsigned *rp2; JF unused */
/* register unsigned *rend; JF unused */

p = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
first_state = last_state = this_state = p;
nstates = 1;
}


void
save_shifts()
{
register shifts *p;
register short *sp1;
register short *sp2;
register short *send;

p = (shifts *) xmalloc((unsigned) (sizeof(shifts) +
(nshifts - 1) * sizeof(short)));

p->number = this_state->number;
p->nshifts = nshifts;

sp1 = shiftset;
sp2 = p->shifts;
send = shiftset + nshifts;

while (sp1 < send)
*sp2++ = *sp1++;

if (last_shift)
{
last_shift->next = p;
last_shift = p;
}
else
{
first_shift = p;
last_shift = p;
}
}



/* find which rules can be used for reduction transitions from the current state
and make a reductions structure for the state to record their rule numbers. */
void
save_reductions()
{
register short *isp;
register short *rp1;
register short *rp2;
register int item;
register int count;
register reductions *p;

short *rend;

/* find and count the active items that represent ends of rules */

count = 0;
for (isp = itemset; isp < itemsetend; isp++)
{
item = ritem[*isp];
if (item < 0)
{
redset[count++] = -item;
}
}

/* make a reductions structure and copy the data into it. */

if (count)
{
p = (reductions *) xmalloc((unsigned) (sizeof(reductions) +
(count - 1) * sizeof(short)));

p->number = this_state->number;
p->nreds = count;

rp1 = redset;
rp2 = p->rules;
rend = rp1 + count;

while (rp1 < rend)
*rp2++ = *rp1++;

if (last_reduction)
{
last_reduction->next = p;
last_reduction = p;
}
else
{
first_reduction = p;
last_reduction = p;
}
}
}



/* Make sure that the initial state has a shift that accepts the
grammar's start symbol and goes to the next-to-final state,
which has a shift going to the final state, which has a shift
to the termination state.
Create such states and shifts if they don't happen to exist already. */
void
augment_automaton()
{
register int i;
register int k;
/* register int found; JF unused */
register core *statep;
register shifts *sp;
register shifts *sp2;
register shifts *sp1;

sp = first_shift;

if (sp)
{
if (sp->number == 0)
{
k = sp->nshifts;
statep = first_state->next;

/* The states reached by shifts from first_state are numbered 1...K.
Look for one reached by start_symbol. */
while (statep->accessing_symbol < start_symbol
&& statep->number < k)
statep = statep->next;

if (statep->accessing_symbol == start_symbol)
{
/* We already have a next-to-final state.
Make sure it has a shift to what will be the final state. */
k = statep->number;

while (sp && sp->number < k)
{
sp1 = sp;
sp = sp->next;
}

if (sp && sp->number == k)
{
sp2 = (shifts *) xmalloc((unsigned) (sizeof(shifts)
+ sp->nshifts * sizeof(short)));
sp2->number = k;
sp2->nshifts = sp->nshifts + 1;
sp2->shifts[0] = nstates;
for (i = sp->nshifts; i > 0; i--)
sp2->shifts[i] = sp->shifts[i - 1];

/* Patch sp2 into the chain of shifts in place of sp,
following sp1. */
sp2->next = sp->next;
sp1->next = sp2;
if (sp == last_shift)
last_shift = sp2;
FREE(sp);
}
else
{
sp2 = NEW(shifts);
sp2->number = k;
sp2->nshifts = 1;
sp2->shifts[0] = nstates;

/* Patch sp2 into the chain of shifts between sp1 and sp. */
sp2->next = sp;
sp1->next = sp2;
if (sp == 0)
last_shift = sp2;
}
}
else
{
/* There is no next-to-final state as yet. */
/* Add one more shift in first_shift,
going to the next-to-final state (yet to be made). */
sp = first_shift;

sp2 = (shifts *) xmalloc(sizeof(shifts)
+ sp->nshifts * sizeof(short));
sp2->nshifts = sp->nshifts + 1;

/* Stick this shift into the vector at the proper place. */
statep = first_state->next;
for (k = 0, i = 0; i < sp->nshifts; k++, i++)
{
if (statep->accessing_symbol > start_symbol && i == k)
sp2->shifts[k++] = nstates;
sp2->shifts[k] = sp->shifts[i];
statep = statep->next;
}
if (i == k)
sp2->shifts[k++] = nstates;

/* Patch sp2 into the chain of shifts
in place of sp, at the beginning. */
sp2->next = sp->next;
first_shift = sp2;
if (last_shift == sp)
last_shift = sp2;

FREE(sp);

/* Create the next-to-final state, with shift to
what will be the final state. */
insert_start_shift();
}
}
else
{
/* The initial state didn't even have any shifts.
Give it one shift, to the next-to-final state. */
sp = NEW(shifts);
sp->nshifts = 1;
sp->shifts[0] = nstates;

/* Patch sp into the chain of shifts at the beginning. */
sp->next = first_shift;
first_shift = sp;

/* Create the next-to-final state, with shift to
what will be the final state. */
insert_start_shift();
}
}
else
{
/* There are no shifts for any state.
Make one shift, from the initial state to the next-to-final state. */

sp = NEW(shifts);
sp->nshifts = 1;
sp->shifts[0] = nstates;

/* Initialize the chain of shifts with sp. */
first_shift = sp;
last_shift = sp;

/* Create the next-to-final state, with shift to
what will be the final state. */
insert_start_shift();
}

/* Make the final state--the one that follows a shift from the
next-to-final state.
The symbol for that shift is 0 (end-of-file). */
statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
statep->number = nstates;
last_state->next = statep;
last_state = statep;

/* Make the shift from the final state to the termination state. */
sp = NEW(shifts);
sp->number = nstates++;
sp->nshifts = 1;
sp->shifts[0] = nstates;
last_shift->next = sp;
last_shift = sp;

/* Note that the variable `final_state' refers to what we sometimes call
the termination state. */
final_state = nstates;

/* Make the termination state. */
statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
statep->number = nstates++;
last_state->next = statep;
last_state = statep;
}


/* subroutine of augment_automaton.
Create the next-to-final state, to which a shift has already been made in
the initial state. */
void
insert_start_shift()
{
register core *statep;
register shifts *sp;

statep = (core *) xmalloc((unsigned) (sizeof(core) - sizeof(short)));
statep->number = nstates;
statep->accessing_symbol = start_symbol;

last_state->next = statep;
last_state = statep;

/* Make a shift from this state to (what will be) the final state. */
sp = NEW(shifts);
sp->number = nstates++;
sp->nshifts = 1;
sp->shifts[0] = nstates;

last_shift->next = sp;
last_shift = sp;
}
bison-1.22/allocate.c100666 21570 13 3041 5364707470 12522 0ustar djmuser/* Allocate and clear storage for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include

extern char *calloc ();
extern char *realloc ();
extern void done ();

extern char *program_name;

char *
xmalloc (n)
register unsigned n;
{
register char *block;

/* Avoid uncertainty about what an arg of 0 will do. */
if (n == 0)
n = 1;
block = calloc (n, 1);
if (block == NULL)
{
fprintf (stderr, "%s: memory exhausted\n", program_name);
done (1);
}

return (block);
}

char *
xrealloc (block, n)
register char *block;
register unsigned n;
{
/* Avoid uncertainty about what an arg of 0 will do. */
if (n == 0)
n = 1;
block = realloc (block, n);
if (block == NULL)
{
fprintf (stderr, "%s: memory exhausted\n", program_name);
done (1);
}

return (block);
}
bison-1.22/closure.c100666 12120 1 15136 5124502051 12700 0ustar rmsdaemon/* Subroutines for bison
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* subroutines of file LR0.c.

Entry points:

closure (items, n)

Given a vector of item numbers items, of length n,
set up ruleset and itemset to indicate what rules could be run
and which items could be accepted when those items are the active ones.

ruleset contains a bit for each rule. closure sets the bits
for all rules which could potentially describe the next input to be read.

itemset is a vector of item numbers; itemsetend points to just beyond the end
of the part of it that is significant.
closure places there the indices of all items which represent units of
input that could arrive next.

initialize_closure (n)

Allocates the itemset and ruleset vectors,
and precomputes useful data so that closure can be called.
n is the number of elements to allocate for itemset.

finalize_closure ()

Frees itemset, ruleset and internal data.

*/

#include
#include "system.h"
#include "machine.h"
#include "new.h"
#include "gram.h"


extern short **derives;
extern char **tags;

void set_fderives();
void set_firsts();

extern void RTC();

short *itemset;
short *itemsetend;
static unsigned *ruleset;

/* internal data. See comments before set_fderives and set_firsts. */
static unsigned *fderives;
static unsigned *firsts;

/* number of words required to hold a bit for each rule */
static int rulesetsize;

/* number of words required to hold a bit for each variable */
static int varsetsize;


void
initialize_closure(n)
int n;
{
itemset = NEW2(n, short);

rulesetsize = WORDSIZE(nrules + 1);
ruleset = NEW2(rulesetsize, unsigned);

set_fderives();
}



/* set fderives to an nvars by nrules matrix of bits
indicating which rules can help derive the beginning of the data
for each nonterminal. For example, if symbol 5 can be derived as
the sequence of symbols 8 3 20, and one of the rules for deriving
symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
void
set_fderives()
{
register unsigned *rrow;
register unsigned *vrow;
register int j;
register unsigned cword;
register short *rp;
register int b;

int ruleno;
int i;

fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;

set_firsts();

rrow = fderives + ntokens * rulesetsize;

for (i = ntokens; i < nsyms; i++)
{
vrow = firsts + ((i - ntokens) * varsetsize);
cword = *vrow++;
b = 0;
for (j = ntokens; j < nsyms; j++)
{
if (cword & (1 << b))
{
rp = derives[j];
while ((ruleno = *rp++) > 0)
{
SETBIT(rrow, ruleno);
}
}

b++;
if (b >= BITS_PER_WORD && j + 1 < nsyms)
{
cword = *vrow++;
b = 0;
}
}

rrow += rulesetsize;
}

#ifdef DEBUG
print_fderives();
#endif

FREE(firsts);
}



/* set firsts to be an nvars by nvars bit matrix indicating which items
can represent the beginning of the input corresponding to which other items.
For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
the symbol 8 can be the beginning of the data for symbol 5,
so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
void
set_firsts()
{
register unsigned *row;
/* register int done; JF unused */
register int symbol;
register short *sp;
register int rowsize;

int i;

varsetsize = rowsize = WORDSIZE(nvars);

firsts = NEW2(nvars * rowsize, unsigned);

row = firsts;
for (i = ntokens; i < nsyms; i++)
{
sp = derives[i];
while (*sp >= 0)
{
symbol = ritem[rrhs[*sp++]];
if (ISVAR(symbol))
{
symbol -= ntokens;
SETBIT(row, symbol);
}
}

row += rowsize;
}

RTC(firsts, nvars);

#ifdef DEBUG
print_firsts();
#endif
}


void
closure(core, n)
short *core;
int n;
{
register int ruleno;
register unsigned word;
register short *csp;
register unsigned *dsp;
register unsigned *rsp;

short *csend;
unsigned *rsend;
int symbol;
int itemno;

rsp = ruleset;
rsend = ruleset + rulesetsize;
csend = core + n;

if (n == 0)
{
dsp = fderives + start_symbol * rulesetsize;
while (rsp < rsend)
*rsp++ = *dsp++;
}
else
{
while (rsp < rsend)
*rsp++ = 0;

csp = core;
while (csp < csend)
{
symbol = ritem[*csp++];
if (ISVAR(symbol))
{
dsp = fderives + symbol * rulesetsize;
rsp = ruleset;
while (rsp < rsend)
*rsp++ |= *dsp++;
}
}
}

ruleno = 0;
itemsetend = itemset;
csp = core;
rsp = ruleset;
while (rsp < rsend)
{
word = *rsp++;
if (word == 0)
{
ruleno += BITS_PER_WORD;
}
else
{
register int b;

for (b = 0; b < BITS_PER_WORD; b++)
{
if (word & (1 << b))
{
itemno = rrhs[ruleno];
while (csp < csend && *csp < itemno)
*itemsetend++ = *csp++;
*itemsetend++ = itemno;
}

ruleno++;
}
}
}

while (csp < csend)
*itemsetend++ = *csp++;

#ifdef DEBUG
print_closure(n);
#endif
}


void
finalize_closure()
{
FREE(itemset);
FREE(ruleset);
FREE(fderives + ntokens * rulesetsize);
}



#ifdef DEBUG

print_closure(n)
int n;
{
register short *isp;

printf("\n\nn = %d\n\n", n);
for (isp = itemset; isp < itemsetend; isp++)
printf(" %d\n", *isp);
}



print_firsts()
{
register int i;
register int j;
register unsigned *rowp;

printf("\n\n\nFIRSTS\n\n");

for (i = ntokens; i < nsyms; i++)
{
printf("\n\n%s firsts\n\n", tags[i]);

rowp = firsts + ((i - ntokens) * varsetsize);

for (j = 0; j < nvars; j++)
if (BITISSET (rowp, j))
printf(" %s\n", tags[j + ntokens]);
}
}



print_fderives()
{
register int i;
register int j;
register unsigned *rp;

printf("\n\n\nFDERIVES\n");

for (i = ntokens; i < nsyms; i++)
{
printf("\n\n%s derives\n\n", tags[i]);
rp = fderives + i * rulesetsize;

for (j = 0; j <= nrules; j++)
if (BITISSET (rp, j))
printf(" %d\n", j);
}

fflush(stdout);
}

#endif
bison-1.22/conflicts.c100666 21570 13 36166 5364706534 12760 0ustar djmuser/* Find and resolve or report look-ahead conflicts for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifdef _AIX
#pragma alloca
#endif
#include
#include "system.h"
#include "machine.h"
#include "new.h"
#include "files.h"
#include "gram.h"
#include "state.h"


#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#ifdef HAVE_ALLOCA_H
#include
#else
#ifndef _AIX
extern char *alloca ();
#endif
#endif
#endif

extern char **tags;
extern int tokensetsize;
extern char *consistent;
extern short *accessing_symbol;
extern shifts **shift_table;
extern unsigned *LA;
extern short *LAruleno;
extern short *lookaheads;
extern int verboseflag;

void set_conflicts();
void resolve_sr_conflict();
void flush_shift();
void log_resolution();
void total_conflicts();
void count_sr_conflicts();
void count_rr_conflicts();

char any_conflicts;
char *conflicts;
errs **err_table;
int expected_conflicts;


static unsigned *shiftset;
static unsigned *lookaheadset;
static int src_total;
static int rrc_total;
static int src_count;
static int rrc_count;


void
initialize_conflicts()
{
register int i;
/* register errs *sp; JF unused */

conflicts = NEW2(nstates, char);
shiftset = NEW2(tokensetsize, unsigned);
lookaheadset = NEW2(tokensetsize, unsigned);

err_table = NEW2(nstates, errs *);

any_conflicts = 0;

for (i = 0; i < nstates; i++)
set_conflicts(i);
}


void
set_conflicts(state)
int state;
{
register int i;
register int k;
register shifts *shiftp;
register unsigned *fp2;
register unsigned *fp3;
register unsigned *fp4;
register unsigned *fp1;
register int symbol;

if (consistent[state]) return;

for (i = 0; i < tokensetsize; i++)
lookaheadset[i] = 0;

shiftp = shift_table[state];
if (shiftp)
{
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
SETBIT(lookaheadset, symbol);
}
}

k = lookaheads[state + 1];
fp4 = lookaheadset + tokensetsize;

/* loop over all rules which require lookahead in this state */
/* first check for shift-reduce conflict, and try to resolve using precedence */

for (i = lookaheads[state]; i < k; i++)
if (rprec[LAruleno[i]])
{
fp1 = LA + i * tokensetsize;
fp2 = fp1;
fp3 = lookaheadset;

while (fp3 < fp4)
{
if (*fp2++ & *fp3++)
{
resolve_sr_conflict(state, i);
break;
}
}
}

/* loop over all rules which require lookahead in this state */
/* Check for conflicts not resolved above. */

for (i = lookaheads[state]; i < k; i++)
{
fp1 = LA + i * tokensetsize;
fp2 = fp1;
fp3 = lookaheadset;

while (fp3 < fp4)
{
if (*fp2++ & *fp3++)
{
conflicts[state] = 1;
any_conflicts = 1;
}
}

fp2 = fp1;
fp3 = lookaheadset;

while (fp3 < fp4)
*fp3++ |= *fp2++;
}
}



/* Attempt to resolve shift-reduce conflict for one rule
by means of precedence declarations.
It has already been checked that the rule has a precedence.
A conflict is resolved by modifying the shift or reduce tables
so that there is no longer a conflict. */

void
resolve_sr_conflict(state, lookaheadnum)
int state;
int lookaheadnum;
{
register int i;
register int mask;
register unsigned *fp1;
register unsigned *fp2;
register int redprec;
/* Extra parens avoid errors on Ultrix 4.3. */
errs *errp = (errs *) alloca ((sizeof(errs) + ntokens * sizeof(short)));
short *errtokens = errp->errs;

/* find the rule to reduce by to get precedence of reduction */
redprec = rprec[LAruleno[lookaheadnum]];

mask = 1;
fp1 = LA + lookaheadnum * tokensetsize;
fp2 = lookaheadset;
for (i = 0; i < ntokens; i++)
{
if ((mask & *fp2 & *fp1) && sprec[i])
/* Shift-reduce conflict occurs for token number i
and it has a precedence.
The precedence of shifting is that of token i. */
{
if (sprec[i] < redprec)
{
if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
*fp2 &= ~mask; /* flush the shift for this token */
flush_shift(state, i);
}
else if (sprec[i] > redprec)
{
if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
*fp1 &= ~mask; /* flush the reduce for this token */
}
else
{
/* Matching precedence levels.
For left association, keep only the reduction.
For right association, keep only the shift.
For nonassociation, keep neither. */

switch (sassoc[i])
{

case RIGHT_ASSOC:
if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
break;

case LEFT_ASSOC:
if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
break;

case NON_ASSOC:
if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
break;
}

if (sassoc[i] != RIGHT_ASSOC)
{
*fp2 &= ~mask; /* flush the shift for this token */
flush_shift(state, i);
}
if (sassoc[i] != LEFT_ASSOC)
{
*fp1 &= ~mask; /* flush the reduce for this token */
}
if (sassoc[i] == NON_ASSOC)
{
/* Record an explicit error for this token. */
*errtokens++ = i;
}
}
}

mask <<= 1;
if (mask == 0)
{
mask = 1;
fp2++; fp1++;
}
}
errp->nerrs = errtokens - errp->errs;
if (errp->nerrs)
{
/* Some tokens have been explicitly made errors. Allocate
a permanent errs structure for this state, to record them. */
i = (char *) errtokens - (char *) errp;
err_table[state] = (errs *) xmalloc ((unsigned int)i);
bcopy (errp, err_table[state], i);
}
else
err_table[state] = 0;
}



/* turn off the shift recorded for the specified token in the specified state.
Used when we resolve a shift-reduce conflict in favor of the reduction. */

void
flush_shift(state, token)
int state;
int token;
{
register shifts *shiftp;
register int k, i;
/* register unsigned symbol; JF unused */

shiftp = shift_table[state];

if (shiftp)
{
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
(shiftp->shifts[i]) = 0;
}
}
}


void
log_resolution(state, LAno, token, resolution)
int state, LAno, token;
char *resolution;
{
fprintf(foutput,
"Conflict in state %d between rule %d and token %s resolved as %s.\n",
state, LAruleno[LAno], tags[token], resolution);
}


void
conflict_log()
{
register int i;

src_total = 0;
rrc_total = 0;

for (i = 0; i < nstates; i++)
{
if (conflicts[i])
{
count_sr_conflicts(i);
count_rr_conflicts(i);
src_total += src_count;
rrc_total += rrc_count;
}
}

total_conflicts();
}


void
verbose_conflict_log()
{
register int i;

src_total = 0;
rrc_total = 0;

for (i = 0; i < nstates; i++)
{
if (conflicts[i])
{
count_sr_conflicts(i);
count_rr_conflicts(i);
src_total += src_count;
rrc_total += rrc_count;

fprintf(foutput, "State %d contains", i);

if (src_count == 1)
fprintf(foutput, " 1 shift/reduce conflict");
else if (src_count > 1)
fprintf(foutput, " %d shift/reduce conflicts", src_count);

if (src_count > 0 && rrc_count > 0)
fprintf(foutput, " and");

if (rrc_count == 1)
fprintf(foutput, " 1 reduce/reduce conflict");
else if (rrc_count > 1)
fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);

putc('.', foutput);
putc('\n', foutput);
}
}

total_conflicts();
}


void
total_conflicts()
{
extern int fixed_outfiles;

if (src_total == expected_conflicts && rrc_total == 0)
return;

if (fixed_outfiles)
{
/* If invoked under the name `yacc', use the output format
specified by POSIX. */
fprintf(stderr, "conflicts: ");
if (src_total > 0)
fprintf(stderr, " %d shift/reduce", src_total);
if (src_total > 0 && rrc_total > 0)
fprintf(stderr, ",");
if (rrc_total > 0)
fprintf(stderr, " %d reduce/reduce", rrc_total);
putc('\n', stderr);
}
else
{
fprintf(stderr, "%s contains", infile);

if (src_total == 1)
fprintf(stderr, " 1 shift/reduce conflict");
else if (src_total > 1)
fprintf(stderr, " %d shift/reduce conflicts", src_total);

if (src_total > 0 && rrc_total > 0)
fprintf(stderr, " and");

if (rrc_total == 1)
fprintf(stderr, " 1 reduce/reduce conflict");
else if (rrc_total > 1)
fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);

putc('.', stderr);
putc('\n', stderr);
}
}


void
count_sr_conflicts(state)
int state;
{
register int i;
register int k;
register int mask;
register shifts *shiftp;
register unsigned *fp1;
register unsigned *fp2;
register unsigned *fp3;
register int symbol;

src_count = 0;

shiftp = shift_table[state];
if (!shiftp) return;

for (i = 0; i < tokensetsize; i++)
{
shiftset[i] = 0;
lookaheadset[i] = 0;
}

k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
if (! shiftp->shifts[i]) continue;
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
SETBIT(shiftset, symbol);
}

k = lookaheads[state + 1];
fp3 = lookaheadset + tokensetsize;

for (i = lookaheads[state]; i < k; i++)
{
fp1 = LA + i * tokensetsize;
fp2 = lookaheadset;

while (fp2 < fp3)
*fp2++ |= *fp1++;
}

fp1 = shiftset;
fp2 = lookaheadset;

while (fp2 < fp3)
*fp2++ &= *fp1++;

mask = 1;
fp2 = lookaheadset;
for (i = 0; i < ntokens; i++)
{
if (mask & *fp2)
src_count++;

mask <<= 1;
if (mask == 0)
{
mask = 1;
fp2++;
}
}
}


void
count_rr_conflicts(state)
int state;
{
register int i;
register int j;
register int count;
register unsigned mask;
register unsigned *baseword;
register unsigned *wordp;
register int m;
register int n;

rrc_count = 0;

m = lookaheads[state];
n = lookaheads[state + 1];

if (n - m < 2) return;

mask = 1;
baseword = LA + m * tokensetsize;
for (i = 0; i < ntokens; i++)
{
wordp = baseword;

count = 0;
for (j = m; j < n; j++)
{
if (mask & *wordp)
count++;

wordp += tokensetsize;
}

if (count >= 2) rrc_count++;

mask <<= 1;
if (mask == 0)
{
mask = 1;
baseword++;
}
}
}


void
print_reductions(state)
int state;
{
register int i;
register int j;
register int k;
register unsigned *fp1;
register unsigned *fp2;
register unsigned *fp3;
register unsigned *fp4;
register int rule;
register int symbol;
register unsigned mask;
register int m;
register int n;
register int default_LA;
register int default_rule;
register int cmax;
register int count;
register shifts *shiftp;
register errs *errp;
int nodefault = 0;

for (i = 0; i < tokensetsize; i++)
shiftset[i] = 0;

shiftp = shift_table[state];
if (shiftp)
{
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
if (! shiftp->shifts[i]) continue;
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
/* if this state has a shift for the error token,
don't use a default rule. */
if (symbol == error_token_number) nodefault = 1;
SETBIT(shiftset, symbol);
}
}

errp = err_table[state];
if (errp)
{
k = errp->nerrs;
for (i = 0; i < k; i++)
{
if (! errp->errs[i]) continue;
symbol = errp->errs[i];
SETBIT(shiftset, symbol);
}
}

m = lookaheads[state];
n = lookaheads[state + 1];

if (n - m == 1 && ! nodefault)
{
default_rule = LAruleno[m];

fp1 = LA + m * tokensetsize;
fp2 = shiftset;
fp3 = lookaheadset;
fp4 = lookaheadset + tokensetsize;

while (fp3 < fp4)
*fp3++ = *fp1++ & *fp2++;

mask = 1;
fp3 = lookaheadset;

for (i = 0; i < ntokens; i++)
{
if (mask & *fp3)
fprintf(foutput, " %-4s\t[reduce using rule %d (%s)]\n",
tags[i], default_rule, tags[rlhs[default_rule]]);

mask <<= 1;
if (mask == 0)
{
mask = 1;
fp3++;
}
}

fprintf(foutput, " $default\treduce using rule %d (%s)\n\n",
default_rule, tags[rlhs[default_rule]]);
}
else if (n - m >= 1)
{
cmax = 0;
default_LA = -1;
fp4 = lookaheadset + tokensetsize;

if (! nodefault)
for (i = m; i < n; i++)
{
fp1 = LA + i * tokensetsize;
fp2 = shiftset;
fp3 = lookaheadset;

while (fp3 < fp4)
*fp3++ = *fp1++ & ( ~ (*fp2++));

count = 0;
mask = 1;
fp3 = lookaheadset;
for (j = 0; j < ntokens; j++)
{
if (mask & *fp3)
count++;

mask <<= 1;
if (mask == 0)
{
mask = 1;
fp3++;
}
}

if (count > cmax)
{
cmax = count;
default_LA = i;
default_rule = LAruleno[i];
}

fp2 = shiftset;
fp3 = lookaheadset;

while (fp3 < fp4)
*fp2++ |= *fp3++;
}

for (i = 0; i < tokensetsize; i++)
shiftset[i] = 0;

if (shiftp)
{
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
if (! shiftp->shifts[i]) continue;
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
SETBIT(shiftset, symbol);
}
}

mask = 1;
fp1 = LA + m * tokensetsize;
fp2 = shiftset;
for (i = 0; i < ntokens; i++)
{
int defaulted = 0;

if (mask & *fp2)
count = 1;
else
count = 0;

fp3 = fp1;
for (j = m; j < n; j++)
{
if (mask & *fp3)
{
if (count == 0)
{
if (j != default_LA)
{
rule = LAruleno[j];
fprintf(foutput, " %-4s\treduce using rule %d (%s)\n",
tags[i], rule, tags[rlhs[rule]]);
}
else defaulted = 1;

count++;
}
else
{
if (defaulted)
{
rule = LAruleno[default_LA];
fprintf(foutput, " %-4s\treduce using rule %d (%s)\n",
tags[i], rule, tags[rlhs[rule]]);
defaulted = 0;
}
rule = LAruleno[j];
fprintf(foutput, " %-4s\t[reduce using rule %d (%s)]\n",
tags[i], rule, tags[rlhs[rule]]);
}
}

fp3 += tokensetsize;
}

mask <<= 1;
if (mask == 0)
{
mask = 1;
/* This used to be fp1, but I think fp2 is right
because fp2 is where the words are fetched to test with mask
in this loop. */
fp2++;
}
}

if (default_LA >= 0)
{
fprintf(foutput, " $default\treduce using rule %d (%s)\n",
default_rule, tags[rlhs[default_rule]]);
}

putc('\n', foutput);
}
}


void
finalize_conflicts()
{
FREE(conflicts);
FREE(shiftset);
FREE(lookaheadset);
}
bison-1.22/derives.c100666 12120 1 4351 5123544411 12647 0ustar rmsdaemon/* Match rules with nonterminals for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* set_derives finds, for each variable (nonterminal), which rules can derive it.
It sets up the value of derives so that
derives[i - ntokens] points to a vector of rule numbers,
terminated with -1. */

#include
#include "system.h"
#include "new.h"
#include "types.h"
#include "gram.h"


short **derives;

void
set_derives()
{
register int i;
register int lhs;
register shorts *p;
register short *q;
register shorts **dset;
register shorts *delts;

dset = NEW2(nvars, shorts *) - ntokens;
delts = NEW2(nrules + 1, shorts);

p = delts;
for (i = nrules; i > 0; i--)
{
lhs = rlhs[i];
if (lhs >= 0)
{
p->next = dset[lhs];
p->value = i;
dset[lhs] = p;
p++;
}
}

derives = NEW2(nvars, short *) - ntokens;
q = NEW2(nvars + nrules, short);

for (i = ntokens; i < nsyms; i++)
{
derives[i] = q;
p = dset[i];
while (p)
{
*q++ = p->value;
p = p->next;
}
*q++ = -1;
}

#ifdef DEBUG
print_derives();
#endif

FREE(dset + ntokens);
FREE(delts);
}

void
free_derives()
{
FREE(derives[ntokens]);
FREE(derives + ntokens);
}



#ifdef DEBUG

print_derives()
{
register int i;
register short *sp;

extern char **tags;

printf("\n\n\nDERIVES\n\n");

for (i = ntokens; i < nsyms; i++)
{
printf("%s derives", tags[i]);
for (sp = derives[i]; *sp > 0; sp++)
{
printf(" %d", *sp);
}
putchar('\n');
}

putchar('\n');
}

#endif

bison-1.22/files.c100666 21570 13 22675 5364710152 12065 0ustar djmuser/* Open and close files for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#ifdef VMS
#include
#define unlink delete
#ifndef XPFILE
#define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
#endif
#ifndef XPFILE1
#define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
#endif
#endif

#include
#include "system.h"
#include "files.h"
#include "new.h"
#include "gram.h"

FILE *finput = NULL;
FILE *foutput = NULL;
FILE *fdefines = NULL;
FILE *ftable = NULL;
FILE *fattrs = NULL;
FILE *fguard = NULL;
FILE *faction = NULL;
FILE *fparser = NULL;

/* File name specified with -o for the output file, or 0 if no -o. */
char *spec_outfile;

char *infile;
char *outfile;
char *defsfile;
char *tabfile;
char *attrsfile;
char *guardfile;
char *actfile;
char *tmpattrsfile;
char *tmptabfile;
char *tmpdefsfile;

extern char *mktemp(); /* So the compiler won't complain */
extern char *getenv();
extern void perror();
FILE *tryopen(); /* This might be a good idea */
void done();

extern char *program_name;
extern int verboseflag;
extern int definesflag;
int fixed_outfiles = 0;


char*
stringappend(string1, end1, string2)
char *string1;
int end1;
char *string2;
{
register char *ostring;
register char *cp, *cp1;
register int i;

cp = string2; i = 0;
while (*cp++) i++;

ostring = NEW2(i+end1+1, char);

cp = ostring;
cp1 = string1;
for (i = 0; i < end1; i++)
*cp++ = *cp1++;

cp1 = string2;
while (*cp++ = *cp1++) ;

return ostring;
}


/* JF this has been hacked to death. Nowaday it sets up the file names for
the output files, and opens the tmp files and the parser */
void
openfiles()
{
char *name_base;
register char *cp;
char *filename;
int base_length;
int short_base_length;

#ifdef VMS
char *tmp_base = "sys$scratch:b_";
#else
char *tmp_base = "/tmp/b.";
#endif
int tmp_len;

#ifdef MSDOS
tmp_base = getenv ("TMP");
if (tmp_base == 0)
tmp_base = "";
strlwr (infile);
#endif /* MSDOS */

tmp_len = strlen (tmp_base);

if (spec_outfile)
{
/* -o was specified. The precise -o name will be used for ftable.
For other output files, remove the ".c" or ".tab.c" suffix. */
name_base = spec_outfile;
#ifdef MSDOS
strlwr (name_base);
#endif /* MSDOS */
/* BASE_LENGTH includes ".tab" but not ".c". */
base_length = strlen (name_base);
if (!strcmp (name_base + base_length - 2, ".c"))
base_length -= 2;
/* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
short_base_length = base_length;
if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
short_base_length -= 4;
else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
short_base_length -= 4;
}
else if (spec_file_prefix)
{
/* -b was specified. Construct names from it. */
/* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
short_base_length = strlen (spec_file_prefix);
/* Count room for `.tab'. */
base_length = short_base_length + 4;
name_base = (char *) xmalloc (base_length + 1);
/* Append `.tab'. */
strcpy (name_base, spec_file_prefix);
#ifdef VMS
strcat (name_base, "_tab");
#else
strcat (name_base, ".tab");
#endif
#ifdef MSDOS
strlwr (name_base);
#endif /* MSDOS */
}
else
{
/* -o was not specified; compute output file name from input
or use y.tab.c, etc., if -y was specified. */

name_base = fixed_outfiles ? "y.y" : infile;

/* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */

base_length = strlen (name_base);
if (!strcmp (name_base + base_length - 2, ".y"))
base_length -= 2;
short_base_length = base_length;

#ifdef VMS
name_base = stringappend(name_base, short_base_length, "_tab");
#else
#ifdef MSDOS
name_base = stringappend(name_base, short_base_length, "_tab");
#else
name_base = stringappend(name_base, short_base_length, ".tab");
#endif /* not MSDOS */
#endif
base_length = short_base_length + 4;
}

finput = tryopen(infile, "r");

filename = getenv("BISON_SIMPLE");
#ifdef MSDOS
/* File doesn't exist in current directory; try in INIT directory. */
cp = getenv("INIT");
if (filename == 0 && cp != NULL)
{
filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
strcpy(filename, cp);
cp = filename + strlen(filename);
*cp++ = '/';
strcpy(cp, PFILE);
}
#endif /* MSDOS */
fparser = tryopen(filename ? filename : PFILE, "r");

if (verboseflag)
{
#ifdef MSDOS
outfile = stringappend(name_base, short_base_length, ".out");
#else
/* We used to use just .out if spec_name_prefix (-p) was used,
but that conflicts with Posix. */
outfile = stringappend(name_base, short_base_length, ".output");
#endif
foutput = tryopen(outfile, "w");
}

#ifdef MSDOS
actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX"));
#else
actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
#endif /* not MSDOS */

faction = tryopen(actfile, "w+");
fattrs = tryopen(tmpattrsfile,"w+");
ftable = tryopen(tmptabfile, "w+");

if (definesflag)
{
defsfile = stringappend(name_base, base_length, ".h");
fdefines = tryopen(tmpdefsfile, "w+");
}

#ifndef MSDOS
unlink(actfile);
unlink(tmpattrsfile);
unlink(tmptabfile);
unlink(tmpdefsfile);
#endif

/* These are opened by `done' or `open_extra_files', if at all */
if (spec_outfile)
tabfile = spec_outfile;
else
tabfile = stringappend(name_base, base_length, ".c");

#ifdef VMS
attrsfile = stringappend(name_base, short_base_length, "_stype.h");
guardfile = stringappend(name_base, short_base_length, "_guard.c");
#else
#ifdef MSDOS
attrsfile = stringappend(name_base, short_base_length, ".sth");
guardfile = stringappend(name_base, short_base_length, ".guc");
#else
attrsfile = stringappend(name_base, short_base_length, ".stype.h");
guardfile = stringappend(name_base, short_base_length, ".guard.c");
#endif /* not MSDOS */
#endif /* not VMS */
}



/* open the output files needed only for the semantic parser.
This is done when %semantic_parser is seen in the declarations section. */

void
open_extra_files()
{
FILE *ftmp;
int c;
char *filename, *cp;

fclose(fparser);

filename = (char *) getenv ("BISON_HAIRY");
#ifdef MSDOS
/* File doesn't exist in current directory; try in INIT directory. */
cp = getenv("INIT");
if (filename == 0 && cp != NULL)
{
filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
strcpy(filename, cp);
cp = filename + strlen(filename);
*cp++ = '/';
strcpy(cp, PFILE1);
}
#endif
fparser= tryopen(filename ? filename : PFILE1, "r");

/* JF change from inline attrs file to separate one */
ftmp = tryopen(attrsfile, "w");
rewind(fattrs);
while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
putc(c,ftmp);
fclose(fattrs);
fattrs=ftmp;

fguard = tryopen(guardfile, "w");

}

/* JF to make file opening easier. This func tries to open file
NAME with mode MODE, and prints an error message if it fails. */
FILE *
tryopen(name, mode)
char *name;
char *mode;
{
FILE *ptr;

ptr = fopen(name, mode);
if (ptr == NULL)
{
fprintf(stderr, "%s: ", program_name);
perror(name);
done(2);
}
return ptr;
}

void
done(k)
int k;
{
if (faction)
fclose(faction);

if (fattrs)
fclose(fattrs);

if (fguard)
fclose(fguard);

if (finput)
fclose(finput);

if (fparser)
fclose(fparser);

if (foutput)
fclose(foutput);

/* JF write out the output file */
if (k == 0 && ftable)
{
FILE *ftmp;
register int c;

ftmp=tryopen(tabfile, "w");
rewind(ftable);
while((c=getc(ftable)) != EOF)
putc(c,ftmp);
fclose(ftmp);
fclose(ftable);

if (definesflag)
{
ftmp = tryopen(defsfile, "w");
fflush(fdefines);
rewind(fdefines);
while((c=getc(fdefines)) != EOF)
putc(c,ftmp);
fclose(ftmp);
fclose(fdefines);
}
}

#ifdef VMS
if (faction)
delete(actfile);
if (fattrs)
delete(tmpattrsfile);
if (ftable)
delete(tmptabfile);
if (k==0) sys$exit(SS$_NORMAL);
sys$exit(SS$_ABORT);
#else
#ifdef MSDOS
if (actfile) unlink(actfile);
if (tmpattrsfile) unlink(tmpattrsfile);
if (tmptabfile) unlink(tmptabfile);
if (tmpdefsfile) unlink(tmpdefsfile);
#endif /* MSDOS */
exit(k);
#endif /* not VMS */
}
bison-1.22/getargs.c100666 21570 1 6042 5413126133 12622 0ustar djmdaemon/* Parse command line arguments for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include "getopt.h"
#include "system.h"
#include "files.h"

int verboseflag;
int definesflag;
int debugflag;
int nolinesflag;
char *spec_name_prefix; /* for -p. */
char *spec_file_prefix; /* for -b. */
extern int fixed_outfiles;/* for -y */

extern char *program_name;
extern char *version_string;

extern void fatal();

struct option longopts[] =
{
{"debug", 0, &debugflag, 1},
{"defines", 0, &definesflag, 1},
{"file-prefix", 1, 0, 'b'},
{"fixed-output-files", 0, &fixed_outfiles, 1},
{"help", 0, 0, 'h'},
{"name-prefix", 1, 0, 'a'},
{"no-lines", 0, &nolinesflag, 1},
{"output", 1, 0, 'o'},
{"output-file", 1, 0, 'o'},
{"verbose", 0, &verboseflag, 1},
{"version", 0, 0, 'V'},
{"yacc", 0, &fixed_outfiles, 1},
{0, 0, 0, 0}
};

void
usage (stream)
FILE *stream;
{
fprintf (stream, "\
Usage: %s [-dhltvyV] [-b file-prefix] [-o outfile] [-p name-prefix]\n\
[--debug] [--defines] [--fixed-output-files] [--no-lines]\n\
[--verbose] [--version] [--help] [--yacc]\n\
[--file-prefix=prefix] [--name-prefix=prefix]\n\
[--output=outfile] grammar-file\n",
program_name);
}

void
getargs(argc, argv)
int argc;
char *argv[];
{
register int c;

verboseflag = 0;
definesflag = 0;
debugflag = 0;
fixed_outfiles = 0;

while ((c = getopt_long (argc, argv, "yvdhltVo:b:p:", longopts, (int *)0))
!= EOF)
{
switch (c)
{
case 0:
/* Certain long options cause getopt_long to return 0. */
break;

case 'y':
fixed_outfiles = 1;
break;

case 'h':
usage (stdout);
exit (0);

case 'V':
printf ("%s", version_string);
exit (0);

case 'v':
verboseflag = 1;
break;

case 'd':
definesflag = 1;
break;

case 'l':
nolinesflag = 1;
break;

case 't':
debugflag = 1;
break;

case 'o':
spec_outfile = optarg;
break;

case 'b':
spec_file_prefix = optarg;
break;

case 'p':
spec_name_prefix = optarg;
break;

default:
usage (stderr);
exit (1);
}
}

if (optind == argc)
{
fprintf(stderr, "%s: no grammar file given\n", program_name);
exit(1);
}
if (optind < argc - 1)
fprintf(stderr, "%s: warning: extra arguments ignored\n", program_name);

infile = argv[optind];
}
bison-1.22/gram.c100666 21570 1 2531 5104660516 12120 0ustar djmdaemon/* Allocate input grammar variables for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* comments for these variables are in gram.h */

int nitems;
int nrules;
int nsyms;
int ntokens;
int nvars;

short *ritem;
short *rlhs;
short *rrhs;
short *rprec;
short *rprecsym;
short *sprec;
short *rassoc;
short *sassoc;
short *token_translations;
short *rline;

int start_symbol;

int translations;

int max_user_token_number;

int semantic_parser;

int pure_parser;

int error_token_number;

/* This is to avoid linker problems which occur on VMS when using GCC,
when the file in question contains data definitions only. */

void
dummy()
{
}
bison-1.22/lalr.c100666 12120 1 33062 5124504050 12155 0ustar rmsdaemon/* Compute look-ahead criteria for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* Compute how to make the finite state machine deterministic;
find which rules need lookahead in each state, and which lookahead tokens they accept.

lalr(), the entry point, builds these data structures:

goto_map, from_state and to_state
record each shift transition which accepts a variable (a nonterminal).
ngotos is the number of such transitions.
from_state[t] is the state number which a transition leads from
and to_state[t] is the state number it leads to.
All the transitions that accept a particular variable are grouped together and
goto_map[i - ntokens] is the index in from_state and to_state of the first of them.

consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.

LAruleno is a vector which records the rules that need lookahead in various states.
The elements of LAruleno that apply to state s are those from
lookaheads[s] through lookaheads[s+1]-1.
Each element of LAruleno is a rule number.

If lr is the length of LAruleno, then a number from 0 to lr-1
can specify both a rule and a state where the rule might be applied.

LA is a lr by ntokens matrix of bits.
LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
when the next token is symbol i.
If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
*/

#include
#include "system.h"
#include "machine.h"
#include "types.h"
#include "state.h"
#include "new.h"
#include "gram.h"


extern short **derives;
extern char *nullable;


int tokensetsize;
short *lookaheads;
short *LAruleno;
unsigned *LA;
short *accessing_symbol;
char *consistent;
core **state_table;
shifts **shift_table;
reductions **reduction_table;
short *goto_map;
short *from_state;
short *to_state;

short **transpose();
void set_state_table();
void set_accessing_symbol();
void set_shift_table();
void set_reduction_table();
void set_maxrhs();
void initialize_LA();
void set_goto_map();
void initialize_F();
void build_relations();
void add_lookback_edge();
void compute_FOLLOWS();
void compute_lookaheads();
void digraph();
void traverse();

extern void toomany();
extern void berror();

static int infinity;
static int maxrhs;
static int ngotos;
static unsigned *F;
static short **includes;
static shorts **lookback;
static short **R;
static short *INDEX;
static short *VERTICES;
static int top;


void
lalr()
{
tokensetsize = WORDSIZE(ntokens);

set_state_table();
set_accessing_symbol();
set_shift_table();
set_reduction_table();
set_maxrhs();
initialize_LA();
set_goto_map();
initialize_F();
build_relations();
compute_FOLLOWS();
compute_lookaheads();
}


void
set_state_table()
{
register core *sp;

state_table = NEW2(nstates, core *);

for (sp = first_state; sp; sp = sp->next)
state_table[sp->number] = sp;
}


void
set_accessing_symbol()
{
register core *sp;

accessing_symbol = NEW2(nstates, short);

for (sp = first_state; sp; sp = sp->next)
accessing_symbol[sp->number] = sp->accessing_symbol;
}


void
set_shift_table()
{
register shifts *sp;

shift_table = NEW2(nstates, shifts *);

for (sp = first_shift; sp; sp = sp->next)
shift_table[sp->number] = sp;
}


void
set_reduction_table()
{
register reductions *rp;

reduction_table = NEW2(nstates, reductions *);

for (rp = first_reduction; rp; rp = rp->next)
reduction_table[rp->number] = rp;
}


void
set_maxrhs()
{
register short *itemp;
register int length;
register int max;

length = 0;
max = 0;
for (itemp = ritem; *itemp; itemp++)
{
if (*itemp > 0)
{
length++;
}
else
{
if (length > max) max = length;
length = 0;
}
}

maxrhs = max;
}


void
initialize_LA()
{
register int i;
register int j;
register int count;
register reductions *rp;
register shifts *sp;
register short *np;

consistent = NEW2(nstates, char);
lookaheads = NEW2(nstates + 1, short);

count = 0;
for (i = 0; i < nstates; i++)
{
register int k;

lookaheads[i] = count;

rp = reduction_table[i];
sp = shift_table[i];
if (rp && (rp->nreds > 1
|| (sp && ! ISVAR(accessing_symbol[sp->shifts[0]]))))
count += rp->nreds;
else
consistent[i] = 1;

if (sp)
for (k = 0; k < sp->nshifts; k++)
{
if (accessing_symbol[sp->shifts[k]] == error_token_number)
{
consistent[i] = 0;
break;
}
}
}

lookaheads[nstates] = count;

if (count == 0)
{
LA = NEW2(1 * tokensetsize, unsigned);
LAruleno = NEW2(1, short);
lookback = NEW2(1, shorts *);
}
else
{
LA = NEW2(count * tokensetsize, unsigned);
LAruleno = NEW2(count, short);
lookback = NEW2(count, shorts *);
}

np = LAruleno;
for (i = 0; i < nstates; i++)
{
if (!consistent[i])
{
if (rp = reduction_table[i])
for (j = 0; j < rp->nreds; j++)
*np++ = rp->rules[j];
}
}
}


void
set_goto_map()
{
register shifts *sp;
register int i;
register int symbol;
register int k;
register short *temp_map;
register int state2;
register int state1;

goto_map = NEW2(nvars + 1, short) - ntokens;
temp_map = NEW2(nvars + 1, short) - ntokens;

ngotos = 0;
for (sp = first_shift; sp; sp = sp->next)
{
for (i = sp->nshifts - 1; i >= 0; i--)
{
symbol = accessing_symbol[sp->shifts[i]];

if (ISTOKEN(symbol)) break;

if (ngotos == MAXSHORT)
toomany("gotos");

ngotos++;
goto_map[symbol]++;
}
}

k = 0;
for (i = ntokens; i < nsyms; i++)
{
temp_map[i] = k;
k += goto_map[i];
}

for (i = ntokens; i < nsyms; i++)
goto_map[i] = temp_map[i];

goto_map[nsyms] = ngotos;
temp_map[nsyms] = ngotos;

from_state = NEW2(ngotos, short);
to_state = NEW2(ngotos, short);

for (sp = first_shift; sp; sp = sp->next)
{
state1 = sp->number;
for (i = sp->nshifts - 1; i >= 0; i--)
{
state2 = sp->shifts[i];
symbol = accessing_symbol[state2];

if (ISTOKEN(symbol)) break;

k = temp_map[symbol]++;
from_state[k] = state1;
to_state[k] = state2;
}
}

FREE(temp_map + ntokens);
}



/* Map_goto maps a state/symbol pair into its numeric representation. */

int
map_goto(state, symbol)
int state;
int symbol;
{
register int high;
register int low;
register int middle;
register int s;

low = goto_map[symbol];
high = goto_map[symbol + 1] - 1;

while (low <= high)
{
middle = (low + high) / 2;
s = from_state[middle];
if (s == state)
return (middle);
else if (s < state)
low = middle + 1;
else
high = middle - 1;
}

berror("map_goto");
/* NOTREACHED */
return 0;
}


void
initialize_F()
{
register int i;
register int j;
register int k;
register shifts *sp;
register short *edge;
register unsigned *rowp;
register short *rp;
register short **reads;
register int nedges;
register int stateno;
register int symbol;
register int nwords;

nwords = ngotos * tokensetsize;
F = NEW2(nwords, unsigned);

reads = NEW2(ngotos, short *);
edge = NEW2(ngotos + 1, short);
nedges = 0;

rowp = F;
for (i = 0; i < ngotos; i++)
{
stateno = to_state[i];
sp = shift_table[stateno];

if (sp)
{
k = sp->nshifts;

for (j = 0; j < k; j++)
{
symbol = accessing_symbol[sp->shifts[j]];
if (ISVAR(symbol))
break;
SETBIT(rowp, symbol);
}

for (; j < k; j++)
{
symbol = accessing_symbol[sp->shifts[j]];
if (nullable[symbol])
edge[nedges++] = map_goto(stateno, symbol);
}

if (nedges)
{
reads[i] = rp = NEW2(nedges + 1, short);

for (j = 0; j < nedges; j++)
rp[j] = edge[j];

rp[nedges] = -1;
nedges = 0;
}
}

rowp += tokensetsize;
}

digraph(reads);

for (i = 0; i < ngotos; i++)
{
if (reads[i])
FREE(reads[i]);
}

FREE(reads);
FREE(edge);
}


void
build_relations()
{
register int i;
register int j;
register int k;
register short *rulep;
register short *rp;
register shifts *sp;
register int length;
register int nedges;
register int done;
register int state1;
register int stateno;
register int symbol1;
register int symbol2;
register short *shortp;
register short *edge;
register short *states;
register short **new_includes;

includes = NEW2(ngotos, short *);
edge = NEW2(ngotos + 1, short);
states = NEW2(maxrhs + 1, short);

for (i = 0; i < ngotos; i++)
{
nedges = 0;
state1 = from_state[i];
symbol1 = accessing_symbol[to_state[i]];

for (rulep = derives[symbol1]; *rulep > 0; rulep++)
{
length = 1;
states[0] = state1;
stateno = state1;

for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
{
symbol2 = *rp;
sp = shift_table[stateno];
k = sp->nshifts;

for (j = 0; j < k; j++)
{
stateno = sp->shifts[j];
if (accessing_symbol[stateno] == symbol2) break;
}

states[length++] = stateno;
}

if (!consistent[stateno])
add_lookback_edge(stateno, *rulep, i);

length--;
done = 0;
while (!done)
{
done = 1;
rp--;
/* JF added rp>=ritem && I hope to god its right! */
if (rp>=ritem && ISVAR(*rp))
{
stateno = states[--length];
edge[nedges++] = map_goto(stateno, *rp);
if (nullable[*rp]) done = 0;
}
}
}

if (nedges)
{
includes[i] = shortp = NEW2(nedges + 1, short);
for (j = 0; j < nedges; j++)
shortp[j] = edge[j];
shortp[nedges] = -1;
}
}

new_includes = transpose(includes, ngotos);

for (i = 0; i < ngotos; i++)
if (includes[i])
FREE(includes[i]);

FREE(includes);

includes = new_includes;

FREE(edge);
FREE(states);
}


void
add_lookback_edge(stateno, ruleno, gotono)
int stateno;
int ruleno;
int gotono;
{
register int i;
register int k;
register int found;
register shorts *sp;

i = lookaheads[stateno];
k = lookaheads[stateno + 1];
found = 0;
while (!found && i < k)
{
if (LAruleno[i] == ruleno)
found = 1;
else
i++;
}

if (found == 0)
berror("add_lookback_edge");

sp = NEW(shorts);
sp->next = lookback[i];
sp->value = gotono;
lookback[i] = sp;
}



short **
transpose(R_arg, n)
short **R_arg;
int n;
{
register short **new_R;
register short **temp_R;
register short *nedges;
register short *sp;
register int i;
register int k;

nedges = NEW2(n, short);

for (i = 0; i < n; i++)
{
sp = R_arg[i];
if (sp)
{
while (*sp >= 0)
nedges[*sp++]++;
}
}

new_R = NEW2(n, short *);
temp_R = NEW2(n, short *);

for (i = 0; i < n; i++)
{
k = nedges[i];
if (k > 0)
{
sp = NEW2(k + 1, short);
new_R[i] = sp;
temp_R[i] = sp;
sp[k] = -1;
}
}

FREE(nedges);

for (i = 0; i < n; i++)
{
sp = R_arg[i];
if (sp)
{
while (*sp >= 0)
*temp_R[*sp++]++ = i;
}
}

FREE(temp_R);

return (new_R);
}


void
compute_FOLLOWS()
{
register int i;

digraph(includes);

for (i = 0; i < ngotos; i++)
{
if (includes[i]) FREE(includes[i]);
}

FREE(includes);
}


void
compute_lookaheads()
{
register int i;
register int n;
register unsigned *fp1;
register unsigned *fp2;
register unsigned *fp3;
register shorts *sp;
register unsigned *rowp;
/* register short *rulep; JF unused */
/* register int count; JF unused */
register shorts *sptmp;/* JF */

rowp = LA;
n = lookaheads[nstates];
for (i = 0; i < n; i++)
{
fp3 = rowp + tokensetsize;
for (sp = lookback[i]; sp; sp = sp->next)
{
fp1 = rowp;
fp2 = F + tokensetsize * sp->value;
while (fp1 < fp3)
*fp1++ |= *fp2++;
}

rowp = fp3;
}

for (i = 0; i < n; i++)
{/* JF removed ref to freed storage */
for (sp = lookback[i]; sp; sp = sptmp) {
sptmp=sp->next;
FREE(sp);
}
}

FREE(lookback);
FREE(F);
}


void
digraph(relation)
short **relation;
{
register int i;

infinity = ngotos + 2;
INDEX = NEW2(ngotos + 1, short);
VERTICES = NEW2(ngotos + 1, short);
top = 0;

R = relation;

for (i = 0; i < ngotos; i++)
INDEX[i] = 0;

for (i = 0; i < ngotos; i++)
{
if (INDEX[i] == 0 && R[i])
traverse(i);
}

FREE(INDEX);
FREE(VERTICES);
}


void
traverse(i)
register int i;
{
register unsigned *fp1;
register unsigned *fp2;
register unsigned *fp3;
register int j;
register short *rp;

int height;
unsigned *base;

VERTICES[++top] = i;
INDEX[i] = height = top;

base = F + i * tokensetsize;
fp3 = base + tokensetsize;

rp = R[i];
if (rp)
{
while ((j = *rp++) >= 0)
{
if (INDEX[j] == 0)
traverse(j);

if (INDEX[i] > INDEX[j])
INDEX[i] = INDEX[j];

fp1 = base;
fp2 = F + j * tokensetsize;

while (fp1 < fp3)
*fp1++ |= *fp2++;
}
}

if (INDEX[i] == height)
{
for (;;)
{
j = VERTICES[top--];
INDEX[j] = infinity;

if (i == j)
break;

fp1 = base;
fp2 = F + j * tokensetsize;

while (fp1 < fp3)
*fp2++ = *fp1++;
}
}
}
bison-1.22/lex.c100666 21570 13 23243 5364707004 11545 0ustar djmuser/* Token-reader for Bison's input parser,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/*
lex() is the entry point. It is called from reader.c.
It returns one of the token-type codes defined in lex.h.
When an identifier is seen, the code IDENTIFIER is returned
and the name is looked up in the symbol table using symtab.c;
symval is set to a pointer to the entry found. */

#include
#include
#include "system.h"
#include "files.h"
#include "symtab.h"
#include "lex.h"
#include "new.h"


extern int lineno;
extern int translations;

int parse_percent_token();

extern void fatals();
extern void fatal();

/* Buffer for storing the current token. */
char *token_buffer;

/* Allocated size of token_buffer, not including space for terminator. */
static int maxtoken;

bucket *symval;
int numval;

static int unlexed; /* these two describe a token to be reread */
static bucket *unlexed_symval; /* by the next call to lex */


void
init_lex()
{
maxtoken = 100;
token_buffer = NEW2 (maxtoken + 1, char);
unlexed = -1;
}


static char *
grow_token_buffer (p)
char *p;
{
int offset = p - token_buffer;
maxtoken *= 2;
token_buffer = (char *) xrealloc(token_buffer, maxtoken + 1);
return token_buffer + offset;
}


int
skip_white_space()
{
register int c;
register int inside;

c = getc(finput);

for (;;)
{
int cplus_comment;

switch (c)
{
case '/':
c = getc(finput);
if (c != '*' && c != '/')
fatals("unexpected `/%c' found",c);
cplus_comment = (c == '/');

c = getc(finput);

inside = 1;
while (inside)
{
if (!cplus_comment && c == '*')
{
while (c == '*')
c = getc(finput);

if (c == '/')
{
inside = 0;
c = getc(finput);
}
}
else if (c == '\n')
{
lineno++;
if (cplus_comment)
inside = 0;
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment");
else
c = getc(finput);
}

break;

case '\n':
lineno++;

case ' ':
case '\t':
case '\f':
c = getc(finput);
break;

default:
return (c);
}
}
}


void
unlex(token)
int token;
{
unlexed = token;
unlexed_symval = symval;
}



int
lex()
{
register int c;
register char *p;

if (unlexed >= 0)
{
symval = unlexed_symval;
c = unlexed;
unlexed = -1;
return (c);
}

c = skip_white_space();

switch (c)
{
case EOF:
return (ENDFILE);

case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
case '.': case '_':
p = token_buffer;
while (isalnum(c) || c == '_' || c == '.')
{
if (p == token_buffer + maxtoken)
p = grow_token_buffer(p);

*p++ = c;
c = getc(finput);
}

*p = 0;
ungetc(c, finput);
symval = getsym(token_buffer);
return (IDENTIFIER);

case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
numval = 0;

while (isdigit(c))
{
numval = numval*10 + c - '0';
c = getc(finput);
}
ungetc(c, finput);
return (NUMBER);
}

case '\'':
translations = -1;

/* parse the literal token and compute character code in code */

c = getc(finput);
{
register int code = 0;

if (c == '\\')
{
c = getc(finput);

if (c <= '7' && c >= '0')
{
while (c <= '7' && c >= '0')
{
code = (code * 8) + (c - '0');
c = getc(finput);
if (code >= 256 || code < 0)
fatals("malformatted literal token `\\%03o'", code);
}
}
else
{
if (c == 't')
code = '\t';
else if (c == 'n')
code = '\n';
else if (c == 'a')
code = '\007';
else if (c == 'r')
code = '\r';
else if (c == 'f')
code = '\f';
else if (c == 'b')
code = '\b';
else if (c == 'v')
code = 013;
else if (c == 'x')
{
c = getc(finput);
while ((c <= '9' && c >= '0')
|| (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
{
code *= 16;
if (c <= '9' && c >= '0')
code += c - '0';
else if (c >= 'a' && c <= 'z')
code += c - 'a' + 10;
else if (c >= 'A' && c <= 'Z')
code += c - 'A' + 10;
if (code >= 256 || code<0)/* JF this said if(c>=128) */
fatals("malformatted literal token `\\x%x'",code);
c = getc(finput);
}
ungetc(c, finput);
}
else if (c == '\\')
code = '\\';
else if (c == '\'')
code = '\'';
else if (c == '\"') /* JF this is a good idea */
code = '\"';
else
{
if (c >= 040 && c <= 0177)
fatals ("unknown escape sequence `\\%c'", c);
else
fatals ("unknown escape sequence: `\\' followed by char code 0x%x", c);
}

c = getc(finput);
}
}
else
{
code = c;
c = getc(finput);
}
if (c != '\'')
fatal("multicharacter literal tokens not supported");

/* now fill token_buffer with the canonical name for this character
as a literal token. Do not use what the user typed,
so that '\012' and '\n' can be interchangeable. */

p = token_buffer;
*p++ = '\'';
if (code == '\\')
{
*p++ = '\\';
*p++ = '\\';
}
else if (code == '\'')
{
*p++ = '\\';
*p++ = '\'';
}
else if (code >= 040 && code != 0177)
*p++ = code;
else if (code == '\t')
{
*p++ = '\\';
*p++ = 't';
}
else if (code == '\n')
{
*p++ = '\\';
*p++ = 'n';
}
else if (code == '\r')
{
*p++ = '\\';
*p++ = 'r';
}
else if (code == '\v')
{
*p++ = '\\';
*p++ = 'v';
}
else if (code == '\b')
{
*p++ = '\\';
*p++ = 'b';
}
else if (code == '\f')
{
*p++ = '\\';
*p++ = 'f';
}
else
{
*p++ = code / 0100 + '0';
*p++ = ((code / 010) & 07) + '0';
*p++ = (code & 07) + '0';
}
*p++ = '\'';
*p = 0;
symval = getsym(token_buffer);
symval->class = STOKEN;
if (! symval->user_token_number)
symval->user_token_number = code;
return (IDENTIFIER);
}

case ',':
return (COMMA);

case ':':
return (COLON);

case ';':
return (SEMICOLON);

case '|':
return (BAR);

case '{':
return (LEFT_CURLY);

case '=':
do
{
c = getc(finput);
if (c == '\n') lineno++;
}
while(c==' ' || c=='\n' || c=='\t');

if (c == '{')
return(LEFT_CURLY);
else
{
ungetc(c, finput);
return(ILLEGAL);
}

case '<':
p = token_buffer;
c = getc(finput);
while (c != '>')
{
if (c == '\n' || c == EOF)
fatal("unterminated type name");

if (p == token_buffer + maxtoken)
p = grow_token_buffer(p);

*p++ = c;
c = getc(finput);
}
*p = 0;
return (TYPENAME);


case '%':
return (parse_percent_token());

default:
return (ILLEGAL);
}
}


/* parse a token which starts with %. Assumes the % has already been read and discarded. */

int
parse_percent_token ()
{
register int c;
register char *p;

p = token_buffer;
c = getc(finput);

switch (c)
{
case '%':
return (TWO_PERCENTS);

case '{':
return (PERCENT_LEFT_CURLY);

case '<':
return (LEFT);

case '>':
return (RIGHT);

case '2':
return (NONASSOC);

case '0':
return (TOKEN);

case '=':
return (PREC);
}
if (!isalpha(c))
return (ILLEGAL);

while (isalpha(c) || c == '_')
{
if (p == token_buffer + maxtoken)
p = grow_token_buffer(p);

*p++ = c;
c = getc(finput);
}

ungetc(c, finput);

*p = 0;

if (strcmp(token_buffer, "token") == 0
||
strcmp(token_buffer, "term") == 0)
return (TOKEN);
else if (strcmp(token_buffer, "nterm") == 0)
return (NTERM);
else if (strcmp(token_buffer, "type") == 0)
return (TYPE);
else if (strcmp(token_buffer, "guard") == 0)
return (GUARD);
else if (strcmp(token_buffer, "union") == 0)
return (UNION);
else if (strcmp(token_buffer, "expect") == 0)
return (EXPECT);
else if (strcmp(token_buffer, "start") == 0)
return (START);
else if (strcmp(token_buffer, "left") == 0)
return (LEFT);
else if (strcmp(token_buffer, "right") == 0)
return (RIGHT);
else if (strcmp(token_buffer, "nonassoc") == 0
||
strcmp(token_buffer, "binary") == 0)
return (NONASSOC);
else if (strcmp(token_buffer, "semantic_parser") == 0)
return (SEMANTIC_PARSER);
else if (strcmp(token_buffer, "pure_parser") == 0)
return (PURE_PARSER);
else if (strcmp(token_buffer, "prec") == 0)
return (PREC);
else return (ILLEGAL);
}
bison-1.22/main.c100666 12120 1 6752 5363334621 12147 0ustar rmsdaemon/* Top level entry point of bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include "system.h"
#include "machine.h" /* JF for MAXSHORT */

extern int lineno;
extern int verboseflag;

/* Nonzero means failure has been detected; don't write a parser file. */
int failure;

/* The name this program was run with, for messages. */
char *program_name;

extern void getargs(), openfiles(), reader(), reduce_grammar();
extern void set_derives(), set_nullable(), generate_states();
extern void lalr(), initialize_conflicts(), verbose(), terse();
extern void output(), done();


/* VMS complained about using `int'. */
int
main(argc, argv)
int argc;
char *argv[];
{
program_name = argv[0];
failure = 0;
lineno = 0;
getargs(argc, argv);
openfiles();

/* read the input. Copy some parts of it to fguard, faction, ftable and fattrs.
In file reader.c.
The other parts are recorded in the grammar; see gram.h. */
reader();

/* find useless nonterminals and productions and reduce the grammar. In
file reduce.c */
reduce_grammar();

/* record other info about the grammar. In files derives and nullable. */
set_derives();
set_nullable();

/* convert to nondeterministic finite state machine. In file LR0.
See state.h for more info. */
generate_states();

/* make it deterministic. In file lalr. */
lalr();

/* Find and record any conflicts: places where one token of lookahead is not
enough to disambiguate the parsing. In file conflicts.
Currently this does not do anything to resolve them;
the trivial form of conflict resolution that exists is done in output. */
initialize_conflicts();

/* print information about results, if requested. In file print. */
if (verboseflag)
verbose();
else
terse();

/* output the tables and the parser to ftable. In file output. */
output();
done(failure);
}

/* functions to report errors which prevent a parser from being generated */

void
fatal(s)
char *s;
{
extern char *infile;

if (infile == 0)
fprintf(stderr, "fatal error: %s\n", s);
else
fprintf(stderr, "\"%s\", line %d: %s\n", infile, lineno, s);
done(1);
}


/* JF changed to accept/deal with variable args.
DO NOT change this to use varargs. It will appear to work
but will break on systems that don't have the necessary library
functions. This is the ONLY safe way to write such a function. */
/*VARARGS1*/

void
fatals(fmt,x1,x2,x3,x4,x5,x6,x7,x8)
char *fmt;
{
char buffer[200];

sprintf(buffer, fmt, x1,x2,x3,x4,x5,x6,x7,x8);
fatal(buffer);
}


void
toomany(s)
char *s;
{
char buffer[200];

/* JF new msg */
sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s);
fatal(buffer);
}


void
berror(s)
char *s;
{
fprintf(stderr, "internal error, %s\n", s);
abort();
}
bison-1.22/nullable.c100666 12120 1 5323 5123544514 13010 0ustar rmsdaemon/* Part of the bison parser generator,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* set up nullable, a vector saying which nonterminals can expand into the null string.
nullable[i - ntokens] is nonzero if symbol i can do so. */

#include
#include "system.h"
#include "types.h"
#include "gram.h"
#include "new.h"


char *nullable;


void
set_nullable()
{
register short *r;
register short *s1;
register short *s2;
register int ruleno;
register int symbol;
register shorts *p;

short *squeue;
short *rcount;
shorts **rsets;
shorts *relts;
char any_tokens;
short *r1;

#ifdef TRACE
fprintf(stderr, "Entering set_nullable");
#endif

nullable = NEW2(nvars, char) - ntokens;

squeue = NEW2(nvars, short);
s1 = s2 = squeue;

rcount = NEW2(nrules + 1, short);
rsets = NEW2(nvars, shorts *) - ntokens;
/* This is said to be more elements than we actually use.
Supposedly nitems - nrules is enough.
But why take the risk? */
relts = NEW2(nitems + nvars + 1, shorts);
p = relts;

r = ritem;
while (*r)
{
if (*r < 0)
{
symbol = rlhs[-(*r++)];
if (symbol >= 0 && !nullable[symbol])
{
nullable[symbol] = 1;
*s2++ = symbol;
}
}
else
{
r1 = r;
any_tokens = 0;
for (symbol = *r++; symbol > 0; symbol = *r++)
{
if (ISTOKEN(symbol))
any_tokens = 1;
}

if (!any_tokens)
{
ruleno = -symbol;
r = r1;
for (symbol = *r++; symbol > 0; symbol = *r++)
{
rcount[ruleno]++;
p->next = rsets[symbol];
p->value = ruleno;
rsets[symbol] = p;
p++;
}
}
}
}

while (s1 < s2)
{
p = rsets[*s1++];
while (p)
{
ruleno = p->value;
p = p->next;
if (--rcount[ruleno] == 0)
{
symbol = rlhs[ruleno];
if (symbol >= 0 && !nullable[symbol])
{
nullable[symbol] = 1;
*s2++ = symbol;
}
}
}
}

FREE(squeue);
FREE(rcount);
FREE(rsets + ntokens);
FREE(relts);
}


void
free_nullable()
{
FREE(nullable + ntokens);
}
bison-1.22/output.c100666 12120 1 64050 5354216332 12574 0ustar rmsdaemon/* Output the generated parsing program for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* functions to output parsing data to various files. Entries are:

output_headers ()

Output constant strings to the beginning of certain files.

output_trailers()

Output constant strings to the ends of certain files.

output ()

Output the parsing tables and the parser code to ftable.

The parser tables consist of these tables.
Starred ones needed only for the semantic parser.

yytranslate = vector mapping yylex's token numbers into bison's token numbers.

yytname = vector of string-names indexed by bison token number

yyrline = vector of line-numbers of all rules. For yydebug printouts.

yyrhs = vector of items of all rules.
This is exactly what ritems contains. For yydebug and for semantic
parser.

yyprhs[r] = index in yyrhs of first item for rule r.

yyr1[r] = symbol number of symbol that rule r derives.

yyr2[r] = number of symbols composing right hand side of rule r.

* yystos[s] = the symbol number of the symbol that leads to state s.

yydefact[s] = default rule to reduce with in state s,
when yytable doesn't specify something else to do.
Zero means the default is an error.

yydefgoto[i] = default state to go to after a reduction of a rule that
generates variable ntokens + i, except when yytable
specifies something else to do.

yypact[s] = index in yytable of the portion describing state s.
The lookahead token's type is used to index that portion
to find out what to do.

If the value in yytable is positive,
we shift the token and go to that state.

If the value is negative, it is minus a rule number to reduce by.

If the value is zero, the default action from yydefact[s] is used.

yypgoto[i] = the index in yytable of the portion describing
what to do after reducing a rule that derives variable i + ntokens.
This portion is indexed by the parser state number
as of before the text for this nonterminal was read.
The value from yytable is the state to go to.

yytable = a vector filled with portions for different uses,
found via yypact and yypgoto.

yycheck = a vector indexed in parallel with yytable.
It indicates, in a roundabout way, the bounds of the
portion you are trying to examine.

Suppose that the portion of yytable starts at index p
and the index to be examined within the portion is i.
Then if yycheck[p+i] != i, i is outside the bounds
of what is actually allocated, and the default
(from yydefact or yydefgoto) should be used.
Otherwise, yytable[p+i] should be used.

YYFINAL = the state number of the termination state.
YYFLAG = most negative short int. Used to flag ??
YYNTBASE = ntokens.

*/

#include
#include "system.h"
#include "machine.h"
#include "new.h"
#include "files.h"
#include "gram.h"
#include "state.h"


extern int debugflag;
extern int nolinesflag;

extern char **tags;
extern int tokensetsize;
extern int final_state;
extern core **state_table;
extern shifts **shift_table;
extern errs **err_table;
extern reductions **reduction_table;
extern short *accessing_symbol;
extern unsigned *LA;
extern short *LAruleno;
extern short *lookaheads;
extern char *consistent;
extern short *goto_map;
extern short *from_state;
extern short *to_state;

void output_token_translations();
void output_gram();
void output_stos();
void output_rule_data();
void output_defines();
void output_actions();
void token_actions();
void save_row();
void goto_actions();
void save_column();
void sort_actions();
void pack_table();
void output_base();
void output_table();
void output_check();
void output_parser();
void output_program();
void free_itemset();
void free_shifts();
void free_reductions();
void free_itemsets();
int action_row();
int default_goto();
int matching_state();
int pack_vector();

extern void berror();
extern void fatals();

static int nvectors;
static int nentries;
static short **froms;
static short **tos;
static short *tally;
static short *width;
static short *actrow;
static short *state_count;
static short *order;
static short *base;
static short *pos;
static short *table;
static short *check;
static int lowzero;
static int high;



#define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
register YYLTYPE *yylsp;\n\
{\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"

#define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
\nextern int yychar;\
yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
register YYLTYPE *yylsp;\n{\n switch (n)\n{"

#define ACTSTR_SIMPLE "\n switch (yyn) {\n"


void
output_headers()
{
if (semantic_parser)
fprintf(fguard, GUARDSTR, attrsfile);
fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
/* if (semantic_parser) JF moved this below
fprintf(ftable, "#include \"%s\"\n", attrsfile);
fprintf(ftable, "#include \n\n");
*/

/* Rename certain symbols if -p was specified. */
if (spec_name_prefix)
{
fprintf(ftable, "#define yyparse %sparse\n", spec_name_prefix);
fprintf(ftable, "#define yylex %slex\n", spec_name_prefix);
fprintf(ftable, "#define yyerror %serror\n", spec_name_prefix);
fprintf(ftable, "#define yylval %slval\n", spec_name_prefix);
fprintf(ftable, "#define yychar %schar\n", spec_name_prefix);
fprintf(ftable, "#define yydebug %sdebug\n", spec_name_prefix);
fprintf(ftable, "#define yynerrs %snerrs\n", spec_name_prefix);
}
}


void
output_trailers()
{
if (semantic_parser)
{
fprintf(fguard, "\n }\n}\n");
fprintf(faction, "\n }\n}\n");
}
else
fprintf(faction, "\n}\n");
}


void
output()
{
int c;

/* output_token_defines(ftable); / * JF put out token defines FIRST */
if (!semantic_parser) /* JF Put out other stuff */
{
rewind(fattrs);
while ((c=getc(fattrs))!=EOF)
putc(c,ftable);
}

if (debugflag)
fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
!!debugflag);

if (semantic_parser)
fprintf(ftable, "#include \"%s\"\n", attrsfile);
fprintf(ftable, "#include \n\n");

/* Make "const" do nothing if not in ANSI C. */
fprintf (ftable, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n");

free_itemsets();
output_defines();
output_token_translations();
/* if (semantic_parser) */
/* This is now unconditional because debugging printouts can use it. */
output_gram();
FREE(ritem);
if (semantic_parser)
output_stos();
output_rule_data();
output_actions();
output_parser();
output_program();
}


void
output_token_translations()
{
register int i, j;
/* register short *sp; JF unused */

if (translations)
{
fprintf(ftable,
"\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
max_user_token_number, nsyms);

if (ntokens < 127) /* play it very safe; check maximum element value. */
fprintf(ftable, "\nstatic const char yytranslate[] = { 0");
else
fprintf(ftable, "\nstatic const short yytranslate[] = { 0");

j = 10;
for (i = 1; i <= max_user_token_number; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", token_translations[i]);
}

fprintf(ftable, "\n};\n");
}
else
{
fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
}
}


void
output_gram()
{
register int i;
register int j;
register short *sp;

/* With the ordinary parser,
yyprhs and yyrhs are needed only for yydebug. */
if (!semantic_parser)
fprintf(ftable, "\n#if YYDEBUG != 0");

fprintf(ftable, "\nstatic const short yyprhs[] = { 0");

j = 10;
for (i = 1; i <= nrules; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", rrhs[i]);
}

fprintf(ftable, "\n};\n");

fprintf(ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);

j = 10;
for (sp = ritem + 1; *sp; sp++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

if (*sp > 0)
fprintf(ftable, "%6d", *sp);
else
fprintf(ftable, " 0");
}

fprintf(ftable, "\n};\n");

if(!semantic_parser)
fprintf(ftable, "\n#endif\n");
}


void
output_stos()
{
register int i;
register int j;

fprintf(ftable, "\nstatic const short yystos[] = { 0");

j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", accessing_symbol[i]);
}

fprintf(ftable, "\n};\n");
}


void
output_rule_data()
{
register int i;
register int j;

fprintf(ftable, "\n#if YYDEBUG != 0\nstatic const short yyrline[] = { 0");

j = 10;
for (i = 1; i <= nrules; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", rline[i]);
}

/* Output the table of symbol names. */

fprintf(ftable,
"\n};\n\nstatic const char * const yytname[] = { \"%s\"",
tags[0]);

j = strlen (tags[0]) + 44;
for (i = 1; i <= nsyms; i++)
{
register char *p;
putc(',', ftable);
j++;

if (j > 75)
{
putc('\n', ftable);
j = 0;
}

putc ('\"', ftable);
j++;

for (p = tags[i]; p && *p; p++)
{
if (*p == '"' || *p == '\\')
{
fprintf(ftable, "\\%c", *p);
j += 2;
}
else if (*p == '\n')
{
fprintf(ftable, "\\n");
j += 2;
}
else if (*p == '\t')
{
fprintf(ftable, "\\t");
j += 2;
}
else if (*p == '\b')
{
fprintf(ftable, "\\b");
j += 2;
}
else if (*p < 040 || *p >= 0177)
{
fprintf(ftable, "\\%03o", *p);
j += 4;
}
else
{
putc(*p, ftable);
j++;
}
}

putc ('\"', ftable);
j++;
}

fprintf(ftable, "\n};\n#endif\n\nstatic const short yyr1[] = { 0");

j = 10;
for (i = 1; i <= nrules; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", rlhs[i]);
}

FREE(rlhs + 1);

fprintf(ftable, "\n};\n\nstatic const short yyr2[] = { 0");

j = 10;
for (i = 1; i < nrules; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
}

putc(',', ftable);
if (j >= 10)
putc('\n', ftable);

fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
FREE(rrhs + 1);
}


void
output_defines()
{
fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
}



/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */

void
output_actions()
{
nvectors = nstates + nvars;

froms = NEW2(nvectors, short *);
tos = NEW2(nvectors, short *);
tally = NEW2(nvectors, short);
width = NEW2(nvectors, short);

token_actions();
free_shifts();
free_reductions();
FREE(lookaheads);
FREE(LA);
FREE(LAruleno);
FREE(accessing_symbol);

goto_actions();
FREE(goto_map + ntokens);
FREE(from_state);
FREE(to_state);

sort_actions();
pack_table();
output_base();
output_table();
output_check();
}



/* figure out the actions for the specified state, indexed by lookahead token type.

The yydefact table is output now. The detailed info
is saved for putting into yytable later. */

void
token_actions()
{
register int i;
register int j;
register int k;

actrow = NEW2(ntokens, short);

k = action_row(0);
fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
save_row(0);

j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

k = action_row(i);
fprintf(ftable, "%6d", k);
save_row(i);
}

fprintf(ftable, "\n};\n");
FREE(actrow);
}



/* Decide what to do for each type of token if seen as the lookahead token in specified state.
The value returned is used as the default action (yydefact) for the state.
In addition, actrow is filled with what to do for each kind of token,
index by symbol number, with zero meaning do the default action.
The value MINSHORT, a very negative number, means this situation
is an error. The parser recognizes this value specially.

This is where conflicts are resolved. The loop over lookahead rules
considered lower-numbered rules last, and the last rule considered that likes
a token gets to handle it. */

int
action_row(state)
int state;
{
register int i;
register int j;
register int k;
register int m;
register int n;
register int count;
register int default_rule;
register int nreds;
register int max;
register int rule;
register int shift_state;
register int symbol;
register unsigned mask;
register unsigned *wordp;
register reductions *redp;
register shifts *shiftp;
register errs *errp;
int nodefault = 0; /* set nonzero to inhibit having any default reduction */

for (i = 0; i < ntokens; i++)
actrow[i] = 0;

default_rule = 0;
nreds = 0;
redp = reduction_table[state];

if (redp)
{
nreds = redp->nreds;

if (nreds >= 1)
{
/* loop over all the rules available here which require lookahead */
m = lookaheads[state];
n = lookaheads[state + 1];

for (i = n - 1; i >= m; i--)
{
rule = - LAruleno[i];
wordp = LA + i * tokensetsize;
mask = 1;

/* and find each token which the rule finds acceptable to come next */
for (j = 0; j < ntokens; j++)
{
/* and record this rule as the rule to use if that token follows. */
if (mask & *wordp)
actrow[j] = rule;

mask <<= 1;
if (mask == 0)
{
mask = 1;
wordp++;
}
}
}
}
}

shiftp = shift_table[state];

/* now see which tokens are allowed for shifts in this state.
For them, record the shift as the thing to do. So shift is preferred to reduce. */

if (shiftp)
{
k = shiftp->nshifts;

for (i = 0; i < k; i++)
{
shift_state = shiftp->shifts[i];
if (! shift_state) continue;

symbol = accessing_symbol[shift_state];

if (ISVAR(symbol))
break;

actrow[symbol] = shift_state;

/* do not use any default reduction if there is a shift for error */

if (symbol == error_token_number) nodefault = 1;
}
}

errp = err_table[state];

/* See which tokens are an explicit error in this state
(due to %nonassoc). For them, record MINSHORT as the action. */

if (errp)
{
k = errp->nerrs;

for (i = 0; i < k; i++)
{
symbol = errp->errs[i];
actrow[symbol] = MINSHORT;
}
}

/* now find the most common reduction and make it the default action for this state. */

if (nreds >= 1 && ! nodefault)
{
if (consistent[state])
default_rule = redp->rules[0];
else
{
max = 0;
for (i = m; i < n; i++)
{
count = 0;
rule = - LAruleno[i];

for (j = 0; j < ntokens; j++)
{
if (actrow[j] == rule)
count++;
}

if (count > max)
{
max = count;
default_rule = rule;
}
}

/* actions which match the default are replaced with zero,
which means "use the default" */

if (max > 0)
{
for (j = 0; j < ntokens; j++)
{
if (actrow[j] == default_rule)
actrow[j] = 0;
}

default_rule = - default_rule;
}
}
}

/* If have no default rule, the default is an error.
So replace any action which says "error" with "use default". */

if (default_rule == 0)
for (j = 0; j < ntokens; j++)
{
if (actrow[j] == MINSHORT)
actrow[j] = 0;
}

return (default_rule);
}


void
save_row(state)
int state;
{
register int i;
register int count;
register short *sp;
register short *sp1;
register short *sp2;

count = 0;
for (i = 0; i < ntokens; i++)
{
if (actrow[i] != 0)
count++;
}

if (count == 0)
return;

froms[state] = sp1 = sp = NEW2(count, short);
tos[state] = sp2 = NEW2(count, short);

for (i = 0; i < ntokens; i++)
{
if (actrow[i] != 0)
{
*sp1++ = i;
*sp2++ = actrow[i];
}
}

tally[state] = count;
width[state] = sp1[-1] - sp[0] + 1;
}



/* figure out what to do after reducing with each rule,
depending on the saved state from before the beginning
of parsing the data that matched this rule.

The yydefgoto table is output now. The detailed info
is saved for putting into yytable later. */

void
goto_actions()
{
register int i;
register int j;
register int k;

state_count = NEW2(nstates, short);

k = default_goto(ntokens);
fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
save_column(ntokens, k);

j = 10;
for (i = ntokens + 1; i < nsyms; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

k = default_goto(i);
fprintf(ftable, "%6d", k);
save_column(i, k);
}

fprintf(ftable, "\n};\n");
FREE(state_count);
}



int
default_goto(symbol)
int symbol;
{
register int i;
register int m;
register int n;
register int default_state;
register int max;

m = goto_map[symbol];
n = goto_map[symbol + 1];

if (m == n)
return (-1);

for (i = 0; i < nstates; i++)
state_count[i] = 0;

for (i = m; i < n; i++)
state_count[to_state[i]]++;

max = 0;
default_state = -1;

for (i = 0; i < nstates; i++)
{
if (state_count[i] > max)
{
max = state_count[i];
default_state = i;
}
}

return (default_state);
}


void
save_column(symbol, default_state)
int symbol;
int default_state;
{
register int i;
register int m;
register int n;
register short *sp;
register short *sp1;
register short *sp2;
register int count;
register int symno;

m = goto_map[symbol];
n = goto_map[symbol + 1];

count = 0;
for (i = m; i < n; i++)
{
if (to_state[i] != default_state)
count++;
}

if (count == 0)
return;

symno = symbol - ntokens + nstates;

froms[symno] = sp1 = sp = NEW2(count, short);
tos[symno] = sp2 = NEW2(count, short);

for (i = m; i < n; i++)
{
if (to_state[i] != default_state)
{
*sp1++ = from_state[i];
*sp2++ = to_state[i];
}
}

tally[symno] = count;
width[symno] = sp1[-1] - sp[0] + 1;
}



/* the next few functions decide how to pack
the actions and gotos information into yytable. */

void
sort_actions()
{
register int i;
register int j;
register int k;
register int t;
register int w;

order = NEW2(nvectors, short);
nentries = 0;

for (i = 0; i < nvectors; i++)
{
if (tally[i] > 0)
{
t = tally[i];
w = width[i];
j = nentries - 1;

while (j >= 0 && (width[order[j]] < w))
j--;

while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
j--;

for (k = nentries - 1; k > j; k--)
order[k + 1] = order[k];

order[j + 1] = i;
nentries++;
}
}
}


void
pack_table()
{
register int i;
register int place;
register int state;

base = NEW2(nvectors, short);
pos = NEW2(nentries, short);
table = NEW2(MAXTABLE, short);
check = NEW2(MAXTABLE, short);

lowzero = 0;
high = 0;

for (i = 0; i < nvectors; i++)
base[i] = MINSHORT;

for (i = 0; i < MAXTABLE; i++)
check[i] = -1;

for (i = 0; i < nentries; i++)
{
state = matching_state(i);

if (state < 0)
place = pack_vector(i);
else
place = base[state];

pos[i] = place;
base[order[i]] = place;
}

for (i = 0; i < nvectors; i++)
{
if (froms[i])
FREE(froms[i]);
if (tos[i])
FREE(tos[i]);
}

FREE(froms);
FREE(tos);
FREE(pos);
}



int
matching_state(vector)
int vector;
{
register int i;
register int j;
register int k;
register int t;
register int w;
register int match;
register int prev;

i = order[vector];
if (i >= nstates)
return (-1);

t = tally[i];
w = width[i];

for (prev = vector - 1; prev >= 0; prev--)
{
j = order[prev];
if (width[j] != w || tally[j] != t)
return (-1);

match = 1;
for (k = 0; match && k < t; k++)
{
if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
match = 0;
}

if (match)
return (j);
}

return (-1);
}



int
pack_vector(vector)
int vector;
{
register int i;
register int j;
register int k;
register int t;
register int loc;
register int ok;
register short *from;
register short *to;

i = order[vector];
t = tally[i];

if (t == 0)
berror("pack_vector");

from = froms[i];
to = tos[i];

for (j = lowzero - from[0]; j < MAXTABLE; j++)
{
ok = 1;

for (k = 0; ok && k < t; k++)
{
loc = j + from[k];
if (loc > MAXTABLE)
fatals("maximum table size (%d) exceeded",MAXTABLE);

if (table[loc] != 0)
ok = 0;
}

for (k = 0; ok && k < vector; k++)
{
if (pos[k] == j)
ok = 0;
}

if (ok)
{
for (k = 0; k < t; k++)
{
loc = j + from[k];
table[loc] = to[k];
check[loc] = from[k];
}

while (table[lowzero] != 0)
lowzero++;

if (loc > high)
high = loc;

return (j);
}
}

berror("pack_vector");
return 0; /* JF keep lint happy */
}



/* the following functions output yytable, yycheck
and the vectors whose elements index the portion starts */

void
output_base()
{
register int i;
register int j;

fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);

j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", base[i]);
}

fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);

j = 10;
for (i = nstates + 1; i < nvectors; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", base[i]);
}

fprintf(ftable, "\n};\n");
FREE(base);
}


void
output_table()
{
register int i;
register int j;

fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);

j = 10;
for (i = 1; i <= high; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", table[i]);
}

fprintf(ftable, "\n};\n");
FREE(table);
}


void
output_check()
{
register int i;
register int j;

fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);

j = 10;
for (i = 1; i <= high; i++)
{
putc(',', ftable);

if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}

fprintf(ftable, "%6d", check[i]);
}

fprintf(ftable, "\n};\n");
FREE(check);
}



/* copy the parser code into the ftable file at the end. */

void
output_parser()
{
register int c;
#ifdef DONTDEF
FILE *fpars;
#else
#define fpars fparser
#endif

if (pure_parser)
fprintf(ftable, "#define YYPURE 1\n\n");

#ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
currently open parser from bison.simple to bison.hairy */
if (semantic_parser)
fpars = fparser;
else fpars = fparser1;
#endif

/* Loop over lines in the standard parser file. */

while (1)
{
int write_line = 1;

c = getc(fpars);

/* See if the line starts with `#line.
If so, set write_line to 0. */
if (nolinesflag)
if (c == '#')
{
c = getc(fpars);
if (c == 'l')
{
c = getc(fpars);
if (c == 'i')
{
c = getc(fpars);
if (c == 'n')
{
c = getc(fpars);
if (c == 'e')
write_line = 0;
else
fprintf(ftable, "#lin");
}
else
fprintf(ftable, "#li");
}
else
fprintf(ftable, "#l");
}
else
fprintf(ftable, "#");
}

/* now write out the line... */
for ( ; c != '\n' && c != EOF; c = getc(fpars))
if (write_line)
if (c == '$')
{
/* `$' in the parser file indicates where to put the actions.
Copy them in at this point. */
rewind(faction);
for(c=getc(faction);c!=EOF;c=getc(faction))
putc(c,ftable);
}
else
putc(c, ftable);
if (c == EOF)
break;
putc(c, ftable);
}
}


void
output_program()
{
register int c;
extern int lineno;

if (!nolinesflag)
fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);

c = getc(finput);
while (c != EOF)
{
putc(c, ftable);
c = getc(finput);
}
}


void
free_itemsets()
{
register core *cp,*cptmp;

FREE(state_table);

for (cp = first_state; cp; cp = cptmp) {
cptmp=cp->next;
FREE(cp);
}
}


void
free_shifts()
{
register shifts *sp,*sptmp;/* JF derefrenced freed ptr */

FREE(shift_table);

for (sp = first_shift; sp; sp = sptmp) {
sptmp=sp->next;
FREE(sp);
}
}


void
free_reductions()
{
register reductions *rp,*rptmp;/* JF fixed freed ptr */

FREE(reduction_table);

for (rp = first_reduction; rp; rp = rptmp) {
rptmp=rp->next;
FREE(rp);
}
}
bison-1.22/print.c100666 12120 1 16774 5123575506 12407 0ustar rmsdaemon/* Print information on generated parser, for bison,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include "system.h"
#include "machine.h"
#include "new.h"
#include "files.h"
#include "gram.h"
#include "state.h"


extern char **tags;
extern int nstates;
extern short *accessing_symbol;
extern core **state_table;
extern shifts **shift_table;
extern errs **err_table;
extern reductions **reduction_table;
extern char *consistent;
extern char any_conflicts;
extern char *conflicts;
extern int final_state;

extern void conflict_log();
extern void verbose_conflict_log();
extern void print_reductions();

void print_token();
void print_state();
void print_core();
void print_actions();
void print_grammar();

void
terse()
{
if (any_conflicts)
{
conflict_log();
}
}


void
verbose()
{
register int i;

if (any_conflicts)
verbose_conflict_log();

print_grammar();

for (i = 0; i < nstates; i++)
{
print_state(i);
}
}


void
print_token(extnum, token)
int extnum, token;
{
fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
}


void
print_state(state)
int state;
{
fprintf(foutput, "\n\nstate %d\n\n", state);
print_core(state);
print_actions(state);
}


void
print_core(state)
int state;
{
register int i;
register int k;
register int rule;
register core *statep;
register short *sp;
register short *sp1;

statep = state_table[state];
k = statep->nitems;

if (k == 0) return;

for (i = 0; i < k; i++)
{
sp1 = sp = ritem + statep->items[i];

while (*sp > 0)
sp++;

rule = -(*sp);
fprintf(foutput, " %s -> ", tags[rlhs[rule]]);

for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
{
fprintf(foutput, "%s ", tags[*sp]);
}

putc('.', foutput);

while (*sp > 0)
{
fprintf(foutput, " %s", tags[*sp]);
sp++;
}

fprintf (foutput, " (rule %d)", rule);
putc('\n', foutput);
}

putc('\n', foutput);
}


void
print_actions(state)
int state;
{
register int i;
register int k;
register int state1;
register int symbol;
register shifts *shiftp;
register errs *errp;
register reductions *redp;
register int rule;

shiftp = shift_table[state];
redp = reduction_table[state];
errp = err_table[state];

if (!shiftp && !redp)
{
if (final_state == state)
fprintf(foutput, " $default\taccept\n");
else
fprintf(foutput, " NO ACTIONS\n");
return;
}

if (shiftp)
{
k = shiftp->nshifts;

for (i = 0; i < k; i++)
{
if (! shiftp->shifts[i]) continue;
state1 = shiftp->shifts[i];
symbol = accessing_symbol[state1];
/* The following line used to be turned off. */
if (ISVAR(symbol)) break;
if (symbol==0) /* I.e. strcmp(tags[symbol],"$")==0 */
fprintf(foutput, " $ \tgo to state %d\n", state1);
else
fprintf(foutput, " %-4s\tshift, and go to state %d\n",
tags[symbol], state1);
}

if (i > 0)
putc('\n', foutput);
}
else
{
i = 0;
k = 0;
}

if (errp)
{
int j, nerrs;

nerrs = errp->nerrs;

for (j = 0; j < nerrs; j++)
{
if (! errp->errs[j]) continue;
symbol = errp->errs[j];
fprintf(foutput, " %-4s\terror (nonassociative)\n", tags[symbol]);
}

if (j > 0)
putc('\n', foutput);
}

if (consistent[state] && redp)
{
rule = redp->rules[0];
symbol = rlhs[rule];
fprintf(foutput, " $default\treduce using rule %d (%s)\n\n",
rule, tags[symbol]);
}
else if (redp)
{
print_reductions(state);
}

if (i < k)
{
for (; i < k; i++)
{
if (! shiftp->shifts[i]) continue;
state1 = shiftp->shifts[i];
symbol = accessing_symbol[state1];
fprintf(foutput, " %-4s\tgo to state %d\n", tags[symbol], state1);
}

putc('\n', foutput);
}
}

#define END_TEST(end) \
if (column + strlen(buffer) > (end)) \
{ fprintf (foutput, "%s\n ", buffer); column = 3; buffer[0] = 0; } \
else

void
print_grammar()
{
int i, j;
short* rule;
char buffer[90];
int column = 0;

/* rule # : LHS -> RHS */
fputs("\nGrammar\n", foutput);
for (i = 1; i <= nrules; i++)
/* Don't print rules disabled in reduce_grammar_tables. */
if (rlhs[i] >= 0)
{
fprintf(foutput, "rule %-4d %s ->", i, tags[rlhs[i]]);
rule = &ritem[rrhs[i]];
if (*rule > 0)
while (*rule > 0)
fprintf(foutput, " %s", tags[*rule++]);
else
fputs (" /* empty */", foutput);
putc('\n', foutput);
}

/* TERMINAL (type #) : rule #s terminal is on RHS */
fputs("\nTerminals, with rules where they appear\n\n", foutput);
fprintf(foutput, "%s (-1)\n", tags[0]);
if (translations)
{
for (i = 0; i <= max_user_token_number; i++)
if (token_translations[i] != 2)
{
buffer[0] = 0;
column = strlen (tags[token_translations[i]]);
fprintf(foutput, "%s", tags[token_translations[i]]);
END_TEST (50);
sprintf (buffer, " (%d)", i);

for (j = 1; j <= nrules; j++)
{
for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
if (*rule == token_translations[i])
{
END_TEST (65);
sprintf (buffer + strlen(buffer), " %d", j);
break;
}
}
fprintf (foutput, "%s\n", buffer);
}
}
else
for (i = 1; i < ntokens; i++)
{
buffer[0] = 0;
column = strlen (tags[i]);
fprintf(foutput, "%s", tags[i]);
END_TEST (50);
sprintf (buffer, " (%d)", i);

for (j = 1; j <= nrules; j++)
{
for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
if (*rule == i)
{
END_TEST (65);
sprintf (buffer + strlen(buffer), " %d", j);
break;
}
}
fprintf (foutput, "%s\n", buffer);
}

fputs("\nNonterminals, with rules where they appear\n\n", foutput);
for (i = ntokens; i <= nsyms - 1; i++)
{
int left_count = 0, right_count = 0;

for (j = 1; j <= nrules; j++)
{
if (rlhs[j] == i)
left_count++;
for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
if (*rule == i)
{
right_count++;
break;
}
}

buffer[0] = 0;
fprintf(foutput, "%s", tags[i]);
column = strlen (tags[i]);
sprintf (buffer, " (%d)", i);
END_TEST (0);

if (left_count > 0)
{
END_TEST (50);
sprintf (buffer + strlen(buffer), " on left:");

for (j = 1; j <= nrules; j++)
{
END_TEST (65);
if (rlhs[j] == i)
sprintf (buffer + strlen(buffer), " %d", j);
}
}

if (right_count > 0)
{
if (left_count > 0)
sprintf (buffer + strlen(buffer), ",");
END_TEST (50);
sprintf (buffer + strlen(buffer), " on right:");
for (j = 1; j <= nrules; j++)
{
for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
if (*rule == i)
{
END_TEST (65);
sprintf (buffer + strlen(buffer), " %d", j);
break;
}
}
}
fprintf (foutput, "%s\n", buffer);
}
}
bison-1.22/reader.c100666 21641 13 111751 5367553702 13262 0ustar friedmanuser/* Input parser for bison
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* read in the grammar specification and record it in the format described in gram.h.
All guards are copied into the fguard file and all actions into faction,
in each case forming the body of a C function (yyguard or yyaction)
which contains a switch statement to decide which guard or action to execute.

The entry point is reader(). */

#include
#include
#include "system.h"
#include "files.h"
#include "new.h"
#include "symtab.h"
#include "lex.h"
#include "gram.h"
#include "machine.h"

#define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
{\n int timestamp;\n int first_line;\n int first_column;\
\n int last_line;\n int last_column;\n char *text;\n }\n\
yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"

/* Number of slots allocated (but not necessarily used yet) in `rline' */
int rline_allocated;

extern char *program_name;
extern int definesflag;
extern int nolinesflag;
extern bucket *symval;
extern int numval;
extern int failure;
extern int expected_conflicts;
extern char *token_buffer;

extern void init_lex();
extern void tabinit();
extern void output_headers();
extern void output_trailers();
extern void free_symtab();
extern void open_extra_files();
extern void fatal();
extern void fatals();
extern void unlex();
extern void done();

extern int skip_white_space();
extern int parse_percent_token();
extern int lex();

void read_declarations();
void copy_definition();
void parse_token_decl();
void parse_start_decl();
void parse_type_decl();
void parse_assoc_decl();
void parse_union_decl();
void parse_expect_decl();
void copy_action();
void readgram();
void record_rule_line();
void packsymbols();
void output_token_defines();
void packgram();
int read_signed_integer();
int get_type();

typedef
struct symbol_list
{
struct symbol_list *next;
bucket *sym;
bucket *ruleprec;
}
symbol_list;



int lineno;
symbol_list *grammar;
int start_flag;
bucket *startval;
char **tags;

/* Nonzero if components of semantic values are used, implying
they must be unions. */
static int value_components_used;

static int typed; /* nonzero if %union has been seen. */

static int lastprec; /* incremented for each %left, %right or %nonassoc seen */

static int gensym_count; /* incremented for each generated symbol */

static bucket *errtoken;

/* Nonzero if any action or guard uses the @n construct. */
static int yylsp_needed;

extern char *version_string;

void
reader()
{
start_flag = 0;
startval = NULL; /* start symbol not specified yet. */

#if 0
translations = 0; /* initially assume token number translation not needed. */
#endif
/* Nowadays translations is always set to 1,
since we give `error' a user-token-number
to satisfy the Posix demand for YYERRCODE==256. */
translations = 1;

nsyms = 1;
nvars = 0;
nrules = 0;
nitems = 0;
rline_allocated = 10;
rline = NEW2(rline_allocated, short);

typed = 0;
lastprec = 0;

gensym_count = 0;

semantic_parser = 0;
pure_parser = 0;
yylsp_needed = 0;

grammar = NULL;

init_lex();
lineno = 1;

/* initialize the symbol table. */
tabinit();
/* construct the error token */
errtoken = getsym("error");
errtoken->class = STOKEN;
errtoken->user_token_number = 256; /* Value specified by posix. */
/* construct a token that represents all undefined literal tokens. */
/* it is always token number 2. */
getsym("$illegal.")->class = STOKEN;
/* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
Also notice any %token, %left, etc. found there. */
fprintf(ftable, "\n/* A Bison parser, made from %s", infile);
fprintf(ftable, " with Bison version %s */\n\n", version_string);
fprintf(ftable, "#define YYBISON 1 /* Identify Bison output. */\n\n");
read_declarations();
/* output the definition of YYLTYPE into the fattrs and fdefines files. */
/* fattrs winds up in the .tab.c file, before bison.simple. */
fprintf(fattrs, LTYPESTR);
/* start writing the guard and action files, if they are needed. */
output_headers();
/* read in the grammar, build grammar in list form. write out guards and actions. */
readgram();
/* Now we know whether we need the line-number stack.
If we do, write its type into the .tab.h file. */
if (yylsp_needed)
{
if (fdefines)
fprintf(fdefines, LTYPESTR);
}
/* write closing delimiters for actions and guards. */
output_trailers();
if (yylsp_needed)
fprintf(ftable, "#define YYLSP_NEEDED\n\n");
/* assign the symbols their symbol numbers.
Write #defines for the token symbols into fdefines if requested. */
packsymbols();
/* convert the grammar into the format described in gram.h. */
packgram();
/* free the symbol table data structure
since symbols are now all referred to by symbol number. */
free_symtab();
}



/* read from finput until %% is seen. Discard the %%.
Handle any % declarations,
and copy the contents of any %{ ... %} groups to fattrs. */

void
read_declarations ()
{
register int c;
register int tok;

for (;;)
{
c = skip_white_space();

if (c == '%')
{
tok = parse_percent_token();

switch (tok)
{
case TWO_PERCENTS:
return;

case PERCENT_LEFT_CURLY:
copy_definition();
break;

case TOKEN:
parse_token_decl (STOKEN, SNTERM);
break;

case NTERM:
parse_token_decl (SNTERM, STOKEN);
break;

case TYPE:
parse_type_decl();
break;

case START:
parse_start_decl();
break;

case UNION:
parse_union_decl();
break;

case EXPECT:
parse_expect_decl();
break;

case LEFT:
parse_assoc_decl(LEFT_ASSOC);
break;

case RIGHT:
parse_assoc_decl(RIGHT_ASSOC);
break;

case NONASSOC:
parse_assoc_decl(NON_ASSOC);
break;

case SEMANTIC_PARSER:
if (semantic_parser == 0)
{
semantic_parser = 1;
open_extra_files();
}
break;

case PURE_PARSER:
pure_parser = 1;
break;

default:
fatal("junk after `%%' in definition section");
}
}
else if (c == EOF)
fatal("no input grammar");
else if (c >= 040 && c <= 0177)
fatals ("unknown character `%c' in declaration section", c);
else
fatals ("unknown character with code 0x%x in declaration section", c);
}
}


/* copy the contents of a %{ ... %} into the definitions file.
The %{ has already been read. Return after reading the %}. */

void
copy_definition ()
{
register int c;
register int match;
register int ended;
register int after_percent; /* -1 while reading a character if prev char was % */
int cplus_comment;

if (!nolinesflag)
fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);

after_percent = 0;

c = getc(finput);

for (;;)
{
switch (c)
{
case '\n':
putc(c, fattrs);
lineno++;
break;

case '%':
after_percent = -1;
break;

case '\'':
case '"':
match = c;
putc(c, fattrs);
c = getc(finput);

while (c != match)
{
if (c == EOF || c == '\n')
fatal("unterminated string");

putc(c, fattrs);

if (c == '\\')
{
c = getc(finput);
if (c == EOF)
fatal("unterminated string");
putc(c, fattrs);
if (c == '\n')
lineno++;
}

c = getc(finput);
}

putc(c, fattrs);
break;

case '/':
putc(c, fattrs);
c = getc(finput);
if (c != '*' && c != '/')
continue;

cplus_comment = (c == '/');
putc(c, fattrs);
c = getc(finput);

ended = 0;
while (!ended)
{
if (!cplus_comment && c == '*')
{
while (c == '*')
{
putc(c, fattrs);
c = getc(finput);
}

if (c == '/')
{
putc(c, fattrs);
ended = 1;
}
}
else if (c == '\n')
{
lineno++;
putc(c, fattrs);
if (cplus_comment)
ended = 1;
else
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment in `%{' definition");
else
{
putc(c, fattrs);
c = getc(finput);
}
}

break;

case EOF:
fatal("unterminated `%{' definition");

default:
putc(c, fattrs);
}

c = getc(finput);

if (after_percent)
{
if (c == '}')
return;
putc('%', fattrs);
}
after_percent = 0;

}

}



/* parse what comes after %token or %nterm.
For %token, what_is is STOKEN and what_is_not is SNTERM.
For %nterm, the arguments are reversed. */

void
parse_token_decl (what_is, what_is_not)
int what_is, what_is_not;
{
/* register int start_lineno; JF */
register int token = 0;
register int prev;
register char *typename = 0;
int k;

/* start_lineno = lineno; JF */

for (;;)
{
if(ungetc(skip_white_space(), finput) == '%')
return;

/* if (lineno != start_lineno)
return; JF */

/* we have not passed a newline, so the token now starting is in this declaration */
prev = token;

token = lex();
if (token == COMMA)
continue;
if (token == TYPENAME)
{
k = strlen(token_buffer);
typename = NEW2(k + 1, char);
strcpy(typename, token_buffer);
value_components_used = 1;
}
else if (token == IDENTIFIER)
{
int oldclass = symval->class;

if (symval->class == what_is_not)
fatals("symbol %s redefined", symval->tag);
symval->class = what_is;
if (what_is == SNTERM && oldclass != SNTERM)
symval->value = nvars++;

if (typename)
{
if (symval->type_name == NULL)
symval->type_name = typename;
else
fatals("type redeclaration for %s", symval->tag);
}
}
else if (prev == IDENTIFIER && token == NUMBER)
{
symval->user_token_number = numval;
translations = 1;
}
else
fatal("invalid text in %token or %nterm declaration");
}

}



/* parse what comes after %start */

void
parse_start_decl ()
{
if (start_flag)
fatal("multiple %start declarations");
start_flag = 1;
if (lex() != IDENTIFIER)
fatal("invalid %start declaration");
startval = symval;
}



/* read in a %type declaration and record its information for get_type_name to access */

void
parse_type_decl ()
{
register int k;
register char *name;
/* register int start_lineno; JF */

if (lex() != TYPENAME)
fatal("ill-formed %type declaration");

k = strlen(token_buffer);
name = NEW2(k + 1, char);
strcpy(name, token_buffer);

/* start_lineno = lineno; */

for (;;)
{
register int t;

if(ungetc(skip_white_space(), finput) == '%')
return;

/* if (lineno != start_lineno)
return; JF */

/* we have not passed a newline, so the token now starting is in this declaration */

t = lex();

switch (t)
{

case COMMA:
case SEMICOLON:
break;

case IDENTIFIER:
if (symval->type_name == NULL)
symval->type_name = name;
else
fatals("type redeclaration for %s", symval->tag);

break;

default:
fatal("invalid %type declaration");
}
}
}



/* read in a %left, %right or %nonassoc declaration and record its information. */
/* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */

void
parse_assoc_decl (assoc)
int assoc;
{
register int k;
register char *name = NULL;
/* register int start_lineno; JF */
register int prev = 0; /* JF added = 0 to keep lint happy */

lastprec++; /* Assign a new precedence level, never 0. */

/* start_lineno = lineno; */

for (;;)
{
register int t;

if(ungetc(skip_white_space(), finput) == '%')
return;

/* if (lineno != start_lineno)
return; JF */

/* we have not passed a newline, so the token now starting is in this declaration */

t = lex();

switch (t)
{

case TYPENAME:
k = strlen(token_buffer);
name = NEW2(k + 1, char);
strcpy(name, token_buffer);
break;

case COMMA:
break;

case IDENTIFIER:
if (symval->prec != 0)
fatals("redefining precedence of %s", symval->tag);
symval->prec = lastprec;
symval->assoc = assoc;
if (symval->class == SNTERM)
fatals("symbol %s redefined", symval->tag);
symval->class = STOKEN;
if (name)
{ /* record the type, if one is specified */
if (symval->type_name == NULL)
symval->type_name = name;
else
fatals("type redeclaration for %s", symval->tag);
}
break;

case NUMBER:
if (prev == IDENTIFIER)
{
symval->user_token_number = numval;
translations = 1;
}
else
fatal("invalid text in association declaration");
break;

case SEMICOLON:
return;

default:
fatal("malformatted association declaration");
}

prev = t;

}
}



/* copy the union declaration into fattrs (and fdefines),
where it is made into the
definition of YYSTYPE, the type of elements of the parser value stack. */

void
parse_union_decl()
{
register int c;
register int count;
register int in_comment;
int cplus_comment;

if (typed)
fatal("multiple %union declarations");

typed = 1;

if (!nolinesflag)
fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile);
else
fprintf(fattrs, "\n");

fprintf(fattrs, "typedef union");
if (fdefines)
fprintf(fdefines, "typedef union");

count = 0;
in_comment = 0;

c = getc(finput);

while (c != EOF)
{
putc(c, fattrs);
if (fdefines)
putc(c, fdefines);

switch (c)
{
case '\n':
lineno++;
break;

case '/':
c = getc(finput);
if (c != '*' && c != '/')
ungetc(c, finput);
else
{
putc(c, fattrs);
if (fdefines)
putc(c, fdefines);
cplus_comment = (c == '/');
in_comment = 1;
c = getc(finput);
while (in_comment)
{
putc(c, fattrs);
if (fdefines)
putc(c, fdefines);

if (c == '\n')
{
lineno++;
if (cplus_comment)
{
in_comment = 0;
break;
}
}
if (c == EOF)
fatal("unterminated comment");

if (!cplus_comment && c == '*')
{
c = getc(finput);
if (c == '/')
{
putc('/', fattrs);
if (fdefines)
putc('/', fdefines);
in_comment = 0;
}
}
else
c = getc(finput);
}
}
break;


case '{':
count++;
break;

case '}':
if (count == 0)
fatal ("unmatched close-brace (`}')");
count--;
if (count == 0)
{
fprintf(fattrs, " YYSTYPE;\n");
if (fdefines)
fprintf(fdefines, " YYSTYPE;\n");
/* JF don't choke on trailing semi */
c=skip_white_space();
if(c!=';') ungetc(c,finput);
return;
}
}

c = getc(finput);
}
}

/* parse the declaration %expect N which says to expect N
shift-reduce conflicts. */

void
parse_expect_decl()
{
register int c;
register int count;
char buffer[20];

c = getc(finput);
while (c == ' ' || c == '\t')
c = getc(finput);

count = 0;
while (c >= '0' && c <= '9')
{
if (count < 20)
buffer[count++] = c;
c = getc(finput);
}
buffer[count] = 0;

ungetc (c, finput);

expected_conflicts = atoi (buffer);
}

/* that's all of parsing the declaration section */

/* Get the data type (alternative in the union) of the value for symbol n in rule rule. */

char *
get_type_name(n, rule)
int n;
symbol_list *rule;
{
static char *msg = "invalid $ value";

register int i;
register symbol_list *rp;

if (n < 0)
fatal(msg);

rp = rule;
i = 0;

while (i < n)
{
rp = rp->next;
if (rp == NULL || rp->sym == NULL)
fatal(msg);
i++;
}

return (rp->sym->type_name);
}



/* after %guard is seen in the input file,
copy the actual guard into the guards file.
If the guard is followed by an action, copy that into the actions file.
stack_offset is the number of values in the current rule so far,
which says where to find $0 with respect to the top of the stack,
for the simple parser in which the stack is not popped until after the guard is run. */

void
copy_guard(rule, stack_offset)
symbol_list *rule;
int stack_offset;
{
register int c;
register int n;
register int count;
register int match;
register int ended;
register char *type_name;
int brace_flag = 0;
int cplus_comment;

/* offset is always 0 if parser has already popped the stack pointer */
if (semantic_parser) stack_offset = 0;

fprintf(fguard, "\ncase %d:\n", nrules);
if (!nolinesflag)
fprintf(fguard, "#line %d \"%s\"\n", lineno, infile);
putc('{', fguard);

count = 0;
c = getc(finput);

while (brace_flag ? (count > 0) : (c != ';'))
{
switch (c)
{
case '\n':
putc(c, fguard);
lineno++;
break;

case '{':
putc(c, fguard);
brace_flag = 1;
count++;
break;

case '}':
putc(c, fguard);
if (count > 0)
count--;
else
fatal("unmatched right brace ('}')");
break;

case '\'':
case '"':
match = c;
putc(c, fguard);
c = getc(finput);

while (c != match)
{
if (c == EOF || c == '\n')
fatal("unterminated string");

putc(c, fguard);

if (c == '\\')
{
c = getc(finput);
if (c == EOF)
fatal("unterminated string");
putc(c, fguard);
if (c == '\n')
lineno++;
}

c = getc(finput);
}

putc(c, fguard);
break;

case '/':
putc(c, fguard);
c = getc(finput);
if (c != '*' && c != '/')
continue;

cplus_comment = (c == '/');
putc(c, fguard);
c = getc(finput);

ended = 0;
while (!ended)
{
if (!cplus_comment && c == '*')
{
while (c == '*')
{
putc(c, fguard);
c = getc(finput);
}

if (c == '/')
{
putc(c, fguard);
ended = 1;
}
}
else if (c == '\n')
{
lineno++;
putc(c, fguard);
if (cplus_comment)
ended = 1;
else
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment");
else
{
putc(c, fguard);
c = getc(finput);
}
}

break;

case '$':
c = getc(finput);
type_name = NULL;

if (c == '<')
{
register char *cp = token_buffer;

while ((c = getc(finput)) != '>' && c > 0)
*cp++ = c;
*cp = 0;
type_name = token_buffer;

c = getc(finput);
}

if (c == '$')
{
fprintf(fguard, "yyval");
if (!type_name) type_name = rule->sym->type_name;
if (type_name)
fprintf(fguard, ".%s", type_name);
if(!type_name && typed) /* JF */
fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
}

else if (isdigit(c) || c == '-')
{
ungetc (c, finput);
n = read_signed_integer(finput);
c = getc(finput);

if (!type_name && n > 0)
type_name = get_type_name(n, rule);

fprintf(fguard, "yyvsp[%d]", n - stack_offset);
if (type_name)
fprintf(fguard, ".%s", type_name);
if(!type_name && typed) /* JF */
fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
continue;
}
else
fatals("$%c is invalid",c); /* JF changed style */

break;

case '@':
c = getc(finput);
if (isdigit(c) || c == '-')
{
ungetc (c, finput);
n = read_signed_integer(finput);
c = getc(finput);
}
else
fatals("@%c is invalid",c); /* JF changed style */

fprintf(fguard, "yylsp[%d]", n - stack_offset);
yylsp_needed = 1;

continue;

case EOF:
fatal("unterminated %guard clause");

default:
putc(c, fguard);
}

if (c != '}' || count != 0)
c = getc(finput);
}

c = skip_white_space();

fprintf(fguard, ";\n break;}");
if (c == '{')
copy_action(rule, stack_offset);
else if (c == '=')
{
c = getc(finput);
if (c == '{')
copy_action(rule, stack_offset);
}
else
ungetc(c, finput);
}



/* Assuming that a { has just been seen, copy everything up to the matching }
into the actions file.
stack_offset is the number of values in the current rule so far,
which says where to find $0 with respect to the top of the stack. */

void
copy_action(rule, stack_offset)
symbol_list *rule;
int stack_offset;
{
register int c;
register int n;
register int count;
register int match;
register int ended;
register char *type_name;
int cplus_comment;

/* offset is always 0 if parser has already popped the stack pointer */
if (semantic_parser) stack_offset = 0;

fprintf(faction, "\ncase %d:\n", nrules);
if (!nolinesflag)
fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
putc('{', faction);

count = 1;
c = getc(finput);

while (count > 0)
{
while (c != '}')
{
switch (c)
{
case '\n':
putc(c, faction);
lineno++;
break;

case '{':
putc(c, faction);
count++;
break;

case '\'':
case '"':
match = c;
putc(c, faction);
c = getc(finput);

while (c != match)
{
if (c == EOF || c == '\n')
fatal("unterminated string");

putc(c, faction);

if (c == '\\')
{
c = getc(finput);
if (c == EOF)
fatal("unterminated string");
putc(c, faction);
if (c == '\n')
lineno++;
}

c = getc(finput);
}

putc(c, faction);
break;

case '/':
putc(c, faction);
c = getc(finput);
if (c != '*' && c != '/')
continue;

cplus_comment = (c == '/');
putc(c, faction);
c = getc(finput);

ended = 0;
while (!ended)
{
if (!cplus_comment && c == '*')
{
while (c == '*')
{
putc(c, faction);
c = getc(finput);
}

if (c == '/')
{
putc(c, faction);
ended = 1;
}
}
else if (c == '\n')
{
lineno++;
putc(c, faction);
if (cplus_comment)
ended = 1;
else
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment");
else
{
putc(c, faction);
c = getc(finput);
}
}

break;

case '$':
c = getc(finput);
type_name = NULL;

if (c == '<')
{
register char *cp = token_buffer;

while ((c = getc(finput)) != '>' && c > 0)
*cp++ = c;
*cp = 0;
type_name = token_buffer;
value_components_used = 1;

c = getc(finput);
}
if (c == '$')
{
fprintf(faction, "yyval");
if (!type_name) type_name = get_type_name(0, rule);
if (type_name)
fprintf(faction, ".%s", type_name);
if(!type_name && typed) /* JF */
fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
}
else if (isdigit(c) || c == '-')
{
ungetc (c, finput);
n = read_signed_integer(finput);
c = getc(finput);

if (!type_name && n > 0)
type_name = get_type_name(n, rule);

fprintf(faction, "yyvsp[%d]", n - stack_offset);
if (type_name)
fprintf(faction, ".%s", type_name);
if(!type_name && typed) /* JF */
fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
continue;
}
else
fatals("$%c is invalid",c); /* JF changed format */

break;

case '@':
c = getc(finput);
if (isdigit(c) || c == '-')
{
ungetc (c, finput);
n = read_signed_integer(finput);
c = getc(finput);
}
else
fatal("invalid @-construct");

fprintf(faction, "yylsp[%d]", n - stack_offset);
yylsp_needed = 1;

continue;

case EOF:
fatal("unmatched '{'");

default:
putc(c, faction);
}

c = getc(finput);
}

/* above loop exits when c is '}' */

if (--count)
{
putc(c, faction);
c = getc(finput);
}
}

fprintf(faction, ";\n break;}");
}



/* generate a dummy symbol, a nonterminal,
whose name cannot conflict with the user's names. */

bucket *
gensym()
{
register bucket *sym;

sprintf (token_buffer, "@%d", ++gensym_count);
sym = getsym(token_buffer);
sym->class = SNTERM;
sym->value = nvars++;
return (sym);
}

/* Parse the input grammar into a one symbol_list structure.
Each rule is represented by a sequence of symbols: the left hand side
followed by the contents of the right hand side, followed by a null pointer
instead of a symbol to terminate the rule.
The next symbol is the lhs of the following rule.

All guards and actions are copied out to the appropriate files,
labelled by the rule number they apply to. */

void
readgram()
{
register int t;
register bucket *lhs;
register symbol_list *p;
register symbol_list *p1;
register bucket *bp;

symbol_list *crule; /* points to first symbol_list of current rule. */
/* its symbol is the lhs of the rule. */
symbol_list *crule1; /* points to the symbol_list preceding crule. */

p1 = NULL;

t = lex();

while (t != TWO_PERCENTS && t != ENDFILE)
{
if (t == IDENTIFIER || t == BAR)
{
register int actionflag = 0;
int rulelength = 0; /* number of symbols in rhs of this rule so far */
int xactions = 0; /* JF for error checking */
bucket *first_rhs = 0;

if (t == IDENTIFIER)
{
lhs = symval;

t = lex();
if (t != COLON)
fatal("ill-formed rule");
}

if (nrules == 0)
{
if (t == BAR)
fatal("grammar starts with vertical bar");

if (!start_flag)
startval = lhs;
}

/* start a new rule and record its lhs. */

nrules++;
nitems++;

record_rule_line ();

p = NEW(symbol_list);
p->sym = lhs;

crule1 = p1;
if (p1)
p1->next = p;
else
grammar = p;

p1 = p;
crule = p;

/* mark the rule's lhs as a nonterminal if not already so. */

if (lhs->class == SUNKNOWN)
{
lhs->class = SNTERM;
lhs->value = nvars;
nvars++;
}
else if (lhs->class == STOKEN)
fatals("rule given for %s, which is a token", lhs->tag);

/* read the rhs of the rule. */

for (;;)
{
t = lex();

if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;

/* If next token is an identifier, see if a colon follows it.
If one does, exit this rule now. */
if (t == IDENTIFIER)
{
register bucket *ssave;
register int t1;

ssave = symval;
t1 = lex();
unlex(t1);
symval = ssave;
if (t1 == COLON) break;

if(!first_rhs) /* JF */
first_rhs = symval;
/* Not followed by colon =>
process as part of this rule's rhs. */
}

/* If we just passed an action, that action was in the middle
of a rule, so make a dummy rule to reduce it to a
non-terminal. */
if (actionflag)
{
register bucket *sdummy;

/* Since the action was written out with this rule's */
/* number, we must write give the new rule this number */
/* by inserting the new rule before it. */

/* Make a dummy nonterminal, a gensym. */
sdummy = gensym();

/* Make a new rule, whose body is empty,
before the current one, so that the action
just read can belong to it. */
nrules++;
nitems++;
record_rule_line ();
p = NEW(symbol_list);
if (crule1)
crule1->next = p;
else grammar = p;
p->sym = sdummy;
crule1 = NEW(symbol_list);
p->next = crule1;
crule1->next = crule;

/* insert the dummy generated by that rule into this rule. */
nitems++;
p = NEW(symbol_list);
p->sym = sdummy;
p1->next = p;
p1 = p;

actionflag = 0;
}

if (t == IDENTIFIER)
{
nitems++;
p = NEW(symbol_list);
p->sym = symval;
p1->next = p;
p1 = p;
}
else /* handle an action. */
{
copy_action(crule, rulelength);
actionflag = 1;
xactions++; /* JF */
}
rulelength++;
}

/* Put an empty link in the list to mark the end of this rule */
p = NEW(symbol_list);
p1->next = p;
p1 = p;

if (t == PREC)
{
t = lex();
crule->ruleprec = symval;
t = lex();
}
if (t == GUARD)
{
if (! semantic_parser)
fatal("%guard present but %semantic_parser not specified");

copy_guard(crule, rulelength);
t = lex();
}
else if (t == LEFT_CURLY)
{
if (actionflag) fatal("two actions at end of one rule");
copy_action(crule, rulelength);
t = lex();
}
/* If $$ is being set in default way,
warn if any type mismatch. */
else if (!xactions && first_rhs && lhs->type_name != first_rhs->type_name)
{
if (lhs->type_name == 0 || first_rhs->type_name == 0
|| strcmp(lhs->type_name,first_rhs->type_name))
fprintf(stderr, "%s:%d: warning: type clash ('%s' '%s') on default action\n",
infile,
lineno,
lhs->type_name ? lhs->type_name : "",
first_rhs->type_name ? first_rhs->type_name : "");
}
/* Warn if there is no default for $$ but we need one. */
else if (!xactions && !first_rhs && lhs->type_name != 0)
fprintf(stderr,
"%s:%d: warning: empty rule for typed nonterminal, and no action\n",
infile,
lineno);
if (t == SEMICOLON)
t = lex();
}
/* these things can appear as alternatives to rules. */
else if (t == TOKEN)
{
parse_token_decl(STOKEN, SNTERM);
t = lex();
}
else if (t == NTERM)
{
parse_token_decl(SNTERM, STOKEN);
t = lex();
}
else if (t == TYPE)
{
t = get_type();
}
else if (t == UNION)
{
parse_union_decl();
t = lex();
}
else if (t == EXPECT)
{
parse_expect_decl();
t = lex();
}
else if (t == START)
{
parse_start_decl();
t = lex();
}
else
fatal("invalid input");
}

if (nsyms > MAXSHORT)
fatals("too many symbols (tokens plus nonterminals); maximum %d",
MAXSHORT);
if (nrules == 0)
fatal("no input grammar");

if (typed == 0 /* JF put out same default YYSTYPE as YACC does */
&& !value_components_used)
{
/* We used to use `unsigned long' as YYSTYPE on MSDOS,
but it seems better to be consistent.
Most programs should declare their own type anyway. */
fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
if (fdefines)
fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
}

/* Report any undefined symbols and consider them nonterminals. */

for (bp = firstsymbol; bp; bp = bp->next)
if (bp->class == SUNKNOWN)
{
fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
bp->tag);
failure = 1;
bp->class = SNTERM;
bp->value = nvars++;
}

ntokens = nsyms - nvars;
}


void
record_rule_line ()
{
/* Record each rule's source line number in rline table. */

if (nrules >= rline_allocated)
{
rline_allocated = nrules * 2;
rline = (short *) xrealloc (rline,
rline_allocated * sizeof (short));
}
rline[nrules] = lineno;
}


/* read in a %type declaration and record its information for get_type_name to access */

int
get_type()
{
register int k;
register int t;
register char *name;

t = lex();

if (t != TYPENAME)
fatal("ill-formed %type declaration");

k = strlen(token_buffer);
name = NEW2(k + 1, char);
strcpy(name, token_buffer);

for (;;)
{
t = lex();

switch (t)
{
case SEMICOLON:
return (lex());

case COMMA:
break;

case IDENTIFIER:
if (symval->type_name == NULL)
symval->type_name = name;
else
fatals("type redeclaration for %s", symval->tag);

break;

default:
return (t);
}
}
}



/* assign symbol numbers, and write definition of token names into fdefines.
Set up vectors tags and sprec of names and precedences of symbols. */

void
packsymbols()
{
register bucket *bp;
register int tokno = 1;
register int i;
register int last_user_token_number;

/* int lossage = 0; JF set but not used */

tags = NEW2(nsyms + 1, char *);
tags[0] = "$";

sprec = NEW2(nsyms, short);
sassoc = NEW2(nsyms, short);

max_user_token_number = 256;
last_user_token_number = 256;

for (bp = firstsymbol; bp; bp = bp->next)
{
if (bp->class == SNTERM)
{
bp->value += ntokens;
}
else
{
if (translations && !(bp->user_token_number))
bp->user_token_number = ++last_user_token_number;
if (bp->user_token_number > max_user_token_number)
max_user_token_number = bp->user_token_number;
bp->value = tokno++;
}

tags[bp->value] = bp->tag;
sprec[bp->value] = bp->prec;
sassoc[bp->value] = bp->assoc;

}

if (translations)
{
register int i;

token_translations = NEW2(max_user_token_number+1, short);

/* initialize all entries for literal tokens to 2,
the internal token number for $illegal., which represents all invalid inputs. */
for (i = 0; i <= max_user_token_number; i++)
token_translations[i] = 2;
}

for (bp = firstsymbol; bp; bp = bp->next)
{
if (bp->value >= ntokens) continue;
if (translations)
{
if (token_translations[bp->user_token_number] != 2)
{
/* JF made this a call to fatals() */
fatals( "tokens %s and %s both assigned number %d",
tags[token_translations[bp->user_token_number]],
bp->tag,
bp->user_token_number);
}
token_translations[bp->user_token_number] = bp->value;
}
}

error_token_number = errtoken->value;

output_token_defines(ftable);

if (startval->class == SUNKNOWN)
fatals("the start symbol %s is undefined", startval->tag);
else if (startval->class == STOKEN)
fatals("the start symbol %s is a token", startval->tag);

start_symbol = startval->value;

if (definesflag)
{
output_token_defines(fdefines);

if (!pure_parser)
{
if (spec_name_prefix)
fprintf(fdefines, "\nextern YYSTYPE %slval;\n", spec_name_prefix);
else
fprintf(fdefines, "\nextern YYSTYPE yylval;\n");
}

if (semantic_parser)
for (i = ntokens; i < nsyms; i++)
{
/* don't make these for dummy nonterminals made by gensym. */
if (*tags[i] != '@')
fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
}
#if 0
/* `fdefines' is now a temporary file, so we need to copy its
contents in `done', so we can't close it here. */
fclose(fdefines);
fdefines = NULL;
#endif
}
}


void
output_token_defines(file)
FILE *file;
{
bucket *bp;

for (bp = firstsymbol; bp; bp = bp->next)
{
if (bp->value >= ntokens) continue;

/* For named tokens, but not literal ones, define the name. */
/* The value is the user token number. */

if ('\'' != *tags[bp->value] && bp != errtoken)
{
register char *cp = tags[bp->value];
register char c;

/* Don't #define nonliteral tokens whose names contain periods. */

while ((c = *cp++) && c != '.');
if (!c)
{
fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
(translations ? bp->user_token_number : bp->value));
if (semantic_parser)
fprintf(file, "#define\tT%s\t%d\n", tags[bp->value],
bp->value);
}
}
}

putc('\n', file);
}



/* convert the rules into the representation using rrhs, rlhs and ritems. */

void
packgram()
{
register int itemno;
register int ruleno;
register symbol_list *p;
/* register bucket *bp; JF unused */

bucket *ruleprec;

ritem = NEW2(nitems + 1, short);
rlhs = NEW2(nrules, short) - 1;
rrhs = NEW2(nrules, short) - 1;
rprec = NEW2(nrules, short) - 1;
rprecsym = NEW2(nrules, short) - 1;
rassoc = NEW2(nrules, short) - 1;

itemno = 0;
ruleno = 1;

p = grammar;
while (p)
{
rlhs[ruleno] = p->sym->value;
rrhs[ruleno] = itemno;
ruleprec = p->ruleprec;

p = p->next;
while (p && p->sym)
{
ritem[itemno++] = p->sym->value;
/* A rule gets by default the precedence and associativity
of the last token in it. */
if (p->sym->class == STOKEN)
{
rprec[ruleno] = p->sym->prec;
rassoc[ruleno] = p->sym->assoc;
}
if (p) p = p->next;
}

/* If this rule has a %prec,
the specified symbol's precedence replaces the default. */
if (ruleprec)
{
rprec[ruleno] = ruleprec->prec;
rassoc[ruleno] = ruleprec->assoc;
rprecsym[ruleno] = ruleprec->value;
}

ritem[itemno++] = -ruleno;
ruleno++;

if (p) p = p->next;
}

ritem[itemno] = 0;
}

/* Read a signed integer from STREAM and return its value. */

int
read_signed_integer (stream)
FILE *stream;
{
register int c = getc(stream);
register int sign = 1;
register int n;

if (c == '-')
{
c = getc(stream);
sign = -1;
}
n = 0;
while (isdigit(c))
{
n = 10*n + (c - '0');
c = getc(stream);
}

ungetc(c, stream);

return n * sign;
}
bison-1.22/reduce.c100666 21570 13 33324 5104660523 12221 0ustar djmuser/* Grammar reduction for Bison.
Copyright (C) 1988, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/*
* Reduce the grammar: Find and eliminate unreachable terminals,
* nonterminals, and productions. David S. Bakin.
*/

/*
* Don't eliminate unreachable terminals: They may be used by the user's
* parser.
*/

#include
#include "system.h"
#include "files.h"
#include "gram.h"
#include "machine.h"
#include "new.h"


extern char **tags; /* reader.c */
extern int verboseflag; /* getargs.c */
static int statisticsflag; /* XXXXXXX */

#ifndef TRUE
#define TRUE (1)
#define FALSE (0)
#endif
typedef int bool;
typedef unsigned *BSet;
typedef short *rule;


/*
* N is set of all nonterminals which are not useless. P is set of all rules
* which have no useless nonterminals in their RHS. V is the set of all
* accessible symbols.
*/

static BSet N, P, V, V1;

static int nuseful_productions, nuseless_productions,
nuseful_nonterminals, nuseless_nonterminals;


static void useless_nonterminals();
static void inaccessable_symbols();
static void reduce_grammar_tables();
static void print_results();
static void print_notices();
void dump_grammar();

extern void fatals ();


bool
bits_equal (L, R, n)
BSet L;
BSet R;
int n;
{
int i;

for (i = n - 1; i >= 0; i--)
if (L[i] != R[i])
return FALSE;
return TRUE;
}


int
nbits (i)
unsigned i;
{
int count = 0;

while (i != 0) {
i ^= (i & -i);
++count;
}
return count;
}


int
bits_size (S, n)
BSet S;
int n;
{
int i, count = 0;

for (i = n - 1; i >= 0; i--)
count += nbits(S[i]);
return count;
}

void
reduce_grammar ()
{
bool reduced;

/* Allocate the global sets used to compute the reduced grammar */

N = NEW2(WORDSIZE(nvars), unsigned);
P = NEW2(WORDSIZE(nrules + 1), unsigned);
V = NEW2(WORDSIZE(nsyms), unsigned);
V1 = NEW2(WORDSIZE(nsyms), unsigned);

useless_nonterminals();
inaccessable_symbols();

reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);

if (verboseflag)
print_results();

if (reduced == FALSE)
goto done_reducing;

print_notices();

if (!BITISSET(N, start_symbol - ntokens))
fatals("Start symbol %s does not derive any sentence.",
tags[start_symbol]);

reduce_grammar_tables();
/* if (verboseflag) {
fprintf(foutput, "REDUCED GRAMMAR\n\n");
dump_grammar();
}
*/

/**/ statisticsflag = FALSE; /* someday getopts should handle this */
if (statisticsflag == TRUE)
fprintf(stderr,
"reduced %s defines %d terminal%s, %d nonterminal%s\
, and %d production%s.\n", infile,
ntokens, (ntokens == 1 ? "" : "s"),
nvars, (nvars == 1 ? "" : "s"),
nrules, (nrules == 1 ? "" : "s"));

done_reducing:

/* Free the global sets used to compute the reduced grammar */

FREE(N);
FREE(V);
FREE(P);

}

/*
* Another way to do this would be with a set for each production and then do
* subset tests against N, but even for the C grammar the whole reducing
* process takes only 2 seconds on my 8Mhz AT.
*/

static bool
useful_production (i, N)
int i;
BSet N;
{
rule r;
short n;

/*
* A production is useful if all of the nonterminals in its RHS
* appear in the set of useful nonterminals.
*/

for (r = &ritem[rrhs[i]]; *r > 0; r++)
if (ISVAR(n = *r))
if (!BITISSET(N, n - ntokens))
return FALSE;
return TRUE;
}


/* Remember that rules are 1-origin, symbols are 0-origin. */

static void
useless_nonterminals ()
{
BSet Np, Ns;
int i, n;

/*
* N is set as built. Np is set being built this iteration. P is set
* of all productions which have a RHS all in N.
*/

Np = NEW2(WORDSIZE(nvars), unsigned);

/*
* The set being computed is a set of nonterminals which can derive
* the empty string or strings consisting of all terminals. At each
* iteration a nonterminal is added to the set if there is a
* production with that nonterminal as its LHS for which all the
* nonterminals in its RHS are already in the set. Iterate until the
* set being computed remains unchanged. Any nonterminals not in the
* set at that point are useless in that they will never be used in
* deriving a sentence of the language.
*
* This iteration doesn't use any special traversal over the
* productions. A set is kept of all productions for which all the
* nonterminals in the RHS are in useful. Only productions not in
* this set are scanned on each iteration. At the end, this set is
* saved to be used when finding useful productions: only productions
* in this set will appear in the final grammar.
*/

n = 0;
while (1)
{
for (i = WORDSIZE(nvars) - 1; i >= 0; i--)
Np[i] = N[i];
for (i = 1; i <= nrules; i++)
{
if (!BITISSET(P, i))
{
if (useful_production(i, N))
{
SETBIT(Np, rlhs[i] - ntokens);
SETBIT(P, i);
}
}
}
if (bits_equal(N, Np, WORDSIZE(nvars)))
break;
Ns = Np;
Np = N;
N = Ns;
}
FREE(N);
N = Np;
}

static void
inaccessable_symbols ()
{
BSet Vp, Vs, Pp;
int i, n;
short t;
rule r;

/*
* Find out which productions are reachable and which symbols are
* used. Starting with an empty set of productions and a set of
* symbols which only has the start symbol in it, iterate over all
* productions until the set of productions remains unchanged for an
* iteration. For each production which has a LHS in the set of
* reachable symbols, add the production to the set of reachable
* productions, and add all of the nonterminals in the RHS of the
* production to the set of reachable symbols.
*
* Consider only the (partially) reduced grammar which has only
* nonterminals in N and productions in P.
*
* The result is the set P of productions in the reduced grammar, and
* the set V of symbols in the reduced grammar.
*
* Although this algorithm also computes the set of terminals which are
* reachable, no terminal will be deleted from the grammar. Some
* terminals might not be in the grammar but might be generated by
* semantic routines, and so the user might want them available with
* specified numbers. (Is this true?) However, the nonreachable
* terminals are printed (if running in verbose mode) so that the user
* can know.
*/

Vp = NEW2(WORDSIZE(nsyms), unsigned);
Pp = NEW2(WORDSIZE(nrules + 1), unsigned);

/* If the start symbol isn't useful, then nothing will be useful. */
if (!BITISSET(N, start_symbol - ntokens))
goto end_iteration;

SETBIT(V, start_symbol);

n = 0;
while (1)
{
for (i = WORDSIZE(nsyms) - 1; i >= 0; i--)
Vp[i] = V[i];
for (i = 1; i <= nrules; i++)
{
if (!BITISSET(Pp, i) && BITISSET(P, i) &&
BITISSET(V, rlhs[i]))
{
for (r = &ritem[rrhs[i]]; *r >= 0; r++)
{
if (ISTOKEN(t = *r)
|| BITISSET(N, t - ntokens))
{
SETBIT(Vp, t);
}
}
SETBIT(Pp, i);
}
}
if (bits_equal(V, Vp, WORDSIZE(nsyms)))
{
break;
}
Vs = Vp;
Vp = V;
V = Vs;
}
end_iteration:

FREE(V);
V = Vp;

/* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
SETBIT(V, 0); /* end-of-input token */
SETBIT(V, 1); /* error token */
SETBIT(V, 2); /* illegal token */

FREE(P);
P = Pp;

nuseful_productions = bits_size(P, WORDSIZE(nrules + 1));
nuseless_productions = nrules - nuseful_productions;

nuseful_nonterminals = 0;
for (i = ntokens; i < nsyms; i++)
if (BITISSET(V, i))
nuseful_nonterminals++;
nuseless_nonterminals = nvars - nuseful_nonterminals;

/* A token that was used in %prec should not be warned about. */
for (i = 1; i < nrules; i++)
if (rprecsym[i] != 0)
SETBIT(V1, rprecsym[i]);
}

static void
reduce_grammar_tables ()
{
/* This is turned off because we would need to change the numbers
in the case statements in the actions file. */
#if 0
/* remove useless productions */
if (nuseless_productions > 0)
{
short np, pn, ni, pi;

np = 0;
ni = 0;
for (pn = 1; pn <= nrules; pn++)
{
if (BITISSET(P, pn))
{
np++;
if (pn != np)
{
rlhs[np] = rlhs[pn];
rline[np] = rline[pn];
rprec[np] = rprec[pn];
rassoc[np] = rassoc[pn];
rrhs[np] = rrhs[pn];
if (rrhs[np] != ni)
{
pi = rrhs[np];
rrhs[np] = ni;
while (ritem[pi] >= 0)
ritem[ni++] = ritem[pi++];
ritem[ni++] = -np;
}
} else {
while (ritem[ni++] >= 0);
}
}
}
ritem[ni] = 0;
nrules -= nuseless_productions;
nitems = ni;

/*
* Is it worth it to reduce the amount of memory for the
* grammar? Probably not.
*/

}
#endif /* 0 */
/* Disable useless productions,
since they may contain useless nonterms
that would get mapped below to -1 and confuse everyone. */
if (nuseless_productions > 0)
{
int pn;

for (pn = 1; pn <= nrules; pn++)
{
if (!BITISSET(P, pn))
{
rlhs[pn] = -1;
}
}
}

/* remove useless symbols */
if (nuseless_nonterminals > 0)
{

int i, n;
/* short j; JF unused */
short *nontermmap;
rule r;

/*
* create a map of nonterminal number to new nonterminal
* number. -1 in the map means it was useless and is being
* eliminated.
*/

nontermmap = NEW2(nvars, short) - ntokens;
for (i = ntokens; i < nsyms; i++)
nontermmap[i] = -1;

n = ntokens;
for (i = ntokens; i < nsyms; i++)
if (BITISSET(V, i))
nontermmap[i] = n++;

/* Shuffle elements of tables indexed by symbol number. */

for (i = ntokens; i < nsyms; i++)
{
n = nontermmap[i];
if (n >= 0)
{
sassoc[n] = sassoc[i];
sprec[n] = sprec[i];
tags[n] = tags[i];
} else {
free(tags[i]);
}
}

/* Replace all symbol numbers in valid data structures. */

for (i = 1; i <= nrules; i++)
{
/* Ignore the rules disabled above. */
if (rlhs[i] >= 0)
rlhs[i] = nontermmap[rlhs[i]];
if (ISVAR (rprecsym[i]))
/* Can this happen? */
rprecsym[i] = nontermmap[rprecsym[i]];
}

for (r = ritem; *r; r++)
if (ISVAR(*r))
*r = nontermmap[*r];

start_symbol = nontermmap[start_symbol];

nsyms -= nuseless_nonterminals;
nvars -= nuseless_nonterminals;

free(&nontermmap[ntokens]);
}
}

static void
print_results ()
{
int i;
/* short j; JF unused */
rule r;
bool b;

if (nuseless_nonterminals > 0)
{
fprintf(foutput, "Useless nonterminals:\n\n");
for (i = ntokens; i < nsyms; i++)
if (!BITISSET(V, i))
fprintf(foutput, " %s\n", tags[i]);
}
b = FALSE;
for (i = 0; i < ntokens; i++)
{
if (!BITISSET(V, i) && !BITISSET(V1, i))
{
if (!b)
{
fprintf(foutput, "\n\nTerminals which are not used:\n\n");
b = TRUE;
}
fprintf(foutput, " %s\n", tags[i]);
}
}

if (nuseless_productions > 0)
{
fprintf(foutput, "\n\nUseless rules:\n\n");
for (i = 1; i <= nrules; i++)
{
if (!BITISSET(P, i))
{
fprintf(foutput, "#%-4d ", i);
fprintf(foutput, "%s :\t", tags[rlhs[i]]);
for (r = &ritem[rrhs[i]]; *r >= 0; r++)
{
fprintf(foutput, " %s", tags[*r]);
}
fprintf(foutput, ";\n");
}
}
}
if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
fprintf(foutput, "\n\n");
}

void
dump_grammar ()
{
int i;
rule r;

fprintf(foutput,
"ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
ntokens, nvars, nsyms, nrules, nitems);
fprintf(foutput, "Variables\n---------\n\n");
fprintf(foutput, "Value Sprec Sassoc Tag\n");
for (i = ntokens; i < nsyms; i++)
fprintf(foutput, "%5d %5d %5d %s\n",
i, sprec[i], sassoc[i], tags[i]);
fprintf(foutput, "\n\n");
fprintf(foutput, "Rules\n-----\n\n");
for (i = 1; i <= nrules; i++)
{
fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
for (r = &ritem[rrhs[i]]; *r > 0; r++)
fprintf(foutput, "%5d", *r);
fprintf(foutput, " [%d]\n", -(*r));
}
fprintf(foutput, "\n\n");
fprintf(foutput, "Rules interpreted\n-----------------\n\n");
for (i = 1; i <= nrules; i++)
{
fprintf(foutput, "%-5d %s :", i, tags[rlhs[i]]);
for (r = &ritem[rrhs[i]]; *r > 0; r++)
fprintf(foutput, " %s", tags[*r]);
fprintf(foutput, "\n");
}
fprintf(foutput, "\n\n");
}


static void
print_notices ()
{
extern int fixed_outfiles;

if (fixed_outfiles && nuseless_productions)
fprintf(stderr, "%d rules never reduced\n", nuseless_productions);

fprintf(stderr, "%s contains ", infile);

if (nuseless_nonterminals > 0)
{
fprintf(stderr, "%d useless nonterminal%s",
nuseless_nonterminals,
(nuseless_nonterminals == 1 ? "" : "s"));
}
if (nuseless_nonterminals > 0 && nuseless_productions > 0)
fprintf(stderr, " and ");

if (nuseless_productions > 0)
{
fprintf(stderr, "%d useless rule%s",
nuseless_productions,
(nuseless_productions == 1 ? "" : "s"));
}
fprintf(stderr, ".\n");
fflush(stderr);
}
bison-1.22/symtab.c100666 21570 13 4750 5364706514 12243 0ustar djmuser/* Symbol table manager for Bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include "system.h"
#include "new.h"
#include "symtab.h"
#include "gram.h"


bucket **symtab;
bucket *firstsymbol;
bucket *lastsymbol;



int
hash(key)
char *key;
{
register char *cp;
register int k;

cp = key;
k = 0;
while (*cp)
k = ((k << 1) ^ (*cp++)) & 0x3fff;

return (k % TABSIZE);
}



char *
copys(s)
char *s;
{
register int i;
register char *cp;
register char *result;

i = 1;
for (cp = s; *cp; cp++)
i++;

result = xmalloc((unsigned int)i);
strcpy(result, s);
return (result);
}


void
tabinit()
{
/* register int i; JF unused */

symtab = NEW2(TABSIZE, bucket *);

firstsymbol = NULL;
lastsymbol = NULL;
}


bucket *
getsym(key)
char *key;
{
register int hashval;
register bucket *bp;
register int found;

hashval = hash(key);
bp = symtab[hashval];

found = 0;
while (bp != NULL && found == 0)
{
if (strcmp(key, bp->tag) == 0)
found = 1;
else
bp = bp->link;
}

if (found == 0)
{
nsyms++;

bp = NEW(bucket);
bp->link = symtab[hashval];
bp->next = NULL;
bp->tag = copys(key);
bp->class = SUNKNOWN;

if (firstsymbol == NULL)
{
firstsymbol = bp;
lastsymbol = bp;
}
else
{
lastsymbol->next = bp;
lastsymbol = bp;
}

symtab[hashval] = bp;
}

return (bp);
}


void
free_symtab()
{
register int i;
register bucket *bp,*bptmp;/* JF don't use ptr after free */

for (i = 0; i < TABSIZE; i++)
{
bp = symtab[i];
while (bp)
{
bptmp = bp->link;
#if 0 /* This causes crashes because one string can appear more than once. */
if (bp->type_name)
FREE(bp->type_name);
#endif
FREE(bp);
bp = bptmp;
}
}
FREE(symtab);
}
bison-1.22/version.c100666 21641 1 63 5442710112 13620 0ustar friedmandaemonchar *version_string = "GNU Bison version 1.22\n";
bison-1.22/warshall.c100666 21570 1 4574 5104660526 13021 0ustar djmdaemon/* Generate transitive closure of a matrix,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include "system.h"
#include "machine.h"


/* given n by n matrix of bits R, modify its contents
to be the transive closure of what was given. */

void
TC(R, n)
unsigned *R;
int n;
{
register int rowsize;
register unsigned mask;
register unsigned *rowj;
register unsigned *rp;
register unsigned *rend;
register unsigned *ccol;

unsigned *relend;
unsigned *cword;
unsigned *rowi;

rowsize = WORDSIZE(n) * sizeof(unsigned);
relend = (unsigned *) ((char *) R + (n * rowsize));

cword = R;
mask = 1;
rowi = R;
while (rowi < relend)
{
ccol = cword;
rowj = R;

while (rowj < relend)
{
if (*ccol & mask)
{
rp = rowi;
rend = (unsigned *) ((char *) rowj + rowsize);

while (rowj < rend)
*rowj++ |= *rp++;
}
else
{
rowj = (unsigned *) ((char *) rowj + rowsize);
}

ccol = (unsigned *) ((char *) ccol + rowsize);
}

mask <<= 1;
if (mask == 0)
{
mask = 1;
cword++;
}

rowi = (unsigned *) ((char *) rowi + rowsize);
}
}


/* Reflexive Transitive Closure. Same as TC
and then set all the bits on the diagonal of R. */

void
RTC(R, n)
unsigned *R;
int n;
{
register int rowsize;
register unsigned mask;
register unsigned *rp;
register unsigned *relend;

TC(R, n);

rowsize = WORDSIZE(n) * sizeof(unsigned);
relend = (unsigned *) ((char *) R + n*rowsize);

mask = 1;
rp = R;
while (rp < relend)
{
*rp |= mask;

mask <<= 1;
if (mask == 0)
{
mask = 1;
rp++;
}

rp = (unsigned *) ((char *) rp + rowsize);
}
}
bison-1.22/files.h100666 21570 1 4201 5104660514 12273 0ustar djmdaemon/* File names and variables for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* These two should be pathnames for opening the sample parser files.
When bison is installed, they should be absolute pathnames.
XPFILE1 and XPFILE2 normally come from the Makefile. */

#define PFILE XPFILE /* Simple parser */
#define PFILE1 XPFILE1 /* Semantic parser */

extern FILE *finput; /* read grammar specifications */
extern FILE *foutput; /* optionally output messages describing the actions taken */
extern FILE *fdefines; /* optionally output #define's for token numbers. */
extern FILE *ftable; /* output the tables and the parser */
extern FILE *fattrs; /* if semantic parser, output a .h file that defines YYSTYPE */
/* and also contains all the %{ ... %} definitions. */
extern FILE *fguard; /* if semantic parser, output yyguard, containing all the guard code */
extern FILE *faction; /* output all the action code; precise form depends on which parser */
extern FILE *fparser; /* read the parser to copy into ftable */

/* File name specified with -o for the output file, or 0 if no -o. */
extern char *spec_outfile;

extern char *spec_name_prefix; /* for -a, from getargs.c */

/* File name pfx specified with -b, or 0 if no -b. */
extern char *spec_file_prefix;

extern char *infile;
extern char *outfile;
extern char *defsfile;
extern char *tabfile;
extern char *attrsfile;
extern char *guardfile;
extern char *actfile;
bison-1.22/gram.h100666 21570 1 10166 5104660516 12150 0ustar djmdaemon/* Data definitions for internal representation of bison's input,
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* representation of the grammar rules:

ntokens is the number of tokens, and nvars is the number of variables
(nonterminals). nsyms is the total number, ntokens + nvars.

Each symbol (either token or variable) receives a symbol number.
Numbers 0 to ntokens-1 are for tokens, and ntokens to nsyms-1 are for
variables. Symbol number zero is the end-of-input token. This token
is counted in ntokens.

The rules receive rule numbers 1 to nrules in the order they are written.
Actions and guards are accessed via the rule number.

The rules themselves are described by three arrays: rrhs, rlhs and
ritem. rlhs[R] is the symbol number of the left hand side of rule R.
The right hand side is stored as symbol numbers in a portion of
ritem. rrhs[R] contains the index in ritem of the beginning of the
portion for rule R.

If rlhs[R] is -1, the rule has been thrown out by reduce.c
and should be ignored.

The length of the portion is one greater
than the number of symbols in the rule's right hand side.
The last element in the portion contains minus R, which
identifies it as the end of a portion and says which rule it is for.

The portions of ritem come in order of increasing rule number and are
followed by an element which is zero to mark the end. nitems is the
total length of ritem, not counting the final zero. Each element of
ritem is called an "item" and its index in ritem is an item number.

Item numbers are used in the finite state machine to represent
places that parsing can get to.

Precedence levels are recorded in the vectors sprec and rprec.
sprec records the precedence level of each symbol,
rprec the precedence level of each rule.
rprecsym is the symbol-number of the symbol in %prec for this rule (if any).

Precedence levels are assigned in increasing order starting with 1 so
that numerically higher precedence values mean tighter binding as they
ought to. Zero as a symbol or rule's precedence means none is
assigned.

Associativities are recorded similarly in rassoc and sassoc. */


#define ISTOKEN(s) ((s) < ntokens)
#define ISVAR(s) ((s) >= ntokens)


extern int nitems;
extern int nrules;
extern int nsyms;
extern int ntokens;
extern int nvars;

extern short *ritem;
extern short *rlhs;
extern short *rrhs;
extern short *rprec;
extern short *rprecsym;
extern short *sprec;
extern short *rassoc;
extern short *sassoc;
extern short *rline; /* Source line number of each rule */

extern int start_symbol;


/* associativity values in elements of rassoc, sassoc. */

#define RIGHT_ASSOC 1
#define LEFT_ASSOC 2
#define NON_ASSOC 3

/* token translation table:
indexed by a token number as returned by the user's yylex routine,
it yields the internal token number used by the parser and throughout bison.
If translations is zero, the translation table is not used because
the two kinds of token numbers are the same. */

extern short *token_translations;
extern int translations;
extern int max_user_token_number;

/* semantic_parser is nonzero if the input file says to use the hairy parser
that provides for semantic error recovery. If it is zero, the yacc-compatible
simplified parser is used. */

extern int semantic_parser;

/* pure_parser is nonzero if should generate a parser that is all pure and reentrant. */

extern int pure_parser;

/* error_token_number is the token number of the error token. */

extern int error_token_number;
bison-1.22/lex.h100666 21570 1 2571 5104660517 11774 0ustar djmdaemon/* Token type definitions for bison's input reader,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#define ENDFILE 0
#define IDENTIFIER 1
#define COMMA 2
#define COLON 3
#define SEMICOLON 4
#define BAR 5
#define LEFT_CURLY 6
#define TWO_PERCENTS 7
#define PERCENT_LEFT_CURLY 8
#define TOKEN 9
#define NTERM 10
#define GUARD 11
#define TYPE 12
#define UNION 13
#define START 14
#define LEFT 15
#define RIGHT 16
#define NONASSOC 17
#define PREC 18
#define SEMANTIC_PARSER 19
#define PURE_PARSER 20
#define TYPENAME 21
#define NUMBER 22
#define EXPECT 23
#define ILLEGAL 24

#define MAXTOKEN 1024
bison-1.22/machine.h100666 12120 1 2557 5137167750 12641 0ustar rmsdaemon/* Define machine-dependencies for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifdef eta10
#define MAXSHORT 2147483647
#define MINSHORT -2147483648
#else
#define MAXSHORT 32767
#define MINSHORT -32768
#endif

#if defined (MSDOS) && !defined (__GO32__)
#define BITS_PER_WORD 16
#define MAXTABLE 16383
#else
#define BITS_PER_WORD 32
#define MAXTABLE 32767
#endif

#define WORDSIZE(n) (((n) + BITS_PER_WORD - 1) / BITS_PER_WORD)
#define SETBIT(x, i) ((x)[(i)/BITS_PER_WORD] |= (1<<((i) % BITS_PER_WORD)))
#define RESETBIT(x, i) ((x)[(i)/BITS_PER_WORD] &= ~(1<<((i) % BITS_PER_WORD)))
#define BITISSET(x, i) (((x)[(i)/BITS_PER_WORD] & (1<<((i) % BITS_PER_WORD))) != 0)
bison-1.22/new.h100666 21641 13 2140 5366014531 12534 0ustar friedmanuser/* Storage allocation interface for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#define NEW(t) ((t *) xmalloc((unsigned) sizeof(t)))
#define NEW2(n, t) ((t *) xmalloc((unsigned) ((n) * sizeof(t))))

#ifdef __STDC__
#define FREE(x) (x ? (void) free((char *) (x)) : (void)0)
#else
#define FREE(x) ((x) != 0 && (free ((char *) (x)), 0))
#endif

extern char *xmalloc();
extern char *xrealloc();
bison-1.22/state.h100666 12120 1 11067 5124501340 12350 0ustar rmsdaemon/* Type definitions for nondeterministic finite state machine for bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


/* These type definitions are used to represent a nondeterministic
finite state machine that parses the specified grammar.
This information is generated by the function generate_states
in the file LR0.

Each state of the machine is described by a set of items --
particular positions in particular rules -- that are the possible
places where parsing could continue when the machine is in this state.
These symbols at these items are the allowable inputs that can follow now.

A core represents one state. States are numbered in the number field.
When generate_states is finished, the starting state is state 0
and nstates is the number of states. (A transition to a state
whose state number is nstates indicates termination.) All the cores
are chained together and first_state points to the first one (state 0).

For each state there is a particular symbol which must have been the
last thing accepted to reach that state. It is the accessing_symbol
of the core.

Each core contains a vector of nitems items which are the indices
in the ritems vector of the items that are selected in this state.

The link field is used for chaining buckets that hash states by
their itemsets. This is for recognizing equivalent states and
combining them when the states are generated.

The two types of transitions are shifts (push the lookahead token
and read another) and reductions (combine the last n things on the
stack via a rule, replace them with the symbol that the rule derives,
and leave the lookahead token alone). When the states are generated,
these transitions are represented in two other lists.

Each shifts structure describes the possible shift transitions out
of one state, the state whose number is in the number field.
The shifts structures are linked through next and first_shift points to them.
Each contains a vector of numbers of the states that shift transitions
can go to. The accessing_symbol fields of those states' cores say what kind
of input leads to them.

A shift to state zero should be ignored. Conflict resolution
deletes shifts by changing them to zero.

Each reductions structure describes the possible reductions at the state
whose number is in the number field. The data is a list of nreds rules,
represented by their rule numbers. first_reduction points to the list
of these structures.

Conflict resolution can decide that certain tokens in certain
states should explicitly be errors (for implementing %nonassoc).
For each state, the tokens that are errors for this reason
are recorded in an errs structure, which has the state number
in its number field. The rest of the errs structure is full
of token numbers.

There is at least one shift transition present in state zero.
It leads to a next-to-final state whose accessing_symbol is
the grammar's start symbol. The next-to-final state has one shift
to the final state, whose accessing_symbol is zero (end of input).
The final state has one shift, which goes to the termination state
(whose number is nstates-1).
The reason for the extra state at the end is to placate the parser's
strategy of making all decisions one token ahead of its actions. */


typedef
struct core
{
struct core *next;
struct core *link;
short number;
short accessing_symbol;
short nitems;
short items[1];
}
core;



typedef
struct shifts
{
struct shifts *next;
short number;
short nshifts;
short shifts[1];
}
shifts;



typedef
struct errs
{
short nerrs;
short errs[1];
}
errs;



typedef
struct reductions
{
struct reductions *next;
short number;
short nreds;
short rules[1];
}
reductions;



extern int nstates;
extern core *first_state;
extern shifts *first_shift;
extern reductions *first_reduction;
bison-1.22/symtab.h100666 21570 1 2340 5104660524 12473 0ustar djmdaemon/* Definitions for symtab.c and callers, part of bison,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


#define TABSIZE 1009


/* symbol classes */

#define SUNKNOWN 0
#define STOKEN 1
#define SNTERM 2


typedef
struct bucket
{
struct bucket *link;
struct bucket *next;
char *tag;
char *type_name;
short value;
short prec;
short assoc;
short user_token_number;
char class;
}
bucket;


extern bucket **symtab;
extern bucket *firstsymbol;

extern bucket *getsym();
bison-1.22/system.h100666 21641 13 1272 5352000614 13264 0ustar friedmanuser#ifdef MSDOS
#include
#endif

#if defined(HAVE_STDLIB_H) || defined(MSDOS)
#include
#endif

#if (defined(VMS) || defined(MSDOS)) && !defined(HAVE_STRING_H)
#define HAVE_STRING_H 1
#endif

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include
/* An ANSI string.h and pre-ANSI memory.h might conflict. */
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#define bcopy(src, dst, num) memcpy((dst), (src), (num))
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include
/* memory.h and strings.h conflict on some systems. */
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
bison-1.22/types.h100666 21570 1 1643 5104660525 12346 0ustar djmdaemon/* Define data type for representing bison's grammar input as it is parsed,
Copyright (C) 1984, 1989 Free Software Foundation, Inc.

This file is part of Bison, the GNU Compiler Compiler.

Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */


typedef
struct shorts
{
struct shorts *next;
short value;
}
shorts;
bison-1.22/bison.cld100666 12120 1 1201 5221250767 12636 0ustar rmsdaemon!
! VMS BISON command definition file
!
DEFINE VERB BISON
IMAGE GNU_BISON:[000000]BISON

PARAMETER P1,Label=BISON$INFILE,Prompt="File"
value(required,type=$infile)
QUALIFIER VERBOSE,Label=BISON$VERBOSE
QUALIFIER DEFINES,Label=BISON$DEFINES
QUALIFIER FIXED_OUTFILES,Label=BISON$FIXED_OUTFILES
qualifier nolines,Label=BISON$NOLINES
qualifier debug,Label=BISON$DEBUG
qualifier output,value(type=$outfile),Label=BISON$OUTPUT
qualifier version,label=BISON$VERSION
qualifier yacc,label=BISON$YACC
qualifier file_prefix,value(type=$outfile),label=BISON$FILE_PREFIX
qualifier name_prefix,value(type=$outfile),LABEL=BISON$NAME_PREFIX
bison-1.22/build.com100666 12120 1 5065 5221441357 12650 0ustar rmsdaemon$! Set the def dir to proper place for use in batch. Works for interactive too.
$flnm = f$enviroment("PROCEDURE") ! get current procedure name
$set default 'f$parse(flnm,,,"DEVICE")''f$parse(flnm,,,"DIRECTORY")'
$!
$! This command procedure compiles and links BISON for VMS.
$! BISON has been tested with VAXC version 2.3 and VMS version 4.5
$! and on VMS 4.5 with GCC 1.12.
$!
$! Bj|rn Larsen [email protected]
$! With some contributions by Gabor Karsai,
$! KARSAIG1%[email protected]
$! All merged and cleaned by RMS.
$!
$! Adapted for both VAX-11 "C" and VMS/GCC compilation by
$! David L. Kashtan kashtan.iu.ai.sri.com
$!
$! First we try to sense which C compiler we have available. Sensing logic
$! borrowed from Emacs.
$!
$set noon !do not bomb if an error occurs.
$assign nla0: sys$output
$assign nla0: sys$error !so we do not get an error message about this.
$cc nla0:compiler_check.c
$if $status.eq.%x38090 then goto try_gcc
$ CC :== CC
$ cc_options:="/NOLIST/define=(""index=strchr"",""rindex=strrchr"")"
$ extra_linker_files:="VMSHLP,"
$goto have_compiler
$!
$try_gcc:
$gcc nla0:compiler_check.c
$if $status.eq.%x38090 then goto whoops
$ CC :== GCC
$ cc_options:="/DEBUG"
$ extra_linker_files:="GNU_CC:[000000]GCCLIB/LIB,"
$goto have_compiler
$!
$whoops:
$write sys$output "You must have a C compiler to build BISON. Sorry."
$deassign sys$output
$deassign sys$error
$exit %x38090
$!
$!
$have_compiler:
$deassign sys$output
$deassign sys$error
$set on
$if f$search("compiler_check.obj").nes."" then dele/nolog compiler_check.obj;
$write sys$output "Building BISON with the ''cc' compiler."
$!
$! Do the compilation (compiler type is all set up)
$!
$ Compile:
$ if "''p1'" .eqs. "LINK" then goto Link
$ 'CC' 'cc_options' files.c
$ 'CC' 'cc_options' LR0.C
$ 'CC' 'cc_options' ALLOCATE.C
$ 'CC' 'cc_options' CLOSURE.C
$ 'CC' 'cc_options' CONFLICTS.C
$ 'CC' 'cc_options' DERIVES.C
$ 'CC' 'cc_options' VMSGETARGS.C
$ 'CC' 'cc_options' GRAM.C
$ 'CC' 'cc_options' LALR.C
$ 'CC' 'cc_options' LEX.C
$ 'CC' 'cc_options' MAIN.C
$ 'CC' 'cc_options' NULLABLE.C
$ 'CC' 'cc_options' OUTPUT.C
$ 'CC' 'cc_options' PRINT.C
$ 'CC' 'cc_options' READER.C
$ 'CC' 'cc_options' REDUCE.C
$ 'CC' 'cc_options' SYMTAB.C
$ 'CC' 'cc_options' WARSHALL.C
$ 'CC' 'cc_options' VERSION.C
$ if "''CC'" .eqs. "CC" then macro vmshlp.mar
$ Link:
$ link/exec=bison main,LR0,allocate,closure,conflicts,derives,files,-
vmsgetargs,gram,lalr,lex,nullable,output,print,reader,reduce,symtab,warshall,-
version,'extra_linker_files'sys$library:vaxcrtl/lib
$!
$! Generate bison.hlp (for online help).
$!
$runoff bison.rnh
bison-1.22/vmsgetargs.c100666 12120 1 7462 5221250772 13401 0ustar rmsdaemon/* VMS version of getargs; Uses DCL command parsing.
Copyright (C) 1989 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */


#include
#include
#include "files.h"

/*
* VMS version of getargs(): Uses DCL command parsing
* (argc and argv are ignored)
*/
int verboseflag;
int definesflag;
int debugflag;
int nolinesflag;
extern int fixed_outfiles;
extern char * version_string;

/* Allocate storgate and initialize, since bison uses them elsewhere. */
char *spec_name_prefix;
char *spec_file_prefix;

getargs(argc,argv)
int argc;
char *argv[];
{
register char *cp;
static char Input_File[256];
static char output_spec[256], name_prefix_spec[256], file_prefix_spec[256];
extern char *infile;

verboseflag = 0;
definesflag = 0;
debugflag = 0;
fixed_outfiles = 0;
nolinesflag = 0;
/*
* Check for /VERBOSE qualifier
*/
if (cli_present("BISON$VERBOSE")) verboseflag = 1;
/*
* Check for /DEFINES qualifier
*/
if (cli_present("BISON$DEFINES")) definesflag = 1;
/*
* Check for /FIXED_OUTFILES qualifier
*/
if (cli_present("BISON$FIXED_OUTFILES")) fixed_outfiles = 1;
if (cli_present("BISON$YACC")) fixed_outfiles = 1;
/*
* Check for /VERSION qualifier
*/
if (cli_present("BISON$VERSION")) printf("%s",version_string);
/*
* Check for /NOLINES qualifier
*/
if (cli_present("BISON$NOLINES")) nolinesflag = 1;
/*
* Check for /DEBUG qualifier
*/
if (cli_present("BISON$DEBUG")) debugflag = 1;
/*
* Get the filename
*/
cli_get_value("BISON$INFILE", Input_File, sizeof(Input_File));
/*
* Lowercaseify the input filename
*/
cp = Input_File;
while(*cp)
{
if (isupper(*cp)) *cp = tolower(*cp);
cp++;
}
infile = Input_File;
/*
* Get the output file
*/
if (cli_present("BISON$OUTPUT"))
{
cli_get_value("BISON$OUTPUT", output_spec, sizeof(output_spec));
for (cp = spec_outfile = output_spec; *cp; cp++)
if (isupper(*cp))
*cp = tolower(*cp);
}
/*
* Get the output file
*/
if (cli_present("BISON$FILE_PREFIX"))
{
cli_get_value("BISON$FILE_PREFIX", file_prefix_spec,
sizeof(file_prefix_spec));
for (cp = spec_file_prefix = file_prefix_spec; *cp; cp++)
if (isupper(*cp))
*cp = tolower(*cp);
}
/*
* Get the output file
*/
if (cli_present("BISON$NAME_PREFIX"))
{
cli_get_value("BISON$NAME_PREFIX", name_prefix_spec,
sizeof(name_prefix_spec));
for (cp = spec_name_prefix = name_prefix_spec; *cp; cp++)
if (isupper(*cp))
*cp = tolower(*cp);
}
}

/************ DCL PARSING ROUTINES **********/

/*
* See if "NAME" is present
*/
int
cli_present(Name)
char *Name;
{
struct {int Size; char *Ptr;} Descr;

Descr.Ptr = Name;
Descr.Size = strlen(Name);
return((cli$present(&Descr) & 1) ? 1 : 0);
}

/*
* Get value of "NAME"
*/
int
cli_get_value(Name,Buffer,Size)
char *Name;
char *Buffer;
{
struct {int Size; char *Ptr;} Descr1,Descr2;

Descr1.Ptr = Name;
Descr1.Size = strlen(Name);
Descr2.Ptr = Buffer;
Descr2.Size = Size-1;
if (cli$get_value(&Descr1,&Descr2,&Descr2.Size) & 1) {
Buffer[Descr2.Size] = 0;
return(1);
}
return(0);
}
bison-1.22/vmshlp.mar100666 12120 1 2764 4125672766 13102 0ustar rmsdaemon;/* Macro help routines for the BISON/VMS program
; Gabor Karsai, Vanderbilt University
;
;BISON is distributed in the hope that it will be useful, but WITHOUT ANY
;WARRANTY. No author or distributor accepts responsibility to anyone
;for the consequences of using it or for whether it serves any
;particular purpose or works at all, unless he says so in writing.
;Refer to the BISON General Public License for full details.
;
;Everyone is granted permission to copy, modify and redistribute BISON,
;but only under the conditions described in the BISON General Public
;License. A copy of this license is supposed to have been given to you
;along with BISON so you can know your rights and responsibilities. It
;should be in a file named COPYING. Among other things, the copyright
;notice and this notice must be preserved on all copies.
;
; In other words, you are welcome to use, share and improve this program.
; You are forbidden to forbid anyone else to use, share and improve
; what you give them. Help stamp out software-hoarding! */
;
.psect vmshlp pic,usr,rel,ovr,shr,long,exe,nowrt

alloca::
.word 0
subl2 ^X4(ap),sp
movl ^X10(fp),r1
movq ^X8(fp),ap
bicl2 #03,sp
addl2 #^X1c,sp
movl sp,r0
jmp (r1)

bcopy::
.word ^X0e00
movl ^X04(ap),r11
movl ^X08(ap),r10
movl ^X0c(ap),r9
brb 1$
2$: movb (r10)+,(r11)+
1$: sobgeq r9,2$
ret
.end
bison-1.22/README100666 21570 1 1147 5174462631 11715 0ustar djmdaemonThis directory contains the Bison parser generator.

See the file INSTALL for compilation and installation instructions.

It was once true that, when installing Bison on Sequent (or Pyramid?)
systems, you had to be in the Berkeley universe. This may no longer
be true; we have no way to tell.

On VMS, you will probably have to create Makefile from Makefile.in by
hand. Remember to do `SET COMMAND BISON' to install the data in
`BISON.CLD'.

Send bug reports to [email protected]. Please include the
version number from `bison --version', and a complete, self-contained
test case in each bug report.
bison-1.22/INSTALL100666 21641 1 13220 5375025751 13114 0ustar friedmandaemonThis is a generic INSTALL file for utilities distributions.
If this package does not come with, e.g., installable documentation or
data files, please ignore the references to them below.

To compile this package:

1. Configure the package for your system. In the directory that this
file is in, type `./configure'. If you're using `csh' on an old
version of System V, you might need to type `sh configure' instead to
prevent `csh' from trying to execute `configure' itself.

The `configure' shell script attempts to guess correct values for
various system-dependent variables used during compilation, and
creates the Makefile(s) (one in each subdirectory of the source
directory). In some packages it creates a C header file containing
system-dependent definitions. It also creates a file `config.status'
that you can run in the future to recreate the current configuration.

Running `configure' takes a minute or two. While it is running, it
prints some messages that tell what it is doing. If you don't want to
see the messages, run `configure' with its standard output redirected
to `/dev/null'; for example, `./configure >/dev/null'.

To compile the package in a different directory from the one
containing the source code, you must use a version of `make' that
supports the VPATH variable, such as GNU `make'. `cd' to the directory
where you want the object files and executables to go and run
`configure'. `configure' automatically checks for the source code in
the directory that `configure' is in and in `..'. If for some reason
`configure' is not in the source code directory that you are
configuring, then it will report that it can't find the source code.
In that case, run `configure' with the option `--srcdir=DIR', where
DIR is the directory that contains the source code.

By default, `make install' will install the package's files in
/usr/local/bin, /usr/local/lib, /usr/local/man, etc. You can specify an
installation prefix other than /usr/local by giving `configure' the option
`--prefix=PATH'. Alternately, you can do so by consistently giving a value
for the `prefix' variable when you run `make', e.g.,
make prefix=/usr/gnu
make prefix=/usr/gnu install

You can specify separate installation prefixes for
architecture-specific files and architecture-independent files. If
you give `configure' the option `--exec-prefix=PATH' or set the
`make' variable `exec_prefix' to PATH, the package will use PATH as
the prefix for installing programs and libraries. Data files and
documentation will still use the regular prefix. Normally, all files
are installed using the regular prefix.

Another `configure' option is useful mainly in `Makefile' rules for
updating `config.status' and `Makefile'. The `--no-create' option
figures out the configuration for your system and records it in
`config.status', without actually configuring the package (creating
`Makefile's and perhaps a configuration header file). Later, you can
run `./config.status' to actually configure the package. You can also
give `config.status' the `--recheck' option, which makes it re-run
`configure' with the same arguments you used before. This option is
useful if you change `configure'.

Some packages pay attention to `--with-PACKAGE' options to `configure',
where PACKAGE is something like `gnu-libc' or `x' (for the X Window System).
The README should mention any --with- options that the package recognizes.

`configure' ignores any other arguments that you give it.

If your system requires unusual options for compilation or linking
that `configure' doesn't know about, you can give `configure' initial
values for some variables by setting them in the environment. In
Bourne-compatible shells, you can do that on the command line like
this:
CC='gcc -traditional' DEFS=-D_POSIX_SOURCE ./configure

The `make' variables that you might want to override with environment
variables when running `configure' are:

(For these variables, any value given in the environment overrides the
value that `configure' would choose:)
CC C compiler program.
Default is `cc', or `gcc' if `gcc' is in your PATH.
INSTALL Program to use to install files.
Default is `install' if you have it, `cp' otherwise.

(For these variables, any value given in the environment is added to
the value that `configure' chooses:)
DEFS Configuration options, in the form `-Dfoo -Dbar ...'
Do not use this variable in packages that create a
configuration header file.
LIBS Libraries to link with, in the form `-lfoo -lbar ...'

If you need to do unusual things to compile the package, we encourage
you to figure out how `configure' could check whether to do them, and
mail diffs or instructions to the address given in the README so we
can include them in the next release.

2. Type `make' to compile the package. If you want, you can override
the `make' variables CFLAGS and LDFLAGS like this:

make CFLAGS=-O2 LDFLAGS=-s

3. If the package comes with self-tests and you want to run them,
type `make check'. If you're not sure whether there are any, try it;
if `make' responds with something like
make: *** No way to make target `check'. Stop.
then the package does not come with self-tests.

4. Type `make install' to install programs, data files, and
documentation.

5. You can remove the program binaries and object files from the
source directory by typing `make clean'. To also remove the
Makefile(s), the header file containing system-dependent definitions
(if the package uses one), and `config.status' (all the files that
`configure' created), type `make distclean'.

The file `configure.in' is used as a template to create `configure' by
a program called `autoconf'. You will only need it if you want to
regenerate `configure' using a newer version of `autoconf'.
bison-1.22/bison.texinfo100666 21570 1 574143 5413126405 13610 0ustar djmdaemon\input texinfo @c -*-texinfo-*-
@comment %**start of header
@setfilename bison.info
@settitle Bison 1.20
@setchapternewpage odd

@c SMALL BOOK version
@c This edition has been formatted so that you can format and print it in
@c the smallbook format.
@c @smallbook

@c next time, consider using @set for edition number, etc...

@c Set following if you have the new `shorttitlepage' command
@c @clear shorttitlepage-enabled
@c @set shorttitlepage-enabled

@c ISPELL CHECK: done, 14 Jan 1993 --bob

@c Check COPYRIGHT dates. should be updated in the titlepage, ifinfo
@c titlepage; should NOT be changed in the GPL. --mew

@iftex
@syncodeindex fn cp
@syncodeindex vr cp
@syncodeindex tp cp
@end iftex
@ifinfo
@synindex fn cp
@synindex vr cp
@synindex tp cp
@end ifinfo
@comment %**end of header

@ifinfo
This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@ignore
Permission is granted to process this file through Tex and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided also that the
sections entitled ``GNU General Public License'' and ``Conditions for
Using Bison'' are included exactly as in the original, and provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that the sections entitled ``GNU General Public License'',
``Conditions for Using Bison'' and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.
@end ifinfo

@ifset shorttitlepage-enabled
@shorttitlepage Bison
@end ifset
@titlepage
@title Bison
@subtitle The YACC-compatible Parser Generator
@subtitle December 1992, Bison Version 1.20

@author by Charles Donnelly and Richard Stallman

@page
@vskip 0pt plus 1filll
Copyright @copyright{} 1988, 1989, 1990, 1991, 1992 Free Software
Foundation

@sp 2
Published by the Free Software Foundation @*
675 Massachusetts Avenue @*
Cambridge, MA 02139 USA @*
Printed copies are available for $15 each.@*
ISBN-1-882114-30-2

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@ignore
Permission is granted to process this file through TeX and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided also that the
sections entitled ``GNU General Public License'' and ``Conditions for
Using Bison'' are included exactly as in the original, and provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that the sections entitled ``GNU General Public License'',
``Conditions for Using Bison'' and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.
@sp 2
Cover art by Etienne Suvasa.
@end titlepage
@page

@node Top, Introduction, (dir), (dir)

@ifinfo
This manual documents version 1.20 of Bison.
@end ifinfo

@menu
* Introduction::
* Conditions::
* Copying:: The GNU General Public License says
how you can copy and share Bison

Tutorial sections:
* Concepts:: Basic concepts for understanding Bison.
* Examples:: Three simple explained examples of using Bison.

Reference sections:
* Grammar File:: Writing Bison declarations and rules.
* Interface:: C-language interface to the parser function @code{yyparse}.
* Algorithm:: How the Bison parser works at run-time.
* Error Recovery:: Writing rules for error recovery.
* Context Dependency:: What to do if your language syntax is too
messy for Bison to handle straightforwardly.
* Debugging:: Debugging Bison parsers that parse wrong.
* Invocation:: How to run Bison (to produce the parser source file).
* Table of Symbols:: All the keywords of the Bison language are explained.
* Glossary:: Basic concepts are explained.
* Index:: Cross-references to the text.

--- The Detailed Node Listing ---

The Concepts of Bison

* Language and Grammar:: Languages and context-free grammars,
as mathematical ideas.
* Grammar in Bison:: How we represent grammars for Bison's sake.
* Semantic Values:: Each token or syntactic grouping can have
a semantic value (the value of an integer,
the name of an identifier, etc.).
* Semantic Actions:: Each rule can have an action containing C code.
* Bison Parser:: What are Bison's input and output,
how is the output used?
* Stages:: Stages in writing and running Bison grammars.
* Grammar Layout:: Overall structure of a Bison grammar file.

Examples

* RPN Calc:: Reverse polish notation calculator;
a first example with no operator precedence.
* Infix Calc:: Infix (algebraic) notation calculator.
Operator precedence is introduced.
* Simple Error Recovery:: Continuing after syntax errors.
* Multi-function Calc:: Calculator with memory and trig functions.
It uses multiple data-types for semantic values.
* Exercises:: Ideas for improving the multi-function calculator.

Reverse Polish Notation Calculator

* Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
* Lexer: Rpcalc Lexer. The lexical analyzer.
* Main: Rpcalc Main. The controlling function.
* Error: Rpcalc Error. The error reporting function.
* Gen: Rpcalc Gen. Running Bison on the grammar file.
* Comp: Rpcalc Compile. Run the C compiler on the output code.

Grammar Rules for @code{rpcalc}

* Rpcalc Input::
* Rpcalc Line::
* Rpcalc Expr::

Multi-Function Calculator: @code{mfcalc}

* Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
* Rules: Mfcalc Rules. Grammar rules for the calculator.
* Symtab: Mfcalc Symtab. Symbol table management subroutines.

Bison Grammar Files

* Grammar Outline:: Overall layout of the grammar file.
* Symbols:: Terminal and nonterminal symbols.
* Rules:: How to write grammar rules.
* Recursion:: Writing recursive rules.
* Semantics:: Semantic values and actions.
* Declarations:: All kinds of Bison declarations are described here.
* Multiple Parsers:: Putting more than one Bison parser in one program.

Outline of a Bison Grammar

* C Declarations:: Syntax and usage of the C declarations section.
* Bison Declarations:: Syntax and usage of the Bison declarations section.
* Grammar Rules:: Syntax and usage of the grammar rules section.
* C Code:: Syntax and usage of the additional C code section.

Defining Language Semantics

* Value Type:: Specifying one data type for all semantic values.
* Multiple Types:: Specifying several alternative data types.
* Actions:: An action is the semantic definition of a grammar rule.
* Action Types:: Specifying data types for actions to operate on.
* Mid-Rule Actions:: Most actions go at the end of a rule.
This says when, why and how to use the exceptional
action in the middle of a rule.

Bison Declarations

* Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity.
* Union Decl:: Declaring the set of all semantic value types.
* Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Expect Decl:: Suppressing warnings about shift/reduce conflicts.
* Start Decl:: Specifying the start symbol.
* Pure Decl:: Requesting a reentrant parser.
* Decl Summary:: Table of all Bison declarations.

Parser C-Language Interface

* Parser Function:: How to call @code{yyparse} and what it returns.
* Lexical:: You must supply a function @code{yylex}
which reads tokens.
* Error Reporting:: You must supply a function @code{yyerror}.
* Action Features:: Special features for use in actions.

The Lexical Analyzer Function @code{yylex}

* Calling Convention:: How @code{yyparse} calls @code{yylex}.
* Token Values:: How @code{yylex} must return the semantic value
of the token it has read.
* Token Positions:: How @code{yylex} must return the text position
(line number, etc.) of the token, if the
actions want that.
* Pure Calling:: How the calling convention differs
in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).

The Bison Parser Algorithm

* Look-Ahead:: Parser looks one token ahead when deciding what to do.
* Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
* Precedence:: Operator precedence works by resolving conflicts.
* Contextual Precedence:: When an operator's precedence depends on context.
* Parser States:: The parser is a finite-state-machine with stack.
* Reduce/Reduce:: When two rules are applicable in the same situation.
* Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
* Stack Overflow:: What happens when stack gets full. How to avoid it.

Operator Precedence

* Why Precedence:: An example showing why precedence is needed.
* Using Precedence:: How to specify precedence in Bison grammars.
* Precedence Examples:: How these features are used in the previous example.
* How Precedence:: How they work.

Handling Context Dependencies

* Semantic Tokens:: Token parsing can depend on the semantic context.
* Lexical Tie-ins:: Token parsing can depend on the syntactic context.
* Tie-in Recovery:: Lexical tie-ins have implications for how
error recovery rules must be written.

Invoking Bison

* Bison Options:: All the options described in detail,
in alphabetical order by short options.
* Option Cross Key:: Alphabetical list of long options.
* VMS Invocation:: Bison command syntax on VMS.
@end menu

@node Introduction, Conditions, Top, Top
@unnumbered Introduction
@cindex introduction

@dfn{Bison} is a general-purpose parser generator that converts a
grammar description for an LALR(1) context-free grammar into a C
program to parse that grammar. Once you are proficient with Bison,
you may use it to develop a wide range of language parsers, from those
used in simple desk calculators to complex programming languages.

Bison is upward compatible with Yacc: all properly-written Yacc grammars
ought to work with Bison with no change. Anyone familiar with Yacc
should be able to use Bison with little trouble. You need to be fluent in
C programming in order to use Bison or to understand this manual.

We begin with tutorial chapters that explain the basic concepts of using
Bison and show three explained examples, each building on the last. If you
don't know Bison or Yacc, start by reading these chapters. Reference
chapters follow which describe specific aspects of Bison in detail.

Bison was written primarily by Robert Corbett; Richard Stallman made
it Yacc-compatible. This edition corresponds to version 1.20 of Bison.

@node Conditions, Copying, Introduction, Top
@unnumbered Conditions for Using Bison

Bison grammars can be used only in programs that are free software. This
is in contrast to what happens with the GNU C compiler and the other
GNU programming tools.

The reason Bison is special is that the output of the Bison utility---the
Bison parser file---contains a verbatim copy of a sizable piece of Bison,
which is the code for the @code{yyparse} function. (The actions from your
grammar are inserted into this function at one point, but the rest of the
function is not changed.)

As a result, the Bison parser file is covered by the same copying
conditions that cover Bison itself and the rest of the GNU system: any
program containing it has to be distributed under the standard GNU copying
conditions.

Occasionally people who would like to use Bison to develop proprietary
programs complain about this.

We don't particularly sympathize with their complaints. The purpose of the
GNU project is to promote the right to share software and the practice of
sharing software; it is a means of changing society. The people who
complain are planning to be uncooperative toward the rest of the world; why
should they deserve our help in doing so?

However, it's possible that a change in these conditions might encourage
computer companies to use and distribute the GNU system. If so, then we
might decide to change the terms on @code{yyparse} as a matter of the
strategy of promoting the right to share. Such a change would be
irrevocable. Since we stand by the copying permissions we have announced,
we cannot withdraw them once given.

We mustn't make an irrevocable change hastily. We have to wait until there
is a complete GNU system and there has been time to learn how this issue
affects its reception.

@node Copying, Concepts, Conditions, Top
@unnumbered GNU GENERAL PUBLIC LICENSE
@center Version 2, June 1991

@display
Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
@end display

@unnumberedsec Preamble

The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software---to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.

When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.

We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

The precise terms and conditions for copying, distribution and
modification follow.

@iftex
@unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
@end iftex
@ifinfo
@center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
@end ifinfo

@enumerate 0
@item
This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The ``Program'', below,
refers to any such program or work, and a ``work based on the Program''
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term ``modification''.) Each licensee is addressed as ``you''.

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

@item
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

@item
You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

@enumerate a
@item
You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.

@item
You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.

@item
If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
@end enumerate

These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

@item
You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

@enumerate a
@item
Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,

@item
Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,

@item
Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
@end enumerate

The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

@item
You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

@item
You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

@item
Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

@item
If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

@item
If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.

@item
The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and ``any
later version'', you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

@item
If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

@iftex
@heading NO WARRANTY
@end iftex
@ifinfo
@center NO WARRANTY
@end ifinfo

@item
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

@item
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
@end enumerate

@iftex
@heading END OF TERMS AND CONDITIONS
@end iftex
@ifinfo
@center END OF TERMS AND CONDITIONS
@end ifinfo

@page
@unnumberedsec How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the ``copyright'' line and a pointer to where the full notice is found.

@smallexample
@var{one line to give the program's name and a brief idea of what it does.}
Copyright (C) 19@var{yy} @var{name of author}

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
@end smallexample

Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

@smallexample
Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
@end smallexample

The hypothetical commands @samp{show w} and @samp{show c} should show
the appropriate parts of the General Public License. Of course, the
commands you use may be called something other than @samp{show w} and
@samp{show c}; they could even be mouse-clicks or menu items---whatever
suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a ``copyright disclaimer'' for the program, if
necessary. Here is a sample; alter the names:

@smallexample
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.

@var{signature of Ty Coon}, 1 April 1989
Ty Coon, President of Vice
@end smallexample

This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.

@node Concepts, Examples, Copying, Top
@chapter The Concepts of Bison

This chapter introduces many of the basic concepts without which the
details of Bison will not make sense. If you do not already know how to
use Bison or Yacc, we suggest you start by reading this chapter carefully.

@menu
* Language and Grammar:: Languages and context-free grammars,
as mathematical ideas.
* Grammar in Bison:: How we represent grammars for Bison's sake.
* Semantic Values:: Each token or syntactic grouping can have
a semantic value (the value of an integer,
the name of an identifier, etc.).
* Semantic Actions:: Each rule can have an action containing C code.
* Bison Parser:: What are Bison's input and output,
how is the output used?
* Stages:: Stages in writing and running Bison grammars.
* Grammar Layout:: Overall structure of a Bison grammar file.
@end menu

@node Language and Grammar, Grammar in Bison, , Concepts
@section Languages and Context-Free Grammars

@c !!! ``An expression can be an integer'' is not a valid Bison
@c expression---Bison cannot read English! --rjc 6 Feb 1992
@cindex context-free grammar
@cindex grammar, context-free
In order for Bison to parse a language, it must be described by a
@dfn{context-free grammar}. This means that you specify one or more
@dfn{syntactic groupings} and give rules for constructing them from their
parts. For example, in the C language, one kind of grouping is called an
`expression'. One rule for making an expression might be, ``An expression
can be made of a minus sign and another expression''. Another would be,
``An expression can be an integer''. As you can see, rules are often
recursive, but there must be at least one rule which leads out of the
recursion.

@cindex BNF
@cindex Backus-Naur form
The most common formal system for presenting such rules for humans to read
is @dfn{Backus-Naur Form} or ``BNF'', which was developed in order to
specify the language Algol 60. Any grammar expressed in BNF is a
context-free grammar. The input to Bison is essentially machine-readable
BNF.

Not all context-free languages can be handled by Bison, only those
that are LALR(1). In brief, this means that it must be possible to
tell how to parse any portion of an input string with just a single
token of look-ahead. Strictly speaking, that is a description of an
LR(1) grammar, and LALR(1) involves additional restrictions that are
hard to explain simply; but it is rare in actual practice to find an
LR(1) grammar that fails to be LALR(1). @xref{Mystery Conflicts, ,
Mysterious Reduce/Reduce Conflicts}, for more information on this.

@cindex symbols (abstract)
@cindex token
@cindex syntactic grouping
@cindex grouping, syntactic
In the formal grammatical rules for a language, each kind of syntactic unit
or grouping is named by a @dfn{symbol}. Those which are built by grouping
smaller constructs according to grammatical rules are called
@dfn{nonterminal symbols}; those which can't be subdivided are called
@dfn{terminal symbols} or @dfn{token types}. We call a piece of input
corresponding to a single terminal symbol a @dfn{token}, and a piece
corresponding to a single nonterminal symbol a @dfn{grouping}.@refill

We can use the C language as an example of what symbols, terminal and
nonterminal, mean. The tokens of C are identifiers, constants (numeric and
string), and the various keywords, arithmetic operators and punctuation
marks. So the terminal symbols of a grammar for C include `identifier',
`number', `string', plus one symbol for each keyword, operator or
punctuation mark: `if', `return', `const', `static', `int', `char',
`plus-sign', `open-brace', `close-brace', `comma' and many more. (These
tokens can be subdivided into characters, but that is a matter of
lexicography, not grammar.)

Here is a simple C function subdivided into tokens:

@example
int /* @r{keyword `int'} */
square (x) /* @r{identifier, open-paren,} */
/* @r{identifier, close-paren} */
int x; /* @r{keyword `int', identifier, semicolon} */
@{ /* @r{open-brace} */
return x * x; /* @r{keyword `return', identifier,} */
/* @r{asterisk, identifier, semicolon} */
@} /* @r{close-brace} */
@end example

The syntactic groupings of C include the expression, the statement, the
declaration, and the function definition. These are represented in the
grammar of C by nonterminal symbols `expression', `statement',
`declaration' and `function definition'. The full grammar uses dozens of
additional language constructs, each with its own nonterminal symbol, in
order to express the meanings of these four. The example above is a
function definition; it contains one declaration, and one statement. In
the statement, each @samp{x} is an expression and so is @samp{x * x}.

Each nonterminal symbol must have grammatical rules showing how it is made
out of simpler constructs. For example, one kind of C statement is the
@code{return} statement; this would be described with a grammar rule which
reads informally as follows:

@quotation
A `statement' can be made of a `return' keyword, an `expression' and a
`semicolon'.
@end quotation

@noindent
There would be many other rules for `statement', one for each kind of
statement in C.

@cindex start symbol
One nonterminal symbol must be distinguished as the special one which
defines a complete utterance in the language. It is called the @dfn{start
symbol}. In a compiler, this means a complete input program. In the C
language, the nonterminal symbol `sequence of definitions and declarations'
plays this role.

For example, @samp{1 + 2} is a valid C expression---a valid part of a C
program---but it is not valid as an @emph{entire} C program. In the
context-free grammar of C, this follows from the fact that `expression' is
not the start symbol.

The Bison parser reads a sequence of tokens as its input, and groups the
tokens using the grammar rules. If the input is valid, the end result is
that the entire token sequence reduces to a single grouping whose symbol is
the grammar's start symbol. If we use a grammar for C, the entire input
must be a `sequence of definitions and declarations'. If not, the parser
reports a syntax error.

@node Grammar in Bison, Semantic Values, Language and Grammar, Concepts
@section From Formal Rules to Bison Input
@cindex Bison grammar
@cindex grammar, Bison
@cindex formal grammar

A formal grammar is a mathematical construct. To define the language
for Bison, you must write a file expressing the grammar in Bison syntax:
a @dfn{Bison grammar} file. @xref{Grammar File, ,Bison Grammar Files}.

A nonterminal symbol in the formal grammar is represented in Bison input
as an identifier, like an identifier in C. By convention, it should be
in lower case, such as @code{expr}, @code{stmt} or @code{declaration}.

The Bison representation for a terminal symbol is also called a @dfn{token
type}. Token types as well can be represented as C-like identifiers. By
convention, these identifiers should be upper case to distinguish them from
nonterminals: for example, @code{INTEGER}, @code{IDENTIFIER}, @code{IF} or
@code{RETURN}. A terminal symbol that stands for a particular keyword in
the language should be named after that keyword converted to upper case.
The terminal symbol @code{error} is reserved for error recovery.
@xref{Symbols}.@refill

A terminal symbol can also be represented as a character literal, just like
a C character constant. You should do this whenever a token is just a
single character (parenthesis, plus-sign, etc.): use that same character in
a literal as the terminal symbol for that token.

The grammar rules also have an expression in Bison syntax. For example,
here is the Bison rule for a C @code{return} statement. The semicolon in
quotes is a literal character token, representing part of the C syntax for
the statement; the naked semicolon, and the colon, are Bison punctuation
used in every rule.

@example
stmt: RETURN expr ';'
;
@end example

@noindent
@xref{Rules, ,Syntax of Grammar Rules}.

@node Semantic Values, Semantic Actions, Grammar in Bison, Concepts
@section Semantic Values
@cindex semantic value
@cindex value, semantic

A formal grammar selects tokens only by their classifications: for example,
if a rule mentions the terminal symbol `integer constant', it means that
@emph{any} integer constant is grammatically valid in that position. The
precise value of the constant is irrelevant to how to parse the input: if
@samp{x+4} is grammatical then @samp{x+1} or @samp{x+3989} is equally
grammatical.@refill

But the precise value is very important for what the input means once it is
parsed. A compiler is useless if it fails to distinguish between 4, 1 and
3989 as constants in the program! Therefore, each token in a Bison grammar
has both a token type and a @dfn{semantic value}. @xref{Semantics, ,Defining Language Semantics},
for details.

The token type is a terminal symbol defined in the grammar, such as
@code{INTEGER}, @code{IDENTIFIER} or @code{','}. It tells everything
you need to know to decide where the token may validly appear and how to
group it with other tokens. The grammar rules know nothing about tokens
except their types.@refill

The semantic value has all the rest of the information about the
meaning of the token, such as the value of an integer, or the name of an
identifier. (A token such as @code{','} which is just punctuation doesn't
need to have any semantic value.)

For example, an input token might be classified as token type
@code{INTEGER} and have the semantic value 4. Another input token might
have the same token type @code{INTEGER} but value 3989. When a grammar
rule says that @code{INTEGER} is allowed, either of these tokens is
acceptable because each is an @code{INTEGER}. When the parser accepts the
token, it keeps track of the token's semantic value.

Each grouping can also have a semantic value as well as its nonterminal
symbol. For example, in a calculator, an expression typically has a
semantic value that is a number. In a compiler for a programming
language, an expression typically has a semantic value that is a tree
structure describing the meaning of the expression.

@node Semantic Actions, Bison Parser, Semantic Values, Concepts
@section Semantic Actions
@cindex semantic actions
@cindex actions, semantic

In order to be useful, a program must do more than parse input; it must
also produce some output based on the input. In a Bison grammar, a grammar
rule can have an @dfn{action} made up of C statements. Each time the
parser recognizes a match for that rule, the action is executed.
@xref{Actions}.

Most of the time, the purpose of an action is to compute the semantic value
of the whole construct from the semantic values of its parts. For example,
suppose we have a rule which says an expression can be the sum of two
expressions. When the parser recognizes such a sum, each of the
subexpressions has a semantic value which describes how it was built up.
The action for this rule should create a similar sort of value for the
newly recognized larger expression.

For example, here is a rule that says an expression can be the sum of
two subexpressions:

@example
expr: expr '+' expr @{ $$ = $1 + $3; @}
;
@end example

@noindent
The action says how to produce the semantic value of the sum expression
from the values of the two subexpressions.

@node Bison Parser, Stages, Semantic Actions, Concepts
@section Bison Output: the Parser File
@cindex Bison parser
@cindex Bison utility
@cindex lexical analyzer, purpose
@cindex parser

When you run Bison, you give it a Bison grammar file as input. The output
is a C source file that parses the language described by the grammar.
This file is called a @dfn{Bison parser}. Keep in mind that the Bison
utility and the Bison parser are two distinct programs: the Bison utility
is a program whose output is the Bison parser that becomes part of your
program.

The job of the Bison parser is to group tokens into groupings according to
the grammar rules---for example, to build identifiers and operators into
expressions. As it does this, it runs the actions for the grammar rules it
uses.

The tokens come from a function called the @dfn{lexical analyzer} that you
must supply in some fashion (such as by writing it in C). The Bison parser
calls the lexical analyzer each time it wants a new token. It doesn't know
what is ``inside'' the tokens (though their semantic values may reflect
this). Typically the lexical analyzer makes the tokens by parsing
characters of text, but Bison does not depend on this. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.

The Bison parser file is C code which defines a function named
@code{yyparse} which implements that grammar. This function does not make
a complete C program: you must supply some additional functions. One is
the lexical analyzer. Another is an error-reporting function which the
parser calls to report an error. In addition, a complete C program must
start with a function called @code{main}; you have to provide this, and
arrange for it to call @code{yyparse} or the parser will never run.
@xref{Interface, ,Parser C-Language Interface}.

Aside from the token type names and the symbols in the actions you
write, all variable and function names used in the Bison parser file
begin with @samp{yy} or @samp{YY}. This includes interface functions
such as the lexical analyzer function @code{yylex}, the error reporting
function @code{yyerror} and the parser function @code{yyparse} itself.
This also includes numerous identifiers used for internal purposes.
Therefore, you should avoid using C identifiers starting with @samp{yy}
or @samp{YY} in the Bison grammar file except for the ones defined in
this manual.

@node Stages, Grammar Layout, Bison Parser, Concepts
@section Stages in Using Bison
@cindex stages in using Bison
@cindex using Bison

The actual language-design process using Bison, from grammar specification
to a working compiler or interpreter, has these parts:

@enumerate
@item
Formally specify the grammar in a form recognized by Bison
(@pxref{Grammar File, ,Bison Grammar Files}). For each grammatical rule in the language,
describe the action that is to be taken when an instance of that rule
is recognized. The action is described by a sequence of C statements.

@item
Write a lexical analyzer to process input and pass tokens to the
parser. The lexical analyzer may be written by hand in C
(@pxref{Lexical, ,The Lexical Analyzer Function @code{yylex}}). It could also be produced using Lex, but the use
of Lex is not discussed in this manual.

@item
Write a controlling function that calls the Bison-produced parser.

@item
Write error-reporting routines.
@end enumerate

To turn this source code as written into a runnable program, you
must follow these steps:

@enumerate
@item
Run Bison on the grammar to produce the parser.

@item
Compile the code output by Bison, as well as any other source files.

@item
Link the object files to produce the finished product.
@end enumerate

@node Grammar Layout, , Stages, Concepts
@section The Overall Layout of a Bison Grammar
@cindex grammar file
@cindex file format
@cindex format of grammar file
@cindex layout of Bison grammar

The input file for the Bison utility is a @dfn{Bison grammar file}. The
general form of a Bison grammar file is as follows:

@example
%@{
@var{C declarations}
%@}

@var{Bison declarations}

%%
@var{Grammar rules}
%%
@var{Additional C code}
@end example

@noindent
The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears
in every Bison grammar file to separate the sections.

The C declarations may define types and variables used in the actions.
You can also use preprocessor commands to define macros used there, and use
@code{#include} to include header files that do any of these things.

The Bison declarations declare the names of the terminal and nonterminal
symbols, and may also describe operator precedence and the data types of
semantic values of various symbols.

The grammar rules define how to construct each nonterminal symbol from its
parts.

The additional C code can contain any C code you want to use. Often the
definition of the lexical analyzer @code{yylex} goes here, plus subroutines
called by the actions in the grammar rules. In a simple program, all the
rest of the program can go here.

@node Examples, Grammar File, Concepts, Top
@chapter Examples
@cindex simple examples
@cindex examples, simple

Now we show and explain three sample programs written using Bison: a
reverse polish notation calculator, an algebraic (infix) notation
calculator, and a multi-function calculator. All three have been tested
under BSD Unix 4.3; each produces a usable, though limited, interactive
desk-top calculator.

These examples are simple, but Bison grammars for real programming
languages are written the same way.
@ifinfo
You can copy these examples out of the Info file and into a source file
to try them.
@end ifinfo

@menu
* RPN Calc:: Reverse polish notation calculator;
a first example with no operator precedence.
* Infix Calc:: Infix (algebraic) notation calculator.
Operator precedence is introduced.
* Simple Error Recovery:: Continuing after syntax errors.
* Multi-function Calc:: Calculator with memory and trig functions.
It uses multiple data-types for semantic values.
* Exercises:: Ideas for improving the multi-function calculator.
@end menu

@node RPN Calc, Infix Calc, , Examples
@section Reverse Polish Notation Calculator
@cindex reverse polish notation
@cindex polish notation calculator
@cindex @code{rpcalc}
@cindex calculator, simple

The first example is that of a simple double-precision @dfn{reverse polish
notation} calculator (a calculator using postfix operators). This example
provides a good starting point, since operator precedence is not an issue.
The second example will illustrate how operator precedence is handled.

The source code for this calculator is named @file{rpcalc.y}. The
@samp{.y} extension is a convention used for Bison input files.

@menu
* Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
* Lexer: Rpcalc Lexer. The lexical analyzer.
* Main: Rpcalc Main. The controlling function.
* Error: Rpcalc Error. The error reporting function.
* Gen: Rpcalc Gen. Running Bison on the grammar file.
* Comp: Rpcalc Compile. Run the C compiler on the output code.
@end menu

@node Rpcalc Decls, Rpcalc Rules, , RPN Calc
@subsection Declarations for @code{rpcalc}

Here are the C and Bison declarations for the reverse polish notation
calculator. As in C, comments are placed between @samp{/*@dots{}*/}.

@example
/* Reverse polish notation calculator. */

%@{
#define YYSTYPE double
#include
%@}

%token NUM

%% /* Grammar rules and actions follow */
@end example

The C declarations section (@pxref{C Declarations, ,The C Declarations Section}) contains two
preprocessor directives.

The @code{#define} directive defines the macro @code{YYSTYPE}, thus
specifying the C data type for semantic values of both tokens and groupings
(@pxref{Value Type, ,Data Types of Semantic Values}). The Bison parser will use whatever type
@code{YYSTYPE} is defined as; if you don't define it, @code{int} is the
default. Because we specify @code{double}, each token and each expression
has an associated value, which is a floating point number.

The @code{#include} directive is used to declare the exponentiation
function @code{pow}.

The second section, Bison declarations, provides information to Bison about
the token types (@pxref{Bison Declarations, ,The Bison Declarations Section}). Each terminal symbol that is
not a single-character literal must be declared here. (Single-character
literals normally don't need to be declared.) In this example, all the
arithmetic operators are designated by single-character literals, so the
only terminal symbol that needs to be declared is @code{NUM}, the token
type for numeric constants.

@node Rpcalc Rules, Rpcalc Lexer, Rpcalc Decls, RPN Calc
@subsection Grammar Rules for @code{rpcalc}

Here are the grammar rules for the reverse polish notation calculator.

@example
input: /* empty */
| input line
;

line: '\n'
| exp '\n' @{ printf ("\t%.10g\n", $1); @}
;

exp: NUM @{ $$ = $1; @}
| exp exp '+' @{ $$ = $1 + $2; @}
| exp exp '-' @{ $$ = $1 - $2; @}
| exp exp '*' @{ $$ = $1 * $2; @}
| exp exp '/' @{ $$ = $1 / $2; @}
/* Exponentiation */
| exp exp '^' @{ $$ = pow ($1, $2); @}
/* Unary minus */
| exp 'n' @{ $$ = -$1; @}
;
%%
@end example

The groupings of the rpcalc ``language'' defined here are the expression
(given the name @code{exp}), the line of input (@code{line}), and the
complete input transcript (@code{input}). Each of these nonterminal
symbols has several alternate rules, joined by the @samp{|} punctuator
which is read as ``or''. The following sections explain what these rules
mean.

The semantics of the language is determined by the actions taken when a
grouping is recognized. The actions are the C code that appears inside
braces. @xref{Actions}.

You must specify these actions in C, but Bison provides the means for
passing semantic values between the rules. In each action, the
pseudo-variable @code{$$} stands for the semantic value for the grouping
that the rule is going to construct. Assigning a value to @code{$$} is the
main job of most actions. The semantic values of the components of the
rule are referred to as @code{$1}, @code{$2}, and so on.

@menu
* Rpcalc Input::
* Rpcalc Line::
* Rpcalc Expr::
@end menu

@node Rpcalc Input, Rpcalc Line, , Rpcalc Rules
@subsubsection Explanation of @code{input}

Consider the definition of @code{input}:

@example
input: /* empty */
| input line
;
@end example

This definition reads as follows: ``A complete input is either an empty
string, or a complete input followed by an input line''. Notice that
``complete input'' is defined in terms of itself. This definition is said
to be @dfn{left recursive} since @code{input} appears always as the
leftmost symbol in the sequence. @xref{Recursion, ,Recursive Rules}.

The first alternative is empty because there are no symbols between the
colon and the first @samp{|}; this means that @code{input} can match an
empty string of input (no tokens). We write the rules this way because it
is legitimate to type @kbd{Ctrl-d} right after you start the calculator.
It's conventional to put an empty alternative first and write the comment
@samp{/* empty */} in it.

The second alternate rule (@code{input line}) handles all nontrivial input.
It means, ``After reading any number of lines, read one more line if
possible.'' The left recursion makes this rule into a loop. Since the
first alternative matches empty input, the loop can be executed zero or
more times.

The parser function @code{yyparse} continues to process input until a
grammatical error is seen or the lexical analyzer says there are no more
input tokens; we will arrange for the latter to happen at end of file.

@node Rpcalc Line, Rpcalc Expr, Rpcalc Input, Rpcalc Rules
@subsubsection Explanation of @code{line}

Now consider the definition of @code{line}:

@example
line: '\n'
| exp '\n' @{ printf ("\t%.10g\n", $1); @}
;
@end example

The first alternative is a token which is a newline character; this means
that rpcalc accepts a blank line (and ignores it, since there is no
action). The second alternative is an expression followed by a newline.
This is the alternative that makes rpcalc useful. The semantic value of
the @code{exp} grouping is the value of @code{$1} because the @code{exp} in
question is the first symbol in the alternative. The action prints this
value, which is the result of the computation the user asked for.

This action is unusual because it does not assign a value to @code{$$}. As
a consequence, the semantic value associated with the @code{line} is
uninitialized (its value will be unpredictable). This would be a bug if
that value were ever used, but we don't use it: once rpcalc has printed the
value of the user's input line, that value is no longer needed.

@node Rpcalc Expr, , Rpcalc Line, Rpcalc Rules
@subsubsection Explanation of @code{expr}

The @code{exp} grouping has several rules, one for each kind of expression.
The first rule handles the simplest expressions: those that are just numbers.
The second handles an addition-expression, which looks like two expressions
followed by a plus-sign. The third handles subtraction, and so on.

@example
exp: NUM
| exp exp '+' @{ $$ = $1 + $2; @}
| exp exp '-' @{ $$ = $1 - $2; @}
@dots{}
;
@end example

We have used @samp{|} to join all the rules for @code{exp}, but we could
equally well have written them separately:

@example
exp: NUM ;
exp: exp exp '+' @{ $$ = $1 + $2; @} ;
exp: exp exp '-' @{ $$ = $1 - $2; @} ;
@dots{}
@end example

Most of the rules have actions that compute the value of the expression in
terms of the value of its parts. For example, in the rule for addition,
@code{$1} refers to the first component @code{exp} and @code{$2} refers to
the second one. The third component, @code{'+'}, has no meaningful
associated semantic value, but if it had one you could refer to it as
@code{$3}. When @code{yyparse} recognizes a sum expression using this
rule, the sum of the two subexpressions' values is produced as the value of
the entire expression. @xref{Actions}.

You don't have to give an action for every rule. When a rule has no
action, Bison by default copies the value of @code{$1} into @code{$$}.
This is what happens in the first rule (the one that uses @code{NUM}).

The formatting shown here is the recommended convention, but Bison does
not require it. You can add or change whitespace as much as you wish.
For example, this:

@example
exp : NUM | exp exp '+' @{$$ = $1 + $2; @} | @dots{}
@end example

@noindent
means the same thing as this:

@example
exp: NUM
| exp exp '+' @{ $$ = $1 + $2; @}
| @dots{}
@end example

@noindent
The latter, however, is much more readable.

@node Rpcalc Lexer, Rpcalc Main, Rpcalc Rules, RPN Calc
@subsection The @code{rpcalc} Lexical Analyzer
@cindex writing a lexical analyzer
@cindex lexical analyzer, writing

The lexical analyzer's job is low-level parsing: converting characters or
sequences of characters into tokens. The Bison parser gets its tokens by
calling the lexical analyzer. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.

Only a simple lexical analyzer is needed for the RPN calculator. This
lexical analyzer skips blanks and tabs, then reads in numbers as
@code{double} and returns them as @code{NUM} tokens. Any other character
that isn't part of a number is a separate token. Note that the token-code
for such a single-character token is the character itself.

The return value of the lexical analyzer function is a numeric code which
represents a token type. The same text used in Bison rules to stand for
this token type is also a C expression for the numeric code for the type.
This works in two ways. If the token type is a character literal, then its
numeric code is the ASCII code for that character; you can use the same
character literal in the lexical analyzer to express the number. If the
token type is an identifier, that identifier is defined by Bison as a C
macro whose definition is the appropriate number. In this example,
therefore, @code{NUM} becomes a macro for @code{yylex} to use.

The semantic value of the token (if it has one) is stored into the global
variable @code{yylval}, which is where the Bison parser will look for it.
(The C data type of @code{yylval} is @code{YYSTYPE}, which was defined
at the beginning of the grammar; @pxref{Rpcalc Decls, ,Declarations for @code{rpcalc}}.)

A token type code of zero is returned if the end-of-file is encountered.
(Bison recognizes any nonpositive value as indicating the end of the
input.)

Here is the code for the lexical analyzer:

@example
@group
/* Lexical analyzer returns a double floating point
number on the stack and the token NUM, or the ASCII
character read if not a number. Skips all blanks
and tabs, returns 0 for EOF. */

#include
@end group

@group
yylex ()
@{
int c;

/* skip white space */
while ((c = getchar ()) == ' ' || c == '\t')
;
@end group
@group
/* process numbers */
if (c == '.' || isdigit (c))
@{
ungetc (c, stdin);
scanf ("%lf", &yylval);
return NUM;
@}
@end group
@group
/* return end-of-file */
if (c == EOF)
return 0;
/* return single chars */
return c;
@}
@end group
@end example

@node Rpcalc Main, Rpcalc Error, Rpcalc Lexer, RPN Calc
@subsection The Controlling Function
@cindex controlling function
@cindex main function in simple example

In keeping with the spirit of this example, the controlling function is
kept to the bare minimum. The only requirement is that it call
@code{yyparse} to start the process of parsing.

@example
@group
main ()
@{
yyparse ();
@}
@end group
@end example

@node Rpcalc Error, Rpcalc Gen, Rpcalc Main, RPN Calc
@subsection The Error Reporting Routine
@cindex error reporting routine

When @code{yyparse} detects a syntax error, it calls the error reporting
function @code{yyerror} to print an error message (usually but not always
@code{"parse error"}). It is up to the programmer to supply @code{yyerror}
(@pxref{Interface, ,Parser C-Language Interface}), so here is the definition we will use:

@example
@group
#include

yyerror (s) /* Called by yyparse on error */
char *s;
@{
printf ("%s\n", s);
@}
@end group
@end example

After @code{yyerror} returns, the Bison parser may recover from the error
and continue parsing if the grammar contains a suitable error rule
(@pxref{Error Recovery}). Otherwise, @code{yyparse} returns nonzero. We
have not written any error rules in this example, so any invalid input will
cause the calculator program to exit. This is not clean behavior for a
real calculator, but it is adequate in the first example.

@node Rpcalc Gen, Rpcalc Compile, Rpcalc Error, RPN Calc
@subsection Running Bison to Make the Parser
@cindex running Bison (introduction)

Before running Bison to produce a parser, we need to decide how to arrange
all the source code in one or more source files. For such a simple example,
the easiest thing is to put everything in one file. The definitions of
@code{yylex}, @code{yyerror} and @code{main} go at the end, in the
``additional C code'' section of the file (@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}).

For a large project, you would probably have several source files, and use
@code{make} to arrange to recompile them.

With all the source in a single file, you use the following command to
convert it into a parser file:

@example
bison @var{file_name}.y
@end example

@noindent
In this example the file was called @file{rpcalc.y} (for ``Reverse Polish
CALCulator''). Bison produces a file named @file{@var{file_name}.tab.c},
removing the @samp{.y} from the original file name. The file output by
Bison contains the source code for @code{yyparse}. The additional
functions in the input file (@code{yylex}, @code{yyerror} and @code{main})
are copied verbatim to the output.

@node Rpcalc Compile, , Rpcalc Gen, RPN Calc
@subsection Compiling the Parser File
@cindex compiling the parser

Here is how to compile and run the parser file:

@example
@group
# @r{List files in current directory.}
% ls
rpcalc.tab.c rpcalc.y
@end group

@group
# @r{Compile the Bison parser.}
# @r{@samp{-lm} tells compiler to search math library for @code{pow}.}
% cc rpcalc.tab.c -lm -o rpcalc
@end group

@group
# @r{List files again.}
% ls
rpcalc rpcalc.tab.c rpcalc.y
@end group
@end example

The file @file{rpcalc} now contains the executable code. Here is an
example session using @code{rpcalc}.

@example
% rpcalc
4 9 +
13
3 7 + 3 4 5 *+-
-13
3 7 + 3 4 5 * + - n @r{Note the unary minus, @samp{n}}
13
5 6 / 4 n +
-3.166666667
3 4 ^ @r{Exponentiation}
81
^D @r{End-of-file indicator}
%
@end example

@node Infix Calc, Simple Error Recovery, RPN Calc, Examples
@section Infix Notation Calculator: @code{calc}
@cindex infix notation calculator
@cindex @code{calc}
@cindex calculator, infix notation

We now modify rpcalc to handle infix operators instead of postfix. Infix
notation involves the concept of operator precedence and the need for
parentheses nested to arbitrary depth. Here is the Bison code for
@file{calc.y}, an infix desk-top calculator.

@example
/* Infix notation calculator--calc */

%@{
#define YYSTYPE double
#include
%@}

/* BISON Declarations */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%right '^' /* exponentiation */

/* Grammar follows */
%%
input: /* empty string */
| input line
;

line: '\n'
| exp '\n' @{ printf ("\t%.10g\n", $1); @}
;

exp: NUM @{ $$ = $1; @}
| exp '+' exp @{ $$ = $1 + $3; @}
| exp '-' exp @{ $$ = $1 - $3; @}
| exp '*' exp @{ $$ = $1 * $3; @}
| exp '/' exp @{ $$ = $1 / $3; @}
| '-' exp %prec NEG @{ $$ = -$2; @}
| exp '^' exp @{ $$ = pow ($1, $3); @}
| '(' exp ')' @{ $$ = $2; @}
;
%%
@end example

@noindent
The functions @code{yylex}, @code{yyerror} and @code{main} can be the same
as before.

There are two important new features shown in this code.

In the second section (Bison declarations), @code{%left} declares token
types and says they are left-associative operators. The declarations
@code{%left} and @code{%right} (right associativity) take the place of
@code{%token} which is used to declare a token type name without
associativity. (These tokens are single-character literals, which
ordinarily don't need to be declared. We declare them here to specify
the associativity.)

Operator precedence is determined by the line ordering of the
declarations; the higher the line number of the declaration (lower on
the page or screen), the higher the precedence. Hence, exponentiation
has the highest precedence, unary minus (@code{NEG}) is next, followed
by @samp{*} and @samp{/}, and so on. @xref{Precedence, ,Operator Precedence}.

The other important new feature is the @code{%prec} in the grammar section
for the unary minus operator. The @code{%prec} simply instructs Bison that
the rule @samp{| '-' exp} has the same precedence as @code{NEG}---in this
case the next-to-highest. @xref{Contextual Precedence, ,Context-Dependent Precedence}.

Here is a sample run of @file{calc.y}:

@need 500
@example
% calc
4 + 4.5 - (34/(8*3+-3))
6.880952381
-56 + 2
-54
3 ^ 2
9
@end example

@node Simple Error Recovery, Multi-function Calc, Infix Calc, Examples
@section Simple Error Recovery
@cindex error recovery, simple

Up to this point, this manual has not addressed the issue of @dfn{error
recovery}---how to continue parsing after the parser detects a syntax
error. All we have handled is error reporting with @code{yyerror}. Recall
that by default @code{yyparse} returns after calling @code{yyerror}. This
means that an erroneous input line causes the calculator program to exit.
Now we show how to rectify this deficiency.

The Bison language itself includes the reserved word @code{error}, which
may be included in the grammar rules. In the example below it has
been added to one of the alternatives for @code{line}:

@example
@group
line: '\n'
| exp '\n' @{ printf ("\t%.10g\n", $1); @}
| error '\n' @{ yyerrok; @}
;
@end group
@end example

This addition to the grammar allows for simple error recovery in the event
of a parse error. If an expression that cannot be evaluated is read, the
error will be recognized by the third rule for @code{line}, and parsing
will continue. (The @code{yyerror} function is still called upon to print
its message as well.) The action executes the statement @code{yyerrok}, a
macro defined automatically by Bison; its meaning is that error recovery is
complete (@pxref{Error Recovery}). Note the difference between
@code{yyerrok} and @code{yyerror}; neither one is a misprint.@refill

This form of error recovery deals with syntax errors. There are other
kinds of errors; for example, division by zero, which raises an exception
signal that is normally fatal. A real calculator program must handle this
signal and use @code{longjmp} to return to @code{main} and resume parsing
input lines; it would also have to discard the rest of the current line of
input. We won't discuss this issue further because it is not specific to
Bison programs.

@node Multi-function Calc, Exercises, Simple Error Recovery, Examples
@section Multi-Function Calculator: @code{mfcalc}
@cindex multi-function calculator
@cindex @code{mfcalc}
@cindex calculator, multi-function

Now that the basics of Bison have been discussed, it is time to move on to
a more advanced problem. The above calculators provided only five
functions, @samp{+}, @samp{-}, @samp{*}, @samp{/} and @samp{^}. It would
be nice to have a calculator that provides other mathematical functions such
as @code{sin}, @code{cos}, etc.

It is easy to add new operators to the infix calculator as long as they are
only single-character literals. The lexical analyzer @code{yylex} passes
back all non-number characters as tokens, so new grammar rules suffice for
adding a new operator. But we want something more flexible: built-in
functions whose syntax has this form:

@example
@var{function_name} (@var{argument})
@end example

@noindent
At the same time, we will add memory to the calculator, by allowing you
to create named variables, store values in them, and use them later.
Here is a sample session with the multi-function calculator:

@example
% acalc
pi = 3.141592653589
3.1415926536
sin(pi)
0.0000000000
alpha = beta1 = 2.3
2.3000000000
alpha
2.3000000000
ln(alpha)
0.8329091229
exp(ln(beta1))
2.3000000000
%
@end example

Note that multiple assignment and nested function calls are permitted.

@menu
* Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
* Rules: Mfcalc Rules. Grammar rules for the calculator.
* Symtab: Mfcalc Symtab. Symbol table management subroutines.
@end menu

@node Mfcalc Decl, Mfcalc Rules, , Multi-function Calc
@subsection Declarations for @code{mfcalc}

Here are the C and Bison declarations for the multi-function calculator.

@smallexample
%@{
#include /* For math functions, cos(), sin(), etc. */
#include "calc.h" /* Contains definition of `symrec' */
%@}
%union @{
double val; /* For returning numbers. */
symrec *tptr; /* For returning symbol-table pointers */
@}

%token NUM /* Simple double precision number */
%token VAR FNCT /* Variable and Function */
%type exp

%right '='
%left '-' '+'
%left '*' '/'
%left NEG /* Negation--unary minus */
%right '^' /* Exponentiation */

/* Grammar follows */

%%
@end smallexample

The above grammar introduces only two new features of the Bison language.
These features allow semantic values to have various data types
(@pxref{Multiple Types, ,More Than One Value Type}).

The @code{%union} declaration specifies the entire list of possible types;
this is instead of defining @code{YYSTYPE}. The allowable types are now
double-floats (for @code{exp} and @code{NUM}) and pointers to entries in
the symbol table. @xref{Union Decl, ,The Collection of Value Types}.

Since values can now have various types, it is necessary to associate a
type with each grammar symbol whose semantic value is used. These symbols
are @code{NUM}, @code{VAR}, @code{FNCT}, and @code{exp}. Their
declarations are augmented with information about their data type (placed
between angle brackets).

The Bison construct @code{%type} is used for declaring nonterminal symbols,
just as @code{%token} is used for declaring token types. We have not used
@code{%type} before because nonterminal symbols are normally declared
implicitly by the rules that define them. But @code{exp} must be declared
explicitly so we can specify its value type. @xref{Type Decl, ,Nonterminal Symbols}.

@node Mfcalc Rules, Mfcalc Symtab, Mfcalc Decl, Multi-function Calc
@subsection Grammar Rules for @code{mfcalc}

Here are the grammar rules for the multi-function calculator.
Most of them are copied directly from @code{calc}; three rules,
those which mention @code{VAR} or @code{FNCT}, are new.

@smallexample
input: /* empty */
| input line
;

line:
'\n'
| exp '\n' @{ printf ("\t%.10g\n", $1); @}
| error '\n' @{ yyerrok; @}
;

exp: NUM @{ $$ = $1; @}
| VAR @{ $$ = $1->value.var; @}
| VAR '=' exp @{ $$ = $3; $1->value.var = $3; @}
| FNCT '(' exp ')' @{ $$ = (*($1->value.fnctptr))($3); @}
| exp '+' exp @{ $$ = $1 + $3; @}
| exp '-' exp @{ $$ = $1 - $3; @}
| exp '*' exp @{ $$ = $1 * $3; @}
| exp '/' exp @{ $$ = $1 / $3; @}
| '-' exp %prec NEG @{ $$ = -$2; @}
| exp '^' exp @{ $$ = pow ($1, $3); @}
| '(' exp ')' @{ $$ = $2; @}
;
/* End of grammar */
%%
@end smallexample

@node Mfcalc Symtab, , Mfcalc Rules, Multi-function Calc
@subsection The @code{mfcalc} Symbol Table
@cindex symbol table example

The multi-function calculator requires a symbol table to keep track of the
names and meanings of variables and functions. This doesn't affect the
grammar rules (except for the actions) or the Bison declarations, but it
requires some additional C functions for support.

The symbol table itself consists of a linked list of records. Its
definition, which is kept in the header @file{calc.h}, is as follows. It
provides for either functions or variables to be placed in the table.

@smallexample
@group
/* Data type for links in the chain of symbols. */
struct symrec
@{
char *name; /* name of symbol */
int type; /* type of symbol: either VAR or FNCT */
union @{
double var; /* value of a VAR */
double (*fnctptr)(); /* value of a FNCT */
@} value;
struct symrec *next; /* link field */
@};
@end group

@group
typedef struct symrec symrec;

/* The symbol table: a chain of `struct symrec'. */
extern symrec *sym_table;

symrec *putsym ();
symrec *getsym ();
@end group
@end smallexample

The new version of @code{main} includes a call to @code{init_table}, a
function that initializes the symbol table. Here it is, and
@code{init_table} as well:

@smallexample
@group
#include

main ()
@{
init_table ();
yyparse ();
@}
@end group

@group
yyerror (s) /* Called by yyparse on error */
char *s;
@{
printf ("%s\n", s);
@}

struct init
@{
char *fname;
double (*fnct)();
@};
@end group

@group
struct init arith_fncts[]
= @{
"sin", sin,
"cos", cos,
"atan", atan,
"ln", log,
"exp", exp,
"sqrt", sqrt,
0, 0
@};

/* The symbol table: a chain of `struct symrec'. */
symrec *sym_table = (symrec *)0;
@end group

@group
init_table () /* puts arithmetic functions in table. */
@{
int i;
symrec *ptr;
for (i = 0; arith_fncts[i].fname != 0; i++)
@{
ptr = putsym (arith_fncts[i].fname, FNCT);
ptr->value.fnctptr = arith_fncts[i].fnct;
@}
@}
@end group
@end smallexample

By simply editing the initialization list and adding the necessary include
files, you can add additional functions to the calculator.

Two important functions allow look-up and installation of symbols in the
symbol table. The function @code{putsym} is passed a name and the type
(@code{VAR} or @code{FNCT}) of the object to be installed. The object is
linked to the front of the list, and a pointer to the object is returned.
The function @code{getsym} is passed the name of the symbol to look up. If
found, a pointer to that symbol is returned; otherwise zero is returned.

@smallexample
symrec *
putsym (sym_name,sym_type)
char *sym_name;
int sym_type;
@{
symrec *ptr;
ptr = (symrec *) malloc (sizeof (symrec));
ptr->name = (char *) malloc (strlen (sym_name) + 1);
strcpy (ptr->name,sym_name);
ptr->type = sym_type;
ptr->value.var = 0; /* set value to 0 even if fctn. */
ptr->next = (struct symrec *)sym_table;
sym_table = ptr;
return ptr;
@}

symrec *
getsym (sym_name)
char *sym_name;
@{
symrec *ptr;
for (ptr = sym_table; ptr != (symrec *) 0;
ptr = (symrec *)ptr->next)
if (strcmp (ptr->name,sym_name) == 0)
return ptr;
return 0;
@}
@end smallexample

The function @code{yylex} must now recognize variables, numeric values, and
the single-character arithmetic operators. Strings of alphanumeric
characters with a leading nondigit are recognized as either variables or
functions depending on what the symbol table says about them.

The string is passed to @code{getsym} for look up in the symbol table. If
the name appears in the table, a pointer to its location and its type
(@code{VAR} or @code{FNCT}) is returned to @code{yyparse}. If it is not
already in the table, then it is installed as a @code{VAR} using
@code{putsym}. Again, a pointer and its type (which must be @code{VAR}) is
returned to @code{yyparse}.@refill

No change is needed in the handling of numeric values and arithmetic
operators in @code{yylex}.

@smallexample
@group
#include
yylex ()
@{
int c;

/* Ignore whitespace, get first nonwhite character. */
while ((c = getchar ()) == ' ' || c == '\t');

if (c == EOF)
return 0;
@end group

@group
/* Char starts a number => parse the number. */
if (c == '.' || isdigit (c))
@{
ungetc (c, stdin);
scanf ("%lf", &yylval.val);
return NUM;
@}
@end group

@group
/* Char starts an identifier => read the name. */
if (isalpha (c))
@{
symrec *s;
static char *symbuf = 0;
static int length = 0;
int i;
@end group

@group
/* Initially make the buffer long enough
for a 40-character symbol name. */
if (length == 0)
length = 40, symbuf = (char *)malloc (length + 1);

i = 0;
do
@end group
@group
@{
/* If buffer is full, make it bigger. */
if (i == length)
@{
length *= 2;
symbuf = (char *)realloc (symbuf, length + 1);
@}
/* Add this character to the buffer. */
symbuf[i++] = c;
/* Get another character. */
c = getchar ();
@}
@end group
@group
while (c != EOF && isalnum (c));

ungetc (c, stdin);
symbuf[i] = '\0';
@end group

@group
s = getsym (symbuf);
if (s == 0)
s = putsym (symbuf, VAR);
yylval.tptr = s;
return s->type;
@}

/* Any other character is a token by itself. */
return c;
@}
@end group
@end smallexample

This program is both powerful and flexible. You may easily add new
functions, and it is a simple job to modify this code to install predefined
variables such as @code{pi} or @code{e} as well.

@node Exercises, , Multi-function Calc, Examples
@section Exercises
@cindex exercises

@enumerate
@item
Add some new functions from @file{math.h} to the initialization list.

@item
Add another array that contains constants and their values. Then
modify @code{init_table} to add these constants to the symbol table.
It will be easiest to give the constants type @code{VAR}.

@item
Make the program report an error if the user refers to an
uninitialized variable in any way except to store a value in it.
@end enumerate

@node Grammar File, Interface, Examples, Top
@chapter Bison Grammar Files

Bison takes as input a context-free grammar specification and produces a
C-language function that recognizes correct instances of the grammar.

The Bison grammar input file conventionally has a name ending in @samp{.y}.

@menu
* Grammar Outline:: Overall layout of the grammar file.
* Symbols:: Terminal and nonterminal symbols.
* Rules:: How to write grammar rules.
* Recursion:: Writing recursive rules.
* Semantics:: Semantic values and actions.
* Declarations:: All kinds of Bison declarations are described here.
* Multiple Parsers:: Putting more than one Bison parser in one program.
@end menu

@node Grammar Outline, Symbols, , Grammar File
@section Outline of a Bison Grammar

A Bison grammar file has four main sections, shown here with the
appropriate delimiters:

@example
%@{
@var{C declarations}
%@}

@var{Bison declarations}

%%
@var{Grammar rules}
%%

@var{Additional C code}
@end example

Comments enclosed in @samp{/* @dots{} */} may appear in any of the sections.

@menu
* C Declarations:: Syntax and usage of the C declarations section.
* Bison Declarations:: Syntax and usage of the Bison declarations section.
* Grammar Rules:: Syntax and usage of the grammar rules section.
* C Code:: Syntax and usage of the additional C code section.
@end menu

@node C Declarations, Bison Declarations, , Grammar Outline
@subsection The C Declarations Section
@cindex C declarations section
@cindex declarations, C

The @var{C declarations} section contains macro definitions and
declarations of functions and variables that are used in the actions in the
grammar rules. These are copied to the beginning of the parser file so
that they precede the definition of @code{yyparse}. You can use
@samp{#include} to get the declarations from a header file. If you don't
need any C declarations, you may omit the @samp{%@{} and @samp{%@}}
delimiters that bracket this section.

@node Bison Declarations, Grammar Rules, C Declarations, Grammar Outline
@subsection The Bison Declarations Section
@cindex Bison declarations (introduction)
@cindex declarations, Bison (introduction)

The @var{Bison declarations} section contains declarations that define
terminal and nonterminal symbols, specify precedence, and so on.
In some simple grammars you may not need any declarations.
@xref{Declarations, ,Bison Declarations}.

@node Grammar Rules, C Code, Bison Declarations, Grammar Outline
@subsection The Grammar Rules Section
@cindex grammar rules section
@cindex rules section for grammar

The @dfn{grammar rules} section contains one or more Bison grammar
rules, and nothing else. @xref{Rules, ,Syntax of Grammar Rules}.

There must always be at least one grammar rule, and the first
@samp{%%} (which precedes the grammar rules) may never be omitted even
if it is the first thing in the file.

@node C Code, , Grammar Rules, Grammar Outline
@subsection The Additional C Code Section
@cindex additional C code section
@cindex C code, section for additional

The @var{additional C code} section is copied verbatim to the end of
the parser file, just as the @var{C declarations} section is copied to
the beginning. This is the most convenient place to put anything
that you want to have in the parser file but which need not come before
the definition of @code{yyparse}. For example, the definitions of
@code{yylex} and @code{yyerror} often go here. @xref{Interface, ,Parser C-Language Interface}.

If the last section is empty, you may omit the @samp{%%} that separates it
from the grammar rules.

The Bison parser itself contains many static variables whose names start
with @samp{yy} and many macros whose names start with @samp{YY}. It is a
good idea to avoid using any such names (except those documented in this
manual) in the additional C code section of the grammar file.

@node Symbols, Rules, Grammar Outline, Grammar File
@section Symbols, Terminal and Nonterminal
@cindex nonterminal symbol
@cindex terminal symbol
@cindex token type
@cindex symbol

@dfn{Symbols} in Bison grammars represent the grammatical classifications
of the language.

A @dfn{terminal symbol} (also known as a @dfn{token type}) represents a
class of syntactically equivalent tokens. You use the symbol in grammar
rules to mean that a token in that class is allowed. The symbol is
represented in the Bison parser by a numeric code, and the @code{yylex}
function returns a token type code to indicate what kind of token has been
read. You don't need to know what the code value is; you can use the
symbol to stand for it.

A @dfn{nonterminal symbol} stands for a class of syntactically equivalent
groupings. The symbol name is used in writing grammar rules. By convention,
it should be all lower case.

Symbol names can contain letters, digits (not at the beginning),
underscores and periods. Periods make sense only in nonterminals.

There are two ways of writing terminal symbols in the grammar:

@itemize @bullet
@item
A @dfn{named token type} is written with an identifier, like an
identifier in C. By convention, it should be all upper case. Each
such name must be defined with a Bison declaration such as
@code{%token}. @xref{Token Decl, ,Token Type Names}.

@item
@cindex character token
@cindex literal token
@cindex single-character literal
A @dfn{character token type} (or @dfn{literal token}) is written in
the grammar using the same syntax used in C for character constants;
for example, @code{'+'} is a character token type. A character token
type doesn't need to be declared unless you need to specify its
semantic value data type (@pxref{Value Type, ,Data Types of Semantic Values}), associativity, or
precedence (@pxref{Precedence, ,Operator Precedence}).

By convention, a character token type is used only to represent a
token that consists of that particular character. Thus, the token
type @code{'+'} is used to represent the character @samp{+} as a
token. Nothing enforces this convention, but if you depart from it,
your program will confuse other readers.

All the usual escape sequences used in character literals in C can be
used in Bison as well, but you must not use the null character as a
character literal because its ASCII code, zero, is the code
@code{yylex} returns for end-of-input (@pxref{Calling Convention, ,Calling Convention for @code{yylex}}).
@end itemize

How you choose to write a terminal symbol has no effect on its
grammatical meaning. That depends only on where it appears in rules and
on when the parser function returns that symbol.

The value returned by @code{yylex} is always one of the terminal symbols
(or 0 for end-of-input). Whichever way you write the token type in the
grammar rules, you write it the same way in the definition of @code{yylex}.
The numeric code for a character token type is simply the ASCII code for
the character, so @code{yylex} can use the identical character constant to
generate the requisite code. Each named token type becomes a C macro in
the parser file, so @code{yylex} can use the name to stand for the code.
(This is why periods don't make sense in terminal symbols.)
@xref{Calling Convention, ,Calling Convention for @code{yylex}}.

If @code{yylex} is defined in a separate file, you need to arrange for the
token-type macro definitions to be available there. Use the @samp{-d}
option when you run Bison, so that it will write these macro definitions
into a separate header file @file{@var{name}.tab.h} which you can include
in the other source files that need it. @xref{Invocation, ,Invoking Bison}.

The symbol @code{error} is a terminal symbol reserved for error recovery
(@pxref{Error Recovery}); you shouldn't use it for any other purpose.
In particular, @code{yylex} should never return this value.

@node Rules, Recursion, Symbols, Grammar File
@section Syntax of Grammar Rules
@cindex rule syntax
@cindex grammar rule syntax
@cindex syntax of grammar rules

A Bison grammar rule has the following general form:

@example
@var{result}: @var{components}@dots{}
;
@end example

@noindent
where @var{result} is the nonterminal symbol that this rule describes
and @var{components} are various terminal and nonterminal symbols that
are put together by this rule (@pxref{Symbols}).

For example,

@example
@group
exp: exp '+' exp
;
@end group
@end example

@noindent
says that two groupings of type @code{exp}, with a @samp{+} token in between,
can be combined into a larger grouping of type @code{exp}.

Whitespace in rules is significant only to separate symbols. You can add
extra whitespace as you wish.

Scattered among the components can be @var{actions} that determine
the semantics of the rule. An action looks like this:

@example
@{@var{C statements}@}
@end example

@noindent
Usually there is only one action and it follows the components.
@xref{Actions}.

@findex |
Multiple rules for the same @var{result} can be written separately or can
be joined with the vertical-bar character @samp{|} as follows:

@ifinfo
@example
@var{result}: @var{rule1-components}@dots{}
| @var{rule2-components}@dots{}
@dots{}
;
@end example
@end ifinfo
@iftex
@example
@group
@var{result}: @var{rule1-components}@dots{}
| @var{rule2-components}@dots{}
@dots{}
;
@end group
@end example
@end iftex

@noindent
They are still considered distinct rules even when joined in this way.

If @var{components} in a rule is empty, it means that @var{result} can
match the empty string. For example, here is how to define a
comma-separated sequence of zero or more @code{exp} groupings:

@example
@group
expseq: /* empty */
| expseq1
;
@end group

@group
expseq1: exp
| expseq1 ',' exp
;
@end group
@end example

@noindent
It is customary to write a comment @samp{/* empty */} in each rule
with no components.

@node Recursion, Semantics, Rules, Grammar File
@section Recursive Rules
@cindex recursive rule

A rule is called @dfn{recursive} when its @var{result} nonterminal appears
also on its right hand side. Nearly all Bison grammars need to use
recursion, because that is the only way to define a sequence of any number
of somethings. Consider this recursive definition of a comma-separated
sequence of one or more expressions:

@example
@group
expseq1: exp
| expseq1 ',' exp
;
@end group
@end example

@cindex left recursion
@cindex right recursion
@noindent
Since the recursive use of @code{expseq1} is the leftmost symbol in the
right hand side, we call this @dfn{left recursion}. By contrast, here
the same construct is defined using @dfn{right recursion}:

@example
@group
expseq1: exp
| exp ',' expseq1
;
@end group
@end example

@noindent
Any kind of sequence can be defined using either left recursion or
right recursion, but you should always use left recursion, because it
can parse a sequence of any number of elements with bounded stack
space. Right recursion uses up space on the Bison stack in proportion
to the number of elements in the sequence, because all the elements
must be shifted onto the stack before the rule can be applied even
once. @xref{Algorithm, ,The Bison Parser Algorithm }, for
further explanation of this.

@cindex mutual recursion
@dfn{Indirect} or @dfn{mutual} recursion occurs when the result of the
rule does not appear directly on its right hand side, but does appear
in rules for other nonterminals which do appear on its right hand
side.

For example:

@example
@group
expr: primary
| primary '+' primary
;
@end group

@group
primary: constant
| '(' expr ')'
;
@end group
@end example

@noindent
defines two mutually-recursive nonterminals, since each refers to the
other.

@node Semantics, Declarations, Recursion, Grammar File
@section Defining Language Semantics
@cindex defining language semantics
@cindex language semantics, defining

The grammar rules for a language determine only the syntax. The semantics
are determined by the semantic values associated with various tokens and
groupings, and by the actions taken when various groupings are recognized.

For example, the calculator calculates properly because the value
associated with each expression is the proper number; it adds properly
because the action for the grouping @w{@samp{@var{x} + @var{y}}} is to add
the numbers associated with @var{x} and @var{y}.

@menu
* Value Type:: Specifying one data type for all semantic values.
* Multiple Types:: Specifying several alternative data types.
* Actions:: An action is the semantic definition of a grammar rule.
* Action Types:: Specifying data types for actions to operate on.
* Mid-Rule Actions:: Most actions go at the end of a rule.
This says when, why and how to use the exceptional
action in the middle of a rule.
@end menu

@node Value Type, Multiple Types, , Semantics
@subsection Data Types of Semantic Values
@cindex semantic value type
@cindex value type, semantic
@cindex data types of semantic values
@cindex default data type

In a simple program it may be sufficient to use the same data type for
the semantic values of all language constructs. This was true in the
RPN and infix calculator examples (@pxref{RPN Calc, ,Reverse Polish Notation Calculator}).

Bison's default is to use type @code{int} for all semantic values. To
specify some other type, define @code{YYSTYPE} as a macro, like this:

@example
#define YYSTYPE double
@end example

@noindent
This macro definition must go in the C declarations section of the grammar
file (@pxref{Grammar Outline, ,Outline of a Bison Grammar}).

@node Multiple Types, Actions, Value Type, Semantics
@subsection More Than One Value Type

In most programs, you will need different data types for different kinds
of tokens and groupings. For example, a numeric constant may need type
@code{int} or @code{long}, while a string constant needs type @code{char *},
and an identifier might need a pointer to an entry in the symbol table.

To use more than one data type for semantic values in one parser, Bison
requires you to do two things:

@itemize @bullet
@item
Specify the entire collection of possible data types, with the
@code{%union} Bison declaration (@pxref{Union Decl, ,The Collection of Value Types}).

@item
Choose one of those types for each symbol (terminal or nonterminal)
for which semantic values are used. This is done for tokens with the
@code{%token} Bison declaration (@pxref{Token Decl, ,Token Type Names}) and for groupings
with the @code{%type} Bison declaration (@pxref{Type Decl, ,Nonterminal Symbols}).
@end itemize

@node Actions, Action Types, Multiple Types, Semantics
@subsection Actions
@cindex action
@vindex $$
@vindex $@var{n}

An action accompanies a syntactic rule and contains C code to be executed
each time an instance of that rule is recognized. The task of most actions
is to compute a semantic value for the grouping built by the rule from the
semantic values associated with tokens or smaller groupings.

An action consists of C statements surrounded by braces, much like a
compound statement in C. It can be placed at any position in the rule; it
is executed at that position. Most rules have just one action at the end
of the rule, following all the components. Actions in the middle of a rule
are tricky and used only for special purposes (@pxref{Mid-Rule Actions, ,Actions in Mid-Rule}).

The C code in an action can refer to the semantic values of the components
matched by the rule with the construct @code{$@var{n}}, which stands for
the value of the @var{n}th component. The semantic value for the grouping
being constructed is @code{$$}. (Bison translates both of these constructs
into array element references when it copies the actions into the parser
file.)

Here is a typical example:

@example
@group
exp: @dots{}
| exp '+' exp
@{ $$ = $1 + $3; @}
@end group
@end example

@noindent
This rule constructs an @code{exp} from two smaller @code{exp} groupings
connected by a plus-sign token. In the action, @code{$1} and @code{$3}
refer to the semantic values of the two component @code{exp} groupings,
which are the first and third symbols on the right hand side of the rule.
The sum is stored into @code{$$} so that it becomes the semantic value of
the addition-expression just recognized by the rule. If there were a
useful semantic value associated with the @samp{+} token, it could be
referred to as @code{$2}.@refill

@cindex default action
If you don't specify an action for a rule, Bison supplies a default:
@w{@code{$$ = $1}.} Thus, the value of the first symbol in the rule becomes
the value of the whole rule. Of course, the default rule is valid only
if the two data types match. There is no meaningful default action for
an empty rule; every empty rule must have an explicit action unless the
rule's value does not matter.

@code{$@var{n}} with @var{n} zero or negative is allowed for reference
to tokens and groupings on the stack @emph{before} those that match the
current rule. This is a very risky practice, and to use it reliably
you must be certain of the context in which the rule is applied. Here
is a case in which you can use this reliably:

@example
@group
foo: expr bar '+' expr @{ @dots{} @}
| expr bar '-' expr @{ @dots{} @}
;
@end group

@group
bar: /* empty */
@{ previous_expr = $0; @}
;
@end group
@end example

As long as @code{bar} is used only in the fashion shown here, @code{$0}
always refers to the @code{expr} which precedes @code{bar} in the
definition of @code{foo}.

@node Action Types, Mid-Rule Actions, Actions, Semantics
@subsection Data Types of Values in Actions
@cindex action data types
@cindex data types in actions

If you have chosen a single data type for semantic values, the @code{$$}
and @code{$@var{n}} constructs always have that data type.

If you have used @code{%union} to specify a variety of data types, then you
must declare a choice among these types for each terminal or nonterminal
symbol that can have a semantic value. Then each time you use @code{$$} or
@code{$@var{n}}, its data type is determined by which symbol it refers to
in the rule. In this example,@refill

@example
@group
exp: @dots{}
| exp '+' exp
@{ $$ = $1 + $3; @}
@end group
@end example

@noindent
@code{$1} and @code{$3} refer to instances of @code{exp}, so they all
have the data type declared for the nonterminal symbol @code{exp}. If
@code{$2} were used, it would have the data type declared for the
terminal symbol @code{'+'}, whatever that might be.@refill

Alternatively, you can specify the data type when you refer to the value,
by inserting @samp{<@var{type}>} after the @samp{$} at the beginning of the
reference. For example, if you have defined types as shown here:

@example
@group
%union @{
int itype;
double dtype;
@}
@end group
@end example

@noindent
then you can write @code{$1} to refer to the first subunit of the
rule as an integer, or @code{$1} to refer to it as a double.

@node Mid-Rule Actions, , Action Types, Semantics
@subsection Actions in Mid-Rule
@cindex actions in mid-rule
@cindex mid-rule actions

Occasionally it is useful to put an action in the middle of a rule.
These actions are written just like usual end-of-rule actions, but they
are executed before the parser even recognizes the following components.

A mid-rule action may refer to the components preceding it using
@code{$@var{n}}, but it may not refer to subsequent components because
it is run before they are parsed.

The mid-rule action itself counts as one of the components of the rule.
This makes a difference when there is another action later in the same rule
(and usually there is another at the end): you have to count the actions
along with the symbols when working out which number @var{n} to use in
@code{$@var{n}}.

The mid-rule action can also have a semantic value. The action can set
its value with an assignment to @code{$$}, and actions later in the rule
can refer to the value using @code{$@var{n}}. Since there is no symbol
to name the action, there is no way to declare a data type for the value
in advance, so you must use the @samp{$<@dots{}>} construct to specify a
data type each time you refer to this value.

There is no way to set the value of the entire rule with a mid-rule
action, because assignments to @code{$$} do not have that effect. The
only way to set the value for the entire rule is with an ordinary action
at the end of the rule.

Here is an example from a hypothetical compiler, handling a @code{let}
statement that looks like @samp{let (@var{variable}) @var{statement}} and
serves to create a variable named @var{variable} temporarily for the
duration of @var{statement}. To parse this construct, we must put
@var{variable} into the symbol table while @var{statement} is parsed, then
remove it afterward. Here is how it is done:

@example
@group
stmt: LET '(' var ')'
@{ $$ = push_context ();
declare_variable ($3); @}
stmt @{ $$ = $6;
pop_context ($5); @}
@end group
@end example

@noindent
As soon as @samp{let (@var{variable})} has been recognized, the first
action is run. It saves a copy of the current semantic context (the
list of accessible variables) as its semantic value, using alternative
@code{context} in the data-type union. Then it calls
@code{declare_variable} to add the new variable to that list. Once the
first action is finished, the embedded statement @code{stmt} can be
parsed. Note that the mid-rule action is component number 5, so the
@samp{stmt} is component number 6.

After the embedded statement is parsed, its semantic value becomes the
value of the entire @code{let}-statement. Then the semantic value from the
earlier action is used to restore the prior list of variables. This
removes the temporary @code{let}-variable from the list so that it won't
appear to exist while the rest of the program is parsed.

Taking action before a rule is completely recognized often leads to
conflicts since the parser must commit to a parse in order to execute the
action. For example, the following two rules, without mid-rule actions,
can coexist in a working parser because the parser can shift the open-brace
token and look at what follows before deciding whether there is a
declaration or not:

@example
@group
compound: '@{' declarations statements '@}'
| '@{' statements '@}'
;
@end group
@end example

@noindent
But when we add a mid-rule action as follows, the rules become nonfunctional:

@example
@group
compound: @{ prepare_for_local_variables (); @}
'@{' declarations statements '@}'
@end group
@group
| '@{' statements '@}'
;
@end group
@end example

@noindent
Now the parser is forced to decide whether to run the mid-rule action
when it has read no farther than the open-brace. In other words, it
must commit to using one rule or the other, without sufficient
information to do it correctly. (The open-brace token is what is called
the @dfn{look-ahead} token at this time, since the parser is still
deciding what to do about it. @xref{Look-Ahead, ,Look-Ahead Tokens}.)

You might think that you could correct the problem by putting identical
actions into the two rules, like this:

@example
@group
compound: @{ prepare_for_local_variables (); @}
'@{' declarations statements '@}'
| @{ prepare_for_local_variables (); @}
'@{' statements '@}'
;
@end group
@end example

@noindent
But this does not help, because Bison does not realize that the two actions
are identical. (Bison never tries to understand the C code in an action.)

If the grammar is such that a declaration can be distinguished from a
statement by the first token (which is true in C), then one solution which
does work is to put the action after the open-brace, like this:

@example
@group
compound: '@{' @{ prepare_for_local_variables (); @}
declarations statements '@}'
| '@{' statements '@}'
;
@end group
@end example

@noindent
Now the first token of the following declaration or statement,
which would in any case tell Bison which rule to use, can still do so.

Another solution is to bury the action inside a nonterminal symbol which
serves as a subroutine:

@example
@group
subroutine: /* empty */
@{ prepare_for_local_variables (); @}
;

@end group

@group
compound: subroutine
'@{' declarations statements '@}'
| subroutine
'@{' statements '@}'
;
@end group
@end example

@noindent
Now Bison can execute the action in the rule for @code{subroutine} without
deciding which rule for @code{compound} it will eventually use. Note that
the action is now at the end of its rule. Any mid-rule action can be
converted to an end-of-rule action in this way, and this is what Bison
actually does to implement mid-rule actions.

@node Declarations, Multiple Parsers, Semantics, Grammar File
@section Bison Declarations
@cindex declarations, Bison
@cindex Bison declarations

The @dfn{Bison declarations} section of a Bison grammar defines the symbols
used in formulating the grammar and the data types of semantic values.
@xref{Symbols}.

All token type names (but not single-character literal tokens such as
@code{'+'} and @code{'*'}) must be declared. Nonterminal symbols must be
declared if you need to specify which data type to use for the semantic
value (@pxref{Multiple Types, ,More Than One Value Type}).

The first rule in the file also specifies the start symbol, by default.
If you want some other symbol to be the start symbol, you must declare
it explicitly (@pxref{Language and Grammar, ,Languages and Context-Free Grammars}).

@menu
* Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity.
* Union Decl:: Declaring the set of all semantic value types.
* Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Expect Decl:: Suppressing warnings about shift/reduce conflicts.
* Start Decl:: Specifying the start symbol.
* Pure Decl:: Requesting a reentrant parser.
* Decl Summary:: Table of all Bison declarations.
@end menu

@node Token Decl, Precedence Decl, , Declarations
@subsection Token Type Names
@cindex declaring token type names
@cindex token type names, declaring
@findex %token

The basic way to declare a token type name (terminal symbol) is as follows:

@example
%token @var{name}
@end example

Bison will convert this into a @code{#define} directive in
the parser, so that the function @code{yylex} (if it is in this file)
can use the name @var{name} to stand for this token type's code.

Alternatively, you can use @code{%left}, @code{%right}, or @code{%nonassoc}
instead of @code{%token}, if you wish to specify precedence.
@xref{Precedence Decl, ,Operator Precedence}.

You can explicitly specify the numeric code for a token type by appending
an integer value in the field immediately following the token name:

@example
%token NUM 300
@end example

@noindent
It is generally best, however, to let Bison choose the numeric codes for
all token types. Bison will automatically select codes that don't conflict
with each other or with ASCII characters.

In the event that the stack type is a union, you must augment the
@code{%token} or other token declaration to include the data type
alternative delimited by angle-brackets (@pxref{Multiple Types, ,More Than One Value Type}).

For example:

@example
@group
%union @{ /* define stack type */
double val;
symrec *tptr;
@}
%token NUM /* define token NUM and its type */
@end group
@end example

@node Precedence Decl, Union Decl, Token Decl, Declarations
@subsection Operator Precedence
@cindex precedence declarations
@cindex declaring operator precedence
@cindex operator precedence, declaring

Use the @code{%left}, @code{%right} or @code{%nonassoc} declaration to
declare a token and specify its precedence and associativity, all at
once. These are called @dfn{precedence declarations}.
@xref{Precedence, ,Operator Precedence}, for general information on operator precedence.

The syntax of a precedence declaration is the same as that of
@code{%token}: either

@example
%left @var{symbols}@dots{}
@end example

@noindent
or

@example
%left <@var{type}> @var{symbols}@dots{}
@end example

And indeed any of these declarations serves the purposes of @code{%token}.
But in addition, they specify the associativity and relative precedence for
all the @var{symbols}:

@itemize @bullet
@item
The associativity of an operator @var{op} determines how repeated uses
of the operator nest: whether @samp{@var{x} @var{op} @var{y} @var{op}
@var{z}} is parsed by grouping @var{x} with @var{y} first or by
grouping @var{y} with @var{z} first. @code{%left} specifies
left-associativity (grouping @var{x} with @var{y} first) and
@code{%right} specifies right-associativity (grouping @var{y} with
@var{z} first). @code{%nonassoc} specifies no associativity, which
means that @samp{@var{x} @var{op} @var{y} @var{op} @var{z}} is
considered a syntax error.

@item
The precedence of an operator determines how it nests with other operators.
All the tokens declared in a single precedence declaration have equal
precedence and nest together according to their associativity.
When two tokens declared in different precedence declarations associate,
the one declared later has the higher precedence and is grouped first.
@end itemize

@node Union Decl, Type Decl, Precedence Decl, Declarations
@subsection The Collection of Value Types
@cindex declaring value types
@cindex value types, declaring
@findex %union

The @code{%union} declaration specifies the entire collection of possible
data types for semantic values. The keyword @code{%union} is followed by a
pair of braces containing the same thing that goes inside a @code{union} in
C.

For example:

@example
@group
%union @{
double val;
symrec *tptr;
@}
@end group
@end example

@noindent
This says that the two alternative types are @code{double} and @code{symrec
*}. They are given names @code{val} and @code{tptr}; these names are used
in the @code{%token} and @code{%type} declarations to pick one of the types
for a terminal or nonterminal symbol (@pxref{Type Decl, ,Nonterminal Symbols}).

Note that, unlike making a @code{union} declaration in C, you do not write
a semicolon after the closing brace.

@node Type Decl, Expect Decl, Union Decl, Declarations
@subsection Nonterminal Symbols
@cindex declaring value types, nonterminals
@cindex value types, nonterminals, declaring
@findex %type

@noindent
When you use @code{%union} to specify multiple value types, you must
declare the value type of each nonterminal symbol for which values are
used. This is done with a @code{%type} declaration, like this:

@example
%type <@var{type}> @var{nonterminal}@dots{}
@end example

@noindent
Here @var{nonterminal} is the name of a nonterminal symbol, and @var{type}
is the name given in the @code{%union} to the alternative that you want
(@pxref{Union Decl, ,The Collection of Value Types}). You can give any number of nonterminal symbols in
the same @code{%type} declaration, if they have the same value type. Use
spaces to separate the symbol names.

@node Expect Decl, Start Decl, Type Decl, Declarations
@subsection Suppressing Conflict Warnings
@cindex suppressing conflict warnings
@cindex preventing warnings about conflicts
@cindex warnings, preventing
@cindex conflicts, suppressing warnings of
@findex %expect

Bison normally warns if there are any conflicts in the grammar
(@pxref{Shift/Reduce, ,Shift/Reduce Conflicts}), but most real grammars have harmless shift/reduce
conflicts which are resolved in a predictable way and would be difficult to
eliminate. It is desirable to suppress the warning about these conflicts
unless the number of conflicts changes. You can do this with the
@code{%expect} declaration.

The declaration looks like this:

@example
%expect @var{n}
@end example

Here @var{n} is a decimal integer. The declaration says there should be no
warning if there are @var{n} shift/reduce conflicts and no reduce/reduce
conflicts. The usual warning is given if there are either more or fewer
conflicts, or if there are any reduce/reduce conflicts.

In general, using @code{%expect} involves these steps:

@itemize @bullet
@item
Compile your grammar without @code{%expect}. Use the @samp{-v} option
to get a verbose list of where the conflicts occur. Bison will also
print the number of conflicts.

@item
Check each of the conflicts to make sure that Bison's default
resolution is what you really want. If not, rewrite the grammar and
go back to the beginning.

@item
Add an @code{%expect} declaration, copying the number @var{n} from the
number which Bison printed.
@end itemize

Now Bison will stop annoying you about the conflicts you have checked, but
it will warn you again if changes in the grammar result in additional
conflicts.

@node Start Decl, Pure Decl, Expect Decl, Declarations
@subsection The Start-Symbol
@cindex declaring the start symbol
@cindex start symbol, declaring
@cindex default start symbol
@findex %start

Bison assumes by default that the start symbol for the grammar is the first
nonterminal specified in the grammar specification section. The programmer
may override this restriction with the @code{%start} declaration as follows:

@example
%start @var{symbol}
@end example

@node Pure Decl, Decl Summary, Start Decl, Declarations
@subsection A Pure (Reentrant) Parser
@cindex reentrant parser
@cindex pure parser
@findex %pure_parser

A @dfn{reentrant} program is one which does not alter in the course of
execution; in other words, it consists entirely of @dfn{pure} (read-only)
code. Reentrancy is important whenever asynchronous execution is possible;
for example, a nonreentrant program may not be safe to call from a signal
handler. In systems with multiple threads of control, a nonreentrant
program must be called only within interlocks.

The Bison parser is not normally a reentrant program, because it uses
statically allocated variables for communication with @code{yylex}. These
variables include @code{yylval} and @code{yylloc}.

The Bison declaration @code{%pure_parser} says that you want the parser
to be reentrant. It looks like this:

@example
%pure_parser
@end example

The effect is that the two communication variables become local
variables in @code{yyparse}, and a different calling convention is used for
the lexical analyzer function @code{yylex}. @xref{Pure Calling, ,Calling for Pure Parsers}, for the
details of this. The variable @code{yynerrs} also becomes local in
@code{yyparse} (@pxref{Error Reporting, ,The Error Reporting Function @code{yyerror}}). The convention for calling
@code{yyparse} itself is unchanged.

@node Decl Summary, , Pure Decl, Declarations
@subsection Bison Declaration Summary
@cindex Bison declaration summary
@cindex declaration summary
@cindex summary, Bison declaration

Here is a summary of all Bison declarations:

@table @code
@item %union
Declare the collection of data types that semantic values may have
(@pxref{Union Decl, ,The Collection of Value Types}).

@item %token
Declare a terminal symbol (token type name) with no precedence
or associativity specified (@pxref{Token Decl, ,Token Type Names}).

@item %right
Declare a terminal symbol (token type name) that is right-associative
(@pxref{Precedence Decl, ,Operator Precedence}).

@item %left
Declare a terminal symbol (token type name) that is left-associative
(@pxref{Precedence Decl, ,Operator Precedence}).

@item %nonassoc
Declare a terminal symbol (token type name) that is nonassociative
(using it in a way that would be associative is a syntax error)
(@pxref{Precedence Decl, ,Operator Precedence}).

@item %type
Declare the type of semantic values for a nonterminal symbol
(@pxref{Type Decl, ,Nonterminal Symbols}).

@item %start
Specify the grammar's start symbol (@pxref{Start Decl, ,The Start-Symbol}).

@item %expect
Declare the expected number of shift-reduce conflicts
(@pxref{Expect Decl, ,Suppressing Conflict Warnings}).

@item %pure_parser
Request a pure (reentrant) parser program (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
@end table

@node Multiple Parsers, , Declarations, Grammar File
@section Multiple Parsers in the Same Program

Most programs that use Bison parse only one language and therefore contain
only one Bison parser. But what if you want to parse more than one
language with the same program? Then you need to avoid a name conflict
between different definitions of @code{yyparse}, @code{yylval}, and so on.

The easy way to do this is to use the option @samp{-p @var{prefix}}
(@pxref{Invocation, ,Invoking Bison}). This renames the interface functions and
variables of the Bison parser to start with @var{prefix} instead of
@samp{yy}. You can use this to give each parser distinct names that do
not conflict.

The precise list of symbols renamed is @code{yyparse}, @code{yylex},
@code{yyerror}, @code{yylval}, @code{yychar} and @code{yydebug}. For
example, if you use @samp{-p c}, the names become @code{cparse},
@code{clex}, and so on.

@strong{All the other variables and macros associated with Bison are not
renamed.} These others are not global; there is no conflict if the same
name is used in different parsers. For example, @code{YYSTYPE} is not
renamed, but defining this in different ways in different parsers causes
no trouble (@pxref{Value Type, ,Data Types of Semantic Values}).

The @samp{-p} option works by adding macro definitions to the beginning
of the parser source file, defining @code{yyparse} as
@code{@var{prefix}parse}, and so on. This effectively substitutes one
name for the other in the entire parser file.

@node Interface, Algorithm, Grammar File, Top
@chapter Parser C-Language Interface
@cindex C-language interface
@cindex interface

The Bison parser is actually a C function named @code{yyparse}. Here we
describe the interface conventions of @code{yyparse} and the other
functions that it needs to use.

Keep in mind that the parser uses many C identifiers starting with
@samp{yy} and @samp{YY} for internal purposes. If you use such an
identifier (aside from those in this manual) in an action or in additional
C code in the grammar file, you are likely to run into trouble.

@menu
* Parser Function:: How to call @code{yyparse} and what it returns.
* Lexical:: You must supply a function @code{yylex}
which reads tokens.
* Error Reporting:: You must supply a function @code{yyerror}.
* Action Features:: Special features for use in actions.
@end menu

@node Parser Function, Lexical, , Interface
@section The Parser Function @code{yyparse}
@findex yyparse

You call the function @code{yyparse} to cause parsing to occur. This
function reads tokens, executes actions, and ultimately returns when it
encounters end-of-input or an unrecoverable syntax error. You can also
write an action which directs @code{yyparse} to return immediately without
reading further.

The value returned by @code{yyparse} is 0 if parsing was successful (return
is due to end-of-input).

The value is 1 if parsing failed (return is due to a syntax error).

In an action, you can cause immediate return from @code{yyparse} by using
these macros:

@table @code
@item YYACCEPT
@findex YYACCEPT
Return immediately with value 0 (to report success).

@item YYABORT
@findex YYABORT
Return immediately with value 1 (to report failure).
@end table

@node Lexical, Error Reporting, Parser Function, Interface
@section The Lexical Analyzer Function @code{yylex}
@findex yylex
@cindex lexical analyzer

The @dfn{lexical analyzer} function, @code{yylex}, recognizes tokens from
the input stream and returns them to the parser. Bison does not create
this function automatically; you must write it so that @code{yyparse} can
call it. The function is sometimes referred to as a lexical scanner.

In simple programs, @code{yylex} is often defined at the end of the Bison
grammar file. If @code{yylex} is defined in a separate source file, you
need to arrange for the token-type macro definitions to be available there.
To do this, use the @samp{-d} option when you run Bison, so that it will
write these macro definitions into a separate header file
@file{@var{name}.tab.h} which you can include in the other source files
that need it. @xref{Invocation, ,Invoking Bison}.@refill

@menu
* Calling Convention:: How @code{yyparse} calls @code{yylex}.
* Token Values:: How @code{yylex} must return the semantic value
of the token it has read.
* Token Positions:: How @code{yylex} must return the text position
(line number, etc.) of the token, if the
actions want that.
* Pure Calling:: How the calling convention differs
in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
@end menu

@node Calling Convention, Token Values, , Lexical
@subsection Calling Convention for @code{yylex}

The value that @code{yylex} returns must be the numeric code for the type
of token it has just found, or 0 for end-of-input.

When a token is referred to in the grammar rules by a name, that name
in the parser file becomes a C macro whose definition is the proper
numeric code for that token type. So @code{yylex} can use the name
to indicate that type. @xref{Symbols}.

When a token is referred to in the grammar rules by a character literal,
the numeric code for that character is also the code for the token type.
So @code{yylex} can simply return that character code. The null character
must not be used this way, because its code is zero and that is what
signifies end-of-input.

Here is an example showing these things:

@example
yylex ()
@{
@dots{}
if (c == EOF) /* Detect end of file. */
return 0;
@dots{}
if (c == '+' || c == '-')
return c; /* Assume token type for `+' is '+'. */
@dots{}
return INT; /* Return the type of the token. */
@dots{}
@}
@end example

@noindent
This interface has been designed so that the output from the @code{lex}
utility can be used without change as the definition of @code{yylex}.

@node Token Values, Token Positions, Calling Convention, Lexical
@subsection Semantic Values of Tokens

@vindex yylval
In an ordinary (nonreentrant) parser, the semantic value of the token must
be stored into the global variable @code{yylval}. When you are using
just one data type for semantic values, @code{yylval} has that type.
Thus, if the type is @code{int} (the default), you might write this in
@code{yylex}:

@example
@group
@dots{}
yylval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
@dots{}
@end group
@end example

When you are using multiple data types, @code{yylval}'s type is a union
made from the @code{%union} declaration (@pxref{Union Decl, ,The Collection of Value Types}). So when
you store a token's value, you must use the proper member of the union.
If the @code{%union} declaration looks like this:

@example
@group
%union @{
int intval;
double val;
symrec *tptr;
@}
@end group
@end example

@noindent
then the code in @code{yylex} might look like this:

@example
@group
@dots{}
yylval.intval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
@dots{}
@end group
@end example

@node Token Positions, Pure Calling, Token Values, Lexical
@subsection Textual Positions of Tokens

@vindex yylloc
If you are using the @samp{@@@var{n}}-feature (@pxref{Action Features, ,Special Features for Use in Actions}) in
actions to keep track of the textual locations of tokens and groupings,
then you must provide this information in @code{yylex}. The function
@code{yyparse} expects to find the textual location of a token just parsed
in the global variable @code{yylloc}. So @code{yylex} must store the
proper data in that variable. The value of @code{yylloc} is a structure
and you need only initialize the members that are going to be used by the
actions. The four members are called @code{first_line},
@code{first_column}, @code{last_line} and @code{last_column}. Note that
the use of this feature makes the parser noticeably slower.

@tindex YYLTYPE
The data type of @code{yylloc} has the name @code{YYLTYPE}.

@node Pure Calling, , Token Positions, Lexical
@subsection Calling for Pure Parsers

When you use the Bison declaration @code{%pure_parser} to request a pure,
reentrant parser, the global communication variables @code{yylval} and
@code{yylloc} cannot be used. (@xref{Pure Decl, ,A Pure (Reentrant) Parser}.) In such parsers the
two global variables are replaced by pointers passed as arguments to
@code{yylex}. You must declare them as shown here, and pass the
information back by storing it through those pointers.

@example
yylex (lvalp, llocp)
YYSTYPE *lvalp;
YYLTYPE *llocp;
@{
@dots{}
*lvalp = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
@dots{}
@}
@end example

If the grammar file does not use the @samp{@@} constructs to refer to
textual positions, then the type @code{YYLTYPE} will not be defined. In
this case, omit the second argument; @code{yylex} will be called with
only one argument.

@node Error Reporting, Action Features, Lexical, Interface
@section The Error Reporting Function @code{yyerror}
@cindex error reporting function
@findex yyerror
@cindex parse error
@cindex syntax error

The Bison parser detects a @dfn{parse error} or @dfn{syntax error}
whenever it reads a token which cannot satisfy any syntax rule. A
action in the grammar can also explicitly proclaim an error, using the
macro @code{YYERROR} (@pxref{Action Features, ,Special Features for Use in Actions}).

The Bison parser expects to report the error by calling an error
reporting function named @code{yyerror}, which you must supply. It is
called by @code{yyparse} whenever a syntax error is found, and it
receives one argument. For a parse error, the string is normally
@w{@code{"parse error"}}.

@findex YYERROR_VERBOSE
If you define the macro @code{YYERROR_VERBOSE} in the Bison declarations
section (@pxref{Bison Declarations, ,The Bison Declarations Section}), then Bison provides a more verbose
and specific error message string instead of just plain @w{@code{"parse
error"}}. It doesn't matter what definition you use for
@code{YYERROR_VERBOSE}, just whether you define it.

The parser can detect one other kind of error: stack overflow. This
happens when the input contains constructions that are very deeply
nested. It isn't likely you will encounter this, since the Bison
parser extends its stack automatically up to a very large limit. But
if overflow happens, @code{yyparse} calls @code{yyerror} in the usual
fashion, except that the argument string is @w{@code{"parser stack
overflow"}}.

The following definition suffices in simple programs:

@example
@group
yyerror (s)
char *s;
@{
@end group
@group
fprintf (stderr, "%s\n", s);
@}
@end group
@end example

After @code{yyerror} returns to @code{yyparse}, the latter will attempt
error recovery if you have written suitable error recovery grammar rules
(@pxref{Error Recovery}). If recovery is impossible, @code{yyparse} will
immediately return 1.

@vindex yynerrs
The variable @code{yynerrs} contains the number of syntax errors
encountered so far. Normally this variable is global; but if you
request a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}) then it is a local variable
which only the actions can access.

@node Action Features, , Error Reporting, Interface
@section Special Features for Use in Actions
@cindex summary, action features
@cindex action features summary

Here is a table of Bison constructs, variables and macros that
are useful in actions.

@table @samp
@item $$
Acts like a variable that contains the semantic value for the
grouping made by the current rule. @xref{Actions}.

@item $@var{n}
Acts like a variable that contains the semantic value for the
@var{n}th component of the current rule. @xref{Actions}.

@item $<@var{typealt}>$
Like @code{$$} but specifies alternative @var{typealt} in the union
specified by the @code{%union} declaration. @xref{Action Types, ,Data Types of Values in Actions}.

@item $<@var{typealt}>@var{n}
Like @code{$@var{n}} but specifies alternative @var{typealt} in the
union specified by the @code{%union} declaration.
@xref{Action Types, ,Data Types of Values in Actions}.@refill

@item YYABORT;
Return immediately from @code{yyparse}, indicating failure.
@xref{Parser Function, ,The Parser Function @code{yyparse}}.

@item YYACCEPT;
Return immediately from @code{yyparse}, indicating success.
@xref{Parser Function, ,The Parser Function @code{yyparse}}.

@item YYBACKUP (@var{token}, @var{value});
@findex YYBACKUP
Unshift a token. This macro is allowed only for rules that reduce
a single value, and only when there is no look-ahead token.
It installs a look-ahead token with token type @var{token} and
semantic value @var{value}; then it discards the value that was
going to be reduced by this rule.

If the macro is used when it is not valid, such as when there is
a look-ahead token already, then it reports a syntax error with
a message @samp{cannot back up} and performs ordinary error
recovery.

In either case, the rest of the action is not executed.

@item YYEMPTY
@vindex YYEMPTY
Value stored in @code{yychar} when there is no look-ahead token.

@item YYERROR;
@findex YYERROR
Cause an immediate syntax error. This statement initiates error
recovery just as if the parser itself had detected an error; however, it
does not call @code{yyerror}, and does not print any message. If you
want to print an error message, call @code{yyerror} explicitly before
the @samp{YYERROR;} statement. @xref{Error Recovery}.

@item YYRECOVERING
This macro stands for an expression that has the value 1 when the parser
is recovering from a syntax error, and 0 the rest of the time.
@xref{Error Recovery}.

@item yychar
Variable containing the current look-ahead token. (In a pure parser,
this is actually a local variable within @code{yyparse}.) When there is
no look-ahead token, the value @code{YYEMPTY} is stored in the variable.
@xref{Look-Ahead, ,Look-Ahead Tokens}.

@item yyclearin;
Discard the current look-ahead token. This is useful primarily in
error rules. @xref{Error Recovery}.

@item yyerrok;
Resume generating error messages immediately for subsequent syntax
errors. This is useful primarily in error rules.
@xref{Error Recovery}.

@item @@@var{n}
@findex @@@var{n}
Acts like a structure variable containing information on the line
numbers and column numbers of the @var{n}th component of the current
rule. The structure has four members, like this:

@example
struct @{
int first_line, last_line;
int first_column, last_column;
@};
@end example

Thus, to get the starting line number of the third component, use
@samp{@@3.first_line}.

In order for the members of this structure to contain valid information,
you must make @code{yylex} supply this information about each token.
If you need only certain members, then @code{yylex} need only fill in
those members.

The use of this feature makes the parser noticeably slower.
@end table

@node Algorithm, Error Recovery, Interface, Top
@chapter The Bison Parser Algorithm
@cindex Bison parser algorithm
@cindex algorithm of parser
@cindex shifting
@cindex reduction
@cindex parser stack
@cindex stack, parser

As Bison reads tokens, it pushes them onto a stack along with their
semantic values. The stack is called the @dfn{parser stack}. Pushing a
token is traditionally called @dfn{shifting}.

For example, suppose the infix calculator has read @samp{1 + 5 *}, with a
@samp{3} to come. The stack will have four elements, one for each token
that was shifted.

But the stack does not always have an element for each token read. When
the last @var{n} tokens and groupings shifted match the components of a
grammar rule, they can be combined according to that rule. This is called
@dfn{reduction}. Those tokens and groupings are replaced on the stack by a
single grouping whose symbol is the result (left hand side) of that rule.
Running the rule's action is part of the process of reduction, because this
is what computes the semantic value of the resulting grouping.

For example, if the infix calculator's parser stack contains this:

@example
1 + 5 * 3
@end example

@noindent
and the next input token is a newline character, then the last three
elements can be reduced to 15 via the rule:

@example
expr: expr '*' expr;
@end example

@noindent
Then the stack contains just these three elements:

@example
1 + 15
@end example

@noindent
At this point, another reduction can be made, resulting in the single value
16. Then the newline token can be shifted.

The parser tries, by shifts and reductions, to reduce the entire input down
to a single grouping whose symbol is the grammar's start-symbol
(@pxref{Language and Grammar, ,Languages and Context-Free Grammars}).

This kind of parser is known in the literature as a bottom-up parser.

@menu
* Look-Ahead:: Parser looks one token ahead when deciding what to do.
* Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
* Precedence:: Operator precedence works by resolving conflicts.
* Contextual Precedence:: When an operator's precedence depends on context.
* Parser States:: The parser is a finite-state-machine with stack.
* Reduce/Reduce:: When two rules are applicable in the same situation.
* Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
* Stack Overflow:: What happens when stack gets full. How to avoid it.
@end menu

@node Look-Ahead, Shift/Reduce, , Algorithm
@section Look-Ahead Tokens
@cindex look-ahead token

The Bison parser does @emph{not} always reduce immediately as soon as the
last @var{n} tokens and groupings match a rule. This is because such a
simple strategy is inadequate to handle most languages. Instead, when a
reduction is possible, the parser sometimes ``looks ahead'' at the next
token in order to decide what to do.

When a token is read, it is not immediately shifted; first it becomes the
@dfn{look-ahead token}, which is not on the stack. Now the parser can
perform one or more reductions of tokens and groupings on the stack, while
the look-ahead token remains off to the side. When no more reductions
should take place, the look-ahead token is shifted onto the stack. This
does not mean that all possible reductions have been done; depending on the
token type of the look-ahead token, some rules may choose to delay their
application.

Here is a simple case where look-ahead is needed. These three rules define
expressions which contain binary addition operators and postfix unary
factorial operators (@samp{!}), and allow parentheses for grouping.

@example
@group
expr: term '+' expr
| term
;
@end group

@group
term: '(' expr ')'
| term '!'
| NUMBER
;
@end group
@end example

Suppose that the tokens @w{@samp{1 + 2}} have been read and shifted; what
should be done? If the following token is @samp{)}, then the first three
tokens must be reduced to form an @code{expr}. This is the only valid
course, because shifting the @samp{)} would produce a sequence of symbols
@w{@code{term ')'}}, and no rule allows this.

If the following token is @samp{!}, then it must be shifted immediately so
that @w{@samp{2 !}} can be reduced to make a @code{term}. If instead the
parser were to reduce before shifting, @w{@samp{1 + 2}} would become an
@code{expr}. It would then be impossible to shift the @samp{!} because
doing so would produce on the stack the sequence of symbols @code{expr
'!'}. No rule allows that sequence.

@vindex yychar
The current look-ahead token is stored in the variable @code{yychar}.
@xref{Action Features, ,Special Features for Use in Actions}.

@node Shift/Reduce, Precedence, Look-Ahead, Algorithm
@section Shift/Reduce Conflicts
@cindex conflicts
@cindex shift/reduce conflicts
@cindex dangling @code{else}
@cindex @code{else}, dangling

Suppose we are parsing a language which has if-then and if-then-else
statements, with a pair of rules like this:

@example
@group
if_stmt:
IF expr THEN stmt
| IF expr THEN stmt ELSE stmt
;
@end group
@end example

@noindent
Here we assume that @code{IF}, @code{THEN} and @code{ELSE} are
terminal symbols for specific keyword tokens.

When the @code{ELSE} token is read and becomes the look-ahead token, the
contents of the stack (assuming the input is valid) are just right for
reduction by the first rule. But it is also legitimate to shift the
@code{ELSE}, because that would lead to eventual reduction by the second
rule.

This situation, where either a shift or a reduction would be valid, is
called a @dfn{shift/reduce conflict}. Bison is designed to resolve
these conflicts by choosing to shift, unless otherwise directed by
operator precedence declarations. To see the reason for this, let's
contrast it with the other alternative.

Since the parser prefers to shift the @code{ELSE}, the result is to attach
the else-clause to the innermost if-statement, making these two inputs
equivalent:

@example
if x then if y then win (); else lose;

if x then do; if y then win (); else lose; end;
@end example

But if the parser chose to reduce when possible rather than shift, the
result would be to attach the else-clause to the outermost if-statement,
making these two inputs equivalent:

@example
if x then if y then win (); else lose;

if x then do; if y then win (); end; else lose;
@end example

The conflict exists because the grammar as written is ambiguous: either
parsing of the simple nested if-statement is legitimate. The established
convention is that these ambiguities are resolved by attaching the
else-clause to the innermost if-statement; this is what Bison accomplishes
by choosing to shift rather than reduce. (It would ideally be cleaner to
write an unambiguous grammar, but that is very hard to do in this case.)
This particular ambiguity was first encountered in the specifications of
Algol 60 and is called the ``dangling @code{else}'' ambiguity.

To avoid warnings from Bison about predictable, legitimate shift/reduce
conflicts, use the @code{%expect @var{n}} declaration. There will be no
warning as long as the number of shift/reduce conflicts is exactly @var{n}.
@xref{Expect Decl, ,Suppressing Conflict Warnings}.

The definition of @code{if_stmt} above is solely to blame for the
conflict, but the conflict does not actually appear without additional
rules. Here is a complete Bison input file that actually manifests the
conflict:

@example
@group
%token IF THEN ELSE variable
%%
@end group
@group
stmt: expr
| if_stmt
;
@end group

@group
if_stmt:
IF expr THEN stmt
| IF expr THEN stmt ELSE stmt
;
@end group

expr: variable
;
@end example

@node Precedence, Contextual Precedence, Shift/Reduce, Algorithm
@section Operator Precedence
@cindex operator precedence
@cindex precedence of operators

Another situation where shift/reduce conflicts appear is in arithmetic
expressions. Here shifting is not always the preferred resolution; the
Bison declarations for operator precedence allow you to specify when to
shift and when to reduce.

@menu
* Why Precedence:: An example showing why precedence is needed.
* Using Precedence:: How to specify precedence in Bison grammars.
* Precedence Examples:: How these features are used in the previous example.
* How Precedence:: How they work.
@end menu

@node Why Precedence, Using Precedence, , Precedence
@subsection When Precedence is Needed

Consider the following ambiguous grammar fragment (ambiguous because the
input @w{@samp{1 - 2 * 3}} can be parsed in two different ways):

@example
@group
expr: expr '-' expr
| expr '*' expr
| expr '<' expr
| '(' expr ')'
@dots{}
;
@end group
@end example

@noindent
Suppose the parser has seen the tokens @samp{1}, @samp{-} and @samp{2};
should it reduce them via the rule for the addition operator? It depends
on the next token. Of course, if the next token is @samp{)}, we must
reduce; shifting is invalid because no single rule can reduce the token
sequence @w{@samp{- 2 )}} or anything starting with that. But if the next
token is @samp{*} or @samp{<}, we have a choice: either shifting or
reduction would allow the parse to complete, but with different
results.

To decide which one Bison should do, we must consider the
results. If the next operator token @var{op} is shifted, then it
must be reduced first in order to permit another opportunity to
reduce the sum. The result is (in effect) @w{@samp{1 - (2
@var{op} 3)}}. On the other hand, if the subtraction is reduced
before shifting @var{op}, the result is @w{@samp{(1 - 2) @var{op}
3}}. Clearly, then, the choice of shift or reduce should depend
on the relative precedence of the operators @samp{-} and
@var{op}: @samp{*} should be shifted first, but not @samp{<}.

@cindex associativity
What about input such as @w{@samp{1 - 2 - 5}}; should this be
@w{@samp{(1 - 2) - 5}} or should it be @w{@samp{1 - (2 - 5)}}? For
most operators we prefer the former, which is called @dfn{left
association}. The latter alternative, @dfn{right association}, is
desirable for assignment operators. The choice of left or right
association is a matter of whether the parser chooses to shift or
reduce when the stack contains @w{@samp{1 - 2}} and the look-ahead
token is @samp{-}: shifting makes right-associativity.

@node Using Precedence, Precedence Examples, Why Precedence, Precedence
@subsection Specifying Operator Precedence
@findex %left
@findex %right
@findex %nonassoc

Bison allows you to specify these choices with the operator precedence
declarations @code{%left} and @code{%right}. Each such declaration
contains a list of tokens, which are operators whose precedence and
associativity is being declared. The @code{%left} declaration makes all
those operators left-associative and the @code{%right} declaration makes
them right-associative. A third alternative is @code{%nonassoc}, which
declares that it is a syntax error to find the same operator twice ``in a
row''.

The relative precedence of different operators is controlled by the
order in which they are declared. The first @code{%left} or
@code{%right} declaration in the file declares the operators whose
precedence is lowest, the next such declaration declares the operators
whose precedence is a little higher, and so on.

@node Precedence Examples, How Precedence, Using Precedence, Precedence
@subsection Precedence Examples

In our example, we would want the following declarations:

@example
%left '<'
%left '-'
%left '*'
@end example

In a more complete example, which supports other operators as well, we
would declare them in groups of equal precedence. For example, @code{'+'} is
declared with @code{'-'}:

@example
%left '<' '>' '=' NE LE GE
%left '+' '-'
%left '*' '/'
@end example

@noindent
(Here @code{NE} and so on stand for the operators for ``not equal''
and so on. We assume that these tokens are more than one character long
and therefore are represented by names, not character literals.)

@node How Precedence, , Precedence Examples, Precedence
@subsection How Precedence Works

The first effect of the precedence declarations is to assign precedence
levels to the terminal symbols declared. The second effect is to assign
precedence levels to certain rules: each rule gets its precedence from the
last terminal symbol mentioned in the components. (You can also specify
explicitly the precedence of a rule. @xref{Contextual Precedence, ,Context-Dependent Precedence}.)

Finally, the resolution of conflicts works by comparing the
precedence of the rule being considered with that of the
look-ahead token. If the token's precedence is higher, the
choice is to shift. If the rule's precedence is higher, the
choice is to reduce. If they have equal precedence, the choice
is made based on the associativity of that precedence level. The
verbose output file made by @samp{-v} (@pxref{Invocation, ,Invoking Bison}) says
how each conflict was resolved.

Not all rules and not all tokens have precedence. If either the rule or
the look-ahead token has no precedence, then the default is to shift.

@node Contextual Precedence, Parser States, Precedence, Algorithm
@section Context-Dependent Precedence
@cindex context-dependent precedence
@cindex unary operator precedence
@cindex precedence, context-dependent
@cindex precedence, unary operator
@findex %prec

Often the precedence of an operator depends on the context. This sounds
outlandish at first, but it is really very common. For example, a minus
sign typically has a very high precedence as a unary operator, and a
somewhat lower precedence (lower than multiplication) as a binary operator.

The Bison precedence declarations, @code{%left}, @code{%right} and
@code{%nonassoc}, can only be used once for a given token; so a token has
only one precedence declared in this way. For context-dependent
precedence, you need to use an additional mechanism: the @code{%prec}
modifier for rules.@refill

The @code{%prec} modifier declares the precedence of a particular rule by
specifying a terminal symbol whose precedence should be used for that rule.
It's not necessary for that symbol to appear otherwise in the rule. The
modifier's syntax is:

@example
%prec @var{terminal-symbol}
@end example

@noindent
and it is written after the components of the rule. Its effect is to
assign the rule the precedence of @var{terminal-symbol}, overriding
the precedence that would be deduced for it in the ordinary way. The
altered rule precedence then affects how conflicts involving that rule
are resolved (@pxref{Precedence, ,Operator Precedence}).

Here is how @code{%prec} solves the problem of unary minus. First, declare
a precedence for a fictitious terminal symbol named @code{UMINUS}. There
are no tokens of this type, but the symbol serves to stand for its
precedence:

@example
@dots{}
%left '+' '-'
%left '*'
%left UMINUS
@end example

Now the precedence of @code{UMINUS} can be used in specific rules:

@example
@group
exp: @dots{}
| exp '-' exp
@dots{}
| '-' exp %prec UMINUS
@end group
@end example

@node Parser States, Reduce/Reduce, Contextual Precedence, Algorithm
@section Parser States
@cindex finite-state machine
@cindex parser state
@cindex state (of parser)

The function @code{yyparse} is implemented using a finite-state machine.
The values pushed on the parser stack are not simply token type codes; they
represent the entire sequence of terminal and nonterminal symbols at or
near the top of the stack. The current state collects all the information
about previous input which is relevant to deciding what to do next.

Each time a look-ahead token is read, the current parser state together
with the type of look-ahead token are looked up in a table. This table
entry can say, ``Shift the look-ahead token.'' In this case, it also
specifies the new parser state, which is pushed onto the top of the
parser stack. Or it can say, ``Reduce using rule number @var{n}.''
This means that a certain number of tokens or groupings are taken off
the top of the stack, and replaced by one grouping. In other words,
that number of states are popped from the stack, and one new state is
pushed.

There is one other alternative: the table can say that the look-ahead token
is erroneous in the current state. This causes error processing to begin
(@pxref{Error Recovery}).

@node Reduce/Reduce, Mystery Conflicts, Parser States, Algorithm
@section Reduce/Reduce Conflicts
@cindex reduce/reduce conflict
@cindex conflicts, reduce/reduce

A reduce/reduce conflict occurs if there are two or more rules that apply
to the same sequence of input. This usually indicates a serious error
in the grammar.

For example, here is an erroneous attempt to define a sequence
of zero or more @code{word} groupings.

@example
sequence: /* empty */
@{ printf ("empty sequence\n"); @}
| maybeword
| sequence word
@{ printf ("added word %s\n", $2); @}
;

maybeword: /* empty */
@{ printf ("empty maybeword\n"); @}
| word
@{ printf ("single word %s\n", $1); @}
;
@end example

@noindent
The error is an ambiguity: there is more than one way to parse a single
@code{word} into a @code{sequence}. It could be reduced to a
@code{maybeword} and then into a @code{sequence} via the second rule.
Alternatively, nothing-at-all could be reduced into a @code{sequence}
via the first rule, and this could be combined with the @code{word}
using the third rule for @code{sequence}.

There is also more than one way to reduce nothing-at-all into a
@code{sequence}. This can be done directly via the first rule,
or indirectly via @code{maybeword} and then the second rule.

You might think that this is a distinction without a difference, because it
does not change whether any particular input is valid or not. But it does
affect which actions are run. One parsing order runs the second rule's
action; the other runs the first rule's action and the third rule's action.
In this example, the output of the program changes.

Bison resolves a reduce/reduce conflict by choosing to use the rule that
appears first in the grammar, but it is very risky to rely on this. Every
reduce/reduce conflict must be studied and usually eliminated. Here is the
proper way to define @code{sequence}:

@example
sequence: /* empty */
@{ printf ("empty sequence\n"); @}
| sequence word
@{ printf ("added word %s\n", $2); @}
;
@end example

Here is another common error that yields a reduce/reduce conflict:

@example
sequence: /* empty */
| sequence words
| sequence redirects
;

words: /* empty */
| words word
;

redirects:/* empty */
| redirects redirect
;
@end example

@noindent
The intention here is to define a sequence which can contain either
@code{word} or @code{redirect} groupings. The individual definitions of
@code{sequence}, @code{words} and @code{redirects} are error-free, but the
three together make a subtle ambiguity: even an empty input can be parsed
in infinitely many ways!

Consider: nothing-at-all could be a @code{words}. Or it could be two
@code{words} in a row, or three, or any number. It could equally well be a
@code{redirects}, or two, or any number. Or it could be a @code{words}
followed by three @code{redirects} and another @code{words}. And so on.

Here are two ways to correct these rules. First, to make it a single level
of sequence:

@example
sequence: /* empty */
| sequence word
| sequence redirect
;
@end example

Second, to prevent either a @code{words} or a @code{redirects}
from being empty:

@example
sequence: /* empty */
| sequence words
| sequence redirects
;

words: word
| words word
;

redirects:redirect
| redirects redirect
;
@end example

@node Mystery Conflicts, Stack Overflow, Reduce/Reduce, Algorithm
@section Mysterious Reduce/Reduce Conflicts

Sometimes reduce/reduce conflicts can occur that don't look warranted.
Here is an example:

@example
@group
%token ID

%%
def: param_spec return_spec ','
;
param_spec:
type
| name_list ':' type
;
@end group
@group
return_spec:
type
| name ':' type
;
@end group
@group
type: ID
;
@end group
@group
name: ID
;
name_list:
name
| name ',' name_list
;
@end group
@end example

It would seem that this grammar can be parsed with only a single token
of look-ahead: when a @code{param_spec} is being read, an @code{ID} is
a @code{name} if a comma or colon follows, or a @code{type} if another
@code{ID} follows. In other words, this grammar is LR(1).

@cindex LR(1)
@cindex LALR(1)
However, Bison, like most parser generators, cannot actually handle all
LR(1) grammars. In this grammar, two contexts, that after an @code{ID}
at the beginning of a @code{param_spec} and likewise at the beginning of
a @code{return_spec}, are similar enough that Bison assumes they are the
same. They appear similar because the same set of rules would be
active---the rule for reducing to a @code{name} and that for reducing to
a @code{type}. Bison is unable to determine at that stage of processing
that the rules would require different look-ahead tokens in the two
contexts, so it makes a single parser state for them both. Combining
the two contexts causes a conflict later. In parser terminology, this
occurrence means that the grammar is not LALR(1).

In general, it is better to fix deficiencies than to document them. But
this particular deficiency is intrinsically hard to fix; parser
generators that can handle LR(1) grammars are hard to write and tend to
produce parsers that are very large. In practice, Bison is more useful
as it is now.

When the problem arises, you can often fix it by identifying the two
parser states that are being confused, and adding something to make them
look distinct. In the above example, adding one rule to
@code{return_spec} as follows makes the problem go away:

@example
@group
%token BOGUS
@dots{}
%%
@dots{}
return_spec:
type
| name ':' type
/* This rule is never used. */
| ID BOGUS
;
@end group
@end example

This corrects the problem because it introduces the possibility of an
additional active rule in the context after the @code{ID} at the beginning of
@code{return_spec}. This rule is not active in the corresponding context
in a @code{param_spec}, so the two contexts receive distinct parser states.
As long as the token @code{BOGUS} is never generated by @code{yylex},
the added rule cannot alter the way actual input is parsed.

In this particular example, there is another way to solve the problem:
rewrite the rule for @code{return_spec} to use @code{ID} directly
instead of via @code{name}. This also causes the two confusing
contexts to have different sets of active rules, because the one for
@code{return_spec} activates the altered rule for @code{return_spec}
rather than the one for @code{name}.

@example
param_spec:
type
| name_list ':' type
;
return_spec:
type
| ID ':' type
;
@end example

@node Stack Overflow, , Mystery Conflicts, Algorithm
@section Stack Overflow, and How to Avoid It
@cindex stack overflow
@cindex parser stack overflow
@cindex overflow of parser stack

The Bison parser stack can overflow if too many tokens are shifted and
not reduced. When this happens, the parser function @code{yyparse}
returns a nonzero value, pausing only to call @code{yyerror} to report
the overflow.

@vindex YYMAXDEPTH
By defining the macro @code{YYMAXDEPTH}, you can control how deep the
parser stack can become before a stack overflow occurs. Define the
macro with a value that is an integer. This value is the maximum number
of tokens that can be shifted (and not reduced) before overflow.
It must be a constant expression whose value is known at compile time.

The stack space allowed is not necessarily allocated. If you specify a
large value for @code{YYMAXDEPTH}, the parser actually allocates a small
stack at first, and then makes it bigger by stages as needed. This
increasing allocation happens automatically and silently. Therefore,
you do not need to make @code{YYMAXDEPTH} painfully small merely to save
space for ordinary inputs that do not need much stack.

@cindex default stack limit
The default value of @code{YYMAXDEPTH}, if you do not define it, is
10000.

@vindex YYINITDEPTH
You can control how much stack is allocated initially by defining the
macro @code{YYINITDEPTH}. This value too must be a compile-time
constant integer. The default is 200.

@node Error Recovery, Context Dependency, Algorithm, Top
@chapter Error Recovery
@cindex error recovery
@cindex recovery from errors

It is not usually acceptable to have a program terminate on a parse
error. For example, a compiler should recover sufficiently to parse the
rest of the input file and check it for errors; a calculator should accept
another expression.

In a simple interactive command parser where each input is one line, it may
be sufficient to allow @code{yyparse} to return 1 on error and have the
caller ignore the rest of the input line when that happens (and then call
@code{yyparse} again). But this is inadequate for a compiler, because it
forgets all the syntactic context leading up to the error. A syntax error
deep within a function in the compiler input should not cause the compiler
to treat the following line like the beginning of a source file.

@findex error
You can define how to recover from a syntax error by writing rules to
recognize the special token @code{error}. This is a terminal symbol that
is always defined (you need not declare it) and reserved for error
handling. The Bison parser generates an @code{error} token whenever a
syntax error happens; if you have provided a rule to recognize this token
in the current context, the parse can continue.

For example:

@example
stmnts: /* empty string */
| stmnts '\n'
| stmnts exp '\n'
| stmnts error '\n'
@end example

The fourth rule in this example says that an error followed by a newline
makes a valid addition to any @code{stmnts}.

What happens if a syntax error occurs in the middle of an @code{exp}? The
error recovery rule, interpreted strictly, applies to the precise sequence
of a @code{stmnts}, an @code{error} and a newline. If an error occurs in
the middle of an @code{exp}, there will probably be some additional tokens
and subexpressions on the stack after the last @code{stmnts}, and there
will be tokens to read before the next newline. So the rule is not
applicable in the ordinary way.

But Bison can force the situation to fit the rule, by discarding part of
the semantic context and part of the input. First it discards states and
objects from the stack until it gets back to a state in which the
@code{error} token is acceptable. (This means that the subexpressions
already parsed are discarded, back to the last complete @code{stmnts}.) At
this point the @code{error} token can be shifted. Then, if the old
look-ahead token is not acceptable to be shifted next, the parser reads
tokens and discards them until it finds a token which is acceptable. In
this example, Bison reads and discards input until the next newline
so that the fourth rule can apply.

The choice of error rules in the grammar is a choice of strategies for
error recovery. A simple and useful strategy is simply to skip the rest of
the current input line or current statement if an error is detected:

@example
stmnt: error ';' /* on error, skip until ';' is read */
@end example

It is also useful to recover to the matching close-delimiter of an
opening-delimiter that has already been parsed. Otherwise the
close-delimiter will probably appear to be unmatched, and generate another,
spurious error message:

@example
primary: '(' expr ')'
| '(' error ')'
@dots{}
;
@end example

Error recovery strategies are necessarily guesses. When they guess wrong,
one syntax error often leads to another. In the above example, the error
recovery rule guesses that an error is due to bad input within one
@code{stmnt}. Suppose that instead a spurious semicolon is inserted in the
middle of a valid @code{stmnt}. After the error recovery rule recovers
from the first error, another syntax error will be found straightaway,
since the text following the spurious semicolon is also an invalid
@code{stmnt}.

To prevent an outpouring of error messages, the parser will output no error
message for another syntax error that happens shortly after the first; only
after three consecutive input tokens have been successfully shifted will
error messages resume.

Note that rules which accept the @code{error} token may have actions, just
as any other rules can.

@findex yyerrok
You can make error messages resume immediately by using the macro
@code{yyerrok} in an action. If you do this in the error rule's action, no
error messages will be suppressed. This macro requires no arguments;
@samp{yyerrok;} is a valid C statement.

@findex yyclearin
The previous look-ahead token is reanalyzed immediately after an error. If
this is unacceptable, then the macro @code{yyclearin} may be used to clear
this token. Write the statement @samp{yyclearin;} in the error rule's
action.

For example, suppose that on a parse error, an error handling routine is
called that advances the input stream to some point where parsing should
once again commence. The next symbol returned by the lexical scanner is
probably correct. The previous look-ahead token ought to be discarded
with @samp{yyclearin;}.

@vindex YYRECOVERING
The macro @code{YYRECOVERING} stands for an expression that has the
value 1 when the parser is recovering from a syntax error, and 0 the
rest of the time. A value of 1 indicates that error messages are
currently suppressed for new syntax errors.

@node Context Dependency, Debugging, Error Recovery, Top
@chapter Handling Context Dependencies

The Bison paradigm is to parse tokens first, then group them into larger
syntactic units. In many languages, the meaning of a token is affected by
its context. Although this violates the Bison paradigm, certain techniques
(known as @dfn{kludges}) may enable you to write Bison parsers for such
languages.

@menu
* Semantic Tokens:: Token parsing can depend on the semantic context.
* Lexical Tie-ins:: Token parsing can depend on the syntactic context.
* Tie-in Recovery:: Lexical tie-ins have implications for how
error recovery rules must be written.
@end menu

(Actually, ``kludge'' means any technique that gets its job done but is
neither clean nor robust.)

@node Semantic Tokens, Lexical Tie-ins, , Context Dependency
@section Semantic Info in Token Types

The C language has a context dependency: the way an identifier is used
depends on what its current meaning is. For example, consider this:

@example
foo (x);
@end example

This looks like a function call statement, but if @code{foo} is a typedef
name, then this is actually a declaration of @code{x}. How can a Bison
parser for C decide how to parse this input?

The method used in GNU C is to have two different token types,
@code{IDENTIFIER} and @code{TYPENAME}. When @code{yylex} finds an
identifier, it looks up the current declaration of the identifier in order
to decide which token type to return: @code{TYPENAME} if the identifier is
declared as a typedef, @code{IDENTIFIER} otherwise.

The grammar rules can then express the context dependency by the choice of
token type to recognize. @code{IDENTIFIER} is accepted as an expression,
but @code{TYPENAME} is not. @code{TYPENAME} can start a declaration, but
@code{IDENTIFIER} cannot. In contexts where the meaning of the identifier
is @emph{not} significant, such as in declarations that can shadow a
typedef name, either @code{TYPENAME} or @code{IDENTIFIER} is
accepted---there is one rule for each of the two token types.

This technique is simple to use if the decision of which kinds of
identifiers to allow is made at a place close to where the identifier is
parsed. But in C this is not always so: C allows a declaration to
redeclare a typedef name provided an explicit type has been specified
earlier:

@example
typedef int foo, bar, lose;
static foo (bar); /* @r{redeclare @code{bar} as static variable} */
static int foo (lose); /* @r{redeclare @code{foo} as function} */
@end example

Unfortunately, the name being declared is separated from the declaration
construct itself by a complicated syntactic structure---the ``declarator''.

As a result, the part of Bison parser for C needs to be duplicated, with
all the nonterminal names changed: once for parsing a declaration in which
a typedef name can be redefined, and once for parsing a declaration in
which that can't be done. Here is a part of the duplication, with actions
omitted for brevity:

@example
initdcl:
declarator maybeasm '='
init
| declarator maybeasm
;

notype_initdcl:
notype_declarator maybeasm '='
init
| notype_declarator maybeasm
;
@end example

@noindent
Here @code{initdcl} can redeclare a typedef name, but @code{notype_initdcl}
cannot. The distinction between @code{declarator} and
@code{notype_declarator} is the same sort of thing.

There is some similarity between this technique and a lexical tie-in
(described next), in that information which alters the lexical analysis is
changed during parsing by other parts of the program. The difference is
here the information is global, and is used for other purposes in the
program. A true lexical tie-in has a special-purpose flag controlled by
the syntactic context.

@node Lexical Tie-ins, Tie-in Recovery, Semantic Tokens, Context Dependency
@section Lexical Tie-ins
@cindex lexical tie-in

One way to handle context-dependency is the @dfn{lexical tie-in}: a flag
which is set by Bison actions, whose purpose is to alter the way tokens are
parsed.

For example, suppose we have a language vaguely like C, but with a special
construct @samp{hex (@var{hex-expr})}. After the keyword @code{hex} comes
an expression in parentheses in which all integers are hexadecimal. In
particular, the token @samp{a1b} must be treated as an integer rather than
as an identifier if it appears in that context. Here is how you can do it:

@example
@group
%@{
int hexflag;
%@}
%%
@dots{}
@end group
@group
expr: IDENTIFIER
| constant
| HEX '('
@{ hexflag = 1; @}
expr ')'
@{ hexflag = 0;
$$ = $4; @}
| expr '+' expr
@{ $$ = make_sum ($1, $3); @}
@dots{}
;
@end group

@group
constant:
INTEGER
| STRING
;
@end group
@end example

@noindent
Here we assume that @code{yylex} looks at the value of @code{hexflag}; when
it is nonzero, all integers are parsed in hexadecimal, and tokens starting
with letters are parsed as integers if possible.

The declaration of @code{hexflag} shown in the C declarations section of
the parser file is needed to make it accessible to the actions
(@pxref{C Declarations, ,The C Declarations Section}). You must also write the code in @code{yylex}
to obey the flag.

@node Tie-in Recovery, , Lexical Tie-ins, Context Dependency
@section Lexical Tie-ins and Error Recovery

Lexical tie-ins make strict demands on any error recovery rules you have.
@xref{Error Recovery}.

The reason for this is that the purpose of an error recovery rule is to
abort the parsing of one construct and resume in some larger construct.
For example, in C-like languages, a typical error recovery rule is to skip
tokens until the next semicolon, and then start a new statement, like this:

@example
stmt: expr ';'
| IF '(' expr ')' stmt @{ @dots{} @}
@dots{}
error ';'
@{ hexflag = 0; @}
;
@end example

If there is a syntax error in the middle of a @samp{hex (@var{expr})}
construct, this error rule will apply, and then the action for the
completed @samp{hex (@var{expr})} will never run. So @code{hexflag} would
remain set for the entire rest of the input, or until the next @code{hex}
keyword, causing identifiers to be misinterpreted as integers.

To avoid this problem the error recovery rule itself clears @code{hexflag}.

There may also be an error recovery rule that works within expressions.
For example, there could be a rule which applies within parentheses
and skips to the close-parenthesis:

@example
@group
expr: @dots{}
| '(' expr ')'
@{ $$ = $2; @}
| '(' error ')'
@dots{}
@end group
@end example

If this rule acts within the @code{hex} construct, it is not going to abort
that construct (since it applies to an inner level of parentheses within
the construct). Therefore, it should not clear the flag: the rest of
the @code{hex} construct should be parsed with the flag still in effect.

What if there is an error recovery rule which might abort out of the
@code{hex} construct or might not, depending on circumstances? There is no
way you can write the action to determine whether a @code{hex} construct is
being aborted or not. So if you are using a lexical tie-in, you had better
make sure your error recovery rules are not of this kind. Each rule must
be such that you can be sure that it always will, or always won't, have to
clear the flag.

@node Debugging, Invocation, Context Dependency, Top
@chapter Debugging Your Parser
@findex YYDEBUG
@findex yydebug
@cindex debugging
@cindex tracing the parser

If a Bison grammar compiles properly but doesn't do what you want when it
runs, the @code{yydebug} parser-trace feature can help you figure out why.

To enable compilation of trace facilities, you must define the macro
@code{YYDEBUG} when you compile the parser. You could use
@samp{-DYYDEBUG=1} as a compiler option or you could put @samp{#define
YYDEBUG 1} in the C declarations section of the grammar file
(@pxref{C Declarations, ,The C Declarations Section}). Alternatively, use the @samp{-t} option when
you run Bison (@pxref{Invocation, ,Invoking Bison}). We always define @code{YYDEBUG} so that
debugging is always possible.

The trace facility uses @code{stderr}, so you must add @w{@code{#include
}} to the C declarations section unless it is already there.

Once you have compiled the program with trace facilities, the way to
request a trace is to store a nonzero value in the variable @code{yydebug}.
You can do this by making the C code do it (in @code{main}, perhaps), or
you can alter the value with a C debugger.

Each step taken by the parser when @code{yydebug} is nonzero produces a
line or two of trace information, written on @code{stderr}. The trace
messages tell you these things:

@itemize @bullet
@item
Each time the parser calls @code{yylex}, what kind of token was read.

@item
Each time a token is shifted, the depth and complete contents of the
state stack (@pxref{Parser States}).

@item
Each time a rule is reduced, which rule it is, and the complete contents
of the state stack afterward.
@end itemize

To make sense of this information, it helps to refer to the listing file
produced by the Bison @samp{-v} option (@pxref{Invocation, ,Invoking Bison}). This file
shows the meaning of each state in terms of positions in various rules, and
also what each state will do with each possible input token. As you read
the successive trace messages, you can see that the parser is functioning
according to its specification in the listing file. Eventually you will
arrive at the place where something undesirable happens, and you will see
which parts of the grammar are to blame.

The parser file is a C program and you can use C debuggers on it, but it's
not easy to interpret what it is doing. The parser function is a
finite-state machine interpreter, and aside from the actions it executes
the same code over and over. Only the values of variables show where in
the grammar it is working.

@findex YYPRINT
The debugging information normally gives the token type of each token
read, but not its semantic value. You can optionally define a macro
named @code{YYPRINT} to provide a way to print the value. If you define
@code{YYPRINT}, it should take three arguments. The parser will pass a
standard I/O stream, the numeric code for the token type, and the token
value (from @code{yylval}).

Here is an example of @code{YYPRINT} suitable for the multi-function
calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):

@smallexample
#define YYPRINT(file, type, value) yyprint (file, type, value)

static void
yyprint (file, type, value)
FILE *file;
int type;
YYSTYPE value;
@{
if (type == VAR)
fprintf (file, " %s", value.tptr->name);
else if (type == NUM)
fprintf (file, " %d", value.val);
@}
@end smallexample

@node Invocation, Table of Symbols, Debugging, Top
@chapter Invoking Bison
@cindex invoking Bison
@cindex Bison invocation
@cindex options for invoking Bison

The usual way to invoke Bison is as follows:

@example
bison @var{infile}
@end example

Here @var{infile} is the grammar file name, which usually ends in
@samp{.y}. The parser file's name is made by replacing the @samp{.y}
with @samp{.tab.c}. Thus, the @samp{bison foo.y} filename yields
@file{foo.tab.c}, and the @samp{bison hack/foo.y} filename yields
@file{hack/foo.tab.c}.@refill

@menu
* Bison Options:: All the options described in detail,
in alphabetical order by short options.
* Option Cross Key:: Alphabetical list of long options.
* VMS Invocation:: Bison command syntax on VMS.
@end menu

@node Bison Options, Option Cross Key, , Invocation
@section Bison Options

Bison supports both traditional single-letter options and mnemonic long
option names. Long option names are indicated with @samp{--} instead of
@samp{-}. Abbreviations for option names are allowed as long as they
are unique. When a long option takes an argument, like
@samp{--file-prefix}, connect the option name and the argument with
@samp{=}.

Here is a list of options that can be used with Bison, alphabetized by
short option. It is followed by a cross key alphabetized by long
option.

@table @samp
@item -b @var{file-prefix}
@itemx --file-prefix=@var{prefix}
Specify a prefix to use for all Bison output file names. The names are
chosen as if the input file were named @file{@var{prefix}.c}.

@item -d
@itemx --defines
Write an extra output file containing macro definitions for the token
type names defined in the grammar and the semantic value type
@code{YYSTYPE}, as well as a few @code{extern} variable declarations.

If the parser output file is named @file{@var{name}.c} then this file
is named @file{@var{name}.h}.@refill

This output file is essential if you wish to put the definition of
@code{yylex} in a separate source file, because @code{yylex} needs to
be able to refer to token type codes and the variable
@code{yylval}. @xref{Token Values, ,Semantic Values of Tokens}.@refill

@item -l
@itemx --no-lines
Don't put any @code{#line} preprocessor commands in the parser file.
Ordinarily Bison puts them in the parser file so that the C compiler
and debuggers will associate errors with your source file, the
grammar file. This option causes them to associate errors with the
parser file, treating it an independent source file in its own right.

@item -o @var{outfile}
@itemx --output-file=@var{outfile}
Specify the name @var{outfile} for the parser file.

The other output files' names are constructed from @var{outfile}
as described under the @samp{-v} and @samp{-d} switches.

@item -p @var{prefix}
@itemx --name-prefix=@var{prefix}
Rename the external symbols used in the parser so that they start with
@var{prefix} instead of @samp{yy}. The precise list of symbols renamed
is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yylval},
@code{yychar} and @code{yydebug}.

For example, if you use @samp{-p c}, the names become @code{cparse},
@code{clex}, and so on.

@xref{Multiple Parsers, ,Multiple Parsers in the Same Program}.

@item -t
@itemx --debug
Output a definition of the macro @code{YYDEBUG} into the parser file,
so that the debugging facilities are compiled. @xref{Debugging, ,Debugging Your Parser}.

@item -v
@itemx --verbose
Write an extra output file containing verbose descriptions of the
parser states and what is done for each type of look-ahead token in
that state.

This file also describes all the conflicts, both those resolved by
operator precedence and the unresolved ones.

The file's name is made by removing @samp{.tab.c} or @samp{.c} from
the parser output file name, and adding @samp{.output} instead.@refill

Therefore, if the input file is @file{foo.y}, then the parser file is
called @file{foo.tab.c} by default. As a consequence, the verbose
output file is called @file{foo.output}.@refill

@item -V
@itemx --version
Print the version number of Bison and exit.

@item -h
@itemx --help
Print a summary of the command-line options to Bison and exit.

@need 1750
@item -y
@itemx --yacc
@itemx --fixed-output-files
Equivalent to @samp{-o y.tab.c}; the parser output file is called
@file{y.tab.c}, and the other outputs are called @file{y.output} and
@file{y.tab.h}. The purpose of this switch is to imitate Yacc's output
file name conventions. Thus, the following shell script can substitute
for Yacc:@refill

@example
bison -y $*
@end example
@end table

@node Option Cross Key, VMS Invocation, Bison Options, Invocation
@section Option Cross Key

Here is a list of options, alphabetized by long option, to help you find
the corresponding short option.

@tex
\def\leaderfill{\leaders\hbox to 1em{\hss.\hss}\hfill}

{\tt
\line{ --debug \leaderfill -t}
\line{ --defines \leaderfill -d}
\line{ --file-prefix \leaderfill -b}
\line{ --fixed-output-files \leaderfill -y}
\line{ --help \leaderfill -h}
\line{ --name-prefix \leaderfill -p}
\line{ --no-lines \leaderfill -l}
\line{ --output-file \leaderfill -o}
\line{ --verbose \leaderfill -v}
\line{ --version \leaderfill -V}
\line{ --yacc \leaderfill -y}
}
@end tex

@ifinfo
@example
--debug -t
--defines -d
--file-prefix=@var{prefix} -b @var{file-prefix}
--fixed-output-files --yacc -y
--help -h
--name-prefix -p
--no-lines -l
--output-file=@var{outfile} -o @var{outfile}
--verbose -v
--version -V
@end example
@end ifinfo

@node VMS Invocation, , Option Cross Key, Invocation
@section Invoking Bison under VMS
@cindex invoking Bison under VMS
@cindex VMS

The command line syntax for Bison on VMS is a variant of the usual
Bison command syntax---adapted to fit VMS conventions.

To find the VMS equivalent for any Bison option, start with the long
option, and substitute a @samp{/} for the leading @samp{--}, and
substitute a @samp{_} for each @samp{-} in the name of the long option.
For example, the following invocation under VMS:

@example
bison /debug/name_prefix=bar foo.y
@end example

@noindent
is equivalent to the following command under POSIX.

@example
bison --debug --name-prefix=bar foo.y
@end example

The VMS file system does not permit filenames such as
@file{foo.tab.c}. In the above example, the output file
would instead be named @file{foo_tab.c}.

@node Table of Symbols, Glossary, Invocation, Top
@appendix Bison Symbols
@cindex Bison symbols, table of
@cindex symbols in Bison, table of

@table @code
@item error
A token name reserved for error recovery. This token may be used in
grammar rules so as to allow the Bison parser to recognize an error in
the grammar without halting the process. In effect, a sentence
containing an error may be recognized as valid. On a parse error, the
token @code{error} becomes the current look-ahead token. Actions
corresponding to @code{error} are then executed, and the look-ahead
token is reset to the token that originally caused the violation.
@xref{Error Recovery}.

@item YYABORT
Macro to pretend that an unrecoverable syntax error has occurred, by
making @code{yyparse} return 1 immediately. The error reporting
function @code{yyerror} is not called. @xref{Parser Function, ,The Parser Function @code{yyparse}}.

@item YYACCEPT
Macro to pretend that a complete utterance of the language has been
read, by making @code{yyparse} return 0 immediately.
@xref{Parser Function, ,The Parser Function @code{yyparse}}.

@item YYBACKUP
Macro to discard a value from the parser stack and fake a look-ahead
token. @xref{Action Features, ,Special Features for Use in Actions}.

@item YYERROR
Macro to pretend that a syntax error has just been detected: call
@code{yyerror} and then perform normal error recovery if possible
(@pxref{Error Recovery}), or (if recovery is impossible) make
@code{yyparse} return 1. @xref{Error Recovery}.

@item YYERROR_VERBOSE
Macro that you define with @code{#define} in the Bison declarations
section to request verbose, specific error message strings when
@code{yyerror} is called.

@item YYINITDEPTH
Macro for specifying the initial size of the parser stack.
@xref{Stack Overflow}.

@item YYLTYPE
Macro for the data type of @code{yylloc}; a structure with four
members. @xref{Token Positions, ,Textual Positions of Tokens}.

@item YYMAXDEPTH
Macro for specifying the maximum size of the parser stack.
@xref{Stack Overflow}.

@item YYRECOVERING
Macro whose value indicates whether the parser is recovering from a
syntax error. @xref{Action Features, ,Special Features for Use in Actions}.

@item YYSTYPE
Macro for the data type of semantic values; @code{int} by default.
@xref{Value Type, ,Data Types of Semantic Values}.

@item yychar
External integer variable that contains the integer value of the
current look-ahead token. (In a pure parser, it is a local variable
within @code{yyparse}.) Error-recovery rule actions may examine this
variable. @xref{Action Features, ,Special Features for Use in Actions}.

@item yyclearin
Macro used in error-recovery rule actions. It clears the previous
look-ahead token. @xref{Error Recovery}.

@item yydebug
External integer variable set to zero by default. If @code{yydebug}
is given a nonzero value, the parser will output information on input
symbols and parser action. @xref{Debugging, ,Debugging Your Parser}.

@item yyerrok
Macro to cause parser to recover immediately to its normal mode
after a parse error. @xref{Error Recovery}.

@item yyerror
User-supplied function to be called by @code{yyparse} on error. The
function receives one argument, a pointer to a character string
containing an error message. @xref{Error Reporting, ,The Error Reporting Function @code{yyerror}}.

@item yylex
User-supplied lexical analyzer function, called with no arguments
to get the next token. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.

@item yylval
External variable in which @code{yylex} should place the semantic
value associated with a token. (In a pure parser, it is a local
variable within @code{yyparse}, and its address is passed to
@code{yylex}.) @xref{Token Values, ,Semantic Values of Tokens}.

@item yylloc
External variable in which @code{yylex} should place the line and
column numbers associated with a token. (In a pure parser, it is a
local variable within @code{yyparse}, and its address is passed to
@code{yylex}.) You can ignore this variable if you don't use the
@samp{@@} feature in the grammar actions. @xref{Token Positions, ,Textual Positions of Tokens}.

@item yynerrs
Global variable which Bison increments each time there is a parse
error. (In a pure parser, it is a local variable within
@code{yyparse}.) @xref{Error Reporting, ,The Error Reporting Function @code{yyerror}}.

@item yyparse
The parser function produced by Bison; call this function to start
parsing. @xref{Parser Function, ,The Parser Function @code{yyparse}}.

@item %left
Bison declaration to assign left associativity to token(s).
@xref{Precedence Decl, ,Operator Precedence}.

@item %nonassoc
Bison declaration to assign nonassociativity to token(s).
@xref{Precedence Decl, ,Operator Precedence}.

@item %prec
Bison declaration to assign a precedence to a specific rule.
@xref{Contextual Precedence, ,Context-Dependent Precedence}.

@item %pure_parser
Bison declaration to request a pure (reentrant) parser.
@xref{Pure Decl, ,A Pure (Reentrant) Parser}.

@item %right
Bison declaration to assign right associativity to token(s).
@xref{Precedence Decl, ,Operator Precedence}.

@item %start
Bison declaration to specify the start symbol. @xref{Start Decl, ,The Start-Symbol}.

@item %token
Bison declaration to declare token(s) without specifying precedence.
@xref{Token Decl, ,Token Type Names}.

@item %type
Bison declaration to declare nonterminals. @xref{Type Decl, ,Nonterminal Symbols}.

@item %union
Bison declaration to specify several possible data types for semantic
values. @xref{Union Decl, ,The Collection of Value Types}.
@end table

These are the punctuation and delimiters used in Bison input:

@table @samp
@item %%
Delimiter used to separate the grammar rule section from the
Bison declarations section or the additional C code section.
@xref{Grammar Layout, ,The Overall Layout of a Bison Grammar}.

@item %@{ %@}
All code listed between @samp{%@{} and @samp{%@}} is copied directly
to the output file uninterpreted. Such code forms the ``C
declarations'' section of the input file. @xref{Grammar Outline, ,Outline of a Bison Grammar}.

@item /*@dots{}*/
Comment delimiters, as in C.

@item :
Separates a rule's result from its components. @xref{Rules, ,Syntax of Grammar Rules}.

@item ;
Terminates a rule. @xref{Rules, ,Syntax of Grammar Rules}.

@item |
Separates alternate rules for the same result nonterminal.
@xref{Rules, ,Syntax of Grammar Rules}.
@end table

@node Glossary, Index, Table of Symbols, Top
@appendix Glossary
@cindex glossary

@table @asis
@item Backus-Naur Form (BNF)
Formal method of specifying context-free grammars. BNF was first used
in the @cite{ALGOL-60} report, 1963. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.

@item Context-free grammars
Grammars specified as rules that can be applied regardless of context.
Thus, if there is a rule which says that an integer can be used as an
expression, integers are allowed @emph{anywhere} an expression is
permitted. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.

@item Dynamic allocation
Allocation of memory that occurs during execution, rather than at
compile time or on entry to a function.

@item Empty string
Analogous to the empty set in set theory, the empty string is a
character string of length zero.

@item Finite-state stack machine
A ``machine'' that has discrete states in which it is said to exist at
each instant in time. As input to the machine is processed, the
machine moves from state to state as specified by the logic of the
machine. In the case of the parser, the input is the language being
parsed, and the states correspond to various stages in the grammar
rules. @xref{Algorithm, ,The Bison Parser Algorithm }.

@item Grouping
A language construct that is (in general) grammatically divisible;
for example, `expression' or `declaration' in C.
@xref{Language and Grammar, ,Languages and Context-Free Grammars}.

@item Infix operator
An arithmetic operator that is placed between the operands on which it
performs some operation.

@item Input stream
A continuous flow of data between devices or programs.

@item Language construct
One of the typical usage schemas of the language. For example, one of
the constructs of the C language is the @code{if} statement.
@xref{Language and Grammar, ,Languages and Context-Free Grammars}.

@item Left associativity
Operators having left associativity are analyzed from left to right:
@samp{a+b+c} first computes @samp{a+b} and then combines with
@samp{c}. @xref{Precedence, ,Operator Precedence}.

@item Left recursion
A rule whose result symbol is also its first component symbol;
for example, @samp{expseq1 : expseq1 ',' exp;}. @xref{Recursion, ,Recursive Rules}.

@item Left-to-right parsing
Parsing a sentence of a language by analyzing it token by token from
left to right. @xref{Algorithm, ,The Bison Parser Algorithm }.

@item Lexical analyzer (scanner)
A function that reads an input stream and returns tokens one by one.
@xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.

@item Lexical tie-in
A flag, set by actions in the grammar rules, which alters the way
tokens are parsed. @xref{Lexical Tie-ins}.

@item Look-ahead token
A token already read but not yet shifted. @xref{Look-Ahead, ,Look-Ahead Tokens}.

@item LALR(1)
The class of context-free grammars that Bison (like most other parser
generators) can handle; a subset of LR(1). @xref{Mystery Conflicts, ,
Mysterious Reduce/Reduce Conflicts}.

@item LR(1)
The class of context-free grammars in which at most one token of
look-ahead is needed to disambiguate the parsing of any piece of input.

@item Nonterminal symbol
A grammar symbol standing for a grammatical construct that can
be expressed through rules in terms of smaller constructs; in other
words, a construct that is not a token. @xref{Symbols}.

@item Parse error
An error encountered during parsing of an input stream due to invalid
syntax. @xref{Error Recovery}.

@item Parser
A function that recognizes valid sentences of a language by analyzing
the syntax structure of a set of tokens passed to it from a lexical
analyzer.

@item Postfix operator
An arithmetic operator that is placed after the operands upon which it
performs some operation.

@item Reduction
Replacing a string of nonterminals and/or terminals with a single
nonterminal, according to a grammar rule. @xref{Algorithm, ,The Bison Parser Algorithm }.

@item Reentrant
A reentrant subprogram is a subprogram which can be in invoked any
number of times in parallel, without interference between the various
invocations. @xref{Pure Decl, ,A Pure (Reentrant) Parser}.

@item Reverse polish notation
A language in which all operators are postfix operators.

@item Right recursion
A rule whose result symbol is also its last component symbol;
for example, @samp{expseq1: exp ',' expseq1;}. @xref{Recursion, ,Recursive Rules}.

@item Semantics
In computer languages, the semantics are specified by the actions
taken for each instance of the language, i.e., the meaning of
each statement. @xref{Semantics, ,Defining Language Semantics}.

@item Shift
A parser is said to shift when it makes the choice of analyzing
further input from the stream rather than reducing immediately some
already-recognized rule. @xref{Algorithm, ,The Bison Parser Algorithm }.

@item Single-character literal
A single character that is recognized and interpreted as is.
@xref{Grammar in Bison, ,From Formal Rules to Bison Input}.

@item Start symbol
The nonterminal symbol that stands for a complete valid utterance in
the language being parsed. The start symbol is usually listed as the
first nonterminal symbol in a language specification.
@xref{Start Decl, ,The Start-Symbol}.

@item Symbol table
A data structure where symbol names and associated data are stored
during parsing to allow for recognition and use of existing
information in repeated uses of a symbol. @xref{Multi-function Calc}.

@item Token
A basic, grammatically indivisible unit of a language. The symbol
that describes a token in the grammar is a terminal symbol.
The input of the Bison parser is a stream of tokens which comes from
the lexical analyzer. @xref{Symbols}.

@item Terminal symbol
A grammar symbol that has no rules in the grammar and therefore
is grammatically indivisible. The piece of text it represents
is a token. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
@end table

@node Index, , Glossary, Top
@unnumbered Index

@printindex cp

@contents

@bye




@c old menu

* Introduction::
* Conditions::
* Copying:: The GNU General Public License says
how you can copy and share Bison

Tutorial sections:
* Concepts:: Basic concepts for understanding Bison.
* Examples:: Three simple explained examples of using Bison.

Reference sections:
* Grammar File:: Writing Bison declarations and rules.
* Interface:: C-language interface to the parser function @code{yyparse}.
* Algorithm:: How the Bison parser works at run-time.
* Error Recovery:: Writing rules for error recovery.
* Context Dependency::What to do if your language syntax is too
messy for Bison to handle straightforwardly.
* Debugging:: Debugging Bison parsers that parse wrong.
* Invocation:: How to run Bison (to produce the parser source file).
* Table of Symbols:: All the keywords of the Bison language are explained.
* Glossary:: Basic concepts are explained.
* Index:: Cross-references to the text.

bison-1.22/bison.info100666 12120 1 7112 5413126475 13037 0ustar rmsdaemonThis is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.

This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.


Indirect:
bison.info-1: 1185
bison.info-2: 50267
bison.info-3: 96678
bison.info-4: 146593

Tag Table:
(Indirect)
Node: Top1185
Node: Introduction8418
Node: Conditions9585
Node: Copying11457
Node: Concepts30610
Node: Language and Grammar31643
Node: Grammar in Bison36659
Node: Semantic Values38437
Node: Semantic Actions40538
Node: Bison Parser41721
Node: Stages44031
Node: Grammar Layout45314
Node: Examples46571
Node: RPN Calc47706
Node: Rpcalc Decls48680
Node: Rpcalc Rules50267
Node: Rpcalc Input52067
Node: Rpcalc Line53528
Node: Rpcalc Expr54643
Node: Rpcalc Lexer56588
Node: Rpcalc Main59147
Node: Rpcalc Error59525
Node: Rpcalc Gen60530
Node: Rpcalc Compile61678
Node: Infix Calc62553
Node: Simple Error Recovery65260
Node: Multi-function Calc67147
Node: Mfcalc Decl68713
Node: Mfcalc Rules70736
Node: Mfcalc Symtab72116
Node: Exercises78290
Node: Grammar File78796
Node: Grammar Outline79564
Node: C Declarations80298
Node: Bison Declarations80878
Node: Grammar Rules81290
Node: C Code81750
Node: Symbols82680
Node: Rules86455
Node: Recursion88094
Node: Semantics89805
Node: Value Type90902
Node: Multiple Types91574
Node: Actions92590
Node: Action Types95375
Node: Mid-Rule Actions96678
Node: Declarations102247
Node: Token Decl103566
Node: Precedence Decl104889
Node: Union Decl106440
Node: Type Decl107284
Node: Expect Decl107989
Node: Start Decl109535
Node: Pure Decl109913
Node: Decl Summary111215
Node: Multiple Parsers112619
Node: Interface114102
Node: Parser Function114974
Node: Lexical115809
Node: Calling Convention117215
Node: Token Values118522
Node: Token Positions119670
Node: Pure Calling120562
Node: Error Reporting121562
Node: Action Features123687
Node: Algorithm127338
Node: Look-Ahead129631
Node: Shift/Reduce131763
Node: Precedence134674
Node: Why Precedence135325
Node: Using Precedence137180
Node: Precedence Examples138148
Node: How Precedence138849
Node: Contextual Precedence139998
Node: Parser States141789
Node: Reduce/Reduce143032
Node: Mystery Conflicts146593
Node: Stack Overflow149979
Node: Error Recovery151352
Node: Context Dependency156488
Node: Semantic Tokens157336
Node: Lexical Tie-ins160353
Node: Tie-in Recovery161901
Node: Debugging164073
Node: Invocation167424
Node: Bison Options168087
Node: Option Cross Key171724
Node: VMS Invocation172456
Node: Table of Symbols173240
Node: Glossary179827
Node: Index186011

End Tag Table
bison-1.22/bison.info-1100666 12120 1 142133 5413126474 13237 0ustar rmsdaemonThis is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.

This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.


File: bison.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir)

This manual documents version 1.20 of Bison.

* Menu:

* Introduction::
* Conditions::
* Copying:: The GNU General Public License says
how you can copy and share Bison

Tutorial sections:
* Concepts:: Basic concepts for understanding Bison.
* Examples:: Three simple explained examples of using Bison.

Reference sections:
* Grammar File:: Writing Bison declarations and rules.
* Interface:: C-language interface to the parser function `yyparse'.
* Algorithm:: How the Bison parser works at run-time.
* Error Recovery:: Writing rules for error recovery.
* Context Dependency:: What to do if your language syntax is too
messy for Bison to handle straightforwardly.
* Debugging:: Debugging Bison parsers that parse wrong.
* Invocation:: How to run Bison (to produce the parser source file).
* Table of Symbols:: All the keywords of the Bison language are explained.
* Glossary:: Basic concepts are explained.
* Index:: Cross-references to the text.

-- The Detailed Node Listing --

The Concepts of Bison

* Language and Grammar:: Languages and context-free grammars,
as mathematical ideas.
* Grammar in Bison:: How we represent grammars for Bison's sake.
* Semantic Values:: Each token or syntactic grouping can have
a semantic value (the value of an integer,
the name of an identifier, etc.).
* Semantic Actions:: Each rule can have an action containing C code.
* Bison Parser:: What are Bison's input and output,
how is the output used?
* Stages:: Stages in writing and running Bison grammars.
* Grammar Layout:: Overall structure of a Bison grammar file.

Examples

* RPN Calc:: Reverse polish notation calculator;
a first example with no operator precedence.
* Infix Calc:: Infix (algebraic) notation calculator.
Operator precedence is introduced.
* Simple Error Recovery:: Continuing after syntax errors.
* Multi-function Calc:: Calculator with memory and trig functions.
It uses multiple data-types for semantic values.
* Exercises:: Ideas for improving the multi-function calculator.

Reverse Polish Notation Calculator

* Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
* Lexer: Rpcalc Lexer. The lexical analyzer.
* Main: Rpcalc Main. The controlling function.
* Error: Rpcalc Error. The error reporting function.
* Gen: Rpcalc Gen. Running Bison on the grammar file.
* Comp: Rpcalc Compile. Run the C compiler on the output code.

Grammar Rules for `rpcalc'

* Rpcalc Input::
* Rpcalc Line::
* Rpcalc Expr::

Multi-Function Calculator: `mfcalc'

* Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
* Rules: Mfcalc Rules. Grammar rules for the calculator.
* Symtab: Mfcalc Symtab. Symbol table management subroutines.

Bison Grammar Files

* Grammar Outline:: Overall layout of the grammar file.
* Symbols:: Terminal and nonterminal symbols.
* Rules:: How to write grammar rules.
* Recursion:: Writing recursive rules.
* Semantics:: Semantic values and actions.
* Declarations:: All kinds of Bison declarations are described here.
* Multiple Parsers:: Putting more than one Bison parser in one program.

Outline of a Bison Grammar

* C Declarations:: Syntax and usage of the C declarations section.
* Bison Declarations:: Syntax and usage of the Bison declarations section.
* Grammar Rules:: Syntax and usage of the grammar rules section.
* C Code:: Syntax and usage of the additional C code section.

Defining Language Semantics

* Value Type:: Specifying one data type for all semantic values.
* Multiple Types:: Specifying several alternative data types.
* Actions:: An action is the semantic definition of a grammar rule.
* Action Types:: Specifying data types for actions to operate on.
* Mid-Rule Actions:: Most actions go at the end of a rule.
This says when, why and how to use the exceptional
action in the middle of a rule.

Bison Declarations

* Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity.
* Union Decl:: Declaring the set of all semantic value types.
* Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Expect Decl:: Suppressing warnings about shift/reduce conflicts.
* Start Decl:: Specifying the start symbol.
* Pure Decl:: Requesting a reentrant parser.
* Decl Summary:: Table of all Bison declarations.

Parser C-Language Interface

* Parser Function:: How to call `yyparse' and what it returns.
* Lexical:: You must supply a function `yylex'
which reads tokens.
* Error Reporting:: You must supply a function `yyerror'.
* Action Features:: Special features for use in actions.

The Lexical Analyzer Function `yylex'

* Calling Convention:: How `yyparse' calls `yylex'.
* Token Values:: How `yylex' must return the semantic value
of the token it has read.
* Token Positions:: How `yylex' must return the text position
(line number, etc.) of the token, if the
actions want that.
* Pure Calling:: How the calling convention differs
in a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.).

The Bison Parser Algorithm

* Look-Ahead:: Parser looks one token ahead when deciding what to do.
* Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
* Precedence:: Operator precedence works by resolving conflicts.
* Contextual Precedence:: When an operator's precedence depends on context.
* Parser States:: The parser is a finite-state-machine with stack.
* Reduce/Reduce:: When two rules are applicable in the same situation.
* Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
* Stack Overflow:: What happens when stack gets full. How to avoid it.

Operator Precedence

* Why Precedence:: An example showing why precedence is needed.
* Using Precedence:: How to specify precedence in Bison grammars.
* Precedence Examples:: How these features are used in the previous example.
* How Precedence:: How they work.

Handling Context Dependencies

* Semantic Tokens:: Token parsing can depend on the semantic context.
* Lexical Tie-ins:: Token parsing can depend on the syntactic context.
* Tie-in Recovery:: Lexical tie-ins have implications for how
error recovery rules must be written.

Invoking Bison

* Bison Options:: All the options described in detail,
in alphabetical order by short options.
* Option Cross Key:: Alphabetical list of long options.
* VMS Invocation:: Bison command syntax on VMS.


File: bison.info, Node: Introduction, Next: Conditions, Prev: Top, Up: Top

Introduction
************

"Bison" is a general-purpose parser generator that converts a
grammar description for an LALR(1) context-free grammar into a C
program to parse that grammar. Once you are proficient with Bison, you
may use it to develop a wide range of language parsers, from those used
in simple desk calculators to complex programming languages.

Bison is upward compatible with Yacc: all properly-written Yacc
grammars ought to work with Bison with no change. Anyone familiar with
Yacc should be able to use Bison with little trouble. You need to be
fluent in C programming in order to use Bison or to understand this
manual.

We begin with tutorial chapters that explain the basic concepts of
using Bison and show three explained examples, each building on the
last. If you don't know Bison or Yacc, start by reading these
chapters. Reference chapters follow which describe specific aspects of
Bison in detail.

Bison was written primarily by Robert Corbett; Richard Stallman made
it Yacc-compatible. This edition corresponds to version 1.20 of Bison.


File: bison.info, Node: Conditions, Next: Copying, Prev: Introduction, Up: Top

Conditions for Using Bison
**************************

Bison grammars can be used only in programs that are free software.
This is in contrast to what happens with the GNU C compiler and the
other GNU programming tools.

The reason Bison is special is that the output of the Bison
utility--the Bison parser file--contains a verbatim copy of a sizable
piece of Bison, which is the code for the `yyparse' function. (The
actions from your grammar are inserted into this function at one point,
but the rest of the function is not changed.)

As a result, the Bison parser file is covered by the same copying
conditions that cover Bison itself and the rest of the GNU system: any
program containing it has to be distributed under the standard GNU
copying conditions.

Occasionally people who would like to use Bison to develop
proprietary programs complain about this.

We don't particularly sympathize with their complaints. The purpose
of the GNU project is to promote the right to share software and the
practice of sharing software; it is a means of changing society. The
people who complain are planning to be uncooperative toward the rest of
the world; why should they deserve our help in doing so?

However, it's possible that a change in these conditions might
encourage computer companies to use and distribute the GNU system. If
so, then we might decide to change the terms on `yyparse' as a matter
of the strategy of promoting the right to share. Such a change would be
irrevocable. Since we stand by the copying permissions we have
announced, we cannot withdraw them once given.

We mustn't make an irrevocable change hastily. We have to wait
until there is a complete GNU system and there has been time to learn
how this issue affects its reception.


File: bison.info, Node: Copying, Next: Concepts, Prev: Conditions, Up: Top

GNU GENERAL PUBLIC LICENSE
**************************

Version 2, June 1991

Copyright (C) 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Preamble
========

The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.

When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it in
new free programs; and that you know you can do these things.

To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.

We protect your rights with two steps: (1) copyright the software,
and (2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

The precise terms and conditions for copying, distribution and
modification follow.

TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. This License applies to any program or other work which contains a
notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program",
below, refers to any such program or work, and a "work based on
the Program" means either the Program or any derivative work under
copyright law: that is to say, a work containing the Program or a
portion of it, either verbatim or with modifications and/or
translated into another language. (Hereinafter, translation is
included without limitation in the term "modification".) Each
licensee is addressed as "you".

Activities other than copying, distribution and modification are
not covered by this License; they are outside its scope. The act
of running the Program is not restricted, and the output from the
Program is covered only if its contents constitute a work based on
the Program (independent of having been made by running the
Program). Whether that is true depends on what the Program does.

1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any
warranty; and give any other recipients of the Program a copy of
this License along with the Program.

You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange
for a fee.

2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

a. You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.

b. You must cause any work that you distribute or publish, that
in whole or in part contains or is derived from the Program
or any part thereof, to be licensed as a whole at no charge
to all third parties under the terms of this License.

c. If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display
an announcement including an appropriate copyright notice and
a notice that there is no warranty (or else, saying that you
provide a warranty) and that users may redistribute the
program under these conditions, and telling the user how to
view a copy of this License. (Exception: if the Program
itself is interactive but does not normally print such an
announcement, your work based on the Program is not required
to print an announcement.)

These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the
Program, and can be reasonably considered independent and separate
works in themselves, then this License, and its terms, do not
apply to those sections when you distribute them as separate
works. But when you distribute the same sections as part of a
whole which is a work based on the Program, the distribution of
the whole must be on the terms of this License, whose permissions
for other licensees extend to the entire whole, and thus to each
and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or
contest your rights to work written entirely by you; rather, the
intent is to exercise the right to control the distribution of
derivative or collective works based on the Program.

In addition, mere aggregation of another work not based on the
Program with the Program (or with a work based on the Program) on
a volume of a storage or distribution medium does not bring the
other work under the scope of this License.

3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms
of Sections 1 and 2 above provided that you also do one of the
following:

a. Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of
Sections 1 and 2 above on a medium customarily used for
software interchange; or,

b. Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange; or,

c. Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with
such an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete
source code means all the source code for all modules it contains,
plus any associated interface definition files, plus the scripts
used to control compilation and installation of the executable.
However, as a special exception, the source code distributed need
not include anything that is normally distributed (in either
source or binary form) with the major components (compiler,
kernel, and so on) of the operating system on which the executable
runs, unless that component itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this
License. However, parties who have received copies, or rights,
from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.

5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify
or distribute the Program or its derivative works. These actions
are prohibited by law if you do not accept this License.
Therefore, by modifying or distributing the Program (or any work
based on the Program), you indicate your acceptance of this
License to do so, and all its terms and conditions for copying,
distributing or modifying the Program or works based on it.

6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program
subject to these terms and conditions. You may not impose any
further restrictions on the recipients' exercise of the rights
granted herein. You are not responsible for enforcing compliance
by third parties to this License.

7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent
issues), conditions are imposed on you (whether by court order,
agreement or otherwise) that contradict the conditions of this
License, they do not excuse you from the conditions of this
License. If you cannot distribute so as to satisfy simultaneously
your obligations under this License and any other pertinent
obligations, then as a consequence you may not distribute the
Program at all. For example, if a patent license would not permit
royalty-free redistribution of the Program by all those who
receive copies directly or indirectly through you, then the only
way you could satisfy both it and this License would be to refrain
entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable
under any particular circumstance, the balance of the section is
intended to apply and the section as a whole is intended to apply
in other circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of
any such claims; this section has the sole purpose of protecting
the integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is
willing to distribute software through any other system and a
licensee cannot impose that choice.

This section is intended to make thoroughly clear what is believed
to be a consequence of the rest of this License.

8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces,
the original copyright holder who places the Program under this
License may add an explicit geographical distribution limitation
excluding those countries, so that distribution is permitted only
in or among countries not thus excluded. In such case, this
License incorporates the limitation as if written in the body of
this License.

9. The Free Software Foundation may publish revised and/or new
versions of the General Public License from time to time. Such
new versions will be similar in spirit to the present version, but
may differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Program specifies a version number of this License which applies
to it and "any later version", you have the option of following
the terms and conditions either of that version or of any later
version published by the Free Software Foundation. If the Program
does not specify a version number of this License, you may choose
any version ever published by the Free Software Foundation.

10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the
author to ask for permission. For software which is copyrighted
by the Free Software Foundation, write to the Free Software
Foundation; we sometimes make exceptions for this. Our decision
will be guided by the two goals of preserving the free status of
all derivatives of our free software and of promoting the sharing
and reuse of software generally.

NO WARRANTY

11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
SERVICING, REPAIR OR CORRECTION.

12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

END OF TERMS AND CONDITIONS

How to Apply These Terms to Your New Programs
=============================================

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
Copyright (C) 19YY NAME OF AUTHOR

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Also add information on how to contact you by electronic and paper
mail.

If the program is interactive, make it output a short notice like
this when it starts in an interactive mode:

Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License. Of course, the
commands you use may be called something other than `show w' and `show
c'; they could even be mouse-clicks or menu items--whatever suits your
program.

You should also get your employer (if you work as a programmer) or
your school, if any, to sign a "copyright disclaimer" for the program,
if necessary. Here is a sample; alter the names:

Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.

SIGNATURE OF TY COON, 1 April 1989
Ty Coon, President of Vice

This General Public License does not permit incorporating your
program into proprietary programs. If your program is a subroutine
library, you may consider it more useful to permit linking proprietary
applications with the library. If this is what you want to do, use the
GNU Library General Public License instead of this License.


File: bison.info, Node: Concepts, Next: Examples, Prev: Copying, Up: Top

The Concepts of Bison
*********************

This chapter introduces many of the basic concepts without which the
details of Bison will not make sense. If you do not already know how to
use Bison or Yacc, we suggest you start by reading this chapter
carefully.

* Menu:

* Language and Grammar:: Languages and context-free grammars,
as mathematical ideas.
* Grammar in Bison:: How we represent grammars for Bison's sake.
* Semantic Values:: Each token or syntactic grouping can have
a semantic value (the value of an integer,
the name of an identifier, etc.).
* Semantic Actions:: Each rule can have an action containing C code.
* Bison Parser:: What are Bison's input and output,
how is the output used?
* Stages:: Stages in writing and running Bison grammars.
* Grammar Layout:: Overall structure of a Bison grammar file.


File: bison.info, Node: Language and Grammar, Next: Grammar in Bison, Up: Concepts

Languages and Context-Free Grammars
===================================

In order for Bison to parse a language, it must be described by a
"context-free grammar". This means that you specify one or more
"syntactic groupings" and give rules for constructing them from their
parts. For example, in the C language, one kind of grouping is called
an `expression'. One rule for making an expression might be, "An
expression can be made of a minus sign and another expression".
Another would be, "An expression can be an integer". As you can see,
rules are often recursive, but there must be at least one rule which
leads out of the recursion.

The most common formal system for presenting such rules for humans
to read is "Backus-Naur Form" or "BNF", which was developed in order to
specify the language Algol 60. Any grammar expressed in BNF is a
context-free grammar. The input to Bison is essentially
machine-readable BNF.

Not all context-free languages can be handled by Bison, only those
that are LALR(1). In brief, this means that it must be possible to
tell how to parse any portion of an input string with just a single
token of look-ahead. Strictly speaking, that is a description of an
LR(1) grammar, and LALR(1) involves additional restrictions that are
hard to explain simply; but it is rare in actual practice to find an
LR(1) grammar that fails to be LALR(1). *Note Mysterious Reduce/Reduce
Conflicts: Mystery Conflicts, for more information on this.

In the formal grammatical rules for a language, each kind of
syntactic unit or grouping is named by a "symbol". Those which are
built by grouping smaller constructs according to grammatical rules are
called "nonterminal symbols"; those which can't be subdivided are called
"terminal symbols" or "token types". We call a piece of input
corresponding to a single terminal symbol a "token", and a piece
corresponding to a single nonterminal symbol a "grouping".

We can use the C language as an example of what symbols, terminal and
nonterminal, mean. The tokens of C are identifiers, constants (numeric
and string), and the various keywords, arithmetic operators and
punctuation marks. So the terminal symbols of a grammar for C include
`identifier', `number', `string', plus one symbol for each keyword,
operator or punctuation mark: `if', `return', `const', `static', `int',
`char', `plus-sign', `open-brace', `close-brace', `comma' and many
more. (These tokens can be subdivided into characters, but that is a
matter of lexicography, not grammar.)

Here is a simple C function subdivided into tokens:

int /* keyword `int' */
square (x) /* identifier, open-paren, */
/* identifier, close-paren */
int x; /* keyword `int', identifier, semicolon */
{ /* open-brace */
return x * x; /* keyword `return', identifier, */
/* asterisk, identifier, semicolon */
} /* close-brace */

The syntactic groupings of C include the expression, the statement,
the declaration, and the function definition. These are represented in
the grammar of C by nonterminal symbols `expression', `statement',
`declaration' and `function definition'. The full grammar uses dozens
of additional language constructs, each with its own nonterminal
symbol, in order to express the meanings of these four. The example
above is a function definition; it contains one declaration, and one
statement. In the statement, each `x' is an expression and so is `x *
x'.

Each nonterminal symbol must have grammatical rules showing how it
is made out of simpler constructs. For example, one kind of C
statement is the `return' statement; this would be described with a
grammar rule which reads informally as follows:

A `statement' can be made of a `return' keyword, an `expression'
and a `semicolon'.

There would be many other rules for `statement', one for each kind of
statement in C.

One nonterminal symbol must be distinguished as the special one which
defines a complete utterance in the language. It is called the "start
symbol". In a compiler, this means a complete input program. In the C
language, the nonterminal symbol `sequence of definitions and
declarations' plays this role.

For example, `1 + 2' is a valid C expression--a valid part of a C
program--but it is not valid as an *entire* C program. In the
context-free grammar of C, this follows from the fact that `expression'
is not the start symbol.

The Bison parser reads a sequence of tokens as its input, and groups
the tokens using the grammar rules. If the input is valid, the end
result is that the entire token sequence reduces to a single grouping
whose symbol is the grammar's start symbol. If we use a grammar for C,
the entire input must be a `sequence of definitions and declarations'.
If not, the parser reports a syntax error.


File: bison.info, Node: Grammar in Bison, Next: Semantic Values, Prev: Language and Grammar, Up: Concepts

From Formal Rules to Bison Input
================================

A formal grammar is a mathematical construct. To define the language
for Bison, you must write a file expressing the grammar in Bison syntax:
a "Bison grammar" file. *Note Bison Grammar Files: Grammar File.

A nonterminal symbol in the formal grammar is represented in Bison
input as an identifier, like an identifier in C. By convention, it
should be in lower case, such as `expr', `stmt' or `declaration'.

The Bison representation for a terminal symbol is also called a
"token type". Token types as well can be represented as C-like
identifiers. By convention, these identifiers should be upper case to
distinguish them from nonterminals: for example, `INTEGER',
`IDENTIFIER', `IF' or `RETURN'. A terminal symbol that stands for a
particular keyword in the language should be named after that keyword
converted to upper case. The terminal symbol `error' is reserved for
error recovery. *Note Symbols::.

A terminal symbol can also be represented as a character literal,
just like a C character constant. You should do this whenever a token
is just a single character (parenthesis, plus-sign, etc.): use that
same character in a literal as the terminal symbol for that token.

The grammar rules also have an expression in Bison syntax. For
example, here is the Bison rule for a C `return' statement. The
semicolon in quotes is a literal character token, representing part of
the C syntax for the statement; the naked semicolon, and the colon, are
Bison punctuation used in every rule.

stmt: RETURN expr ';'
;

*Note Syntax of Grammar Rules: Rules.


File: bison.info, Node: Semantic Values, Next: Semantic Actions, Prev: Grammar in Bison, Up: Concepts

Semantic Values
===============

A formal grammar selects tokens only by their classifications: for
example, if a rule mentions the terminal symbol `integer constant', it
means that *any* integer constant is grammatically valid in that
position. The precise value of the constant is irrelevant to how to
parse the input: if `x+4' is grammatical then `x+1' or `x+3989' is
equally grammatical.

But the precise value is very important for what the input means
once it is parsed. A compiler is useless if it fails to distinguish
between 4, 1 and 3989 as constants in the program! Therefore, each
token in a Bison grammar has both a token type and a "semantic value".
*Note Defining Language Semantics: Semantics, for details.

The token type is a terminal symbol defined in the grammar, such as
`INTEGER', `IDENTIFIER' or `',''. It tells everything you need to know
to decide where the token may validly appear and how to group it with
other tokens. The grammar rules know nothing about tokens except their
types.

The semantic value has all the rest of the information about the
meaning of the token, such as the value of an integer, or the name of an
identifier. (A token such as `','' which is just punctuation doesn't
need to have any semantic value.)

For example, an input token might be classified as token type
`INTEGER' and have the semantic value 4. Another input token might
have the same token type `INTEGER' but value 3989. When a grammar rule
says that `INTEGER' is allowed, either of these tokens is acceptable
because each is an `INTEGER'. When the parser accepts the token, it
keeps track of the token's semantic value.

Each grouping can also have a semantic value as well as its
nonterminal symbol. For example, in a calculator, an expression
typically has a semantic value that is a number. In a compiler for a
programming language, an expression typically has a semantic value that
is a tree structure describing the meaning of the expression.


File: bison.info, Node: Semantic Actions, Next: Bison Parser, Prev: Semantic Values, Up: Concepts

Semantic Actions
================

In order to be useful, a program must do more than parse input; it
must also produce some output based on the input. In a Bison grammar,
a grammar rule can have an "action" made up of C statements. Each time
the parser recognizes a match for that rule, the action is executed.
*Note Actions::.

Most of the time, the purpose of an action is to compute the
semantic value of the whole construct from the semantic values of its
parts. For example, suppose we have a rule which says an expression
can be the sum of two expressions. When the parser recognizes such a
sum, each of the subexpressions has a semantic value which describes
how it was built up. The action for this rule should create a similar
sort of value for the newly recognized larger expression.

For example, here is a rule that says an expression can be the sum of
two subexpressions:

expr: expr '+' expr { $$ = $1 + $3; }
;

The action says how to produce the semantic value of the sum expression
from the values of the two subexpressions.


File: bison.info, Node: Bison Parser, Next: Stages, Prev: Semantic Actions, Up: Concepts

Bison Output: the Parser File
=============================

When you run Bison, you give it a Bison grammar file as input. The
output is a C source file that parses the language described by the
grammar. This file is called a "Bison parser". Keep in mind that the
Bison utility and the Bison parser are two distinct programs: the Bison
utility is a program whose output is the Bison parser that becomes part
of your program.

The job of the Bison parser is to group tokens into groupings
according to the grammar rules--for example, to build identifiers and
operators into expressions. As it does this, it runs the actions for
the grammar rules it uses.

The tokens come from a function called the "lexical analyzer" that
you must supply in some fashion (such as by writing it in C). The
Bison parser calls the lexical analyzer each time it wants a new token.
It doesn't know what is "inside" the tokens (though their semantic
values may reflect this). Typically the lexical analyzer makes the
tokens by parsing characters of text, but Bison does not depend on
this. *Note The Lexical Analyzer Function `yylex': Lexical.

The Bison parser file is C code which defines a function named
`yyparse' which implements that grammar. This function does not make a
complete C program: you must supply some additional functions. One is
the lexical analyzer. Another is an error-reporting function which the
parser calls to report an error. In addition, a complete C program must
start with a function called `main'; you have to provide this, and
arrange for it to call `yyparse' or the parser will never run. *Note
Parser C-Language Interface: Interface.

Aside from the token type names and the symbols in the actions you
write, all variable and function names used in the Bison parser file
begin with `yy' or `YY'. This includes interface functions such as the
lexical analyzer function `yylex', the error reporting function
`yyerror' and the parser function `yyparse' itself. This also includes
numerous identifiers used for internal purposes. Therefore, you should
avoid using C identifiers starting with `yy' or `YY' in the Bison
grammar file except for the ones defined in this manual.


File: bison.info, Node: Stages, Next: Grammar Layout, Prev: Bison Parser, Up: Concepts

Stages in Using Bison
=====================

The actual language-design process using Bison, from grammar
specification to a working compiler or interpreter, has these parts:

1. Formally specify the grammar in a form recognized by Bison (*note
Bison Grammar Files: Grammar File.). For each grammatical rule in
the language, describe the action that is to be taken when an
instance of that rule is recognized. The action is described by a
sequence of C statements.

2. Write a lexical analyzer to process input and pass tokens to the
parser. The lexical analyzer may be written by hand in C (*note
The Lexical Analyzer Function `yylex': Lexical.). It could also
be produced using Lex, but the use of Lex is not discussed in this
manual.

3. Write a controlling function that calls the Bison-produced parser.

4. Write error-reporting routines.

To turn this source code as written into a runnable program, you
must follow these steps:

1. Run Bison on the grammar to produce the parser.

2. Compile the code output by Bison, as well as any other source
files.

3. Link the object files to produce the finished product.


File: bison.info, Node: Grammar Layout, Prev: Stages, Up: Concepts

The Overall Layout of a Bison Grammar
=====================================

The input file for the Bison utility is a "Bison grammar file". The
general form of a Bison grammar file is as follows:

%{
C DECLARATIONS
%}

BISON DECLARATIONS

%%
GRAMMAR RULES
%%
ADDITIONAL C CODE

The `%%', `%{' and `%}' are punctuation that appears in every Bison
grammar file to separate the sections.

The C declarations may define types and variables used in the
actions. You can also use preprocessor commands to define macros used
there, and use `#include' to include header files that do any of these
things.

The Bison declarations declare the names of the terminal and
nonterminal symbols, and may also describe operator precedence and the
data types of semantic values of various symbols.

The grammar rules define how to construct each nonterminal symbol
from its parts.

The additional C code can contain any C code you want to use. Often
the definition of the lexical analyzer `yylex' goes here, plus
subroutines called by the actions in the grammar rules. In a simple
program, all the rest of the program can go here.


File: bison.info, Node: Examples, Next: Grammar File, Prev: Concepts, Up: Top

Examples
********

Now we show and explain three sample programs written using Bison: a
reverse polish notation calculator, an algebraic (infix) notation
calculator, and a multi-function calculator. All three have been tested
under BSD Unix 4.3; each produces a usable, though limited, interactive
desk-top calculator.

These examples are simple, but Bison grammars for real programming
languages are written the same way. You can copy these examples out of
the Info file and into a source file to try them.

* Menu:

* RPN Calc:: Reverse polish notation calculator;
a first example with no operator precedence.
* Infix Calc:: Infix (algebraic) notation calculator.
Operator precedence is introduced.
* Simple Error Recovery:: Continuing after syntax errors.
* Multi-function Calc:: Calculator with memory and trig functions.
It uses multiple data-types for semantic values.
* Exercises:: Ideas for improving the multi-function calculator.


File: bison.info, Node: RPN Calc, Next: Infix Calc, Up: Examples

Reverse Polish Notation Calculator
==================================

The first example is that of a simple double-precision "reverse
polish notation" calculator (a calculator using postfix operators).
This example provides a good starting point, since operator precedence
is not an issue. The second example will illustrate how operator
precedence is handled.

The source code for this calculator is named `rpcalc.y'. The `.y'
extension is a convention used for Bison input files.

* Menu:

* Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
* Lexer: Rpcalc Lexer. The lexical analyzer.
* Main: Rpcalc Main. The controlling function.
* Error: Rpcalc Error. The error reporting function.
* Gen: Rpcalc Gen. Running Bison on the grammar file.
* Comp: Rpcalc Compile. Run the C compiler on the output code.


File: bison.info, Node: Rpcalc Decls, Next: Rpcalc Rules, Up: RPN Calc

Declarations for `rpcalc'
-------------------------

Here are the C and Bison declarations for the reverse polish notation
calculator. As in C, comments are placed between `/*...*/'.

/* Reverse polish notation calculator. */

%{
#define YYSTYPE double
#include
%}

%token NUM

%% /* Grammar rules and actions follow */

The C declarations section (*note The C Declarations Section: C
Declarations.) contains two preprocessor directives.

The `#define' directive defines the macro `YYSTYPE', thus specifying
the C data type for semantic values of both tokens and groupings (*note
Data Types of Semantic Values: Value Type.). The Bison parser will use
whatever type `YYSTYPE' is defined as; if you don't define it, `int' is
the default. Because we specify `double', each token and each
expression has an associated value, which is a floating point number.

The `#include' directive is used to declare the exponentiation
function `pow'.

The second section, Bison declarations, provides information to
Bison about the token types (*note The Bison Declarations Section:
Bison Declarations.). Each terminal symbol that is not a
single-character literal must be declared here. (Single-character
literals normally don't need to be declared.) In this example, all the
arithmetic operators are designated by single-character literals, so the
only terminal symbol that needs to be declared is `NUM', the token type
for numeric constants.

bison-1.22/bison.info-2100666 12120 1 134754 5413126474 13252 0ustar rmsdaemonThis is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.

This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.


File: bison.info, Node: Rpcalc Rules, Next: Rpcalc Lexer, Prev: Rpcalc Decls, Up: RPN Calc

Grammar Rules for `rpcalc'
--------------------------

Here are the grammar rules for the reverse polish notation
calculator.

input: /* empty */
| input line
;

line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;

exp: NUM { $$ = $1; }
| exp exp '+' { $$ = $1 + $2; }
| exp exp '-' { $$ = $1 - $2; }
| exp exp '*' { $$ = $1 * $2; }
| exp exp '/' { $$ = $1 / $2; }
/* Exponentiation */
| exp exp '^' { $$ = pow ($1, $2); }
/* Unary minus */
| exp 'n' { $$ = -$1; }
;
%%

The groupings of the rpcalc "language" defined here are the
expression (given the name `exp'), the line of input (`line'), and the
complete input transcript (`input'). Each of these nonterminal symbols
has several alternate rules, joined by the `|' punctuator which is read
as "or". The following sections explain what these rules mean.

The semantics of the language is determined by the actions taken
when a grouping is recognized. The actions are the C code that appears
inside braces. *Note Actions::.

You must specify these actions in C, but Bison provides the means for
passing semantic values between the rules. In each action, the
pseudo-variable `$$' stands for the semantic value for the grouping
that the rule is going to construct. Assigning a value to `$$' is the
main job of most actions. The semantic values of the components of the
rule are referred to as `$1', `$2', and so on.

* Menu:

* Rpcalc Input::
* Rpcalc Line::
* Rpcalc Expr::


File: bison.info, Node: Rpcalc Input, Next: Rpcalc Line, Up: Rpcalc Rules

Explanation of `input'
......................

Consider the definition of `input':

input: /* empty */
| input line
;

This definition reads as follows: "A complete input is either an
empty string, or a complete input followed by an input line". Notice
that "complete input" is defined in terms of itself. This definition
is said to be "left recursive" since `input' appears always as the
leftmost symbol in the sequence. *Note Recursive Rules: Recursion.

The first alternative is empty because there are no symbols between
the colon and the first `|'; this means that `input' can match an empty
string of input (no tokens). We write the rules this way because it is
legitimate to type `Ctrl-d' right after you start the calculator. It's
conventional to put an empty alternative first and write the comment
`/* empty */' in it.

The second alternate rule (`input line') handles all nontrivial
input. It means, "After reading any number of lines, read one more
line if possible." The left recursion makes this rule into a loop.
Since the first alternative matches empty input, the loop can be
executed zero or more times.

The parser function `yyparse' continues to process input until a
grammatical error is seen or the lexical analyzer says there are no more
input tokens; we will arrange for the latter to happen at end of file.


File: bison.info, Node: Rpcalc Line, Next: Rpcalc Expr, Prev: Rpcalc Input, Up: Rpcalc Rules

Explanation of `line'
.....................

Now consider the definition of `line':

line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;

The first alternative is a token which is a newline character; this
means that rpcalc accepts a blank line (and ignores it, since there is
no action). The second alternative is an expression followed by a
newline. This is the alternative that makes rpcalc useful. The
semantic value of the `exp' grouping is the value of `$1' because the
`exp' in question is the first symbol in the alternative. The action
prints this value, which is the result of the computation the user
asked for.

This action is unusual because it does not assign a value to `$$'.
As a consequence, the semantic value associated with the `line' is
uninitialized (its value will be unpredictable). This would be a bug if
that value were ever used, but we don't use it: once rpcalc has printed
the value of the user's input line, that value is no longer needed.


File: bison.info, Node: Rpcalc Expr, Prev: Rpcalc Line, Up: Rpcalc Rules

Explanation of `expr'
.....................

The `exp' grouping has several rules, one for each kind of
expression. The first rule handles the simplest expressions: those
that are just numbers. The second handles an addition-expression,
which looks like two expressions followed by a plus-sign. The third
handles subtraction, and so on.

exp: NUM
| exp exp '+' { $$ = $1 + $2; }
| exp exp '-' { $$ = $1 - $2; }
...
;

We have used `|' to join all the rules for `exp', but we could
equally well have written them separately:

exp: NUM ;
exp: exp exp '+' { $$ = $1 + $2; } ;
exp: exp exp '-' { $$ = $1 - $2; } ;
...

Most of the rules have actions that compute the value of the
expression in terms of the value of its parts. For example, in the
rule for addition, `$1' refers to the first component `exp' and `$2'
refers to the second one. The third component, `'+'', has no meaningful
associated semantic value, but if it had one you could refer to it as
`$3'. When `yyparse' recognizes a sum expression using this rule, the
sum of the two subexpressions' values is produced as the value of the
entire expression. *Note Actions::.

You don't have to give an action for every rule. When a rule has no
action, Bison by default copies the value of `$1' into `$$'. This is
what happens in the first rule (the one that uses `NUM').

The formatting shown here is the recommended convention, but Bison
does not require it. You can add or change whitespace as much as you
wish. For example, this:

exp : NUM | exp exp '+' {$$ = $1 + $2; } | ...

means the same thing as this:

exp: NUM
| exp exp '+' { $$ = $1 + $2; }
| ...

The latter, however, is much more readable.


File: bison.info, Node: Rpcalc Lexer, Next: Rpcalc Main, Prev: Rpcalc Rules, Up: RPN Calc

The `rpcalc' Lexical Analyzer
-----------------------------

The lexical analyzer's job is low-level parsing: converting
characters or sequences of characters into tokens. The Bison parser
gets its tokens by calling the lexical analyzer. *Note The Lexical
Analyzer Function `yylex': Lexical.

Only a simple lexical analyzer is needed for the RPN calculator.
This lexical analyzer skips blanks and tabs, then reads in numbers as
`double' and returns them as `NUM' tokens. Any other character that
isn't part of a number is a separate token. Note that the token-code
for such a single-character token is the character itself.

The return value of the lexical analyzer function is a numeric code
which represents a token type. The same text used in Bison rules to
stand for this token type is also a C expression for the numeric code
for the type. This works in two ways. If the token type is a
character literal, then its numeric code is the ASCII code for that
character; you can use the same character literal in the lexical
analyzer to express the number. If the token type is an identifier,
that identifier is defined by Bison as a C macro whose definition is
the appropriate number. In this example, therefore, `NUM' becomes a
macro for `yylex' to use.

The semantic value of the token (if it has one) is stored into the
global variable `yylval', which is where the Bison parser will look for
it. (The C data type of `yylval' is `YYSTYPE', which was defined at
the beginning of the grammar; *note Declarations for `rpcalc': Rpcalc
Decls..)

A token type code of zero is returned if the end-of-file is
encountered. (Bison recognizes any nonpositive value as indicating the
end of the input.)

Here is the code for the lexical analyzer:

/* Lexical analyzer returns a double floating point
number on the stack and the token NUM, or the ASCII
character read if not a number. Skips all blanks
and tabs, returns 0 for EOF. */

#include

yylex ()
{
int c;

/* skip white space */
while ((c = getchar ()) == ' ' || c == '\t')
;
/* process numbers */
if (c == '.' || isdigit (c))
{
ungetc (c, stdin);
scanf ("%lf", &yylval);
return NUM;
}
/* return end-of-file */
if (c == EOF)
return 0;
/* return single chars */
return c;
}


File: bison.info, Node: Rpcalc Main, Next: Rpcalc Error, Prev: Rpcalc Lexer, Up: RPN Calc

The Controlling Function
------------------------

In keeping with the spirit of this example, the controlling function
is kept to the bare minimum. The only requirement is that it call
`yyparse' to start the process of parsing.

main ()
{
yyparse ();
}


File: bison.info, Node: Rpcalc Error, Next: Rpcalc Gen, Prev: Rpcalc Main, Up: RPN Calc

The Error Reporting Routine
---------------------------

When `yyparse' detects a syntax error, it calls the error reporting
function `yyerror' to print an error message (usually but not always
`"parse error"'). It is up to the programmer to supply `yyerror'
(*note Parser C-Language Interface: Interface.), so here is the
definition we will use:

#include

yyerror (s) /* Called by yyparse on error */
char *s;
{
printf ("%s\n", s);
}

After `yyerror' returns, the Bison parser may recover from the error
and continue parsing if the grammar contains a suitable error rule
(*note Error Recovery::.). Otherwise, `yyparse' returns nonzero. We
have not written any error rules in this example, so any invalid input
will cause the calculator program to exit. This is not clean behavior
for a real calculator, but it is adequate in the first example.


File: bison.info, Node: Rpcalc Gen, Next: Rpcalc Compile, Prev: Rpcalc Error, Up: RPN Calc

Running Bison to Make the Parser
--------------------------------

Before running Bison to produce a parser, we need to decide how to
arrange all the source code in one or more source files. For such a
simple example, the easiest thing is to put everything in one file.
The definitions of `yylex', `yyerror' and `main' go at the end, in the
"additional C code" section of the file (*note The Overall Layout of a
Bison Grammar: Grammar Layout.).

For a large project, you would probably have several source files,
and use `make' to arrange to recompile them.

With all the source in a single file, you use the following command
to convert it into a parser file:

bison FILE_NAME.y

In this example the file was called `rpcalc.y' (for "Reverse Polish
CALCulator"). Bison produces a file named `FILE_NAME.tab.c', removing
the `.y' from the original file name. The file output by Bison contains
the source code for `yyparse'. The additional functions in the input
file (`yylex', `yyerror' and `main') are copied verbatim to the output.


File: bison.info, Node: Rpcalc Compile, Prev: Rpcalc Gen, Up: RPN Calc

Compiling the Parser File
-------------------------

Here is how to compile and run the parser file:

# List files in current directory.
% ls
rpcalc.tab.c rpcalc.y

# Compile the Bison parser.
# `-lm' tells compiler to search math library for `pow'.
% cc rpcalc.tab.c -lm -o rpcalc

# List files again.
% ls
rpcalc rpcalc.tab.c rpcalc.y

The file `rpcalc' now contains the executable code. Here is an
example session using `rpcalc'.

% rpcalc
4 9 +
13
3 7 + 3 4 5 *+-
-13
3 7 + 3 4 5 * + - n Note the unary minus, `n'
13
5 6 / 4 n +
-3.166666667
3 4 ^ Exponentiation
81
^D End-of-file indicator
%


File: bison.info, Node: Infix Calc, Next: Simple Error Recovery, Prev: RPN Calc, Up: Examples

Infix Notation Calculator: `calc'
=================================

We now modify rpcalc to handle infix operators instead of postfix.
Infix notation involves the concept of operator precedence and the need
for parentheses nested to arbitrary depth. Here is the Bison code for
`calc.y', an infix desk-top calculator.

/* Infix notation calculator--calc */

%{
#define YYSTYPE double
#include
%}

/* BISON Declarations */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%right '^' /* exponentiation */

/* Grammar follows */
%%
input: /* empty string */
| input line
;

line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;

exp: NUM { $$ = $1; }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;
%%

The functions `yylex', `yyerror' and `main' can be the same as before.

There are two important new features shown in this code.

In the second section (Bison declarations), `%left' declares token
types and says they are left-associative operators. The declarations
`%left' and `%right' (right associativity) take the place of `%token'
which is used to declare a token type name without associativity.
(These tokens are single-character literals, which ordinarily don't
need to be declared. We declare them here to specify the
associativity.)

Operator precedence is determined by the line ordering of the
declarations; the higher the line number of the declaration (lower on
the page or screen), the higher the precedence. Hence, exponentiation
has the highest precedence, unary minus (`NEG') is next, followed by
`*' and `/', and so on. *Note Operator Precedence: Precedence.

The other important new feature is the `%prec' in the grammar section
for the unary minus operator. The `%prec' simply instructs Bison that
the rule `| '-' exp' has the same precedence as `NEG'--in this case the
next-to-highest. *Note Context-Dependent Precedence: Contextual
Precedence.

Here is a sample run of `calc.y':

% calc
4 + 4.5 - (34/(8*3+-3))
6.880952381
-56 + 2
-54
3 ^ 2
9


File: bison.info, Node: Simple Error Recovery, Next: Multi-function Calc, Prev: Infix Calc, Up: Examples

Simple Error Recovery
=====================

Up to this point, this manual has not addressed the issue of "error
recovery"--how to continue parsing after the parser detects a syntax
error. All we have handled is error reporting with `yyerror'. Recall
that by default `yyparse' returns after calling `yyerror'. This means
that an erroneous input line causes the calculator program to exit.
Now we show how to rectify this deficiency.

The Bison language itself includes the reserved word `error', which
may be included in the grammar rules. In the example below it has been
added to one of the alternatives for `line':

line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
| error '\n' { yyerrok; }
;

This addition to the grammar allows for simple error recovery in the
event of a parse error. If an expression that cannot be evaluated is
read, the error will be recognized by the third rule for `line', and
parsing will continue. (The `yyerror' function is still called upon to
print its message as well.) The action executes the statement
`yyerrok', a macro defined automatically by Bison; its meaning is that
error recovery is complete (*note Error Recovery::.). Note the
difference between `yyerrok' and `yyerror'; neither one is a misprint.

This form of error recovery deals with syntax errors. There are
other kinds of errors; for example, division by zero, which raises an
exception signal that is normally fatal. A real calculator program
must handle this signal and use `longjmp' to return to `main' and
resume parsing input lines; it would also have to discard the rest of
the current line of input. We won't discuss this issue further because
it is not specific to Bison programs.


File: bison.info, Node: Multi-function Calc, Next: Exercises, Prev: Simple Error Recovery, Up: Examples

Multi-Function Calculator: `mfcalc'
===================================

Now that the basics of Bison have been discussed, it is time to move
on to a more advanced problem. The above calculators provided only five
functions, `+', `-', `*', `/' and `^'. It would be nice to have a
calculator that provides other mathematical functions such as `sin',
`cos', etc.

It is easy to add new operators to the infix calculator as long as
they are only single-character literals. The lexical analyzer `yylex'
passes back all non-number characters as tokens, so new grammar rules
suffice for adding a new operator. But we want something more
flexible: built-in functions whose syntax has this form:

FUNCTION_NAME (ARGUMENT)

At the same time, we will add memory to the calculator, by allowing you
to create named variables, store values in them, and use them later.
Here is a sample session with the multi-function calculator:

% acalc
pi = 3.141592653589
3.1415926536
sin(pi)
0.0000000000
alpha = beta1 = 2.3
2.3000000000
alpha
2.3000000000
ln(alpha)
0.8329091229
exp(ln(beta1))
2.3000000000
%

Note that multiple assignment and nested function calls are
permitted.

* Menu:

* Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
* Rules: Mfcalc Rules. Grammar rules for the calculator.
* Symtab: Mfcalc Symtab. Symbol table management subroutines.


File: bison.info, Node: Mfcalc Decl, Next: Mfcalc Rules, Up: Multi-function Calc

Declarations for `mfcalc'
-------------------------

Here are the C and Bison declarations for the multi-function
calculator.

%{
#include /* For math functions, cos(), sin(), etc. */
#include "calc.h" /* Contains definition of `symrec' */
%}
%union {
double val; /* For returning numbers. */
symrec *tptr; /* For returning symbol-table pointers */
}

%token NUM /* Simple double precision number */
%token VAR FNCT /* Variable and Function */
%type exp

%right '='
%left '-' '+'
%left '*' '/'
%left NEG /* Negation--unary minus */
%right '^' /* Exponentiation */

/* Grammar follows */

%%

The above grammar introduces only two new features of the Bison
language. These features allow semantic values to have various data
types (*note More Than One Value Type: Multiple Types.).

The `%union' declaration specifies the entire list of possible types;
this is instead of defining `YYSTYPE'. The allowable types are now
double-floats (for `exp' and `NUM') and pointers to entries in the
symbol table. *Note The Collection of Value Types: Union Decl.

Since values can now have various types, it is necessary to
associate a type with each grammar symbol whose semantic value is used.
These symbols are `NUM', `VAR', `FNCT', and `exp'. Their declarations
are augmented with information about their data type (placed between
angle brackets).

The Bison construct `%type' is used for declaring nonterminal
symbols, just as `%token' is used for declaring token types. We have
not used `%type' before because nonterminal symbols are normally
declared implicitly by the rules that define them. But `exp' must be
declared explicitly so we can specify its value type. *Note
Nonterminal Symbols: Type Decl.


File: bison.info, Node: Mfcalc Rules, Next: Mfcalc Symtab, Prev: Mfcalc Decl, Up: Multi-function Calc

Grammar Rules for `mfcalc'
--------------------------

Here are the grammar rules for the multi-function calculator. Most
of them are copied directly from `calc'; three rules, those which
mention `VAR' or `FNCT', are new.

input: /* empty */
| input line
;

line:
'\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
| error '\n' { yyerrok; }
;

exp: NUM { $$ = $1; }
| VAR { $$ = $1->value.var; }
| VAR '=' exp { $$ = $3; $1->value.var = $3; }
| FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;
/* End of grammar */
%%


File: bison.info, Node: Mfcalc Symtab, Prev: Mfcalc Rules, Up: Multi-function Calc

The `mfcalc' Symbol Table
-------------------------

The multi-function calculator requires a symbol table to keep track
of the names and meanings of variables and functions. This doesn't
affect the grammar rules (except for the actions) or the Bison
declarations, but it requires some additional C functions for support.

The symbol table itself consists of a linked list of records. Its
definition, which is kept in the header `calc.h', is as follows. It
provides for either functions or variables to be placed in the table.

/* Data type for links in the chain of symbols. */
struct symrec
{
char *name; /* name of symbol */
int type; /* type of symbol: either VAR or FNCT */
union {
double var; /* value of a VAR */
double (*fnctptr)(); /* value of a FNCT */
} value;
struct symrec *next; /* link field */
};

typedef struct symrec symrec;

/* The symbol table: a chain of `struct symrec'. */
extern symrec *sym_table;

symrec *putsym ();
symrec *getsym ();

The new version of `main' includes a call to `init_table', a
function that initializes the symbol table. Here it is, and
`init_table' as well:

#include

main ()
{
init_table ();
yyparse ();
}

yyerror (s) /* Called by yyparse on error */
char *s;
{
printf ("%s\n", s);
}

struct init
{
char *fname;
double (*fnct)();
};

struct init arith_fncts[]
= {
"sin", sin,
"cos", cos,
"atan", atan,
"ln", log,
"exp", exp,
"sqrt", sqrt,
0, 0
};

/* The symbol table: a chain of `struct symrec'. */
symrec *sym_table = (symrec *)0;

init_table () /* puts arithmetic functions in table. */
{
int i;
symrec *ptr;
for (i = 0; arith_fncts[i].fname != 0; i++)
{
ptr = putsym (arith_fncts[i].fname, FNCT);
ptr->value.fnctptr = arith_fncts[i].fnct;
}
}

By simply editing the initialization list and adding the necessary
include files, you can add additional functions to the calculator.

Two important functions allow look-up and installation of symbols in
the symbol table. The function `putsym' is passed a name and the type
(`VAR' or `FNCT') of the object to be installed. The object is linked
to the front of the list, and a pointer to the object is returned. The
function `getsym' is passed the name of the symbol to look up. If
found, a pointer to that symbol is returned; otherwise zero is returned.

symrec *
putsym (sym_name,sym_type)
char *sym_name;
int sym_type;
{
symrec *ptr;
ptr = (symrec *) malloc (sizeof (symrec));
ptr->name = (char *) malloc (strlen (sym_name) + 1);
strcpy (ptr->name,sym_name);
ptr->type = sym_type;
ptr->value.var = 0; /* set value to 0 even if fctn. */
ptr->next = (struct symrec *)sym_table;
sym_table = ptr;
return ptr;
}

symrec *
getsym (sym_name)
char *sym_name;
{
symrec *ptr;
for (ptr = sym_table; ptr != (symrec *) 0;
ptr = (symrec *)ptr->next)
if (strcmp (ptr->name,sym_name) == 0)
return ptr;
return 0;
}

The function `yylex' must now recognize variables, numeric values,
and the single-character arithmetic operators. Strings of alphanumeric
characters with a leading nondigit are recognized as either variables or
functions depending on what the symbol table says about them.

The string is passed to `getsym' for look up in the symbol table. If
the name appears in the table, a pointer to its location and its type
(`VAR' or `FNCT') is returned to `yyparse'. If it is not already in
the table, then it is installed as a `VAR' using `putsym'. Again, a
pointer and its type (which must be `VAR') is returned to `yyparse'.

No change is needed in the handling of numeric values and arithmetic
operators in `yylex'.

#include
yylex ()
{
int c;

/* Ignore whitespace, get first nonwhite character. */
while ((c = getchar ()) == ' ' || c == '\t');

if (c == EOF)
return 0;

/* Char starts a number => parse the number. */
if (c == '.' || isdigit (c))
{
ungetc (c, stdin);
scanf ("%lf", &yylval.val);
return NUM;
}

/* Char starts an identifier => read the name. */
if (isalpha (c))
{
symrec *s;
static char *symbuf = 0;
static int length = 0;
int i;

/* Initially make the buffer long enough
for a 40-character symbol name. */
if (length == 0)
length = 40, symbuf = (char *)malloc (length + 1);

i = 0;
do

{
/* If buffer is full, make it bigger. */
if (i == length)
{
length *= 2;
symbuf = (char *)realloc (symbuf, length + 1);
}
/* Add this character to the buffer. */
symbuf[i++] = c;
/* Get another character. */
c = getchar ();
}

while (c != EOF && isalnum (c));

ungetc (c, stdin);
symbuf[i] = '\0';

s = getsym (symbuf);
if (s == 0)
s = putsym (symbuf, VAR);
yylval.tptr = s;
return s->type;
}

/* Any other character is a token by itself. */
return c;
}

This program is both powerful and flexible. You may easily add new
functions, and it is a simple job to modify this code to install
predefined variables such as `pi' or `e' as well.


File: bison.info, Node: Exercises, Prev: Multi-function Calc, Up: Examples

Exercises
=========

1. Add some new functions from `math.h' to the initialization list.

2. Add another array that contains constants and their values. Then
modify `init_table' to add these constants to the symbol table.
It will be easiest to give the constants type `VAR'.

3. Make the program report an error if the user refers to an
uninitialized variable in any way except to store a value in it.


File: bison.info, Node: Grammar File, Next: Interface, Prev: Examples, Up: Top

Bison Grammar Files
*******************

Bison takes as input a context-free grammar specification and
produces a C-language function that recognizes correct instances of the
grammar.

The Bison grammar input file conventionally has a name ending in
`.y'.

* Menu:

* Grammar Outline:: Overall layout of the grammar file.
* Symbols:: Terminal and nonterminal symbols.
* Rules:: How to write grammar rules.
* Recursion:: Writing recursive rules.
* Semantics:: Semantic values and actions.
* Declarations:: All kinds of Bison declarations are described here.
* Multiple Parsers:: Putting more than one Bison parser in one program.


File: bison.info, Node: Grammar Outline, Next: Symbols, Up: Grammar File

Outline of a Bison Grammar
==========================

A Bison grammar file has four main sections, shown here with the
appropriate delimiters:

%{
C DECLARATIONS
%}

BISON DECLARATIONS

%%
GRAMMAR RULES
%%

ADDITIONAL C CODE

Comments enclosed in `/* ... */' may appear in any of the sections.

* Menu:

* C Declarations:: Syntax and usage of the C declarations section.
* Bison Declarations:: Syntax and usage of the Bison declarations section.
* Grammar Rules:: Syntax and usage of the grammar rules section.
* C Code:: Syntax and usage of the additional C code section.


File: bison.info, Node: C Declarations, Next: Bison Declarations, Up: Grammar Outline

The C Declarations Section
--------------------------

The C DECLARATIONS section contains macro definitions and
declarations of functions and variables that are used in the actions in
the grammar rules. These are copied to the beginning of the parser
file so that they precede the definition of `yyparse'. You can use
`#include' to get the declarations from a header file. If you don't
need any C declarations, you may omit the `%{' and `%}' delimiters that
bracket this section.


File: bison.info, Node: Bison Declarations, Next: Grammar Rules, Prev: C Declarations, Up: Grammar Outline

The Bison Declarations Section
------------------------------

The BISON DECLARATIONS section contains declarations that define
terminal and nonterminal symbols, specify precedence, and so on. In
some simple grammars you may not need any declarations. *Note Bison
Declarations: Declarations.


File: bison.info, Node: Grammar Rules, Next: C Code, Prev: Bison Declarations, Up: Grammar Outline

The Grammar Rules Section
-------------------------

The "grammar rules" section contains one or more Bison grammar
rules, and nothing else. *Note Syntax of Grammar Rules: Rules.

There must always be at least one grammar rule, and the first `%%'
(which precedes the grammar rules) may never be omitted even if it is
the first thing in the file.


File: bison.info, Node: C Code, Prev: Grammar Rules, Up: Grammar Outline

The Additional C Code Section
-----------------------------

The ADDITIONAL C CODE section is copied verbatim to the end of the
parser file, just as the C DECLARATIONS section is copied to the
beginning. This is the most convenient place to put anything that you
want to have in the parser file but which need not come before the
definition of `yyparse'. For example, the definitions of `yylex' and
`yyerror' often go here. *Note Parser C-Language Interface: Interface.

If the last section is empty, you may omit the `%%' that separates it
from the grammar rules.

The Bison parser itself contains many static variables whose names
start with `yy' and many macros whose names start with `YY'. It is a
good idea to avoid using any such names (except those documented in this
manual) in the additional C code section of the grammar file.


File: bison.info, Node: Symbols, Next: Rules, Prev: Grammar Outline, Up: Grammar File

Symbols, Terminal and Nonterminal
=================================

"Symbols" in Bison grammars represent the grammatical classifications
of the language.

A "terminal symbol" (also known as a "token type") represents a
class of syntactically equivalent tokens. You use the symbol in grammar
rules to mean that a token in that class is allowed. The symbol is
represented in the Bison parser by a numeric code, and the `yylex'
function returns a token type code to indicate what kind of token has
been read. You don't need to know what the code value is; you can use
the symbol to stand for it.

A "nonterminal symbol" stands for a class of syntactically equivalent
groupings. The symbol name is used in writing grammar rules. By
convention, it should be all lower case.

Symbol names can contain letters, digits (not at the beginning),
underscores and periods. Periods make sense only in nonterminals.

There are two ways of writing terminal symbols in the grammar:

* A "named token type" is written with an identifier, like an
identifier in C. By convention, it should be all upper case. Each
such name must be defined with a Bison declaration such as
`%token'. *Note Token Type Names: Token Decl.

* A "character token type" (or "literal token") is written in the
grammar using the same syntax used in C for character constants;
for example, `'+'' is a character token type. A character token
type doesn't need to be declared unless you need to specify its
semantic value data type (*note Data Types of Semantic Values:
Value Type.), associativity, or precedence (*note Operator
Precedence: Precedence.).

By convention, a character token type is used only to represent a
token that consists of that particular character. Thus, the token
type `'+'' is used to represent the character `+' as a token.
Nothing enforces this convention, but if you depart from it, your
program will confuse other readers.

All the usual escape sequences used in character literals in C can
be used in Bison as well, but you must not use the null character
as a character literal because its ASCII code, zero, is the code
`yylex' returns for end-of-input (*note Calling Convention for
`yylex': Calling Convention.).

How you choose to write a terminal symbol has no effect on its
grammatical meaning. That depends only on where it appears in rules and
on when the parser function returns that symbol.

The value returned by `yylex' is always one of the terminal symbols
(or 0 for end-of-input). Whichever way you write the token type in the
grammar rules, you write it the same way in the definition of `yylex'.
The numeric code for a character token type is simply the ASCII code for
the character, so `yylex' can use the identical character constant to
generate the requisite code. Each named token type becomes a C macro in
the parser file, so `yylex' can use the name to stand for the code.
(This is why periods don't make sense in terminal symbols.) *Note
Calling Convention for `yylex': Calling Convention.

If `yylex' is defined in a separate file, you need to arrange for the
token-type macro definitions to be available there. Use the `-d'
option when you run Bison, so that it will write these macro definitions
into a separate header file `NAME.tab.h' which you can include in the
other source files that need it. *Note Invoking Bison: Invocation.

The symbol `error' is a terminal symbol reserved for error recovery
(*note Error Recovery::.); you shouldn't use it for any other purpose.
In particular, `yylex' should never return this value.


File: bison.info, Node: Rules, Next: Recursion, Prev: Symbols, Up: Grammar File

Syntax of Grammar Rules
=======================

A Bison grammar rule has the following general form:

RESULT: COMPONENTS...
;

where RESULT is the nonterminal symbol that this rule describes and
COMPONENTS are various terminal and nonterminal symbols that are put
together by this rule (*note Symbols::.).

For example,

exp: exp '+' exp
;

says that two groupings of type `exp', with a `+' token in between, can
be combined into a larger grouping of type `exp'.

Whitespace in rules is significant only to separate symbols. You
can add extra whitespace as you wish.

Scattered among the components can be ACTIONS that determine the
semantics of the rule. An action looks like this:

{C STATEMENTS}

Usually there is only one action and it follows the components. *Note
Actions::.

Multiple rules for the same RESULT can be written separately or can
be joined with the vertical-bar character `|' as follows:

RESULT: RULE1-COMPONENTS...
| RULE2-COMPONENTS...
...
;

They are still considered distinct rules even when joined in this way.

If COMPONENTS in a rule is empty, it means that RESULT can match the
empty string. For example, here is how to define a comma-separated
sequence of zero or more `exp' groupings:

expseq: /* empty */
| expseq1
;

expseq1: exp
| expseq1 ',' exp
;

It is customary to write a comment `/* empty */' in each rule with no
components.


File: bison.info, Node: Recursion, Next: Semantics, Prev: Rules, Up: Grammar File

Recursive Rules
===============

A rule is called "recursive" when its RESULT nonterminal appears
also on its right hand side. Nearly all Bison grammars need to use
recursion, because that is the only way to define a sequence of any
number of somethings. Consider this recursive definition of a
comma-separated sequence of one or more expressions:

expseq1: exp
| expseq1 ',' exp
;

Since the recursive use of `expseq1' is the leftmost symbol in the
right hand side, we call this "left recursion". By contrast, here the
same construct is defined using "right recursion":

expseq1: exp
| exp ',' expseq1
;

Any kind of sequence can be defined using either left recursion or
right recursion, but you should always use left recursion, because it
can parse a sequence of any number of elements with bounded stack
space. Right recursion uses up space on the Bison stack in proportion
to the number of elements in the sequence, because all the elements
must be shifted onto the stack before the rule can be applied even
once. *Note The Bison Parser Algorithm: Algorithm, for further
explanation of this.

"Indirect" or "mutual" recursion occurs when the result of the rule
does not appear directly on its right hand side, but does appear in
rules for other nonterminals which do appear on its right hand side.

For example:

expr: primary
| primary '+' primary
;

primary: constant
| '(' expr ')'
;

defines two mutually-recursive nonterminals, since each refers to the
other.


File: bison.info, Node: Semantics, Next: Declarations, Prev: Recursion, Up: Grammar File

Defining Language Semantics
===========================

The grammar rules for a language determine only the syntax. The
semantics are determined by the semantic values associated with various
tokens and groupings, and by the actions taken when various groupings
are recognized.

For example, the calculator calculates properly because the value
associated with each expression is the proper number; it adds properly
because the action for the grouping `X + Y' is to add the numbers
associated with X and Y.

* Menu:

* Value Type:: Specifying one data type for all semantic values.
* Multiple Types:: Specifying several alternative data types.
* Actions:: An action is the semantic definition of a grammar rule.
* Action Types:: Specifying data types for actions to operate on.
* Mid-Rule Actions:: Most actions go at the end of a rule.
This says when, why and how to use the exceptional
action in the middle of a rule.


File: bison.info, Node: Value Type, Next: Multiple Types, Up: Semantics

Data Types of Semantic Values
-----------------------------

In a simple program it may be sufficient to use the same data type
for the semantic values of all language constructs. This was true in
the RPN and infix calculator examples (*note Reverse Polish Notation
Calculator: RPN Calc.).

Bison's default is to use type `int' for all semantic values. To
specify some other type, define `YYSTYPE' as a macro, like this:

#define YYSTYPE double

This macro definition must go in the C declarations section of the
grammar file (*note Outline of a Bison Grammar: Grammar Outline.).


File: bison.info, Node: Multiple Types, Next: Actions, Prev: Value Type, Up: Semantics

More Than One Value Type
------------------------

In most programs, you will need different data types for different
kinds of tokens and groupings. For example, a numeric constant may
need type `int' or `long', while a string constant needs type `char *',
and an identifier might need a pointer to an entry in the symbol table.

To use more than one data type for semantic values in one parser,
Bison requires you to do two things:

* Specify the entire collection of possible data types, with the
`%union' Bison declaration (*note The Collection of Value Types:
Union Decl.).

* Choose one of those types for each symbol (terminal or nonterminal)
for which semantic values are used. This is done for tokens with
the `%token' Bison declaration (*note Token Type Names: Token
Decl.) and for groupings with the `%type' Bison declaration (*note
Nonterminal Symbols: Type Decl.).


File: bison.info, Node: Actions, Next: Action Types, Prev: Multiple Types, Up: Semantics

Actions
-------

An action accompanies a syntactic rule and contains C code to be
executed each time an instance of that rule is recognized. The task of
most actions is to compute a semantic value for the grouping built by
the rule from the semantic values associated with tokens or smaller
groupings.

An action consists of C statements surrounded by braces, much like a
compound statement in C. It can be placed at any position in the rule;
it is executed at that position. Most rules have just one action at
the end of the rule, following all the components. Actions in the
middle of a rule are tricky and used only for special purposes (*note
Actions in Mid-Rule: Mid-Rule Actions.).

The C code in an action can refer to the semantic values of the
components matched by the rule with the construct `$N', which stands for
the value of the Nth component. The semantic value for the grouping
being constructed is `$$'. (Bison translates both of these constructs
into array element references when it copies the actions into the parser
file.)

Here is a typical example:

exp: ...
| exp '+' exp
{ $$ = $1 + $3; }

This rule constructs an `exp' from two smaller `exp' groupings
connected by a plus-sign token. In the action, `$1' and `$3' refer to
the semantic values of the two component `exp' groupings, which are the
first and third symbols on the right hand side of the rule. The sum is
stored into `$$' so that it becomes the semantic value of the
addition-expression just recognized by the rule. If there were a
useful semantic value associated with the `+' token, it could be
referred to as `$2'.

If you don't specify an action for a rule, Bison supplies a default:
`$$ = $1'. Thus, the value of the first symbol in the rule becomes the
value of the whole rule. Of course, the default rule is valid only if
the two data types match. There is no meaningful default action for an
empty rule; every empty rule must have an explicit action unless the
rule's value does not matter.

`$N' with N zero or negative is allowed for reference to tokens and
groupings on the stack *before* those that match the current rule.
This is a very risky practice, and to use it reliably you must be
certain of the context in which the rule is applied. Here is a case in
which you can use this reliably:

foo: expr bar '+' expr { ... }
| expr bar '-' expr { ... }
;

bar: /* empty */
{ previous_expr = $0; }
;

As long as `bar' is used only in the fashion shown here, `$0' always
refers to the `expr' which precedes `bar' in the definition of `foo'.


File: bison.info, Node: Action Types, Next: Mid-Rule Actions, Prev: Actions, Up: Semantics

Data Types of Values in Actions
-------------------------------

If you have chosen a single data type for semantic values, the `$$'
and `$N' constructs always have that data type.

If you have used `%union' to specify a variety of data types, then
you must declare a choice among these types for each terminal or
nonterminal symbol that can have a semantic value. Then each time you
use `$$' or `$N', its data type is determined by which symbol it refers
to in the rule. In this example,

exp: ...
| exp '+' exp
{ $$ = $1 + $3; }

`$1' and `$3' refer to instances of `exp', so they all have the data
type declared for the nonterminal symbol `exp'. If `$2' were used, it
would have the data type declared for the terminal symbol `'+'',
whatever that might be.

Alternatively, you can specify the data type when you refer to the
value, by inserting `' after the `$' at the beginning of the
reference. For example, if you have defined types as shown here:

%union {
int itype;
double dtype;
}

then you can write `$1' to refer to the first subunit of the
rule as an integer, or `$1' to refer to it as a double.

bison-1.22/bison.info-3100666 12120 1 143634 5413126475 13251 0ustar rmsdaemonThis is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.

This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.


File: bison.info, Node: Mid-Rule Actions, Prev: Action Types, Up: Semantics

Actions in Mid-Rule
-------------------

Occasionally it is useful to put an action in the middle of a rule.
These actions are written just like usual end-of-rule actions, but they
are executed before the parser even recognizes the following components.

A mid-rule action may refer to the components preceding it using
`$N', but it may not refer to subsequent components because it is run
before they are parsed.

The mid-rule action itself counts as one of the components of the
rule. This makes a difference when there is another action later in
the same rule (and usually there is another at the end): you have to
count the actions along with the symbols when working out which number
N to use in `$N'.

The mid-rule action can also have a semantic value. The action can
set its value with an assignment to `$$', and actions later in the rule
can refer to the value using `$N'. Since there is no symbol to name
the action, there is no way to declare a data type for the value in
advance, so you must use the `$<...>' construct to specify a data type
each time you refer to this value.

There is no way to set the value of the entire rule with a mid-rule
action, because assignments to `$$' do not have that effect. The only
way to set the value for the entire rule is with an ordinary action at
the end of the rule.

Here is an example from a hypothetical compiler, handling a `let'
statement that looks like `let (VARIABLE) STATEMENT' and serves to
create a variable named VARIABLE temporarily for the duration of
STATEMENT. To parse this construct, we must put VARIABLE into the
symbol table while STATEMENT is parsed, then remove it afterward. Here
is how it is done:

stmt: LET '(' var ')'
{ $$ = push_context ();
declare_variable ($3); }
stmt { $$ = $6;
pop_context ($5); }

As soon as `let (VARIABLE)' has been recognized, the first action is
run. It saves a copy of the current semantic context (the list of
accessible variables) as its semantic value, using alternative
`context' in the data-type union. Then it calls `declare_variable' to
add the new variable to that list. Once the first action is finished,
the embedded statement `stmt' can be parsed. Note that the mid-rule
action is component number 5, so the `stmt' is component number 6.

After the embedded statement is parsed, its semantic value becomes
the value of the entire `let'-statement. Then the semantic value from
the earlier action is used to restore the prior list of variables. This
removes the temporary `let'-variable from the list so that it won't
appear to exist while the rest of the program is parsed.

Taking action before a rule is completely recognized often leads to
conflicts since the parser must commit to a parse in order to execute
the action. For example, the following two rules, without mid-rule
actions, can coexist in a working parser because the parser can shift
the open-brace token and look at what follows before deciding whether
there is a declaration or not:

compound: '{' declarations statements '}'
| '{' statements '}'
;

But when we add a mid-rule action as follows, the rules become
nonfunctional:

compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| '{' statements '}'
;

Now the parser is forced to decide whether to run the mid-rule action
when it has read no farther than the open-brace. In other words, it
must commit to using one rule or the other, without sufficient
information to do it correctly. (The open-brace token is what is called
the "look-ahead" token at this time, since the parser is still deciding
what to do about it. *Note Look-Ahead Tokens: Look-Ahead.)

You might think that you could correct the problem by putting
identical actions into the two rules, like this:

compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| { prepare_for_local_variables (); }
'{' statements '}'
;

But this does not help, because Bison does not realize that the two
actions are identical. (Bison never tries to understand the C code in
an action.)

If the grammar is such that a declaration can be distinguished from a
statement by the first token (which is true in C), then one solution
which does work is to put the action after the open-brace, like this:

compound: '{' { prepare_for_local_variables (); }
declarations statements '}'
| '{' statements '}'
;

Now the first token of the following declaration or statement, which
would in any case tell Bison which rule to use, can still do so.

Another solution is to bury the action inside a nonterminal symbol
which serves as a subroutine:

subroutine: /* empty */
{ prepare_for_local_variables (); }
;

compound: subroutine
'{' declarations statements '}'
| subroutine
'{' statements '}'
;

Now Bison can execute the action in the rule for `subroutine' without
deciding which rule for `compound' it will eventually use. Note that
the action is now at the end of its rule. Any mid-rule action can be
converted to an end-of-rule action in this way, and this is what Bison
actually does to implement mid-rule actions.


File: bison.info, Node: Declarations, Next: Multiple Parsers, Prev: Semantics, Up: Grammar File

Bison Declarations
==================

The "Bison declarations" section of a Bison grammar defines the
symbols used in formulating the grammar and the data types of semantic
values. *Note Symbols::.

All token type names (but not single-character literal tokens such as
`'+'' and `'*'') must be declared. Nonterminal symbols must be
declared if you need to specify which data type to use for the semantic
value (*note More Than One Value Type: Multiple Types.).

The first rule in the file also specifies the start symbol, by
default. If you want some other symbol to be the start symbol, you
must declare it explicitly (*note Languages and Context-Free Grammars:
Language and Grammar.).

* Menu:

* Token Decl:: Declaring terminal symbols.
* Precedence Decl:: Declaring terminals with precedence and associativity.
* Union Decl:: Declaring the set of all semantic value types.
* Type Decl:: Declaring the choice of type for a nonterminal symbol.
* Expect Decl:: Suppressing warnings about shift/reduce conflicts.
* Start Decl:: Specifying the start symbol.
* Pure Decl:: Requesting a reentrant parser.
* Decl Summary:: Table of all Bison declarations.


File: bison.info, Node: Token Decl, Next: Precedence Decl, Up: Declarations

Token Type Names
----------------

The basic way to declare a token type name (terminal symbol) is as
follows:

%token NAME

Bison will convert this into a `#define' directive in the parser, so
that the function `yylex' (if it is in this file) can use the name NAME
to stand for this token type's code.

Alternatively, you can use `%left', `%right', or `%nonassoc' instead
of `%token', if you wish to specify precedence. *Note Operator
Precedence: Precedence Decl.

You can explicitly specify the numeric code for a token type by
appending an integer value in the field immediately following the token
name:

%token NUM 300

It is generally best, however, to let Bison choose the numeric codes for
all token types. Bison will automatically select codes that don't
conflict with each other or with ASCII characters.

In the event that the stack type is a union, you must augment the
`%token' or other token declaration to include the data type
alternative delimited by angle-brackets (*note More Than One Value
Type: Multiple Types.).

For example:

%union { /* define stack type */
double val;
symrec *tptr;
}
%token NUM /* define token NUM and its type */


File: bison.info, Node: Precedence Decl, Next: Union Decl, Prev: Token Decl, Up: Declarations

Operator Precedence
-------------------

Use the `%left', `%right' or `%nonassoc' declaration to declare a
token and specify its precedence and associativity, all at once. These
are called "precedence declarations". *Note Operator Precedence:
Precedence, for general information on operator precedence.

The syntax of a precedence declaration is the same as that of
`%token': either

%left SYMBOLS...

or

%left SYMBOLS...

And indeed any of these declarations serves the purposes of `%token'.
But in addition, they specify the associativity and relative precedence
for all the SYMBOLS:

* The associativity of an operator OP determines how repeated uses
of the operator nest: whether `X OP Y OP Z' is parsed by grouping
X with Y first or by grouping Y with Z first. `%left' specifies
left-associativity (grouping X with Y first) and `%right'
specifies right-associativity (grouping Y with Z first).
`%nonassoc' specifies no associativity, which means that `X OP Y
OP Z' is considered a syntax error.

* The precedence of an operator determines how it nests with other
operators. All the tokens declared in a single precedence
declaration have equal precedence and nest together according to
their associativity. When two tokens declared in different
precedence declarations associate, the one declared later has the
higher precedence and is grouped first.


File: bison.info, Node: Union Decl, Next: Type Decl, Prev: Precedence Decl, Up: Declarations

The Collection of Value Types
-----------------------------

The `%union' declaration specifies the entire collection of possible
data types for semantic values. The keyword `%union' is followed by a
pair of braces containing the same thing that goes inside a `union' in
C.

For example:

%union {
double val;
symrec *tptr;
}

This says that the two alternative types are `double' and `symrec *'.
They are given names `val' and `tptr'; these names are used in the
`%token' and `%type' declarations to pick one of the types for a
terminal or nonterminal symbol (*note Nonterminal Symbols: Type Decl.).

Note that, unlike making a `union' declaration in C, you do not write
a semicolon after the closing brace.


File: bison.info, Node: Type Decl, Next: Expect Decl, Prev: Union Decl, Up: Declarations

Nonterminal Symbols
-------------------

When you use `%union' to specify multiple value types, you must declare
the value type of each nonterminal symbol for which values are used.
This is done with a `%type' declaration, like this:

%type NONTERMINAL...

Here NONTERMINAL is the name of a nonterminal symbol, and TYPE is the
name given in the `%union' to the alternative that you want (*note The
Collection of Value Types: Union Decl.). You can give any number of
nonterminal symbols in the same `%type' declaration, if they have the
same value type. Use spaces to separate the symbol names.


File: bison.info, Node: Expect Decl, Next: Start Decl, Prev: Type Decl, Up: Declarations

Suppressing Conflict Warnings
-----------------------------

Bison normally warns if there are any conflicts in the grammar
(*note Shift/Reduce Conflicts: Shift/Reduce.), but most real grammars
have harmless shift/reduce conflicts which are resolved in a
predictable way and would be difficult to eliminate. It is desirable
to suppress the warning about these conflicts unless the number of
conflicts changes. You can do this with the `%expect' declaration.

The declaration looks like this:

%expect N

Here N is a decimal integer. The declaration says there should be no
warning if there are N shift/reduce conflicts and no reduce/reduce
conflicts. The usual warning is given if there are either more or fewer
conflicts, or if there are any reduce/reduce conflicts.

In general, using `%expect' involves these steps:

* Compile your grammar without `%expect'. Use the `-v' option to
get a verbose list of where the conflicts occur. Bison will also
print the number of conflicts.

* Check each of the conflicts to make sure that Bison's default
resolution is what you really want. If not, rewrite the grammar
and go back to the beginning.

* Add an `%expect' declaration, copying the number N from the number
which Bison printed.

Now Bison will stop annoying you about the conflicts you have
checked, but it will warn you again if changes in the grammar result in
additional conflicts.


File: bison.info, Node: Start Decl, Next: Pure Decl, Prev: Expect Decl, Up: Declarations

The Start-Symbol
----------------

Bison assumes by default that the start symbol for the grammar is
the first nonterminal specified in the grammar specification section.
The programmer may override this restriction with the `%start'
declaration as follows:

%start SYMBOL


File: bison.info, Node: Pure Decl, Next: Decl Summary, Prev: Start Decl, Up: Declarations

A Pure (Reentrant) Parser
-------------------------

A "reentrant" program is one which does not alter in the course of
execution; in other words, it consists entirely of "pure" (read-only)
code. Reentrancy is important whenever asynchronous execution is
possible; for example, a nonreentrant program may not be safe to call
from a signal handler. In systems with multiple threads of control, a
nonreentrant program must be called only within interlocks.

The Bison parser is not normally a reentrant program, because it uses
statically allocated variables for communication with `yylex'. These
variables include `yylval' and `yylloc'.

The Bison declaration `%pure_parser' says that you want the parser
to be reentrant. It looks like this:

%pure_parser

The effect is that the two communication variables become local
variables in `yyparse', and a different calling convention is used for
the lexical analyzer function `yylex'. *Note Calling for Pure Parsers:
Pure Calling, for the details of this. The variable `yynerrs' also
becomes local in `yyparse' (*note The Error Reporting Function
`yyerror': Error Reporting.). The convention for calling `yyparse'
itself is unchanged.


File: bison.info, Node: Decl Summary, Prev: Pure Decl, Up: Declarations

Bison Declaration Summary
-------------------------

Here is a summary of all Bison declarations:

`%union'
Declare the collection of data types that semantic values may have
(*note The Collection of Value Types: Union Decl.).

`%token'
Declare a terminal symbol (token type name) with no precedence or
associativity specified (*note Token Type Names: Token Decl.).

`%right'
Declare a terminal symbol (token type name) that is
right-associative (*note Operator Precedence: Precedence Decl.).

`%left'
Declare a terminal symbol (token type name) that is
left-associative (*note Operator Precedence: Precedence Decl.).

`%nonassoc'
Declare a terminal symbol (token type name) that is nonassociative
(using it in a way that would be associative is a syntax error)
(*note Operator Precedence: Precedence Decl.).

`%type'
Declare the type of semantic values for a nonterminal symbol
(*note Nonterminal Symbols: Type Decl.).

`%start'
Specify the grammar's start symbol (*note The Start-Symbol: Start
Decl.).

`%expect'
Declare the expected number of shift-reduce conflicts (*note
Suppressing Conflict Warnings: Expect Decl.).

`%pure_parser'
Request a pure (reentrant) parser program (*note A Pure
(Reentrant) Parser: Pure Decl.).


File: bison.info, Node: Multiple Parsers, Prev: Declarations, Up: Grammar File

Multiple Parsers in the Same Program
====================================

Most programs that use Bison parse only one language and therefore
contain only one Bison parser. But what if you want to parse more than
one language with the same program? Then you need to avoid a name
conflict between different definitions of `yyparse', `yylval', and so
on.

The easy way to do this is to use the option `-p PREFIX' (*note
Invoking Bison: Invocation.). This renames the interface functions and
variables of the Bison parser to start with PREFIX instead of `yy'.
You can use this to give each parser distinct names that do not
conflict.

The precise list of symbols renamed is `yyparse', `yylex',
`yyerror', `yylval', `yychar' and `yydebug'. For example, if you use
`-p c', the names become `cparse', `clex', and so on.

*All the other variables and macros associated with Bison are not
renamed.* These others are not global; there is no conflict if the same
name is used in different parsers. For example, `YYSTYPE' is not
renamed, but defining this in different ways in different parsers causes
no trouble (*note Data Types of Semantic Values: Value Type.).

The `-p' option works by adding macro definitions to the beginning
of the parser source file, defining `yyparse' as `PREFIXparse', and so
on. This effectively substitutes one name for the other in the entire
parser file.


File: bison.info, Node: Interface, Next: Algorithm, Prev: Grammar File, Up: Top

Parser C-Language Interface
***************************

The Bison parser is actually a C function named `yyparse'. Here we
describe the interface conventions of `yyparse' and the other functions
that it needs to use.

Keep in mind that the parser uses many C identifiers starting with
`yy' and `YY' for internal purposes. If you use such an identifier
(aside from those in this manual) in an action or in additional C code
in the grammar file, you are likely to run into trouble.

* Menu:

* Parser Function:: How to call `yyparse' and what it returns.
* Lexical:: You must supply a function `yylex'
which reads tokens.
* Error Reporting:: You must supply a function `yyerror'.
* Action Features:: Special features for use in actions.


File: bison.info, Node: Parser Function, Next: Lexical, Up: Interface

The Parser Function `yyparse'
=============================

You call the function `yyparse' to cause parsing to occur. This
function reads tokens, executes actions, and ultimately returns when it
encounters end-of-input or an unrecoverable syntax error. You can also
write an action which directs `yyparse' to return immediately without
reading further.

The value returned by `yyparse' is 0 if parsing was successful
(return is due to end-of-input).

The value is 1 if parsing failed (return is due to a syntax error).

In an action, you can cause immediate return from `yyparse' by using
these macros:

`YYACCEPT'
Return immediately with value 0 (to report success).

`YYABORT'
Return immediately with value 1 (to report failure).


File: bison.info, Node: Lexical, Next: Error Reporting, Prev: Parser Function, Up: Interface

The Lexical Analyzer Function `yylex'
=====================================

The "lexical analyzer" function, `yylex', recognizes tokens from the
input stream and returns them to the parser. Bison does not create
this function automatically; you must write it so that `yyparse' can
call it. The function is sometimes referred to as a lexical scanner.

In simple programs, `yylex' is often defined at the end of the Bison
grammar file. If `yylex' is defined in a separate source file, you
need to arrange for the token-type macro definitions to be available
there. To do this, use the `-d' option when you run Bison, so that it
will write these macro definitions into a separate header file
`NAME.tab.h' which you can include in the other source files that need
it. *Note Invoking Bison: Invocation.

* Menu:

* Calling Convention:: How `yyparse' calls `yylex'.
* Token Values:: How `yylex' must return the semantic value
of the token it has read.
* Token Positions:: How `yylex' must return the text position
(line number, etc.) of the token, if the
actions want that.
* Pure Calling:: How the calling convention differs
in a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.).


File: bison.info, Node: Calling Convention, Next: Token Values, Up: Lexical

Calling Convention for `yylex'
------------------------------

The value that `yylex' returns must be the numeric code for the type
of token it has just found, or 0 for end-of-input.

When a token is referred to in the grammar rules by a name, that name
in the parser file becomes a C macro whose definition is the proper
numeric code for that token type. So `yylex' can use the name to
indicate that type. *Note Symbols::.

When a token is referred to in the grammar rules by a character
literal, the numeric code for that character is also the code for the
token type. So `yylex' can simply return that character code. The
null character must not be used this way, because its code is zero and
that is what signifies end-of-input.

Here is an example showing these things:

yylex ()
{
...
if (c == EOF) /* Detect end of file. */
return 0;
...
if (c == '+' || c == '-')
return c; /* Assume token type for `+' is '+'. */
...
return INT; /* Return the type of the token. */
...
}

This interface has been designed so that the output from the `lex'
utility can be used without change as the definition of `yylex'.


File: bison.info, Node: Token Values, Next: Token Positions, Prev: Calling Convention, Up: Lexical

Semantic Values of Tokens
-------------------------

In an ordinary (nonreentrant) parser, the semantic value of the
token must be stored into the global variable `yylval'. When you are
using just one data type for semantic values, `yylval' has that type.
Thus, if the type is `int' (the default), you might write this in
`yylex':

...
yylval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
...

When you are using multiple data types, `yylval''s type is a union
made from the `%union' declaration (*note The Collection of Value
Types: Union Decl.). So when you store a token's value, you must use
the proper member of the union. If the `%union' declaration looks like
this:

%union {
int intval;
double val;
symrec *tptr;
}

then the code in `yylex' might look like this:

...
yylval.intval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
...


File: bison.info, Node: Token Positions, Next: Pure Calling, Prev: Token Values, Up: Lexical

Textual Positions of Tokens
---------------------------

If you are using the `@N'-feature (*note Special Features for Use in
Actions: Action Features.) in actions to keep track of the textual
locations of tokens and groupings, then you must provide this
information in `yylex'. The function `yyparse' expects to find the
textual location of a token just parsed in the global variable
`yylloc'. So `yylex' must store the proper data in that variable. The
value of `yylloc' is a structure and you need only initialize the
members that are going to be used by the actions. The four members are
called `first_line', `first_column', `last_line' and `last_column'.
Note that the use of this feature makes the parser noticeably slower.

The data type of `yylloc' has the name `YYLTYPE'.


File: bison.info, Node: Pure Calling, Prev: Token Positions, Up: Lexical

Calling for Pure Parsers
------------------------

When you use the Bison declaration `%pure_parser' to request a pure,
reentrant parser, the global communication variables `yylval' and
`yylloc' cannot be used. (*Note A Pure (Reentrant) Parser: Pure Decl.)
In such parsers the two global variables are replaced by pointers
passed as arguments to `yylex'. You must declare them as shown here,
and pass the information back by storing it through those pointers.

yylex (lvalp, llocp)
YYSTYPE *lvalp;
YYLTYPE *llocp;
{
...
*lvalp = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
...
}

If the grammar file does not use the `@' constructs to refer to
textual positions, then the type `YYLTYPE' will not be defined. In
this case, omit the second argument; `yylex' will be called with only
one argument.


File: bison.info, Node: Error Reporting, Next: Action Features, Prev: Lexical, Up: Interface

The Error Reporting Function `yyerror'
======================================

The Bison parser detects a "parse error" or "syntax error" whenever
it reads a token which cannot satisfy any syntax rule. A action in the
grammar can also explicitly proclaim an error, using the macro
`YYERROR' (*note Special Features for Use in Actions: Action Features.).

The Bison parser expects to report the error by calling an error
reporting function named `yyerror', which you must supply. It is
called by `yyparse' whenever a syntax error is found, and it receives
one argument. For a parse error, the string is normally
`"parse error"'.

If you define the macro `YYERROR_VERBOSE' in the Bison declarations
section (*note The Bison Declarations Section: Bison Declarations.),
then Bison provides a more verbose and specific error message string
instead of just plain `"parse error"'. It doesn't matter what
definition you use for `YYERROR_VERBOSE', just whether you define it.

The parser can detect one other kind of error: stack overflow. This
happens when the input contains constructions that are very deeply
nested. It isn't likely you will encounter this, since the Bison
parser extends its stack automatically up to a very large limit. But
if overflow happens, `yyparse' calls `yyerror' in the usual fashion,
except that the argument string is `"parser stack overflow"'.

The following definition suffices in simple programs:

yyerror (s)
char *s;
{
fprintf (stderr, "%s\n", s);
}

After `yyerror' returns to `yyparse', the latter will attempt error
recovery if you have written suitable error recovery grammar rules
(*note Error Recovery::.). If recovery is impossible, `yyparse' will
immediately return 1.

The variable `yynerrs' contains the number of syntax errors
encountered so far. Normally this variable is global; but if you
request a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.)
then it is a local variable which only the actions can access.


File: bison.info, Node: Action Features, Prev: Error Reporting, Up: Interface

Special Features for Use in Actions
===================================

Here is a table of Bison constructs, variables and macros that are
useful in actions.

`$$'
Acts like a variable that contains the semantic value for the
grouping made by the current rule. *Note Actions::.

`$N'
Acts like a variable that contains the semantic value for the Nth
component of the current rule. *Note Actions::.

`$$'
Like `$$' but specifies alternative TYPEALT in the union specified
by the `%union' declaration. *Note Data Types of Values in
Actions: Action Types.

`$N'
Like `$N' but specifies alternative TYPEALT in the union specified
by the `%union' declaration. *Note Data Types of Values in
Actions: Action Types.

`YYABORT;'
Return immediately from `yyparse', indicating failure. *Note The
Parser Function `yyparse': Parser Function.

`YYACCEPT;'
Return immediately from `yyparse', indicating success. *Note The
Parser Function `yyparse': Parser Function.

`YYBACKUP (TOKEN, VALUE);'
Unshift a token. This macro is allowed only for rules that reduce
a single value, and only when there is no look-ahead token. It
installs a look-ahead token with token type TOKEN and semantic
value VALUE; then it discards the value that was going to be
reduced by this rule.

If the macro is used when it is not valid, such as when there is a
look-ahead token already, then it reports a syntax error with a
message `cannot back up' and performs ordinary error recovery.

In either case, the rest of the action is not executed.

`YYEMPTY'
Value stored in `yychar' when there is no look-ahead token.

`YYERROR;'
Cause an immediate syntax error. This statement initiates error
recovery just as if the parser itself had detected an error;
however, it does not call `yyerror', and does not print any
message. If you want to print an error message, call `yyerror'
explicitly before the `YYERROR;' statement. *Note Error
Recovery::.

`YYRECOVERING'
This macro stands for an expression that has the value 1 when the
parser is recovering from a syntax error, and 0 the rest of the
time. *Note Error Recovery::.

`yychar'
Variable containing the current look-ahead token. (In a pure
parser, this is actually a local variable within `yyparse'.) When
there is no look-ahead token, the value `YYEMPTY' is stored in the
variable. *Note Look-Ahead Tokens: Look-Ahead.

`yyclearin;'
Discard the current look-ahead token. This is useful primarily in
error rules. *Note Error Recovery::.

`yyerrok;'
Resume generating error messages immediately for subsequent syntax
errors. This is useful primarily in error rules. *Note Error
Recovery::.

`@N'
Acts like a structure variable containing information on the line
numbers and column numbers of the Nth component of the current
rule. The structure has four members, like this:

struct {
int first_line, last_line;
int first_column, last_column;
};

Thus, to get the starting line number of the third component, use
`@3.first_line'.

In order for the members of this structure to contain valid
information, you must make `yylex' supply this information about
each token. If you need only certain members, then `yylex' need
only fill in those members.

The use of this feature makes the parser noticeably slower.


File: bison.info, Node: Algorithm, Next: Error Recovery, Prev: Interface, Up: Top

The Bison Parser Algorithm
**************************

As Bison reads tokens, it pushes them onto a stack along with their
semantic values. The stack is called the "parser stack". Pushing a
token is traditionally called "shifting".

For example, suppose the infix calculator has read `1 + 5 *', with a
`3' to come. The stack will have four elements, one for each token
that was shifted.

But the stack does not always have an element for each token read.
When the last N tokens and groupings shifted match the components of a
grammar rule, they can be combined according to that rule. This is
called "reduction". Those tokens and groupings are replaced on the
stack by a single grouping whose symbol is the result (left hand side)
of that rule. Running the rule's action is part of the process of
reduction, because this is what computes the semantic value of the
resulting grouping.

For example, if the infix calculator's parser stack contains this:

1 + 5 * 3

and the next input token is a newline character, then the last three
elements can be reduced to 15 via the rule:

expr: expr '*' expr;

Then the stack contains just these three elements:

1 + 15

At this point, another reduction can be made, resulting in the single
value 16. Then the newline token can be shifted.

The parser tries, by shifts and reductions, to reduce the entire
input down to a single grouping whose symbol is the grammar's
start-symbol (*note Languages and Context-Free Grammars: Language and
Grammar.).

This kind of parser is known in the literature as a bottom-up parser.

* Menu:

* Look-Ahead:: Parser looks one token ahead when deciding what to do.
* Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
* Precedence:: Operator precedence works by resolving conflicts.
* Contextual Precedence:: When an operator's precedence depends on context.
* Parser States:: The parser is a finite-state-machine with stack.
* Reduce/Reduce:: When two rules are applicable in the same situation.
* Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
* Stack Overflow:: What happens when stack gets full. How to avoid it.


File: bison.info, Node: Look-Ahead, Next: Shift/Reduce, Up: Algorithm

Look-Ahead Tokens
=================

The Bison parser does *not* always reduce immediately as soon as the
last N tokens and groupings match a rule. This is because such a
simple strategy is inadequate to handle most languages. Instead, when a
reduction is possible, the parser sometimes "looks ahead" at the next
token in order to decide what to do.

When a token is read, it is not immediately shifted; first it
becomes the "look-ahead token", which is not on the stack. Now the
parser can perform one or more reductions of tokens and groupings on
the stack, while the look-ahead token remains off to the side. When no
more reductions should take place, the look-ahead token is shifted onto
the stack. This does not mean that all possible reductions have been
done; depending on the token type of the look-ahead token, some rules
may choose to delay their application.

Here is a simple case where look-ahead is needed. These three rules
define expressions which contain binary addition operators and postfix
unary factorial operators (`!'), and allow parentheses for grouping.

expr: term '+' expr
| term
;

term: '(' expr ')'
| term '!'
| NUMBER
;

Suppose that the tokens `1 + 2' have been read and shifted; what
should be done? If the following token is `)', then the first three
tokens must be reduced to form an `expr'. This is the only valid
course, because shifting the `)' would produce a sequence of symbols
`term ')'', and no rule allows this.

If the following token is `!', then it must be shifted immediately so
that `2 !' can be reduced to make a `term'. If instead the parser were
to reduce before shifting, `1 + 2' would become an `expr'. It would
then be impossible to shift the `!' because doing so would produce on
the stack the sequence of symbols `expr '!''. No rule allows that
sequence.

The current look-ahead token is stored in the variable `yychar'.
*Note Special Features for Use in Actions: Action Features.


File: bison.info, Node: Shift/Reduce, Next: Precedence, Prev: Look-Ahead, Up: Algorithm

Shift/Reduce Conflicts
======================

Suppose we are parsing a language which has if-then and if-then-else
statements, with a pair of rules like this:

if_stmt:
IF expr THEN stmt
| IF expr THEN stmt ELSE stmt
;

Here we assume that `IF', `THEN' and `ELSE' are terminal symbols for
specific keyword tokens.

When the `ELSE' token is read and becomes the look-ahead token, the
contents of the stack (assuming the input is valid) are just right for
reduction by the first rule. But it is also legitimate to shift the
`ELSE', because that would lead to eventual reduction by the second
rule.

This situation, where either a shift or a reduction would be valid,
is called a "shift/reduce conflict". Bison is designed to resolve
these conflicts by choosing to shift, unless otherwise directed by
operator precedence declarations. To see the reason for this, let's
contrast it with the other alternative.

Since the parser prefers to shift the `ELSE', the result is to attach
the else-clause to the innermost if-statement, making these two inputs
equivalent:

if x then if y then win (); else lose;

if x then do; if y then win (); else lose; end;

But if the parser chose to reduce when possible rather than shift,
the result would be to attach the else-clause to the outermost
if-statement, making these two inputs equivalent:

if x then if y then win (); else lose;

if x then do; if y then win (); end; else lose;

The conflict exists because the grammar as written is ambiguous:
either parsing of the simple nested if-statement is legitimate. The
established convention is that these ambiguities are resolved by
attaching the else-clause to the innermost if-statement; this is what
Bison accomplishes by choosing to shift rather than reduce. (It would
ideally be cleaner to write an unambiguous grammar, but that is very
hard to do in this case.) This particular ambiguity was first
encountered in the specifications of Algol 60 and is called the
"dangling `else'" ambiguity.

To avoid warnings from Bison about predictable, legitimate
shift/reduce conflicts, use the `%expect N' declaration. There will be
no warning as long as the number of shift/reduce conflicts is exactly N.
*Note Suppressing Conflict Warnings: Expect Decl.

The definition of `if_stmt' above is solely to blame for the
conflict, but the conflict does not actually appear without additional
rules. Here is a complete Bison input file that actually manifests the
conflict:

%token IF THEN ELSE variable
%%
stmt: expr
| if_stmt
;

if_stmt:
IF expr THEN stmt
| IF expr THEN stmt ELSE stmt
;

expr: variable
;


File: bison.info, Node: Precedence, Next: Contextual Precedence, Prev: Shift/Reduce, Up: Algorithm

Operator Precedence
===================

Another situation where shift/reduce conflicts appear is in
arithmetic expressions. Here shifting is not always the preferred
resolution; the Bison declarations for operator precedence allow you to
specify when to shift and when to reduce.

* Menu:

* Why Precedence:: An example showing why precedence is needed.
* Using Precedence:: How to specify precedence in Bison grammars.
* Precedence Examples:: How these features are used in the previous example.
* How Precedence:: How they work.


File: bison.info, Node: Why Precedence, Next: Using Precedence, Up: Precedence

When Precedence is Needed
-------------------------

Consider the following ambiguous grammar fragment (ambiguous because
the input `1 - 2 * 3' can be parsed in two different ways):

expr: expr '-' expr
| expr '*' expr
| expr '<' expr
| '(' expr ')'
...
;

Suppose the parser has seen the tokens `1', `-' and `2'; should it
reduce them via the rule for the addition operator? It depends on the
next token. Of course, if the next token is `)', we must reduce;
shifting is invalid because no single rule can reduce the token
sequence `- 2 )' or anything starting with that. But if the next token
is `*' or `<', we have a choice: either shifting or reduction would
allow the parse to complete, but with different results.

To decide which one Bison should do, we must consider the results.
If the next operator token OP is shifted, then it must be reduced first
in order to permit another opportunity to reduce the sum. The result
is (in effect) `1 - (2 OP 3)'. On the other hand, if the subtraction
is reduced before shifting OP, the result is `(1 - 2) OP 3'. Clearly,
then, the choice of shift or reduce should depend on the relative
precedence of the operators `-' and OP: `*' should be shifted first,
but not `<'.

What about input such as `1 - 2 - 5'; should this be `(1 - 2) - 5'
or should it be `1 - (2 - 5)'? For most operators we prefer the
former, which is called "left association". The latter alternative,
"right association", is desirable for assignment operators. The choice
of left or right association is a matter of whether the parser chooses
to shift or reduce when the stack contains `1 - 2' and the look-ahead
token is `-': shifting makes right-associativity.


File: bison.info, Node: Using Precedence, Next: Precedence Examples, Prev: Why Precedence, Up: Precedence

Specifying Operator Precedence
------------------------------

Bison allows you to specify these choices with the operator
precedence declarations `%left' and `%right'. Each such declaration
contains a list of tokens, which are operators whose precedence and
associativity is being declared. The `%left' declaration makes all
those operators left-associative and the `%right' declaration makes
them right-associative. A third alternative is `%nonassoc', which
declares that it is a syntax error to find the same operator twice "in a
row".

The relative precedence of different operators is controlled by the
order in which they are declared. The first `%left' or `%right'
declaration in the file declares the operators whose precedence is
lowest, the next such declaration declares the operators whose
precedence is a little higher, and so on.


File: bison.info, Node: Precedence Examples, Next: How Precedence, Prev: Using Precedence, Up: Precedence

Precedence Examples
-------------------

In our example, we would want the following declarations:

%left '<'
%left '-'
%left '*'

In a more complete example, which supports other operators as well,
we would declare them in groups of equal precedence. For example,
`'+'' is declared with `'-'':

%left '<' '>' '=' NE LE GE
%left '+' '-'
%left '*' '/'

(Here `NE' and so on stand for the operators for "not equal" and so on.
We assume that these tokens are more than one character long and
therefore are represented by names, not character literals.)


File: bison.info, Node: How Precedence, Prev: Precedence Examples, Up: Precedence

How Precedence Works
--------------------

The first effect of the precedence declarations is to assign
precedence levels to the terminal symbols declared. The second effect
is to assign precedence levels to certain rules: each rule gets its
precedence from the last terminal symbol mentioned in the components.
(You can also specify explicitly the precedence of a rule. *Note
Context-Dependent Precedence: Contextual Precedence.)

Finally, the resolution of conflicts works by comparing the
precedence of the rule being considered with that of the look-ahead
token. If the token's precedence is higher, the choice is to shift.
If the rule's precedence is higher, the choice is to reduce. If they
have equal precedence, the choice is made based on the associativity of
that precedence level. The verbose output file made by `-v' (*note
Invoking Bison: Invocation.) says how each conflict was resolved.

Not all rules and not all tokens have precedence. If either the
rule or the look-ahead token has no precedence, then the default is to
shift.


File: bison.info, Node: Contextual Precedence, Next: Parser States, Prev: Precedence, Up: Algorithm

Context-Dependent Precedence
============================

Often the precedence of an operator depends on the context. This
sounds outlandish at first, but it is really very common. For example,
a minus sign typically has a very high precedence as a unary operator,
and a somewhat lower precedence (lower than multiplication) as a binary
operator.

The Bison precedence declarations, `%left', `%right' and
`%nonassoc', can only be used once for a given token; so a token has
only one precedence declared in this way. For context-dependent
precedence, you need to use an additional mechanism: the `%prec'
modifier for rules.

The `%prec' modifier declares the precedence of a particular rule by
specifying a terminal symbol whose precedence should be used for that
rule. It's not necessary for that symbol to appear otherwise in the
rule. The modifier's syntax is:

%prec TERMINAL-SYMBOL

and it is written after the components of the rule. Its effect is to
assign the rule the precedence of TERMINAL-SYMBOL, overriding the
precedence that would be deduced for it in the ordinary way. The
altered rule precedence then affects how conflicts involving that rule
are resolved (*note Operator Precedence: Precedence.).

Here is how `%prec' solves the problem of unary minus. First,
declare a precedence for a fictitious terminal symbol named `UMINUS'.
There are no tokens of this type, but the symbol serves to stand for its
precedence:

...
%left '+' '-'
%left '*'
%left UMINUS

Now the precedence of `UMINUS' can be used in specific rules:

exp: ...
| exp '-' exp
...
| '-' exp %prec UMINUS


File: bison.info, Node: Parser States, Next: Reduce/Reduce, Prev: Contextual Precedence, Up: Algorithm

Parser States
=============

The function `yyparse' is implemented using a finite-state machine.
The values pushed on the parser stack are not simply token type codes;
they represent the entire sequence of terminal and nonterminal symbols
at or near the top of the stack. The current state collects all the
information about previous input which is relevant to deciding what to
do next.

Each time a look-ahead token is read, the current parser state
together with the type of look-ahead token are looked up in a table.
This table entry can say, "Shift the look-ahead token." In this case,
it also specifies the new parser state, which is pushed onto the top of
the parser stack. Or it can say, "Reduce using rule number N." This
means that a certain number of tokens or groupings are taken off the
top of the stack, and replaced by one grouping. In other words, that
number of states are popped from the stack, and one new state is pushed.

There is one other alternative: the table can say that the
look-ahead token is erroneous in the current state. This causes error
processing to begin (*note Error Recovery::.).


File: bison.info, Node: Reduce/Reduce, Next: Mystery Conflicts, Prev: Parser States, Up: Algorithm

Reduce/Reduce Conflicts
=======================

A reduce/reduce conflict occurs if there are two or more rules that
apply to the same sequence of input. This usually indicates a serious
error in the grammar.

For example, here is an erroneous attempt to define a sequence of
zero or more `word' groupings.

sequence: /* empty */
{ printf ("empty sequence\n"); }
| maybeword
| sequence word
{ printf ("added word %s\n", $2); }
;

maybeword: /* empty */
{ printf ("empty maybeword\n"); }
| word
{ printf ("single word %s\n", $1); }
;

The error is an ambiguity: there is more than one way to parse a single
`word' into a `sequence'. It could be reduced to a `maybeword' and
then into a `sequence' via the second rule. Alternatively,
nothing-at-all could be reduced into a `sequence' via the first rule,
and this could be combined with the `word' using the third rule for
`sequence'.

There is also more than one way to reduce nothing-at-all into a
`sequence'. This can be done directly via the first rule, or
indirectly via `maybeword' and then the second rule.

You might think that this is a distinction without a difference,
because it does not change whether any particular input is valid or
not. But it does affect which actions are run. One parsing order runs
the second rule's action; the other runs the first rule's action and
the third rule's action. In this example, the output of the program
changes.

Bison resolves a reduce/reduce conflict by choosing to use the rule
that appears first in the grammar, but it is very risky to rely on
this. Every reduce/reduce conflict must be studied and usually
eliminated. Here is the proper way to define `sequence':

sequence: /* empty */
{ printf ("empty sequence\n"); }
| sequence word
{ printf ("added word %s\n", $2); }
;

Here is another common error that yields a reduce/reduce conflict:

sequence: /* empty */
| sequence words
| sequence redirects
;

words: /* empty */
| words word
;

redirects:/* empty */
| redirects redirect
;

The intention here is to define a sequence which can contain either
`word' or `redirect' groupings. The individual definitions of
`sequence', `words' and `redirects' are error-free, but the three
together make a subtle ambiguity: even an empty input can be parsed in
infinitely many ways!

Consider: nothing-at-all could be a `words'. Or it could be two
`words' in a row, or three, or any number. It could equally well be a
`redirects', or two, or any number. Or it could be a `words' followed
by three `redirects' and another `words'. And so on.

Here are two ways to correct these rules. First, to make it a
single level of sequence:

sequence: /* empty */
| sequence word
| sequence redirect
;

Second, to prevent either a `words' or a `redirects' from being
empty:

sequence: /* empty */
| sequence words
| sequence redirects
;

words: word
| words word
;

redirects:redirect
| redirects redirect
;

bison-1.22/bison.info-4100666 12120 1 143740 5413126475 13250 0ustar rmsdaemonThis is Info file bison.info, produced by Makeinfo-1.54 from the input
file /home/gd2/gnu/bison/bison.texinfo.

This file documents the Bison parser generator.

Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Bison" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the sections entitled "GNU General Public
License", "Conditions for Using Bison" and this permission notice may be
included in translations approved by the Free Software Foundation
instead of in the original English.


File: bison.info, Node: Mystery Conflicts, Next: Stack Overflow, Prev: Reduce/Reduce, Up: Algorithm

Mysterious Reduce/Reduce Conflicts
==================================

Sometimes reduce/reduce conflicts can occur that don't look
warranted. Here is an example:

%token ID

%%
def: param_spec return_spec ','
;
param_spec:
type
| name_list ':' type
;
return_spec:
type
| name ':' type
;
type: ID
;
name: ID
;
name_list:
name
| name ',' name_list
;

It would seem that this grammar can be parsed with only a single
token of look-ahead: when a `param_spec' is being read, an `ID' is a
`name' if a comma or colon follows, or a `type' if another `ID'
follows. In other words, this grammar is LR(1).

However, Bison, like most parser generators, cannot actually handle
all LR(1) grammars. In this grammar, two contexts, that after an `ID'
at the beginning of a `param_spec' and likewise at the beginning of a
`return_spec', are similar enough that Bison assumes they are the same.
They appear similar because the same set of rules would be active--the
rule for reducing to a `name' and that for reducing to a `type'. Bison
is unable to determine at that stage of processing that the rules would
require different look-ahead tokens in the two contexts, so it makes a
single parser state for them both. Combining the two contexts causes a
conflict later. In parser terminology, this occurrence means that the
grammar is not LALR(1).

In general, it is better to fix deficiencies than to document them.
But this particular deficiency is intrinsically hard to fix; parser
generators that can handle LR(1) grammars are hard to write and tend to
produce parsers that are very large. In practice, Bison is more useful
as it is now.

When the problem arises, you can often fix it by identifying the two
parser states that are being confused, and adding something to make them
look distinct. In the above example, adding one rule to `return_spec'
as follows makes the problem go away:

%token BOGUS
...
%%
...
return_spec:
type
| name ':' type
/* This rule is never used. */
| ID BOGUS
;

This corrects the problem because it introduces the possibility of an
additional active rule in the context after the `ID' at the beginning of
`return_spec'. This rule is not active in the corresponding context in
a `param_spec', so the two contexts receive distinct parser states. As
long as the token `BOGUS' is never generated by `yylex', the added rule
cannot alter the way actual input is parsed.

In this particular example, there is another way to solve the
problem: rewrite the rule for `return_spec' to use `ID' directly
instead of via `name'. This also causes the two confusing contexts to
have different sets of active rules, because the one for `return_spec'
activates the altered rule for `return_spec' rather than the one for
`name'.

param_spec:
type
| name_list ':' type
;
return_spec:
type
| ID ':' type
;


File: bison.info, Node: Stack Overflow, Prev: Mystery Conflicts, Up: Algorithm

Stack Overflow, and How to Avoid It
===================================

The Bison parser stack can overflow if too many tokens are shifted
and not reduced. When this happens, the parser function `yyparse'
returns a nonzero value, pausing only to call `yyerror' to report the
overflow.

By defining the macro `YYMAXDEPTH', you can control how deep the
parser stack can become before a stack overflow occurs. Define the
macro with a value that is an integer. This value is the maximum number
of tokens that can be shifted (and not reduced) before overflow. It
must be a constant expression whose value is known at compile time.

The stack space allowed is not necessarily allocated. If you
specify a large value for `YYMAXDEPTH', the parser actually allocates a
small stack at first, and then makes it bigger by stages as needed.
This increasing allocation happens automatically and silently.
Therefore, you do not need to make `YYMAXDEPTH' painfully small merely
to save space for ordinary inputs that do not need much stack.

The default value of `YYMAXDEPTH', if you do not define it, is 10000.

You can control how much stack is allocated initially by defining the
macro `YYINITDEPTH'. This value too must be a compile-time constant
integer. The default is 200.


File: bison.info, Node: Error Recovery, Next: Context Dependency, Prev: Algorithm, Up: Top

Error Recovery
**************

It is not usually acceptable to have a program terminate on a parse
error. For example, a compiler should recover sufficiently to parse the
rest of the input file and check it for errors; a calculator should
accept another expression.

In a simple interactive command parser where each input is one line,
it may be sufficient to allow `yyparse' to return 1 on error and have
the caller ignore the rest of the input line when that happens (and
then call `yyparse' again). But this is inadequate for a compiler,
because it forgets all the syntactic context leading up to the error.
A syntax error deep within a function in the compiler input should not
cause the compiler to treat the following line like the beginning of a
source file.

You can define how to recover from a syntax error by writing rules to
recognize the special token `error'. This is a terminal symbol that is
always defined (you need not declare it) and reserved for error
handling. The Bison parser generates an `error' token whenever a
syntax error happens; if you have provided a rule to recognize this
token in the current context, the parse can continue.

For example:

stmnts: /* empty string */
| stmnts '\n'
| stmnts exp '\n'
| stmnts error '\n'

The fourth rule in this example says that an error followed by a
newline makes a valid addition to any `stmnts'.

What happens if a syntax error occurs in the middle of an `exp'? The
error recovery rule, interpreted strictly, applies to the precise
sequence of a `stmnts', an `error' and a newline. If an error occurs in
the middle of an `exp', there will probably be some additional tokens
and subexpressions on the stack after the last `stmnts', and there will
be tokens to read before the next newline. So the rule is not
applicable in the ordinary way.

But Bison can force the situation to fit the rule, by discarding
part of the semantic context and part of the input. First it discards
states and objects from the stack until it gets back to a state in
which the `error' token is acceptable. (This means that the
subexpressions already parsed are discarded, back to the last complete
`stmnts'.) At this point the `error' token can be shifted. Then, if
the old look-ahead token is not acceptable to be shifted next, the
parser reads tokens and discards them until it finds a token which is
acceptable. In this example, Bison reads and discards input until the
next newline so that the fourth rule can apply.

The choice of error rules in the grammar is a choice of strategies
for error recovery. A simple and useful strategy is simply to skip the
rest of the current input line or current statement if an error is
detected:

stmnt: error ';' /* on error, skip until ';' is read */

It is also useful to recover to the matching close-delimiter of an
opening-delimiter that has already been parsed. Otherwise the
close-delimiter will probably appear to be unmatched, and generate
another, spurious error message:

primary: '(' expr ')'
| '(' error ')'
...
;

Error recovery strategies are necessarily guesses. When they guess
wrong, one syntax error often leads to another. In the above example,
the error recovery rule guesses that an error is due to bad input
within one `stmnt'. Suppose that instead a spurious semicolon is
inserted in the middle of a valid `stmnt'. After the error recovery
rule recovers from the first error, another syntax error will be found
straightaway, since the text following the spurious semicolon is also
an invalid `stmnt'.

To prevent an outpouring of error messages, the parser will output
no error message for another syntax error that happens shortly after
the first; only after three consecutive input tokens have been
successfully shifted will error messages resume.

Note that rules which accept the `error' token may have actions, just
as any other rules can.

You can make error messages resume immediately by using the macro
`yyerrok' in an action. If you do this in the error rule's action, no
error messages will be suppressed. This macro requires no arguments;
`yyerrok;' is a valid C statement.

The previous look-ahead token is reanalyzed immediately after an
error. If this is unacceptable, then the macro `yyclearin' may be used
to clear this token. Write the statement `yyclearin;' in the error
rule's action.

For example, suppose that on a parse error, an error handling
routine is called that advances the input stream to some point where
parsing should once again commence. The next symbol returned by the
lexical scanner is probably correct. The previous look-ahead token
ought to be discarded with `yyclearin;'.

The macro `YYRECOVERING' stands for an expression that has the value
1 when the parser is recovering from a syntax error, and 0 the rest of
the time. A value of 1 indicates that error messages are currently
suppressed for new syntax errors.


File: bison.info, Node: Context Dependency, Next: Debugging, Prev: Error Recovery, Up: Top

Handling Context Dependencies
*****************************

The Bison paradigm is to parse tokens first, then group them into
larger syntactic units. In many languages, the meaning of a token is
affected by its context. Although this violates the Bison paradigm,
certain techniques (known as "kludges") may enable you to write Bison
parsers for such languages.

* Menu:

* Semantic Tokens:: Token parsing can depend on the semantic context.
* Lexical Tie-ins:: Token parsing can depend on the syntactic context.
* Tie-in Recovery:: Lexical tie-ins have implications for how
error recovery rules must be written.

(Actually, "kludge" means any technique that gets its job done but is
neither clean nor robust.)


File: bison.info, Node: Semantic Tokens, Next: Lexical Tie-ins, Up: Context Dependency

Semantic Info in Token Types
============================

The C language has a context dependency: the way an identifier is
used depends on what its current meaning is. For example, consider
this:

foo (x);

This looks like a function call statement, but if `foo' is a typedef
name, then this is actually a declaration of `x'. How can a Bison
parser for C decide how to parse this input?

The method used in GNU C is to have two different token types,
`IDENTIFIER' and `TYPENAME'. When `yylex' finds an identifier, it
looks up the current declaration of the identifier in order to decide
which token type to return: `TYPENAME' if the identifier is declared as
a typedef, `IDENTIFIER' otherwise.

The grammar rules can then express the context dependency by the
choice of token type to recognize. `IDENTIFIER' is accepted as an
expression, but `TYPENAME' is not. `TYPENAME' can start a declaration,
but `IDENTIFIER' cannot. In contexts where the meaning of the
identifier is *not* significant, such as in declarations that can
shadow a typedef name, either `TYPENAME' or `IDENTIFIER' is
accepted--there is one rule for each of the two token types.

This technique is simple to use if the decision of which kinds of
identifiers to allow is made at a place close to where the identifier is
parsed. But in C this is not always so: C allows a declaration to
redeclare a typedef name provided an explicit type has been specified
earlier:

typedef int foo, bar, lose;
static foo (bar); /* redeclare `bar' as static variable */
static int foo (lose); /* redeclare `foo' as function */

Unfortunately, the name being declared is separated from the
declaration construct itself by a complicated syntactic structure--the
"declarator".

As a result, the part of Bison parser for C needs to be duplicated,
with all the nonterminal names changed: once for parsing a declaration
in which a typedef name can be redefined, and once for parsing a
declaration in which that can't be done. Here is a part of the
duplication, with actions omitted for brevity:

initdcl:
declarator maybeasm '='
init
| declarator maybeasm
;

notype_initdcl:
notype_declarator maybeasm '='
init
| notype_declarator maybeasm
;

Here `initdcl' can redeclare a typedef name, but `notype_initdcl'
cannot. The distinction between `declarator' and `notype_declarator'
is the same sort of thing.

There is some similarity between this technique and a lexical tie-in
(described next), in that information which alters the lexical analysis
is changed during parsing by other parts of the program. The
difference is here the information is global, and is used for other
purposes in the program. A true lexical tie-in has a special-purpose
flag controlled by the syntactic context.


File: bison.info, Node: Lexical Tie-ins, Next: Tie-in Recovery, Prev: Semantic Tokens, Up: Context Dependency

Lexical Tie-ins
===============

One way to handle context-dependency is the "lexical tie-in": a flag
which is set by Bison actions, whose purpose is to alter the way tokens
are parsed.

For example, suppose we have a language vaguely like C, but with a
special construct `hex (HEX-EXPR)'. After the keyword `hex' comes an
expression in parentheses in which all integers are hexadecimal. In
particular, the token `a1b' must be treated as an integer rather than
as an identifier if it appears in that context. Here is how you can do
it:

%{
int hexflag;
%}
%%
...
expr: IDENTIFIER
| constant
| HEX '('
{ hexflag = 1; }
expr ')'
{ hexflag = 0;
$$ = $4; }
| expr '+' expr
{ $$ = make_sum ($1, $3); }
...
;

constant:
INTEGER
| STRING
;

Here we assume that `yylex' looks at the value of `hexflag'; when it is
nonzero, all integers are parsed in hexadecimal, and tokens starting
with letters are parsed as integers if possible.

The declaration of `hexflag' shown in the C declarations section of
the parser file is needed to make it accessible to the actions (*note
The C Declarations Section: C Declarations.). You must also write the
code in `yylex' to obey the flag.


File: bison.info, Node: Tie-in Recovery, Prev: Lexical Tie-ins, Up: Context Dependency

Lexical Tie-ins and Error Recovery
==================================

Lexical tie-ins make strict demands on any error recovery rules you
have. *Note Error Recovery::.

The reason for this is that the purpose of an error recovery rule is
to abort the parsing of one construct and resume in some larger
construct. For example, in C-like languages, a typical error recovery
rule is to skip tokens until the next semicolon, and then start a new
statement, like this:

stmt: expr ';'
| IF '(' expr ')' stmt { ... }
...
error ';'
{ hexflag = 0; }
;

If there is a syntax error in the middle of a `hex (EXPR)'
construct, this error rule will apply, and then the action for the
completed `hex (EXPR)' will never run. So `hexflag' would remain set
for the entire rest of the input, or until the next `hex' keyword,
causing identifiers to be misinterpreted as integers.

To avoid this problem the error recovery rule itself clears
`hexflag'.

There may also be an error recovery rule that works within
expressions. For example, there could be a rule which applies within
parentheses and skips to the close-parenthesis:

expr: ...
| '(' expr ')'
{ $$ = $2; }
| '(' error ')'
...

If this rule acts within the `hex' construct, it is not going to
abort that construct (since it applies to an inner level of parentheses
within the construct). Therefore, it should not clear the flag: the
rest of the `hex' construct should be parsed with the flag still in
effect.

What if there is an error recovery rule which might abort out of the
`hex' construct or might not, depending on circumstances? There is no
way you can write the action to determine whether a `hex' construct is
being aborted or not. So if you are using a lexical tie-in, you had
better make sure your error recovery rules are not of this kind. Each
rule must be such that you can be sure that it always will, or always
won't, have to clear the flag.


File: bison.info, Node: Debugging, Next: Invocation, Prev: Context Dependency, Up: Top

Debugging Your Parser
*********************

If a Bison grammar compiles properly but doesn't do what you want
when it runs, the `yydebug' parser-trace feature can help you figure
out why.

To enable compilation of trace facilities, you must define the macro
`YYDEBUG' when you compile the parser. You could use `-DYYDEBUG=1' as
a compiler option or you could put `#define YYDEBUG 1' in the C
declarations section of the grammar file (*note The C Declarations
Section: C Declarations.). Alternatively, use the `-t' option when you
run Bison (*note Invoking Bison: Invocation.). We always define
`YYDEBUG' so that debugging is always possible.

The trace facility uses `stderr', so you must add
`#include ' to the C declarations section unless it is already
there.

Once you have compiled the program with trace facilities, the way to
request a trace is to store a nonzero value in the variable `yydebug'.
You can do this by making the C code do it (in `main', perhaps), or you
can alter the value with a C debugger.

Each step taken by the parser when `yydebug' is nonzero produces a
line or two of trace information, written on `stderr'. The trace
messages tell you these things:

* Each time the parser calls `yylex', what kind of token was read.

* Each time a token is shifted, the depth and complete contents of
the state stack (*note Parser States::.).

* Each time a rule is reduced, which rule it is, and the complete
contents of the state stack afterward.

To make sense of this information, it helps to refer to the listing
file produced by the Bison `-v' option (*note Invoking Bison:
Invocation.). This file shows the meaning of each state in terms of
positions in various rules, and also what each state will do with each
possible input token. As you read the successive trace messages, you
can see that the parser is functioning according to its specification
in the listing file. Eventually you will arrive at the place where
something undesirable happens, and you will see which parts of the
grammar are to blame.

The parser file is a C program and you can use C debuggers on it,
but it's not easy to interpret what it is doing. The parser function
is a finite-state machine interpreter, and aside from the actions it
executes the same code over and over. Only the values of variables
show where in the grammar it is working.

The debugging information normally gives the token type of each token
read, but not its semantic value. You can optionally define a macro
named `YYPRINT' to provide a way to print the value. If you define
`YYPRINT', it should take three arguments. The parser will pass a
standard I/O stream, the numeric code for the token type, and the token
value (from `yylval').

Here is an example of `YYPRINT' suitable for the multi-function
calculator (*note Declarations for `mfcalc': Mfcalc Decl.):

#define YYPRINT(file, type, value) yyprint (file, type, value)

static void
yyprint (file, type, value)
FILE *file;
int type;
YYSTYPE value;
{
if (type == VAR)
fprintf (file, " %s", value.tptr->name);
else if (type == NUM)
fprintf (file, " %d", value.val);
}


File: bison.info, Node: Invocation, Next: Table of Symbols, Prev: Debugging, Up: Top

Invoking Bison
**************

The usual way to invoke Bison is as follows:

bison INFILE

Here INFILE is the grammar file name, which usually ends in `.y'.
The parser file's name is made by replacing the `.y' with `.tab.c'.
Thus, the `bison foo.y' filename yields `foo.tab.c', and the `bison
hack/foo.y' filename yields `hack/foo.tab.c'.

* Menu:

* Bison Options:: All the options described in detail,
in alphabetical order by short options.
* Option Cross Key:: Alphabetical list of long options.
* VMS Invocation:: Bison command syntax on VMS.


File: bison.info, Node: Bison Options, Next: Option Cross Key, Up: Invocation

Bison Options
=============

Bison supports both traditional single-letter options and mnemonic
long option names. Long option names are indicated with `--' instead of
`-'. Abbreviations for option names are allowed as long as they are
unique. When a long option takes an argument, like `--file-prefix',
connect the option name and the argument with `='.

Here is a list of options that can be used with Bison, alphabetized
by short option. It is followed by a cross key alphabetized by long
option.

`-b FILE-PREFIX'
`--file-prefix=PREFIX'
Specify a prefix to use for all Bison output file names. The
names are chosen as if the input file were named `PREFIX.c'.

`-d'
`--defines'
Write an extra output file containing macro definitions for the
token type names defined in the grammar and the semantic value type
`YYSTYPE', as well as a few `extern' variable declarations.

If the parser output file is named `NAME.c' then this file is
named `NAME.h'.

This output file is essential if you wish to put the definition of
`yylex' in a separate source file, because `yylex' needs to be
able to refer to token type codes and the variable `yylval'.
*Note Semantic Values of Tokens: Token Values.

`-l'
`--no-lines'
Don't put any `#line' preprocessor commands in the parser file.
Ordinarily Bison puts them in the parser file so that the C
compiler and debuggers will associate errors with your source
file, the grammar file. This option causes them to associate
errors with the parser file, treating it an independent source
file in its own right.

`-o OUTFILE'
`--output-file=OUTFILE'
Specify the name OUTFILE for the parser file.

The other output files' names are constructed from OUTFILE as
described under the `-v' and `-d' switches.

`-p PREFIX'
`--name-prefix=PREFIX'
Rename the external symbols used in the parser so that they start
with PREFIX instead of `yy'. The precise list of symbols renamed
is `yyparse', `yylex', `yyerror', `yylval', `yychar' and `yydebug'.

For example, if you use `-p c', the names become `cparse', `clex',
and so on.

*Note Multiple Parsers in the Same Program: Multiple Parsers.

`-t'
`--debug'
Output a definition of the macro `YYDEBUG' into the parser file,
so that the debugging facilities are compiled. *Note Debugging
Your Parser: Debugging.

`-v'
`--verbose'
Write an extra output file containing verbose descriptions of the
parser states and what is done for each type of look-ahead token in
that state.

This file also describes all the conflicts, both those resolved by
operator precedence and the unresolved ones.

The file's name is made by removing `.tab.c' or `.c' from the
parser output file name, and adding `.output' instead.

Therefore, if the input file is `foo.y', then the parser file is
called `foo.tab.c' by default. As a consequence, the verbose
output file is called `foo.output'.

`-V'
`--version'
Print the version number of Bison and exit.

`-h'
`--help'
Print a summary of the command-line options to Bison and exit.

`-y'
`--yacc'
`--fixed-output-files'
Equivalent to `-o y.tab.c'; the parser output file is called
`y.tab.c', and the other outputs are called `y.output' and
`y.tab.h'. The purpose of this switch is to imitate Yacc's output
file name conventions. Thus, the following shell script can
substitute for Yacc:

bison -y $*


File: bison.info, Node: Option Cross Key, Next: VMS Invocation, Prev: Bison Options, Up: Invocation

Option Cross Key
================

Here is a list of options, alphabetized by long option, to help you
find the corresponding short option.

--debug -t
--defines -d
--file-prefix=PREFIX -b FILE-PREFIX
--fixed-output-files --yacc -y
--help -h
--name-prefix -p
--no-lines -l
--output-file=OUTFILE -o OUTFILE
--verbose -v
--version -V


File: bison.info, Node: VMS Invocation, Prev: Option Cross Key, Up: Invocation

Invoking Bison under VMS
========================

The command line syntax for Bison on VMS is a variant of the usual
Bison command syntax--adapted to fit VMS conventions.

To find the VMS equivalent for any Bison option, start with the long
option, and substitute a `/' for the leading `--', and substitute a `_'
for each `-' in the name of the long option. For example, the
following invocation under VMS:

bison /debug/name_prefix=bar foo.y

is equivalent to the following command under POSIX.

bison --debug --name-prefix=bar foo.y

The VMS file system does not permit filenames such as `foo.tab.c'.
In the above example, the output file would instead be named
`foo_tab.c'.


File: bison.info, Node: Table of Symbols, Next: Glossary, Prev: Invocation, Up: Top

Bison Symbols
*************

`error'
A token name reserved for error recovery. This token may be used
in grammar rules so as to allow the Bison parser to recognize an
error in the grammar without halting the process. In effect, a
sentence containing an error may be recognized as valid. On a
parse error, the token `error' becomes the current look-ahead
token. Actions corresponding to `error' are then executed, and
the look-ahead token is reset to the token that originally caused
the violation. *Note Error Recovery::.

`YYABORT'
Macro to pretend that an unrecoverable syntax error has occurred,
by making `yyparse' return 1 immediately. The error reporting
function `yyerror' is not called. *Note The Parser Function
`yyparse': Parser Function.

`YYACCEPT'
Macro to pretend that a complete utterance of the language has been
read, by making `yyparse' return 0 immediately. *Note The Parser
Function `yyparse': Parser Function.

`YYBACKUP'
Macro to discard a value from the parser stack and fake a
look-ahead token. *Note Special Features for Use in Actions:
Action Features.

`YYERROR'
Macro to pretend that a syntax error has just been detected: call
`yyerror' and then perform normal error recovery if possible
(*note Error Recovery::.), or (if recovery is impossible) make
`yyparse' return 1. *Note Error Recovery::.

`YYERROR_VERBOSE'
Macro that you define with `#define' in the Bison declarations
section to request verbose, specific error message strings when
`yyerror' is called.

`YYINITDEPTH'
Macro for specifying the initial size of the parser stack. *Note
Stack Overflow::.

`YYLTYPE'
Macro for the data type of `yylloc'; a structure with four
members. *Note Textual Positions of Tokens: Token Positions.

`YYMAXDEPTH'
Macro for specifying the maximum size of the parser stack. *Note
Stack Overflow::.

`YYRECOVERING'
Macro whose value indicates whether the parser is recovering from a
syntax error. *Note Special Features for Use in Actions: Action
Features.

`YYSTYPE'
Macro for the data type of semantic values; `int' by default.
*Note Data Types of Semantic Values: Value Type.

`yychar'
External integer variable that contains the integer value of the
current look-ahead token. (In a pure parser, it is a local
variable within `yyparse'.) Error-recovery rule actions may
examine this variable. *Note Special Features for Use in Actions:
Action Features.

`yyclearin'
Macro used in error-recovery rule actions. It clears the previous
look-ahead token. *Note Error Recovery::.

`yydebug'
External integer variable set to zero by default. If `yydebug' is
given a nonzero value, the parser will output information on input
symbols and parser action. *Note Debugging Your Parser: Debugging.

`yyerrok'
Macro to cause parser to recover immediately to its normal mode
after a parse error. *Note Error Recovery::.

`yyerror'
User-supplied function to be called by `yyparse' on error. The
function receives one argument, a pointer to a character string
containing an error message. *Note The Error Reporting Function
`yyerror': Error Reporting.

`yylex'
User-supplied lexical analyzer function, called with no arguments
to get the next token. *Note The Lexical Analyzer Function
`yylex': Lexical.

`yylval'
External variable in which `yylex' should place the semantic value
associated with a token. (In a pure parser, it is a local
variable within `yyparse', and its address is passed to `yylex'.)
*Note Semantic Values of Tokens: Token Values.

`yylloc'
External variable in which `yylex' should place the line and
column numbers associated with a token. (In a pure parser, it is a
local variable within `yyparse', and its address is passed to
`yylex'.) You can ignore this variable if you don't use the `@'
feature in the grammar actions. *Note Textual Positions of
Tokens: Token Positions.

`yynerrs'
Global variable which Bison increments each time there is a parse
error. (In a pure parser, it is a local variable within
`yyparse'.) *Note The Error Reporting Function `yyerror': Error
Reporting.

`yyparse'
The parser function produced by Bison; call this function to start
parsing. *Note The Parser Function `yyparse': Parser Function.

`%left'
Bison declaration to assign left associativity to token(s). *Note
Operator Precedence: Precedence Decl.

`%nonassoc'
Bison declaration to assign nonassociativity to token(s). *Note
Operator Precedence: Precedence Decl.

`%prec'
Bison declaration to assign a precedence to a specific rule.
*Note Context-Dependent Precedence: Contextual Precedence.

`%pure_parser'
Bison declaration to request a pure (reentrant) parser. *Note A
Pure (Reentrant) Parser: Pure Decl.

`%right'
Bison declaration to assign right associativity to token(s).
*Note Operator Precedence: Precedence Decl.

`%start'
Bison declaration to specify the start symbol. *Note The
Start-Symbol: Start Decl.

`%token'
Bison declaration to declare token(s) without specifying
precedence. *Note Token Type Names: Token Decl.

`%type'
Bison declaration to declare nonterminals. *Note Nonterminal
Symbols: Type Decl.

`%union'
Bison declaration to specify several possible data types for
semantic values. *Note The Collection of Value Types: Union Decl.

These are the punctuation and delimiters used in Bison input:

`%%'
Delimiter used to separate the grammar rule section from the Bison
declarations section or the additional C code section. *Note The
Overall Layout of a Bison Grammar: Grammar Layout.

`%{ %}'
All code listed between `%{' and `%}' is copied directly to the
output file uninterpreted. Such code forms the "C declarations"
section of the input file. *Note Outline of a Bison Grammar:
Grammar Outline.

`/*...*/'
Comment delimiters, as in C.

`:'
Separates a rule's result from its components. *Note Syntax of
Grammar Rules: Rules.

`;'
Terminates a rule. *Note Syntax of Grammar Rules: Rules.

`|'
Separates alternate rules for the same result nonterminal. *Note
Syntax of Grammar Rules: Rules.


File: bison.info, Node: Glossary, Next: Index, Prev: Table of Symbols, Up: Top

Glossary
********

Backus-Naur Form (BNF)
Formal method of specifying context-free grammars. BNF was first
used in the `ALGOL-60' report, 1963. *Note Languages and
Context-Free Grammars: Language and Grammar.

Context-free grammars
Grammars specified as rules that can be applied regardless of
context. Thus, if there is a rule which says that an integer can
be used as an expression, integers are allowed *anywhere* an
expression is permitted. *Note Languages and Context-Free
Grammars: Language and Grammar.

Dynamic allocation
Allocation of memory that occurs during execution, rather than at
compile time or on entry to a function.

Empty string
Analogous to the empty set in set theory, the empty string is a
character string of length zero.

Finite-state stack machine
A "machine" that has discrete states in which it is said to exist
at each instant in time. As input to the machine is processed, the
machine moves from state to state as specified by the logic of the
machine. In the case of the parser, the input is the language
being parsed, and the states correspond to various stages in the
grammar rules. *Note The Bison Parser Algorithm: Algorithm.

Grouping
A language construct that is (in general) grammatically divisible;
for example, `expression' or `declaration' in C. *Note Languages
and Context-Free Grammars: Language and Grammar.

Infix operator
An arithmetic operator that is placed between the operands on
which it performs some operation.

Input stream
A continuous flow of data between devices or programs.

Language construct
One of the typical usage schemas of the language. For example,
one of the constructs of the C language is the `if' statement.
*Note Languages and Context-Free Grammars: Language and Grammar.

Left associativity
Operators having left associativity are analyzed from left to
right: `a+b+c' first computes `a+b' and then combines with `c'.
*Note Operator Precedence: Precedence.

Left recursion
A rule whose result symbol is also its first component symbol; for
example, `expseq1 : expseq1 ',' exp;'. *Note Recursive Rules:
Recursion.

Left-to-right parsing
Parsing a sentence of a language by analyzing it token by token
from left to right. *Note The Bison Parser Algorithm: Algorithm.

Lexical analyzer (scanner)
A function that reads an input stream and returns tokens one by
one. *Note The Lexical Analyzer Function `yylex': Lexical.

Lexical tie-in
A flag, set by actions in the grammar rules, which alters the way
tokens are parsed. *Note Lexical Tie-ins::.

Look-ahead token
A token already read but not yet shifted. *Note Look-Ahead
Tokens: Look-Ahead.

LALR(1)
The class of context-free grammars that Bison (like most other
parser generators) can handle; a subset of LR(1). *Note
Mysterious Reduce/Reduce Conflicts: Mystery Conflicts.

LR(1)
The class of context-free grammars in which at most one token of
look-ahead is needed to disambiguate the parsing of any piece of
input.

Nonterminal symbol
A grammar symbol standing for a grammatical construct that can be
expressed through rules in terms of smaller constructs; in other
words, a construct that is not a token. *Note Symbols::.

Parse error
An error encountered during parsing of an input stream due to
invalid syntax. *Note Error Recovery::.

Parser
A function that recognizes valid sentences of a language by
analyzing the syntax structure of a set of tokens passed to it
from a lexical analyzer.

Postfix operator
An arithmetic operator that is placed after the operands upon
which it performs some operation.

Reduction
Replacing a string of nonterminals and/or terminals with a single
nonterminal, according to a grammar rule. *Note The Bison Parser
Algorithm: Algorithm.

Reentrant
A reentrant subprogram is a subprogram which can be in invoked any
number of times in parallel, without interference between the
various invocations. *Note A Pure (Reentrant) Parser: Pure Decl.

Reverse polish notation
A language in which all operators are postfix operators.

Right recursion
A rule whose result symbol is also its last component symbol; for
example, `expseq1: exp ',' expseq1;'. *Note Recursive Rules:
Recursion.

Semantics
In computer languages, the semantics are specified by the actions
taken for each instance of the language, i.e., the meaning of each
statement. *Note Defining Language Semantics: Semantics.

Shift
A parser is said to shift when it makes the choice of analyzing
further input from the stream rather than reducing immediately some
already-recognized rule. *Note The Bison Parser Algorithm:
Algorithm.

Single-character literal
A single character that is recognized and interpreted as is.
*Note From Formal Rules to Bison Input: Grammar in Bison.

Start symbol
The nonterminal symbol that stands for a complete valid utterance
in the language being parsed. The start symbol is usually listed
as the first nonterminal symbol in a language specification.
*Note The Start-Symbol: Start Decl.

Symbol table
A data structure where symbol names and associated data are stored
during parsing to allow for recognition and use of existing
information in repeated uses of a symbol. *Note Multi-function
Calc::.

Token
A basic, grammatically indivisible unit of a language. The symbol
that describes a token in the grammar is a terminal symbol. The
input of the Bison parser is a stream of tokens which comes from
the lexical analyzer. *Note Symbols::.

Terminal symbol
A grammar symbol that has no rules in the grammar and therefore is
grammatically indivisible. The piece of text it represents is a
token. *Note Languages and Context-Free Grammars: Language and
Grammar.


File: bison.info, Node: Index, Prev: Glossary, Up: Top

Index
*****

* Menu:

* $$: Actions.
* $N: Actions.
* %expect: Expect Decl.
* %left: Using Precedence.
* %nonassoc: Using Precedence.
* %prec: Contextual Precedence.
* %pure_parser: Pure Decl.
* %right: Using Precedence.
* %start: Start Decl.
* %token: Token Decl.
* %type: Type Decl.
* %union: Union Decl.
* @N: Action Features.
* calc: Infix Calc.
* else, dangling: Shift/Reduce.
* mfcalc: Multi-function Calc.
* rpcalc: RPN Calc.
* action: Actions.
* action data types: Action Types.
* action features summary: Action Features.
* actions in mid-rule: Mid-Rule Actions.
* actions, semantic: Semantic Actions.
* additional C code section: C Code.
* algorithm of parser: Algorithm.
* associativity: Why Precedence.
* Backus-Naur form: Language and Grammar.
* Bison declaration summary: Decl Summary.
* Bison declarations: Declarations.
* Bison declarations (introduction): Bison Declarations.
* Bison grammar: Grammar in Bison.
* Bison invocation: Invocation.
* Bison parser: Bison Parser.
* Bison parser algorithm: Algorithm.
* Bison symbols, table of: Table of Symbols.
* Bison utility: Bison Parser.
* BNF: Language and Grammar.
* C code, section for additional: C Code.
* C declarations section: C Declarations.
* C-language interface: Interface.
* calculator, infix notation: Infix Calc.
* calculator, multi-function: Multi-function Calc.
* calculator, simple: RPN Calc.
* character token: Symbols.
* compiling the parser: Rpcalc Compile.
* conflicts: Shift/Reduce.
* conflicts, reduce/reduce: Reduce/Reduce.
* conflicts, suppressing warnings of: Expect Decl.
* context-dependent precedence: Contextual Precedence.
* context-free grammar: Language and Grammar.
* controlling function: Rpcalc Main.
* dangling else: Shift/Reduce.
* data types in actions: Action Types.
* data types of semantic values: Value Type.
* debugging: Debugging.
* declaration summary: Decl Summary.
* declarations, Bison: Declarations.
* declarations, Bison (introduction): Bison Declarations.
* declarations, C: C Declarations.
* declaring operator precedence: Precedence Decl.
* declaring the start symbol: Start Decl.
* declaring token type names: Token Decl.
* declaring value types: Union Decl.
* declaring value types, nonterminals: Type Decl.
* default action: Actions.
* default data type: Value Type.
* default stack limit: Stack Overflow.
* default start symbol: Start Decl.
* defining language semantics: Semantics.
* error: Error Recovery.
* error recovery: Error Recovery.
* error recovery, simple: Simple Error Recovery.
* error reporting function: Error Reporting.
* error reporting routine: Rpcalc Error.
* examples, simple: Examples.
* exercises: Exercises.
* file format: Grammar Layout.
* finite-state machine: Parser States.
* formal grammar: Grammar in Bison.
* format of grammar file: Grammar Layout.
* glossary: Glossary.
* grammar file: Grammar Layout.
* grammar rule syntax: Rules.
* grammar rules section: Grammar Rules.
* grammar, Bison: Grammar in Bison.
* grammar, context-free: Language and Grammar.
* grouping, syntactic: Language and Grammar.
* infix notation calculator: Infix Calc.
* interface: Interface.
* introduction: Introduction.
* invoking Bison: Invocation.
* invoking Bison under VMS: VMS Invocation.
* LALR(1): Mystery Conflicts.
* language semantics, defining: Semantics.
* layout of Bison grammar: Grammar Layout.
* left recursion: Recursion.
* lexical analyzer: Lexical.
* lexical analyzer, purpose: Bison Parser.
* lexical analyzer, writing: Rpcalc Lexer.
* lexical tie-in: Lexical Tie-ins.
* literal token: Symbols.
* look-ahead token: Look-Ahead.
* LR(1): Mystery Conflicts.
* main function in simple example: Rpcalc Main.
* mid-rule actions: Mid-Rule Actions.
* multi-function calculator: Multi-function Calc.
* mutual recursion: Recursion.
* nonterminal symbol: Symbols.
* operator precedence: Precedence.
* operator precedence, declaring: Precedence Decl.
* options for invoking Bison: Invocation.
* overflow of parser stack: Stack Overflow.
* parse error: Error Reporting.
* parser: Bison Parser.
* parser stack: Algorithm.
* parser stack overflow: Stack Overflow.
* parser state: Parser States.
* polish notation calculator: RPN Calc.
* precedence declarations: Precedence Decl.
* precedence of operators: Precedence.
* precedence, context-dependent: Contextual Precedence.
* precedence, unary operator: Contextual Precedence.
* preventing warnings about conflicts: Expect Decl.
* pure parser: Pure Decl.
* recovery from errors: Error Recovery.
* recursive rule: Recursion.
* reduce/reduce conflict: Reduce/Reduce.
* reduction: Algorithm.
* reentrant parser: Pure Decl.
* reverse polish notation: RPN Calc.
* right recursion: Recursion.
* rule syntax: Rules.
* rules section for grammar: Grammar Rules.
* running Bison (introduction): Rpcalc Gen.
* semantic actions: Semantic Actions.
* semantic value: Semantic Values.
* semantic value type: Value Type.
* shift/reduce conflicts: Shift/Reduce.
* shifting: Algorithm.
* simple examples: Examples.
* single-character literal: Symbols.
* stack overflow: Stack Overflow.
* stack, parser: Algorithm.
* stages in using Bison: Stages.
* start symbol: Language and Grammar.
* start symbol, declaring: Start Decl.
* state (of parser): Parser States.
* summary, action features: Action Features.
* summary, Bison declaration: Decl Summary.
* suppressing conflict warnings: Expect Decl.
* symbol: Symbols.
* symbol table example: Mfcalc Symtab.
* symbols (abstract): Language and Grammar.
* symbols in Bison, table of: Table of Symbols.
* syntactic grouping: Language and Grammar.
* syntax error: Error Reporting.
* syntax of grammar rules: Rules.
* terminal symbol: Symbols.
* token: Language and Grammar.
* token type: Symbols.
* token type names, declaring: Token Decl.
* tracing the parser: Debugging.
* unary operator precedence: Contextual Precedence.
* using Bison: Stages.
* value type, semantic: Value Type.
* value types, declaring: Union Decl.
* value types, nonterminals, declaring: Type Decl.
* value, semantic: Semantic Values.
* VMS: VMS Invocation.
* warnings, preventing: Expect Decl.
* writing a lexical analyzer: Rpcalc Lexer.
* YYABORT: Parser Function.
* YYACCEPT: Parser Function.
* YYBACKUP: Action Features.
* yychar: Look-Ahead.
* yyclearin: Error Recovery.
* YYDEBUG: Debugging.
* yydebug: Debugging.
* YYEMPTY: Action Features.
* yyerrok: Error Recovery.
* YYERROR: Action Features.
* yyerror: Error Reporting.
* YYERROR_VERBOSE: Error Reporting.
* YYINITDEPTH: Stack Overflow.
* yylex: Lexical.
* yylloc: Token Positions.
* YYLTYPE: Token Positions.
* yylval: Token Values.
* YYMAXDEPTH: Stack Overflow.
* yynerrs: Error Reporting.
* yyparse: Parser Function.
* YYPRINT: Debugging.
* YYRECOVERING: Error Recovery.
* |: Rules.


bison-1.22/texinfo.tex100444 21641 1 402506 5440441603 14273 0ustar friedmandaemon%% TeX macros to handle texinfo files

% Copyright (C) 1985, 86, 88, 90, 91, 92, 1993 Free Software Foundation, Inc.

%This texinfo.tex file is free software; you can redistribute it and/or
%modify it under the terms of the GNU General Public License as
%published by the Free Software Foundation; either version 2, or (at
%your option) any later version.

%This texinfo.tex file is distributed in the hope that it will be
%useful, but WITHOUT ANY WARRANTY; without even the implied warranty
%of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%General Public License for more details.

%You should have received a copy of the GNU General Public License
%along with this texinfo.tex file; see the file COPYING. If not, write
%to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
%USA.


%In other words, you are welcome to use, share and improve this program.
%You are forbidden to forbid anyone else to use, share and improve
%what you give them. Help stamp out software-hoarding!

\def\texinfoversion{2.112}
\message{Loading texinfo package [Version \texinfoversion]:}

% Print the version number if in a .fmt file.
\everyjob{\message{[Texinfo version \texinfoversion]}\message{}}

% Save some parts of plain tex whose names we will redefine.

\let\ptexlbrace=\{
\let\ptexrbrace=\}
\let\ptexdots=\dots
\let\ptexdot=\.
\let\ptexstar=\*
\let\ptexend=\end
\let\ptexbullet=\bullet
\let\ptexb=\b
\let\ptexc=\c
\let\ptexi=\i
\let\ptext=\t
\let\ptexl=\l
\let\ptexL=\L

\def\tie{\penalty 10000\ } % Save plain tex definition of ~.

\message{Basics,}
\chardef\other=12

% If this character appears in an error message or help string, it
% starts a new line in the output.
\newlinechar = `^^J

% Ignore a token.
%
\def\gobble#1{}

\hyphenation{ap-pen-dix}
\hyphenation{mini-buf-fer mini-buf-fers}
\hyphenation{eshell}

% Margin to add to right of even pages, to left of odd pages.
\newdimen \bindingoffset \bindingoffset=0pt
\newdimen \normaloffset \normaloffset=\hoffset
\newdimen\pagewidth \newdimen\pageheight
\pagewidth=\hsize \pageheight=\vsize

% Sometimes it is convenient to have everything in the transcript file
% and nothing on the terminal. We don't just call \tracingall here,
% since that produces some useless output on the terminal.
%
\def\gloggingall{\begingroup \globaldefs = 1 \loggingall \endgroup}%
\def\loggingall{\tracingcommands2 \tracingstats2
\tracingpages1 \tracingoutput1 \tracinglostchars1
\tracingmacros2 \tracingparagraphs1 \tracingrestores1
\showboxbreadth\maxdimen\showboxdepth\maxdimen
}%

%---------------------Begin change-----------------------
%
%%%% For @cropmarks command.
% Dimensions to add cropmarks at corners Added by P. A. MacKay, 12 Nov. 1986
%
\newdimen\cornerlong \newdimen\cornerthick
\newdimen \topandbottommargin
\newdimen \outerhsize \newdimen \outervsize
\cornerlong=1pc\cornerthick=.3pt % These set size of cropmarks
\outerhsize=7in
%\outervsize=9.5in
% Alternative @smallbook page size is 9.25in
\outervsize=9.25in
\topandbottommargin=.75in
%
%---------------------End change-----------------------

% \onepageout takes a vbox as an argument. Note that \pagecontents
% does insertions itself, but you have to call it yourself.
\chardef\PAGE=255 \output={\onepageout{\pagecontents\PAGE}}
\def\onepageout#1{\hoffset=\normaloffset
\ifodd\pageno \advance\hoffset by \bindingoffset
\else \advance\hoffset by -\bindingoffset\fi
{\escapechar=`\\\relax % makes sure backslash is used in output files.
\shipout\vbox{{\let\hsize=\pagewidth \makeheadline} \pagebody{#1}%
{\let\hsize=\pagewidth \makefootline}}}%
\advancepageno \ifnum\outputpenalty>-20000 \else\dosupereject\fi}

%%%% For @cropmarks command %%%%

% Here is a modification of the main output routine for Near East Publications
% This provides right-angle cropmarks at all four corners.
% The contents of the page are centerlined into the cropmarks,
% and any desired binding offset is added as an \hskip on either
% site of the centerlined box. (P. A. MacKay, 12 November, 1986)
%
\def\croppageout#1{\hoffset=0pt % make sure this doesn't mess things up
{\escapechar=`\\\relax % makes sure backslash is used in output files.
\shipout
\vbox to \outervsize{\hsize=\outerhsize
\vbox{\line{\ewtop\hfill\ewtop}}
\nointerlineskip
\line{\vbox{\moveleft\cornerthick\nstop}
\hfill
\vbox{\moveright\cornerthick\nstop}}
\vskip \topandbottommargin
\centerline{\ifodd\pageno\hskip\bindingoffset\fi
\vbox{
{\let\hsize=\pagewidth \makeheadline}
\pagebody{#1}
{\let\hsize=\pagewidth \makefootline}}
\ifodd\pageno\else\hskip\bindingoffset\fi}
\vskip \topandbottommargin plus1fill minus1fill
\boxmaxdepth\cornerthick
\line{\vbox{\moveleft\cornerthick\nsbot}
\hfill
\vbox{\moveright\cornerthick\nsbot}}
\nointerlineskip
\vbox{\line{\ewbot\hfill\ewbot}}
}}
\advancepageno
\ifnum\outputpenalty>-20000 \else\dosupereject\fi}
%
% Do @cropmarks to get crop marks
\def\cropmarks{\let\onepageout=\croppageout }

\def\pagebody#1{\vbox to\pageheight{\boxmaxdepth=\maxdepth #1}}
{\catcode`\@ =11
\gdef\pagecontents#1{\ifvoid\topins\else\unvbox\topins\fi
\dimen@=\dp#1 \unvbox#1
\ifvoid\footins\else\vskip\skip\footins\footnoterule \unvbox\footins\fi
\ifr@ggedbottom \kern-\dimen@ \vfil \fi}
}

%
% Here are the rules for the cropmarks. Note that they are
% offset so that the space between them is truly \outerhsize or \outervsize
% (P. A. MacKay, 12 November, 1986)
%
\def\ewtop{\vrule height\cornerthick depth0pt width\cornerlong}
\def\nstop{\vbox
{\hrule height\cornerthick depth\cornerlong width\cornerthick}}
\def\ewbot{\vrule height0pt depth\cornerthick width\cornerlong}
\def\nsbot{\vbox
{\hrule height\cornerlong depth\cornerthick width\cornerthick}}

% Parse an argument, then pass it to #1. The argument is the rest of
% the input line (except we remove a trailing comment). #1 should be a
% macro which expects an ordinary undelimited TeX argument.
%
\def\parsearg#1{%
\let\next = #1%
\begingroup
\obeylines
\futurelet\temp\parseargx
}

% If the next token is an obeyed space (from an @example environment or
% the like), remove it and recurse. Otherwise, we're done.
\def\parseargx{%
% \obeyedspace is defined far below, after the definition of \sepspaces.
\ifx\obeyedspace\temp
\expandafter\parseargdiscardspace
\else
\expandafter\parseargline
\fi
}

% Remove a single space (as the delimiter token to the macro call).
{\obeyspaces %
\gdef\parseargdiscardspace {\futurelet\temp\parseargx}}

{\obeylines %
\gdef\parseargline#1^^M{%
\endgroup % End of the group started in \parsearg.
%
% First remove any @c comment, then any @comment.
% Result of each macro is put in \toks0.
\argremovec #1\c\relax %
\expandafter\argremovecomment \the\toks0 \comment\relax %
%
% Call the caller's macro, saved as \next in \parsearg.
\expandafter\next\expandafter{\the\toks0}%
}%
}

% Since all \c{,omment} does is throw away the argument, we can let TeX
% do that for us. The \relax here is matched by the \relax in the call
% in \parseargline; it could be more or less anything, its purpose is
% just to delimit the argument to the \c.
\def\argremovec#1\c#2\relax{\toks0 = {#1}}
\def\argremovecomment#1\comment#2\relax{\toks0 = {#1}}

% \argremovec{,omment} might leave us with trailing spaces, though; e.g.,
% @end itemize @c foo
% will have two active spaces as part of the argument with the
% `itemize'. Here we remove all active spaces from #1, and assign the
% result to \toks0.
%
% This loses if there are any *other* active characters besides spaces
% in the argument -- _ ^ +, for example -- since they get expanded.
% Fortunately, Texinfo does not define any such commands. (If it ever
% does, the catcode of the characters in questionwill have to be changed
% here.) But this means we cannot call \removeactivespaces as part of
% \argremovec{,omment}, since @c uses \parsearg, and thus the argument
% that \parsearg gets might well have any character at all in it.
%
\def\removeactivespaces#1{%
\begingroup
\ignoreactivespaces
\edef\temp{#1}%
\global\toks0 = \expandafter{\temp}%
\endgroup
}

% Change the active space to expand to nothing.
%
\begingroup
\obeyspaces
\gdef\ignoreactivespaces{\obeyspaces\let =\empty}
\endgroup


\def\flushcr{\ifx\par\lisppar \def\next##1{}\else \let\next=\relax \fi \next}

%% These are used to keep @begin/@end levels from running away
%% Call \inENV within environments (after a \begingroup)
\newif\ifENV \ENVfalse \def\inENV{\ifENV\relax\else\ENVtrue\fi}
\def\ENVcheck{%
\ifENV\errmessage{Still within an environment. Type Return to continue.}
\endgroup\fi} % This is not perfect, but it should reduce lossage

% @begin foo is the same as @foo, for now.
\newhelp\EMsimple{Type to continue.}

\outer\def\begin{\parsearg\beginxxx}

\def\beginxxx #1{%
\expandafter\ifx\csname #1\endcsname\relax
{\errhelp=\EMsimple \errmessage{Undefined command @begin #1}}\else
\csname #1\endcsname\fi}

% @end foo executes the definition of \Efoo.
%
\def\end{\parsearg\endxxx}
\def\endxxx #1{%
\removeactivespaces{#1}%
\edef\endthing{\the\toks0}%
%
\expandafter\ifx\csname E\endthing\endcsname\relax
\expandafter\ifx\csname \endthing\endcsname\relax
% There's no \foo, i.e., no ``environment'' foo.
\errhelp = \EMsimple
\errmessage{Undefined command `@end \endthing'}%
\else
\unmatchedenderror\endthing
\fi
\else
% Everything's ok; the right environment has been started.
\csname E\endthing\endcsname
\fi
}

% There is an environment #1, but it hasn't been started. Give an error.
%
\def\unmatchedenderror#1{%
\errhelp = \EMsimple
\errmessage{This `@end #1' doesn't have a matching `@#1'}%
}

% Define the control sequence \E#1 to give an unmatched @end error.
%
\def\defineunmatchedend#1{%
\expandafter\def\csname E#1\endcsname{\unmatchedenderror{#1}}%
}


% Single-spacing is done by various environments (specifically, in
% \nonfillstart and \quotations).
\newskip\singlespaceskip \singlespaceskip = \baselineskip
\def\singlespace{%
% Why was this kern here? It messes up equalizing space above and below
% environments. --karl, 6may93
%{\advance \baselineskip by -\singlespaceskip
%\kern \baselineskip}%
\baselineskip=\singlespaceskip
}

%% Simple single-character @ commands

% @@ prints an @
% Kludge this until the fonts are right (grr).
\def\@{{\tt \char '100}}

% This is turned off because it was never documented
% and you can use @w{...} around a quote to suppress ligatures.
%% Define @` and @' to be the same as ` and '
%% but suppressing ligatures.
%\def\`{{`}}
%\def\'{{'}}

% Used to generate quoted braces.

\def\mylbrace {{\tt \char '173}}
\def\myrbrace {{\tt \char '175}}
\let\{=\mylbrace
\let\}=\myrbrace

% @: forces normal size whitespace following.
\def\:{\spacefactor=1000 }

% @* forces a line break.
\def\*{\hfil\break\hbox{}\ignorespaces}

% @. is an end-of-sentence period.
\def\.{.\spacefactor=3000 }

% @w prevents a word break. Without the \leavevmode, @w at the
% beginning of a paragraph, when TeX is still in vertical mode, would
% produce a whole line of output instead of starting the paragraph.
\def\w#1{\leavevmode\hbox{#1}}

% @group ... @end group forces ... to be all on one page, by enclosing
% it in a TeX vbox. We use \vtop instead of \vbox to construct the box
% to keep its height that of a normal line. According to the rules for
% \topskip (p.114 of the TeXbook), the glue inserted is
% max (\topskip - \ht (first item), 0). If that height is large,
% therefore, no glue is inserted, and the space between the headline and
% the text is small, which looks bad.
%
\def\group{\begingroup
\ifnum\catcode13=\active \else
\errhelp = \groupinvalidhelp
\errmessage{@group invalid in context where filling is enabled}%
\fi
%
% The \vtop we start below produces a box with normal height and large
% depth; thus, TeX puts \baselineskip glue before it, and (when the
% next line of text is done) \lineskip glue after it. (See p.82 of
% the TeXbook.) Thus, space below is not quite equal to space
% above. But it's pretty close.
\def\Egroup{%
\egroup % End the \vtop.
\endgroup % End the \group.
}%
%
\vtop\bgroup
% We have to put a strut on the last line in case the @group is in
% the midst of an example, rather than completely enclosing it.
% Otherwise, the interline space between the last line of the group
% and the first line afterwards is too small. But we can't put the
% strut in \Egroup, since there it would be on a line by itself.
% Hence this just inserts a strut at the beginning of each line.
\everypar = {\strut}%
%
% Since we have a strut on every line, we don't need any of TeX's
% normal interline spacing.
\offinterlineskip
%
% OK, but now we have to do something about blank
% lines in the input in @example-like environments, which normally
% just turn into \lisppar, which will insert no space now that we've
% turned off the interline space. Simplest is to make them be an
% empty paragraph.
\ifx\par\lisppar
\edef\par{\leavevmode \par}%
%
% Reset ^^M's definition to new definition of \par.
\obeylines
\fi
%
% We do @comment here in case we are called inside an environment,
% such as @example, where each end-of-line in the input causes an
% end-of-line in the output. We don't want the end-of-line after
% the `@group' to put extra space in the output. Since @group
% should appear on a line by itself (according to the Texinfo
% manual), we don't worry about eating any user text.
\comment
}
%
% TeX puts in an \escapechar (i.e., `@') at the beginning of the help
% message, so this ends up printing `@group can only ...'.
%
\newhelp\groupinvalidhelp{%
group can only be used in environments such as @example,^^J%
where each line of input produces a line of output.}

% @need space-in-mils
% forces a page break if there is not space-in-mils remaining.

\newdimen\mil \mil=0.001in

\def\need{\parsearg\needx}

% Old definition--didn't work.
%\def\needx #1{\par %
%% This method tries to make TeX break the page naturally
%% if the depth of the box does not fit.
%{\baselineskip=0pt%
%\vtop to #1\mil{\vfil}\kern -#1\mil\penalty 10000
%\prevdepth=-1000pt
%}}

\def\needx#1{%
% Go into vertical mode, so we don't make a big box in the middle of a
% paragraph.
\par
%
% Don't add any leading before our big empty box, but allow a page
% break, since the best break might be right here.
\allowbreak
\nointerlineskip
\vtop to #1\mil{\vfil}%
%
% TeX does not even consider page breaks if a penalty added to the
% main vertical list is 10000 or more. But in order to see if the
% empty box we just added fits on the page, we must make it consider
% page breaks. On the other hand, we don't want to actually break the
% page after the empty box. So we use a penalty of 9999.
%
% There is an extremely small chance that TeX will actually break the
% page at this \penalty, if there are no other feasible breakpoints in
% sight. (If the user is using lots of big @group commands, which
% almost-but-not-quite fill up a page, TeX will have a hard time doing
% good page breaking, for example.) However, I could not construct an
% example where a page broke at this \penalty; if it happens in a real
% document, then we can reconsider our strategy.
\penalty9999
%
% Back up by the size of the box, whether we did a page break or not.
\kern -#1\mil
%
% Do not allow a page break right after this kern.
\nobreak
}

% @br forces paragraph break

\let\br = \par

% @dots{} output some dots

\def\dots{$\ldots$}

% @page forces the start of a new page

\def\page{\par\vfill\supereject}

% @exdent text....
% outputs text on separate line in roman font, starting at standard page margin

% This records the amount of indent in the innermost environment.
% That's how much \exdent should take out.
\newskip\exdentamount

% This defn is used inside fill environments such as @defun.
\def\exdent{\parsearg\exdentyyy}
\def\exdentyyy #1{{\hfil\break\hbox{\kern -\exdentamount{\rm#1}}\hfil\break}}

% This defn is used inside nofill environments such as @example.
\def\nofillexdent{\parsearg\nofillexdentyyy}
\def\nofillexdentyyy #1{{\advance \leftskip by -\exdentamount
\leftline{\hskip\leftskip{\rm#1}}}}

%\hbox{{\rm#1}}\hfil\break}}

% @include file insert text of that file as input.

\def\include{\parsearg\includezzz}
%Use \input\thisfile to avoid blank after \input, which may be an active
%char (in which case the blank would become the \input argument).
%The grouping keeps the value of \thisfile correct even when @include
%is nested.
\def\includezzz #1{\begingroup
\def\thisfile{#1}\input\thisfile
\endgroup}

\def\thisfile{}

% @center line outputs that line, centered

\def\center{\parsearg\centerzzz}
\def\centerzzz #1{{\advance\hsize by -\leftskip
\advance\hsize by -\rightskip
\centerline{#1}}}

% @sp n outputs n lines of vertical space

\def\sp{\parsearg\spxxx}
\def\spxxx #1{\par \vskip #1\baselineskip}

% @comment ...line which is ignored...
% @c is the same as @comment
% @ignore ... @end ignore is another way to write a comment

\def\comment{\catcode 64=\other \catcode 123=\other \catcode 125=\other%
\parsearg \commentxxx}

\def\commentxxx #1{\catcode 64=0 \catcode 123=1 \catcode 125=2 }

\let\c=\comment

% Prevent errors for section commands.
% Used in @ignore and in failing conditionals.
\def\ignoresections{%
\let\chapter=\relax
\let\unnumbered=\relax
\let\top=\relax
\let\unnumberedsec=\relax
\let\unnumberedsection=\relax
\let\unnumberedsubsec=\relax
\let\unnumberedsubsection=\relax
\let\unnumberedsubsubsec=\relax
\let\unnumberedsubsubsection=\relax
\let\section=\relax
\let\subsec=\relax
\let\subsubsec=\relax
\let\subsection=\relax
\let\subsubsection=\relax
\let\appendix=\relax
\let\appendixsec=\relax
\let\appendixsection=\relax
\let\appendixsubsec=\relax
\let\appendixsubsection=\relax
\let\appendixsubsubsec=\relax
\let\appendixsubsubsection=\relax
\let\contents=\relax
\let\smallbook=\relax
\let\titlepage=\relax
}

% Used in nested conditionals, where we have to parse the Texinfo source
% and so want to turn off most commands, in case they are used
% incorrectly.
%
\def\ignoremorecommands{%
\let\defcv = \relax
\let\deffn = \relax
\let\deffnx = \relax
\let\defindex = \relax
\let\defivar = \relax
\let\defmac = \relax
\let\defmethod = \relax
\let\defop = \relax
\let\defopt = \relax
\let\defspec = \relax
\let\deftp = \relax
\let\deftypefn = \relax
\let\deftypefun = \relax
\let\deftypevar = \relax
\let\deftypevr = \relax
\let\defun = \relax
\let\defvar = \relax
\let\defvr = \relax
\let\ref = \relax
\let\xref = \relax
\let\printindex = \relax
\let\pxref = \relax
\let\settitle = \relax
\let\include = \relax
\let\lowersections = \relax
\let\down = \relax
\let\raisesections = \relax
\let\up = \relax
\let\set = \relax
\let\clear = \relax
}

% Ignore @ignore ... @end ignore.
%
\def\ignore{\doignore{ignore}}

% Also ignore @ifinfo, @menu, and @direntry text.
%
\def\ifinfo{\doignore{ifinfo}}
\def\menu{\doignore{menu}}
\def\direntry{\doignore{direntry}}

% Ignore text until a line `@end #1'.
%
\def\doignore#1{\begingroup
% Don't complain about control sequences we have declared \outer.
\ignoresections
%
% Define a command to swallow text until we reach `@end #1'.
\long\def\doignoretext##1\end #1{\enddoignore}%
%
% Make sure that spaces turn into tokens that match what \doignoretext wants.
\catcode32 = 10
%
% And now expand that command.
\doignoretext
}

% What we do to finish off ignored text.
%
\def\enddoignore{\endgroup\ignorespaces}%

\newif\ifwarnedobs\warnedobsfalse
\def\obstexwarn{%
\ifwarnedobs\relax\else
% We need to warn folks that they may have trouble with TeX 3.0.
% This uses \immediate\write16 rather than \message to get newlines.
\immediate\write16{}
\immediate\write16{***WARNING*** for users of Unix TeX 3.0!}
\immediate\write16{This manual trips a bug in TeX version 3.0 (tex hangs).}
\immediate\write16{If you are running another version of TeX, relax.}
\immediate\write16{If you are running Unix TeX 3.0, kill this TeX process.}
\immediate\write16{ Then upgrade your TeX installation if you can.}
\immediate\write16{If you are stuck with version 3.0, run the}
\immediate\write16{ script ``tex3patch'' from the Texinfo distribution}
\immediate\write16{ to use a workaround.}
\immediate\write16{}
\warnedobstrue
\fi
}

% **In TeX 3.0, setting text in \nullfont hangs tex. For a
% workaround (which requires the file ``dummy.tfm'' to be installed),
% uncomment the following line:
%%%%%\font\nullfont=dummy\let\obstexwarn=\relax

% Ignore text, except that we keep track of conditional commands for
% purposes of nesting, up to an `@end #1' command.
%
\def\nestedignore#1{%
\obstexwarn
% We must actually expand the ignored text to look for the @end
% command, so that nested ignore constructs work. Thus, we put the
% text into a \vbox and then do nothing with the result. To minimize
% the change of memory overflow, we follow the approach outlined on
% page 401 of the TeXbook: make the current font be a dummy font.
%
\setbox0 = \vbox\bgroup
% Don't complain about control sequences we have declared \outer.
\ignoresections
%
% Define `@end #1' to end the box, which will in turn undefine the
% @end command again.
\expandafter\def\csname E#1\endcsname{\egroup\ignorespaces}%
%
% We are going to be parsing Texinfo commands. Most cause no
% trouble when they are used incorrectly, but some commands do
% complicated argument parsing or otherwise get confused, so we
% undefine them.
%
% We can't do anything about stray @-signs, unfortunately;
% they'll produce `undefined control sequence' errors.
\ignoremorecommands
%
% Set the current font to be \nullfont, a TeX primitive, and define
% all the font commands to also use \nullfont. We don't use
% dummy.tfm, as suggested in the TeXbook, because not all sites
% might have that installed. Therefore, math mode will still
% produce output, but that should be an extremely small amount of
% stuff compared to the main input.
%
\nullfont
\let\tenrm = \nullfont \let\tenit = \nullfont \let\tensl = \nullfont
\let\tenbf = \nullfont \let\tentt = \nullfont \let\smallcaps = \nullfont
\let\tensf = \nullfont
% Similarly for index fonts (mostly for their use in
% smallexample)
\let\indrm = \nullfont \let\indit = \nullfont \let\indsl = \nullfont
\let\indbf = \nullfont \let\indtt = \nullfont \let\indsc = \nullfont
\let\indsf = \nullfont
%
% Don't complain when characters are missing from the fonts.
\tracinglostchars = 0
%
% Don't bother to do space factor calculations.
\frenchspacing
%
% Don't report underfull hboxes.
\hbadness = 10000
%
% Do minimal line-breaking.
\pretolerance = 10000
%
% Do not execute instructions in @tex
\def\tex{\doignore{tex}}
}

% @set VAR sets the variable VAR to an empty value.
% @set VAR REST-OF-LINE sets VAR to the value REST-OF-LINE.
%
% Since we want to separate VAR from REST-OF-LINE (which might be
% empty), we can't just use \parsearg; we have to insert a space of our
% own to delimit the rest of the line, and then take it out again if we
% didn't need it.
%
\def\set{\parsearg\setxxx}
\def\setxxx#1{\setyyy#1 \endsetyyy}
\def\setyyy#1 #2\endsetyyy{%
\def\temp{#2}%
\ifx\temp\empty \global\expandafter\let\csname SET#1\endcsname = \empty
\else \setzzz{#1}#2\endsetzzz % Remove the trailing space \setxxx inserted.
\fi
}
\def\setzzz#1#2 \endsetzzz{\expandafter\xdef\csname SET#1\endcsname{#2}}

% @clear VAR clears (i.e., unsets) the variable VAR.
%
\def\clear{\parsearg\clearxxx}
\def\clearxxx#1{\global\expandafter\let\csname SET#1\endcsname=\relax}

% @value{foo} gets the text saved in variable foo.
%
\def\value#1{\expandafter
\ifx\csname SET#1\endcsname\relax
{\{No value for ``#1''\}}
\else \csname SET#1\endcsname \fi}

% @ifset VAR ... @end ifset reads the `...' iff VAR has been defined
% with @set.
%
\def\ifset{\parsearg\ifsetxxx}
\def\ifsetxxx #1{%
\expandafter\ifx\csname SET#1\endcsname\relax
\expandafter\ifsetfail
\else
\expandafter\ifsetsucceed
\fi
}
\def\ifsetsucceed{\conditionalsucceed{ifset}}
\def\ifsetfail{\nestedignore{ifset}}
\defineunmatchedend{ifset}

% @ifclear VAR ... @end ifclear reads the `...' iff VAR has never been
% defined with @set, or has been undefined with @clear.
%
\def\ifclear{\parsearg\ifclearxxx}
\def\ifclearxxx #1{%
\expandafter\ifx\csname SET#1\endcsname\relax
\expandafter\ifclearsucceed
\else
\expandafter\ifclearfail
\fi
}
\def\ifclearsucceed{\conditionalsucceed{ifclear}}
\def\ifclearfail{\nestedignore{ifclear}}
\defineunmatchedend{ifclear}

% @iftex always succeeds; we read the text following, through @end
% iftex). But `@end iftex' should be valid only after an @iftex.
%
\def\iftex{\conditionalsucceed{iftex}}
\defineunmatchedend{iftex}

% We can't just want to start a group at @iftex (for example) and end it
% at @end iftex, since then @set commands inside the conditional have no
% effect (they'd get reverted at the end of the group). So we must
% define \Eiftex to redefine itself to be its previous value. (We can't
% just define it to fail again with an ``unmatched end'' error, since
% the @ifset might be nested.)
%
\def\conditionalsucceed#1{%
\edef\temp{%
% Remember the current value of \E#1.
\let\nece{prevE#1} = \nece{E#1}%
%
% At the `@end #1', redefine \E#1 to be its previous value.
\def\nece{E#1}{\let\nece{E#1} = \nece{prevE#1}}%
}%
\temp
}

% We need to expand lots of \csname's, but we don't want to expand the
% control sequences after we've constructed them.
%
\def\nece#1{\expandafter\noexpand\csname#1\endcsname}

% @asis just yields its argument. Used with @table, for example.
%
\def\asis#1{#1}

% @math means output in math mode.
% We don't use $'s directly in the definition of \math because control
% sequences like \math are expanded when the toc file is written. Then,
% we read the toc file back, the $'s will be normal characters (as they
% should be, according to the definition of Texinfo). So we must use a
% control sequence to switch into and out of math mode.
%
% This isn't quite enough for @math to work properly in indices, but it
% seems unlikely it will ever be needed there.
%
\let\implicitmath = $
\def\math#1{\implicitmath #1\implicitmath}

% @bullet and @minus need the same treatment as @math, just above.
\def\bullet{\implicitmath\ptexbullet\implicitmath}
\def\minus{\implicitmath-\implicitmath}

\def\node{\ENVcheck\parsearg\nodezzz}
\def\nodezzz#1{\nodexxx [#1,]}
\def\nodexxx[#1,#2]{\gdef\lastnode{#1}}
\let\nwnode=\node
\let\lastnode=\relax

\def\donoderef{\ifx\lastnode\relax\else
\expandafter\expandafter\expandafter\setref{\lastnode}\fi
\let\lastnode=\relax}

\def\unnumbnoderef{\ifx\lastnode\relax\else
\expandafter\expandafter\expandafter\unnumbsetref{\lastnode}\fi
\let\lastnode=\relax}

\def\appendixnoderef{\ifx\lastnode\relax\else
\expandafter\expandafter\expandafter\appendixsetref{\lastnode}\fi
\let\lastnode=\relax}

\let\refill=\relax

% @setfilename is done at the beginning of every texinfo file.
% So open here the files we need to have open while reading the input.
% This makes it possible to make a .fmt file for texinfo.
\def\setfilename{%
\readauxfile
\opencontents
\openindices
\fixbackslash % Turn off hack to swallow `\input texinfo'.
\global\let\setfilename=\comment % Ignore extra @setfilename cmds.
\comment % Ignore the actual filename.
}

\outer\def\bye{\pagealignmacro\tracingstats=1\ptexend}

\def\inforef #1{\inforefzzz #1,,,,**}
\def\inforefzzz #1,#2,#3,#4**{See Info file \file{\ignorespaces #3{}},
node \samp{\ignorespaces#1{}}}

\message{fonts,}

% Font-change commands.

% Texinfo supports the sans serif font style, which plain TeX does not.
% So we set up a \sf analogous to plain's \rm, etc.
\newfam\sffam
\def\sf{\fam=\sffam \tensf}
\let\li = \sf % Sometimes we call it \li, not \sf.

%% Try out Computer Modern fonts at \magstephalf
\let\mainmagstep=\magstephalf

\ifx\bigger\relax
\let\mainmagstep=\magstep1
\font\textrm=cmr12
\font\texttt=cmtt12
\else
\font\textrm=cmr10 scaled \mainmagstep
\font\texttt=cmtt10 scaled \mainmagstep
\fi
% Instead of cmb10, you many want to use cmbx10.
% cmbx10 is a prettier font on its own, but cmb10
% looks better when embedded in a line with cmr10.
\font\textbf=cmb10 scaled \mainmagstep
\font\textit=cmti10 scaled \mainmagstep
\font\textsl=cmsl10 scaled \mainmagstep
\font\textsf=cmss10 scaled \mainmagstep
\font\textsc=cmcsc10 scaled \mainmagstep
\font\texti=cmmi10 scaled \mainmagstep
\font\textsy=cmsy10 scaled \mainmagstep

% A few fonts for @defun, etc.
\font\defbf=cmbx10 scaled \magstep1 %was 1314
\font\deftt=cmtt10 scaled \magstep1
\def\df{\let\tentt=\deftt \let\tenbf = \defbf \bf}

% Fonts for indices and small examples.
% We actually use the slanted font rather than the italic,
% because texinfo normally uses the slanted fonts for that.
% Do not make many font distinctions in general in the index, since they
% aren't very useful.
\font\ninett=cmtt9
\font\indrm=cmr9
\font\indit=cmsl9
\let\indsl=\indit
\let\indtt=\ninett
\let\indsf=\indrm
\let\indbf=\indrm
\let\indsc=\indrm
\font\indi=cmmi9
\font\indsy=cmsy9

% Fonts for headings
\font\chaprm=cmbx12 scaled \magstep2
\font\chapit=cmti12 scaled \magstep2
\font\chapsl=cmsl12 scaled \magstep2
\font\chaptt=cmtt12 scaled \magstep2
\font\chapsf=cmss12 scaled \magstep2
\let\chapbf=\chaprm
\font\chapsc=cmcsc10 scaled\magstep3
\font\chapi=cmmi12 scaled \magstep2
\font\chapsy=cmsy10 scaled \magstep3

\font\secrm=cmbx12 scaled \magstep1
\font\secit=cmti12 scaled \magstep1
\font\secsl=cmsl12 scaled \magstep1
\font\sectt=cmtt12 scaled \magstep1
\font\secsf=cmss12 scaled \magstep1
\font\secbf=cmbx12 scaled \magstep1
\font\secsc=cmcsc10 scaled\magstep2
\font\seci=cmmi12 scaled \magstep1
\font\secsy=cmsy10 scaled \magstep2

% \font\ssecrm=cmbx10 scaled \magstep1 % This size an font looked bad.
% \font\ssecit=cmti10 scaled \magstep1 % The letters were too crowded.
% \font\ssecsl=cmsl10 scaled \magstep1
% \font\ssectt=cmtt10 scaled \magstep1
% \font\ssecsf=cmss10 scaled \magstep1

%\font\ssecrm=cmb10 scaled 1315 % Note the use of cmb rather than cmbx.
%\font\ssecit=cmti10 scaled 1315 % Also, the size is a little larger than
%\font\ssecsl=cmsl10 scaled 1315 % being scaled magstep1.
%\font\ssectt=cmtt10 scaled 1315
%\font\ssecsf=cmss10 scaled 1315

%\let\ssecbf=\ssecrm

\font\ssecrm=cmbx12 scaled \magstephalf
\font\ssecit=cmti12 scaled \magstephalf
\font\ssecsl=cmsl12 scaled \magstephalf
\font\ssectt=cmtt12 scaled \magstephalf
\font\ssecsf=cmss12 scaled \magstephalf
\font\ssecbf=cmbx12 scaled \magstephalf
\font\ssecsc=cmcsc10 scaled \magstep1
\font\sseci=cmmi12 scaled \magstephalf
\font\ssecsy=cmsy10 scaled \magstep1
% The smallcaps and symbol fonts should actually be scaled \magstep1.5,
% but that is not a standard magnification.

% Fonts for title page:
\font\titlerm = cmbx12 scaled \magstep3
\let\authorrm = \secrm

% In order for the font changes to affect most math symbols and letters,
% we have to define the \textfont of the standard families. Since
% texinfo doesn't allow for producing subscripts and superscripts, we
% don't bother to reset \scriptfont and \scriptscriptfont (which would
% also require loading a lot more fonts).
%
\def\resetmathfonts{%
\textfont0 = \tenrm \textfont1 = \teni \textfont2 = \tensy
\textfont\itfam = \tenit \textfont\slfam = \tensl \textfont\bffam = \tenbf
\textfont\ttfam = \tentt \textfont\sffam = \tensf
}


% The font-changing commands redefine the meanings of \tenSTYLE, instead
% of just \STYLE. We do this so that font changes will continue to work
% in math mode, where it is the current \fam that is relevant in most
% cases, not the current. Plain TeX does, for example,
% \def\bf{\fam=\bffam \tenbf} By redefining \tenbf, we obviate the need
% to redefine \bf itself.
\def\textfonts{%
\let\tenrm=\textrm \let\tenit=\textit \let\tensl=\textsl
\let\tenbf=\textbf \let\tentt=\texttt \let\smallcaps=\textsc
\let\tensf=\textsf \let\teni=\texti \let\tensy=\textsy
\resetmathfonts}
\def\chapfonts{%
\let\tenrm=\chaprm \let\tenit=\chapit \let\tensl=\chapsl
\let\tenbf=\chapbf \let\tentt=\chaptt \let\smallcaps=\chapsc
\let\tensf=\chapsf \let\teni=\chapi \let\tensy=\chapsy
\resetmathfonts}
\def\secfonts{%
\let\tenrm=\secrm \let\tenit=\secit \let\tensl=\secsl
\let\tenbf=\secbf \let\tentt=\sectt \let\smallcaps=\secsc
\let\tensf=\secsf \let\teni=\seci \let\tensy=\secsy
\resetmathfonts}
\def\subsecfonts{%
\let\tenrm=\ssecrm \let\tenit=\ssecit \let\tensl=\ssecsl
\let\tenbf=\ssecbf \let\tentt=\ssectt \let\smallcaps=\ssecsc
\let\tensf=\ssecsf \let\teni=\sseci \let\tensy=\ssecsy
\resetmathfonts}
\def\indexfonts{%
\let\tenrm=\indrm \let\tenit=\indit \let\tensl=\indsl
\let\tenbf=\indbf \let\tentt=\indtt \let\smallcaps=\indsc
\let\tensf=\indsf \let\teni=\indi \let\tensy=\indsy
\resetmathfonts}

% Set up the default fonts, so we can use them for creating boxes.
%
\textfonts

% Count depth in font-changes, for error checks
\newcount\fontdepth \fontdepth=0

% Fonts for short table of contents.
\font\shortcontrm=cmr12
\font\shortcontbf=cmbx12
\font\shortcontsl=cmsl12

%% Add scribe-like font environments, plus @l for inline lisp (usually sans
%% serif) and @ii for TeX italic

% \smartitalic{ARG} outputs arg in italics, followed by an italic correction
% unless the following character is such as not to need one.
\def\smartitalicx{\ifx\next,\else\ifx\next-\else\ifx\next.\else\/\fi\fi\fi}
\def\smartitalic#1{{\sl #1}\futurelet\next\smartitalicx}

\let\i=\smartitalic
\let\var=\smartitalic
\let\dfn=\smartitalic
\let\emph=\smartitalic
\let\cite=\smartitalic

\def\b#1{{\bf #1}}
\let\strong=\b

% We can't just use \exhyphenpenalty, because that only has effect at
% the end of a paragraph. Restore normal hyphenation at the end of the
% group within which \nohyphenation is presumably called.
%
\def\nohyphenation{\hyphenchar\font = -1 \aftergroup\restorehyphenation}
\def\restorehyphenation{\hyphenchar\font = `- }

\def\t#1{%
{\tt \nohyphenation \rawbackslash \frenchspacing #1}%
\null
}
\let\ttfont = \t
%\def\samp #1{`{\tt \rawbackslash \frenchspacing #1}'\null}
\def\samp #1{`\tclose{#1}'\null}
\def\key #1{{\tt \nohyphenation \uppercase{#1}}\null}
\def\ctrl #1{{\tt \rawbackslash \hat}#1}

\let\file=\samp

% @code is a modification of @t,
% which makes spaces the same size as normal in the surrounding text.
\def\tclose#1{%
{%
% Change normal interword space to be same as for the current font.
\spaceskip = \fontdimen2\font
%
% Switch to typewriter.
\tt
%
% But `\ ' produces the large typewriter interword space.
\def\ {{\spaceskip = 0pt{} }}%
%
% Turn off hyphenation.
\nohyphenation
%
\rawbackslash
\frenchspacing
#1%
}%
\null
}

% We *must* turn on hyphenation at `-' and `_' in \code.
% Otherwise, it is too hard to avoid overful hboxes
% in the Emacs manual, the Library manual, etc.

% Unfortunately, TeX uses one parameter (\hyphenchar) to control
% both hyphenation at - and hyphenation within words.
% We must therefore turn them both off (\tclose does that)
% and arrange explicitly to hyphenate an a dash.
% -- rms.
{
\catcode `\-=\active
\catcode `\_=\active
\global\def\code{\begingroup \catcode `\-=\active \let-\codedash \let_\codeunder \codex}
}
\def\codedash{-\discretionary{}{}{}}
\def\codeunder{\normalunderscore\discretionary{}{}{}}
\def\codex #1{\tclose{#1}\endgroup}

%\let\exp=\tclose %Was temporary

% @kbd is like @code, except that if the argument is just one @key command,
% then @kbd has no effect.

\def\xkey{\key}
\def\kbdfoo#1#2#3\par{\def\one{#1}\def\three{#3}\def\threex{??}%
\ifx\one\xkey\ifx\threex\three \key{#2}%
\else\tclose{\look}\fi
\else\tclose{\look}\fi}

% Typeset a dimension, e.g., `in' or `pt'. The only reason for the
% argument is to make the input look right: @dmn{pt} instead of
% @dmn{}pt.
%
\def\dmn#1{\thinspace #1}

\def\kbd#1{\def\look{#1}\expandafter\kbdfoo\look??\par}

\def\l#1{{\li #1}\null} %

\def\r#1{{\rm #1}} % roman font
% Use of \lowercase was suggested.
\def\sc#1{{\smallcaps#1}} % smallcaps font
\def\ii#1{{\it #1}} % italic font

\message{page headings,}

\newskip\titlepagetopglue \titlepagetopglue = 1.5in
\newskip\titlepagebottomglue \titlepagebottomglue = 2pc

% First the title page. Must do @settitle before @titlepage.
\def\titlefont#1{{\titlerm #1}}

\newif\ifseenauthor
\newif\iffinishedtitlepage

\def\shorttitlepage{\parsearg\shorttitlepagezzz}
\def\shorttitlepagezzz #1{\begingroup\hbox{}\vskip 1.5in \chaprm \centerline{#1}%
\endgroup\page\hbox{}\page}

\def\titlepage{\begingroup \parindent=0pt \textfonts
\let\subtitlerm=\tenrm
% I deinstalled the following change because \cmr12 is undefined.
% This change was not in the ChangeLog anyway. --rms.
% \let\subtitlerm=\cmr12
\def\subtitlefont{\subtitlerm \normalbaselineskip = 13pt \normalbaselines}%
%
\def\authorfont{\authorrm \normalbaselineskip = 16pt \normalbaselines}%
%
% Leave some space at the very top of the page.
\vglue\titlepagetopglue
%
% Now you can print the title using @title.
\def\title{\parsearg\titlezzz}%
\def\titlezzz##1{\leftline{\titlefont{##1}}
% print a rule at the page bottom also.
\finishedtitlepagefalse
\vskip4pt \hrule height 4pt width \hsize \vskip4pt}%
% No rule at page bottom unless we print one at the top with @title.
\finishedtitlepagetrue
%
% Now you can put text using @subtitle.
\def\subtitle{\parsearg\subtitlezzz}%
\def\subtitlezzz##1{{\subtitlefont \rightline{##1}}}%
%
% @author should come last, but may come many times.
\def\author{\parsearg\authorzzz}%
\def\authorzzz##1{\ifseenauthor\else\vskip 0pt plus 1filll\seenauthortrue\fi
{\authorfont \leftline{##1}}}%
%
% Most title ``pages'' are actually two pages long, with space
% at the top of the second. We don't want the ragged left on the second.
\let\oldpage = \page
\def\page{%
\iffinishedtitlepage\else
\finishtitlepage
\fi
\oldpage
\let\page = \oldpage
\hbox{}}%
% \def\page{\oldpage \hbox{}}
}

\def\Etitlepage{%
\iffinishedtitlepage\else
\finishtitlepage
\fi
% It is important to do the page break before ending the group,
% because the headline and footline are only empty inside the group.
% If we use the new definition of \page, we always get a blank page
% after the title page, which we certainly don't want.
\oldpage
\endgroup
\HEADINGSon
}

\def\finishtitlepage{%
\vskip4pt \hrule height 2pt width \hsize
\vskip\titlepagebottomglue
\finishedtitlepagetrue
}

%%% Set up page headings and footings.

\let\thispage=\folio

\newtoks \evenheadline % Token sequence for heading line of even pages
\newtoks \oddheadline % Token sequence for heading line of odd pages
\newtoks \evenfootline % Token sequence for footing line of even pages
\newtoks \oddfootline % Token sequence for footing line of odd pages

% Now make Tex use those variables
\headline={{\textfonts\rm \ifodd\pageno \the\oddheadline
\else \the\evenheadline \fi}}
\footline={{\textfonts\rm \ifodd\pageno \the\oddfootline
\else \the\evenfootline \fi}\HEADINGShook}
\let\HEADINGShook=\relax

% Commands to set those variables.
% For example, this is what @headings on does
% @evenheading @thistitle|@thispage|@thischapter
% @oddheading @thischapter|@thispage|@thistitle
% @evenfooting @thisfile||
% @oddfooting ||@thisfile

\def\evenheading{\parsearg\evenheadingxxx}
\def\oddheading{\parsearg\oddheadingxxx}
\def\everyheading{\parsearg\everyheadingxxx}

\def\evenfooting{\parsearg\evenfootingxxx}
\def\oddfooting{\parsearg\oddfootingxxx}
\def\everyfooting{\parsearg\everyfootingxxx}

{\catcode`\@=0 %

\gdef\evenheadingxxx #1{\evenheadingyyy #1@|@|@|@|\finish}
\gdef\evenheadingyyy #1@|#2@|#3@|#4\finish{%
\global\evenheadline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}

\gdef\oddheadingxxx #1{\oddheadingyyy #1@|@|@|@|\finish}
\gdef\oddheadingyyy #1@|#2@|#3@|#4\finish{%
\global\oddheadline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}

\gdef\everyheadingxxx #1{\everyheadingyyy #1@|@|@|@|\finish}
\gdef\everyheadingyyy #1@|#2@|#3@|#4\finish{%
\global\evenheadline={\rlap{\centerline{#2}}\line{#1\hfil#3}}
\global\oddheadline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}

\gdef\evenfootingxxx #1{\evenfootingyyy #1@|@|@|@|\finish}
\gdef\evenfootingyyy #1@|#2@|#3@|#4\finish{%
\global\evenfootline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}

\gdef\oddfootingxxx #1{\oddfootingyyy #1@|@|@|@|\finish}
\gdef\oddfootingyyy #1@|#2@|#3@|#4\finish{%
\global\oddfootline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}

\gdef\everyfootingxxx #1{\everyfootingyyy #1@|@|@|@|\finish}
\gdef\everyfootingyyy #1@|#2@|#3@|#4\finish{%
\global\evenfootline={\rlap{\centerline{#2}}\line{#1\hfil#3}}
\global\oddfootline={\rlap{\centerline{#2}}\line{#1\hfil#3}}}
%
}% unbind the catcode of @.

% @headings double turns headings on for double-sided printing.
% @headings single turns headings on for single-sided printing.
% @headings off turns them off.
% @headings on same as @headings double, retained for compatibility.
% @headings after turns on double-sided headings after this page.
% @headings doubleafter turns on double-sided headings after this page.
% @headings singleafter turns on single-sided headings after this page.
% By default, they are off.

\def\headings #1 {\csname HEADINGS#1\endcsname}

\def\HEADINGSoff{
\global\evenheadline={\hfil} \global\evenfootline={\hfil}
\global\oddheadline={\hfil} \global\oddfootline={\hfil}}
\HEADINGSoff
% When we turn headings on, set the page number to 1.
% For double-sided printing, put current file name in lower left corner,
% chapter name on inside top of right hand pages, document
% title on inside top of left hand pages, and page numbers on outside top
% edge of all pages.
\def\HEADINGSdouble{
%\pagealignmacro
\global\pageno=1
\global\evenfootline={\hfil}
\global\oddfootline={\hfil}
\global\evenheadline={\line{\folio\hfil\thistitle}}
\global\oddheadline={\line{\thischapter\hfil\folio}}
}
% For single-sided printing, chapter title goes across top left of page,
% page number on top right.
\def\HEADINGSsingle{
%\pagealignmacro
\global\pageno=1
\global\evenfootline={\hfil}
\global\oddfootline={\hfil}
\global\evenheadline={\line{\thischapter\hfil\folio}}
\global\oddheadline={\line{\thischapter\hfil\folio}}
}
\def\HEADINGSon{\HEADINGSdouble}

\def\HEADINGSafter{\let\HEADINGShook=\HEADINGSdoublex}
\let\HEADINGSdoubleafter=\HEADINGSafter
\def\HEADINGSdoublex{%
\global\evenfootline={\hfil}
\global\oddfootline={\hfil}
\global\evenheadline={\line{\folio\hfil\thistitle}}
\global\oddheadline={\line{\thischapter\hfil\folio}}
}

\def\HEADINGSsingleafter{\let\HEADINGShook=\HEADINGSsinglex}
\def\HEADINGSsinglex{%
\global\evenfootline={\hfil}
\global\oddfootline={\hfil}
\global\evenheadline={\line{\thischapter\hfil\folio}}
\global\oddheadline={\line{\thischapter\hfil\folio}}
}

% Subroutines used in generating headings
% Produces Day Month Year style of output.
\def\today{\number\day\space
\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\year}

% Use this if you want the Month Day, Year style of output.
%\def\today{\ifcase\month\or
%January\or February\or March\or April\or May\or June\or
%July\or August\or September\or October\or November\or December\fi
%\space\number\day, \number\year}

% @settitle line... specifies the title of the document, for headings
% It generates no output of its own

\def\thistitle{No Title}
\def\settitle{\parsearg\settitlezzz}
\def\settitlezzz #1{\gdef\thistitle{#1}}

\message{tables,}

% @tabs -- simple alignment

% These don't work. For one thing, \+ is defined as outer.
% So these macros cannot even be defined.

%\def\tabs{\parsearg\tabszzz}
%\def\tabszzz #1{\settabs\+#1\cr}
%\def\tabline{\parsearg\tablinezzz}
%\def\tablinezzz #1{\+#1\cr}
%\def\&{&}

% Tables -- @table, @ftable, @vtable, @item(x), @kitem(x), @xitem(x).

% default indentation of table text
\newdimen\tableindent \tableindent=.8in
% default indentation of @itemize and @enumerate text
\newdimen\itemindent \itemindent=.3in
% margin between end of table item and start of table text.
\newdimen\itemmargin \itemmargin=.1in

% used internally for \itemindent minus \itemmargin
\newdimen\itemmax

% Note @table, @vtable, and @vtable define @item, @itemx, etc., with
% these defs.
% They also define \itemindex
% to index the item name in whatever manner is desired (perhaps none).

\newif\ifitemxneedsnegativevskip

\def\itemxpar{\par\ifitemxneedsnegativevskip\vskip-\parskip\nobreak\fi}

\def\internalBitem{\smallbreak \parsearg\itemzzz}
\def\internalBitemx{\itemxpar \parsearg\itemzzz}

\def\internalBxitem "#1"{\def\xitemsubtopix{#1} \smallbreak \parsearg\xitemzzz}
\def\internalBxitemx "#1"{\def\xitemsubtopix{#1} \itemxpar \parsearg\xitemzzz}

\def\internalBkitem{\smallbreak \parsearg\kitemzzz}
\def\internalBkitemx{\itemxpar \parsearg\kitemzzz}

\def\kitemzzz #1{\dosubind {kw}{\code{#1}}{for {\bf \lastfunction}}%
\itemzzz {#1}}

\def\xitemzzz #1{\dosubind {kw}{\code{#1}}{for {\bf \xitemsubtopic}}%
\itemzzz {#1}}

\def\itemzzz #1{\begingroup %
\advance\hsize by -\rightskip
\advance\hsize by -\tableindent
\setbox0=\hbox{\itemfont{#1}}%
\itemindex{#1}%
\nobreak % This prevents a break before @itemx.
%
% Be sure we are not still in the middle of a paragraph.
%{\parskip = 0in
%\par
%}%
%
% If the item text does not fit in the space we have, put it on a line
% by itself, and do not allow a page break either before or after that
% line. We do not start a paragraph here because then if the next
% command is, e.g., @kindex, the whatsit would get put into the
% horizontal list on a line by itself, resulting in extra blank space.
\ifdim \wd0>\itemmax
%
% Make this a paragraph so we get the \parskip glue and wrapping,
% but leave it ragged-right.
\begingroup
\advance\leftskip by-\tableindent
\advance\hsize by\tableindent
\advance\rightskip by0pt plus1fil
\leavevmode\unhbox0\par
\endgroup
%
% We're going to be starting a paragraph, but we don't want the
% \parskip glue -- logically it's part of the @item we just started.
\nobreak \vskip-\parskip
%
% Stop a page break at the \parskip glue coming up. Unfortunately
% we can't prevent a possible page break at the following
% \baselineskip glue.
\nobreak
\endgroup
\itemxneedsnegativevskipfalse
\else
% The item text fits into the space. Start a paragraph, so that the
% following text (if any) will end up on the same line. Since that
% text will be indented by \tableindent, we make the item text be in
% a zero-width box.
\noindent
\rlap{\hskip -\tableindent\box0}\ignorespaces%
\endgroup%
\itemxneedsnegativevskiptrue%
\fi
}

\def\item{\errmessage{@item while not in a table}}
\def\itemx{\errmessage{@itemx while not in a table}}
\def\kitem{\errmessage{@kitem while not in a table}}
\def\kitemx{\errmessage{@kitemx while not in a table}}
\def\xitem{\errmessage{@xitem while not in a table}}
\def\xitemx{\errmessage{@xitemx while not in a table}}

%% Contains a kludge to get @end[description] to work
\def\description{\tablez{\dontindex}{1}{}{}{}{}}

\def\table{\begingroup\inENV\obeylines\obeyspaces\tablex}
{\obeylines\obeyspaces%
\gdef\tablex #1^^M{%
\tabley\dontindex#1 \endtabley}}

\def\ftable{\begingroup\inENV\obeylines\obeyspaces\ftablex}
{\obeylines\obeyspaces%
\gdef\ftablex #1^^M{%
\tabley\fnitemindex#1 \endtabley
\def\Eftable{\endgraf\afterenvbreak\endgroup}%
\let\Etable=\relax}}

\def\vtable{\begingroup\inENV\obeylines\obeyspaces\vtablex}
{\obeylines\obeyspaces%
\gdef\vtablex #1^^M{%
\tabley\vritemindex#1 \endtabley
\def\Evtable{\endgraf\afterenvbreak\endgroup}%
\let\Etable=\relax}}

\def\dontindex #1{}
\def\fnitemindex #1{\doind {fn}{\code{#1}}}%
\def\vritemindex #1{\doind {vr}{\code{#1}}}%

{\obeyspaces %
\gdef\tabley#1#2 #3 #4 #5 #6 #7\endtabley{\endgroup%
\tablez{#1}{#2}{#3}{#4}{#5}{#6}}}

\def\tablez #1#2#3#4#5#6{%
\aboveenvbreak %
\begingroup %
\def\Edescription{\Etable}% Neccessary kludge.
\let\itemindex=#1%
\ifnum 0#3>0 \advance \leftskip by #3\mil \fi %
\ifnum 0#4>0 \tableindent=#4\mil \fi %
\ifnum 0#5>0 \advance \rightskip by #5\mil \fi %
\def\itemfont{#2}%
\itemmax=\tableindent %
\advance \itemmax by -\itemmargin %
\advance \leftskip by \tableindent %
\exdentamount=\tableindent
\parindent = 0pt
\parskip = \smallskipamount
\ifdim \parskip=0pt \parskip=2pt \fi%
\def\Etable{\endgraf\afterenvbreak\endgroup}%
\let\item = \internalBitem %
\let\itemx = \internalBitemx %
\let\kitem = \internalBkitem %
\let\kitemx = \internalBkitemx %
\let\xitem = \internalBxitem %
\let\xitemx = \internalBxitemx %
}

% This is the counter used by @enumerate, which is really @itemize

\newcount \itemno

\def\itemize{\parsearg\itemizezzz}

\def\itemizezzz #1{%
\begingroup % ended by the @end itemsize
\itemizey {#1}{\Eitemize}
}

\def\itemizey #1#2{%
\aboveenvbreak %
\itemmax=\itemindent %
\advance \itemmax by -\itemmargin %
\advance \leftskip by \itemindent %
\exdentamount=\itemindent
\parindent = 0pt %
\parskip = \smallskipamount %
\ifdim \parskip=0pt \parskip=2pt \fi%
\def#2{\endgraf\afterenvbreak\endgroup}%
\def\itemcontents{#1}%
\let\item=\itemizeitem}

% Set sfcode to normal for the chars that usually have another value.
% These are `.?!:;,'
\def\frenchspacing{\sfcode46=1000 \sfcode63=1000 \sfcode33=1000
\sfcode58=1000 \sfcode59=1000 \sfcode44=1000 }

% \splitoff TOKENS\endmark defines \first to be the first token in
% TOKENS, and \rest to be the remainder.
%
\def\splitoff#1#2\endmark{\def\first{#1}\def\rest{#2}}%

% Allow an optional argument of an uppercase letter, lowercase letter,
% or number, to specify the first label in the enumerated list. No
% argument is the same as `1'.
%
\def\enumerate{\parsearg\enumeratezzz}
\def\enumeratezzz #1{\enumeratey #1 \endenumeratey}
\def\enumeratey #1 #2\endenumeratey{%
\begingroup % ended by the @end enumerate
%
% If we were given no argument, pretend we were given `1'.
\def\thearg{#1}%
\ifx\thearg\empty \def\thearg{1}\fi
%
% Detect if the argument is a single token. If so, it might be a
% letter. Otherwise, the only valid thing it can be is a number.
% (We will always have one token, because of the test we just made.
% This is a good thing, since \splitoff doesn't work given nothing at
% all -- the first parameter is undelimited.)
\expandafter\splitoff\thearg\endmark
\ifx\rest\empty
% Only one token in the argument. It could still be anything.
% A ``lowercase letter'' is one whose \lccode is nonzero.
% An ``uppercase letter'' is one whose \lccode is both nonzero, and
% not equal to itself.
% Otherwise, we assume it's a number.
%
% We need the \relax at the end of the \ifnum lines to stop TeX from
% continuing to look for a .
%
\ifnum\lccode\expandafter`\thearg=0\relax
\numericenumerate % a number (we hope)
\else
% It's a letter.
\ifnum\lccode\expandafter`\thearg=\expandafter`\thearg\relax
\lowercaseenumerate % lowercase letter
\else
\uppercaseenumerate % uppercase letter
\fi
\fi
\else
% Multiple tokens in the argument. We hope it's a number.
\numericenumerate
\fi
}

% An @enumerate whose labels are integers. The starting integer is
% given in \thearg.
%
\def\numericenumerate{%
\itemno = \thearg
\startenumeration{\the\itemno}%
}

% The starting (lowercase) letter is in \thearg.
\def\lowercaseenumerate{%
\itemno = \expandafter`\thearg
\startenumeration{%
% Be sure we're not beyond the end of the alphabet.
\ifnum\itemno=0
\errmessage{No more lowercase letters in @enumerate; get a bigger
alphabet}%
\fi
\char\lccode\itemno
}%
}

% The starting (uppercase) letter is in \thearg.
\def\uppercaseenumerate{%
\itemno = \expandafter`\thearg
\startenumeration{%
% Be sure we're not beyond the end of the alphabet.
\ifnum\itemno=0
\errmessage{No more uppercase letters in @enumerate; get a bigger
alphabet}
\fi
\char\uccode\itemno
}%
}

% Call itemizey, adding a period to the first argument and supplying the
% common last two arguments. Also subtract one from the initial value in
% \itemno, since @item increments \itemno.
%
\def\startenumeration#1{%
\advance\itemno by -1
\itemizey{#1.}\Eenumerate\flushcr
}

% @alphaenumerate and @capsenumerate are abbreviations for giving an arg
% to @enumerate.
%
\def\alphaenumerate{\enumerate{a}}
\def\capsenumerate{\enumerate{A}}
\def\Ealphaenumerate{\Eenumerate}
\def\Ecapsenumerate{\Eenumerate}

% Definition of @item while inside @itemize.

\def\itemizeitem{%
\advance\itemno by 1
{\let\par=\endgraf \smallbreak}%
\ifhmode \errmessage{\in hmode at itemizeitem}\fi
{\parskip=0in \hskip 0pt
\hbox to 0pt{\hss \itemcontents\hskip \itemmargin}%
\vadjust{\penalty 1200}}%
\flushcr}

\message{indexing,}
% Index generation facilities

% Define \newwrite to be identical to plain tex's \newwrite
% except not \outer, so it can be used within \newindex.
{\catcode`\@=11
\gdef\newwrite{\alloc@7\write\chardef\sixt@@n}}

% \newindex {foo} defines an index named foo.
% It automatically defines \fooindex such that
% \fooindex ...rest of line... puts an entry in the index foo.
% It also defines \fooindfile to be the number of the output channel for
% the file that accumulates this index. The file's extension is foo.
% The name of an index should be no more than 2 characters long
% for the sake of vms.

\def\newindex #1{
\expandafter\newwrite \csname#1indfile\endcsname% Define number for output file
\openout \csname#1indfile\endcsname \jobname.#1 % Open the file
\expandafter\xdef\csname#1index\endcsname{% % Define \xxxindex
\noexpand\doindex {#1}}
}

% @defindex foo == \newindex{foo}

\def\defindex{\parsearg\newindex}

% Define @defcodeindex, like @defindex except put all entries in @code.

\def\newcodeindex #1{
\expandafter\newwrite \csname#1indfile\endcsname% Define number for output file
\openout \csname#1indfile\endcsname \jobname.#1 % Open the file
\expandafter\xdef\csname#1index\endcsname{% % Define \xxxindex
\noexpand\docodeindex {#1}}
}

\def\defcodeindex{\parsearg\newcodeindex}

% @synindex foo bar makes index foo feed into index bar.
% Do this instead of @defindex foo if you don't want it as a separate index.
\def\synindex #1 #2 {%
\expandafter\let\expandafter\synindexfoo\expandafter=\csname#2indfile\endcsname
\expandafter\let\csname#1indfile\endcsname=\synindexfoo
\expandafter\xdef\csname#1index\endcsname{% % Define \xxxindex
\noexpand\doindex {#2}}%
}

% @syncodeindex foo bar similar, but put all entries made for index foo
% inside @code.
\def\syncodeindex #1 #2 {%
\expandafter\let\expandafter\synindexfoo\expandafter=\csname#2indfile\endcsname
\expandafter\let\csname#1indfile\endcsname=\synindexfoo
\expandafter\xdef\csname#1index\endcsname{% % Define \xxxindex
\noexpand\docodeindex {#2}}%
}

% Define \doindex, the driver for all \fooindex macros.
% Argument #1 is generated by the calling \fooindex macro,
% and it is "foo", the name of the index.

% \doindex just uses \parsearg; it calls \doind for the actual work.
% This is because \doind is more useful to call from other macros.

% There is also \dosubind {index}{topic}{subtopic}
% which makes an entry in a two-level index such as the operation index.

\def\doindex#1{\edef\indexname{#1}\parsearg\singleindexer}
\def\singleindexer #1{\doind{\indexname}{#1}}

% like the previous two, but they put @code around the argument.
\def\docodeindex#1{\edef\indexname{#1}\parsearg\singlecodeindexer}
\def\singlecodeindexer #1{\doind{\indexname}{\code{#1}}}

\def\indexdummies{%
\def\_{{\realbackslash _}}%
\def\w{\realbackslash w }%
\def\bf{\realbackslash bf }%
\def\rm{\realbackslash rm }%
\def\sl{\realbackslash sl }%
\def\sf{\realbackslash sf}%
\def\tt{\realbackslash tt}%
\def\gtr{\realbackslash gtr}%
\def\less{\realbackslash less}%
\def\hat{\realbackslash hat}%
\def\char{\realbackslash char}%
\def\TeX{\realbackslash TeX}%
\def\dots{\realbackslash dots }%
\def\copyright{\realbackslash copyright }%
\def\tclose##1{\realbackslash tclose {##1}}%
\def\code##1{\realbackslash code {##1}}%
\def\samp##1{\realbackslash samp {##1}}%
\def\t##1{\realbackslash r {##1}}%
\def\r##1{\realbackslash r {##1}}%
\def\i##1{\realbackslash i {##1}}%
\def\b##1{\realbackslash b {##1}}%
\def\cite##1{\realbackslash cite {##1}}%
\def\key##1{\realbackslash key {##1}}%
\def\file##1{\realbackslash file {##1}}%
\def\var##1{\realbackslash var {##1}}%
\def\kbd##1{\realbackslash kbd {##1}}%
\def\dfn##1{\realbackslash dfn {##1}}%
\def\emph##1{\realbackslash emph {##1}}%
}

% \indexnofonts no-ops all font-change commands.
% This is used when outputting the strings to sort the index by.
\def\indexdummyfont#1{#1}
\def\indexdummytex{TeX}
\def\indexdummydots{...}

\def\indexnofonts{%
\let\w=\indexdummyfont
\let\t=\indexdummyfont
\let\r=\indexdummyfont
\let\i=\indexdummyfont
\let\b=\indexdummyfont
\let\emph=\indexdummyfont
\let\strong=\indexdummyfont
\let\cite=\indexdummyfont
\let\sc=\indexdummyfont
%Don't no-op \tt, since it isn't a user-level command
% and is used in the definitions of the active chars like <, >, |...
%\let\tt=\indexdummyfont
\let\tclose=\indexdummyfont
\let\code=\indexdummyfont
\let\file=\indexdummyfont
\let\samp=\indexdummyfont
\let\kbd=\indexdummyfont
\let\key=\indexdummyfont
\let\var=\indexdummyfont
\let\TeX=\indexdummytex
\let\dots=\indexdummydots
}

% To define \realbackslash, we must make \ not be an escape.
% We must first make another character (@) an escape
% so we do not become unable to do a definition.

{\catcode`\@=0 \catcode`\\=\other
@gdef@realbackslash{\}}

\let\indexbackslash=0 %overridden during \printindex.

\def\doind #1#2{%
{\count10=\lastpenalty %
{\indexdummies % Must do this here, since \bf, etc expand at this stage
\escapechar=`\\%
{\let\folio=0% Expand all macros now EXCEPT \folio
\def\rawbackslashxx{\indexbackslash}% \indexbackslash isn't defined now
% so it will be output as is; and it will print as backslash in the indx.
%
% Now process the index-string once, with all font commands turned off,
% to get the string to sort the index by.
{\indexnofonts
\xdef\temp1{#2}%
}%
% Now produce the complete index entry. We process the index-string again,
% this time with font commands expanded, to get what to print in the index.
\edef\temp{%
\write \csname#1indfile\endcsname{%
\realbackslash entry {\temp1}{\folio}{#2}}}%
\temp }%
}\penalty\count10}}

\def\dosubind #1#2#3{%
{\count10=\lastpenalty %
{\indexdummies % Must do this here, since \bf, etc expand at this stage
\escapechar=`\\%
{\let\folio=0%
\def\rawbackslashxx{\indexbackslash}%
%
% Now process the index-string once, with all font commands turned off,
% to get the string to sort the index by.
{\indexnofonts
\xdef\temp1{#2 #3}%
}%
% Now produce the complete index entry. We process the index-string again,
% this time with font commands expanded, to get what to print in the index.
\edef\temp{%
\write \csname#1indfile\endcsname{%
\realbackslash entry {\temp1}{\folio}{#2}{#3}}}%
\temp }%
}\penalty\count10}}

% The index entry written in the file actually looks like
% \entry {sortstring}{page}{topic}
% or
% \entry {sortstring}{page}{topic}{subtopic}
% The texindex program reads in these files and writes files
% containing these kinds of lines:
% \initial {c}
% before the first topic whose initial is c
% \entry {topic}{pagelist}
% for a topic that is used without subtopics
% \primary {topic}
% for the beginning of a topic that is used with subtopics
% \secondary {subtopic}{pagelist}
% for each subtopic.

% Define the user-accessible indexing commands
% @findex, @vindex, @kindex, @cindex.

\def\findex {\fnindex}
\def\kindex {\kyindex}
\def\cindex {\cpindex}
\def\vindex {\vrindex}
\def\tindex {\tpindex}
\def\pindex {\pgindex}

\def\cindexsub {\begingroup\obeylines\cindexsub}
{\obeylines %
\gdef\cindexsub "#1" #2^^M{\endgroup %
\dosubind{cp}{#2}{#1}}}

% Define the macros used in formatting output of the sorted index material.

% This is what you call to cause a particular index to get printed.
% Write
% @unnumbered Function Index
% @printindex fn

\def\printindex{\parsearg\doprintindex}

\def\doprintindex#1{%
\tex
\dobreak \chapheadingskip {10000}
\catcode`\%=\other\catcode`\&=\other\catcode`\#=\other
\catcode`\$=\other\catcode`\_=\other
\catcode`\~=\other
%
% The following don't help, since the chars were translated
% when the raw index was written, and their fonts were discarded
% due to \indexnofonts.
%\catcode`\"=\active
%\catcode`\^=\active
%\catcode`\_=\active
%\catcode`\|=\active
%\catcode`\<=\active
%\catcode`\>=\active
% %
\def\indexbackslash{\rawbackslashxx}
\indexfonts\rm \tolerance=9500 \advance\baselineskip -1pt
\begindoublecolumns
%
% See if the index file exists and is nonempty.
\openin 1 \jobname.#1s
\ifeof 1
% \enddoublecolumns gets confused if there is no text in the index,
% and it loses the chapter title and the aux file entries for the
% index. The easiest way to prevent this problem is to make sure
% there is some text.
(Index is nonexistent)
\else
%
% If the index file exists but is empty, then \openin leaves \ifeof
% false. We have to make TeX try to read something from the file, so
% it can discover if there is anything in it.
\read 1 to \temp
\ifeof 1
(Index is empty)
\else
\input \jobname.#1s
\fi
\fi
\closein 1
\enddoublecolumns
\Etex
}

% These macros are used by the sorted index file itself.
% Change them to control the appearance of the index.

% Same as \bigskipamount except no shrink.
% \balancecolumns gets confused if there is any shrink.
\newskip\initialskipamount \initialskipamount 12pt plus4pt

\def\initial #1{%
{\let\tentt=\sectt \let\tt=\sectt \let\sf=\sectt
\ifdim\lastskip<\initialskipamount
\removelastskip \penalty-200 \vskip \initialskipamount\fi
\line{\secbf#1\hfill}\kern 2pt\penalty10000}}

% This typesets a paragraph consisting of #1, dot leaders, and then #2
% flush to the right margin. It is used for index and table of contents
% entries. The paragraph is indented by \leftskip.
%
\def\entry #1#2{\begingroup
%
% Start a new paragraph if necessary, so our assignments below can't
% affect previous text.
\par
%
% Do not fill out the last line with white space.
\parfillskip = 0in
%
% No extra space above this paragraph.
\parskip = 0in
%
% Do not prefer a separate line ending with a hyphen to fewer lines.
\finalhyphendemerits = 0
%
% \hangindent is only relevant when the entry text and page number
% don't both fit on one line. In that case, bob suggests starting the
% dots pretty far over on the line. Unfortunately, a large
% indentation looks wrong when the entry text itself is broken across
% lines. So we use a small indentation and put up with long leaders.
%
% \hangafter is reset to 1 (which is the value we want) at the start
% of each paragraph, so we need not do anything with that.
\hangindent=2em
%
% When the entry text needs to be broken, just fill out the first line
% with blank space.
\rightskip = 0pt plus1fil
%
% Start a ``paragraph'' for the index entry so the line breaking
% parameters we've set above will have an effect.
\noindent
%
% Insert the text of the index entry. TeX will do line-breaking on it.
#1%
% If there are no page numbers, don't output a line of dots.
\def\tempa{#2}
\def\tempb{}
\ifx\tempa\tempb\ \else
%
% If we must, put the page number on a line of its own, and fill out
% this line with blank space. (The \hfil is overwhelmed with the
% fill leaders glue in \indexdotfill if the page number does fit.)
\hfil\penalty50
\null\nobreak\indexdotfill % Have leaders before the page number.
%
% The `\ ' here is removed by the implicit \unskip that TeX does as
% part of (the primitive) \par. Without it, a spurious underfull
% \hbox ensues.
\ #2% The page number ends the paragraph.
\fi%
\par
\endgroup}

% Like \dotfill except takes at least 1 em.
\def\indexdotfill{\cleaders
\hbox{$\mathsurround=0pt \mkern1.5mu . \mkern1.5mu$}\hskip 1em plus 1fill}

\def\primary #1{\line{#1\hfil}}

\newskip\secondaryindent \secondaryindent=0.5cm

\def\secondary #1#2{
{\parfillskip=0in \parskip=0in
\hangindent =1in \hangafter=1
\noindent\hskip\secondaryindent\hbox{#1}\indexdotfill #2\par
}}

%% Define two-column mode, which is used in indexes.
%% Adapted from the TeXbook, page 416.
\catcode `\@=11

\newbox\partialpage

\newdimen\doublecolumnhsize

\def\begindoublecolumns{\begingroup
% Grab any single-column material above us.
\output = {\global\setbox\partialpage
=\vbox{\unvbox255\kern -\topskip \kern \baselineskip}}%
\eject
%
% Now switch to the double-column output routine.
\output={\doublecolumnout}%
%
% Change the page size parameters. We could do this once outside this
% routine, in each of @smallbook, @afourpaper, and the default 8.5x11
% format, but then we repeat the same computation. Repeating a couple
% of assignments once per index is clearly meaningless for the
% execution time, so we may as well do it once.
%
% First we halve the line length, less a little for the gutter between
% the columns. We compute the gutter based on the line length, so it
% changes automatically with the paper format. The magic constant
% below is chosen so that the gutter has the same value (well, +- <
% 1pt) as it did when we hard-coded it.
%
% We put the result in a separate register, \doublecolumhsize, so we
% can restore it in \pagesofar, after \hsize itself has (potentially)
% been clobbered.
%
\doublecolumnhsize = \hsize
\advance\doublecolumnhsize by -.04154\hsize
\divide\doublecolumnhsize by 2
\hsize = \doublecolumnhsize
%
% Double the \vsize as well. (We don't need a separate register here,
% since nobody clobbers \vsize.)
\vsize = 2\vsize
\doublecolumnpagegoal
}

\def\enddoublecolumns{\eject \endgroup \pagegoal=\vsize \unvbox\partialpage}

\def\doublecolumnsplit{\splittopskip=\topskip \splitmaxdepth=\maxdepth
\global\dimen@=\pageheight \global\advance\dimen@ by-\ht\partialpage
\global\setbox1=\vsplit255 to\dimen@ \global\setbox0=\vbox{\unvbox1}
\global\setbox3=\vsplit255 to\dimen@ \global\setbox2=\vbox{\unvbox3}
\ifdim\ht0>\dimen@ \setbox255=\vbox{\unvbox0\unvbox2} \global\setbox255=\copy5 \fi
\ifdim\ht2>\dimen@ \setbox255=\vbox{\unvbox0\unvbox2} \global\setbox255=\copy5 \fi
}
\def\doublecolumnpagegoal{%
\dimen@=\vsize \advance\dimen@ by-2\ht\partialpage \global\pagegoal=\dimen@
}
\def\pagesofar{\unvbox\partialpage %
\hsize=\doublecolumnhsize % have to restore this since output routine
\wd0=\hsize \wd2=\hsize \hbox to\pagewidth{\box0\hfil\box2}}
\def\doublecolumnout{%
\setbox5=\copy255
{\vbadness=10000 \doublecolumnsplit}
\ifvbox255
\setbox0=\vtop to\dimen@{\unvbox0}
\setbox2=\vtop to\dimen@{\unvbox2}
\onepageout\pagesofar \unvbox255 \penalty\outputpenalty
\else
\setbox0=\vbox{\unvbox5}
\ifvbox0
\dimen@=\ht0 \advance\dimen@ by\topskip \advance\dimen@ by-\baselineskip
\divide\dimen@ by2 \splittopskip=\topskip \splitmaxdepth=\maxdepth
{\vbadness=10000
\loop \global\setbox5=\copy0
\setbox1=\vsplit5 to\dimen@
\setbox3=\vsplit5 to\dimen@
\ifvbox5 \global\advance\dimen@ by1pt \repeat
\setbox0=\vbox to\dimen@{\unvbox1}
\setbox2=\vbox to\dimen@{\unvbox3}
\global\setbox\partialpage=\vbox{\pagesofar}
\doublecolumnpagegoal
}
\fi
\fi
}

\catcode `\@=\other
\message{sectioning,}
% Define chapters, sections, etc.

\newcount \chapno
\newcount \secno \secno=0
\newcount \subsecno \subsecno=0
\newcount \subsubsecno \subsubsecno=0

% This counter is funny since it counts through charcodes of letters A, B, ...
\newcount \appendixno \appendixno = `\@
\def\appendixletter{\char\the\appendixno}

\newwrite \contentsfile
% This is called from \setfilename.
\def\opencontents{\openout \contentsfile = \jobname.toc}

% Each @chapter defines this as the name of the chapter.
% page headings and footings can use it. @section does likewise

\def\thischapter{} \def\thissection{}
\def\seccheck#1{\if \pageno<0 %
\errmessage{@#1 not allowed after generating table of contents}\fi
%
}

\def\chapternofonts{%
\let\rawbackslash=\relax%
\let\frenchspacing=\relax%
\def\result{\realbackslash result}
\def\equiv{\realbackslash equiv}
\def\expansion{\realbackslash expansion}
\def\print{\realbackslash print}
\def\TeX{\realbackslash TeX}
\def\dots{\realbackslash dots}
\def\copyright{\realbackslash copyright}
\def\tt{\realbackslash tt}
\def\bf{\realbackslash bf }
\def\w{\realbackslash w}
\def\less{\realbackslash less}
\def\gtr{\realbackslash gtr}
\def\hat{\realbackslash hat}
\def\char{\realbackslash char}
\def\tclose##1{\realbackslash tclose {##1}}
\def\code##1{\realbackslash code {##1}}
\def\samp##1{\realbackslash samp {##1}}
\def\r##1{\realbackslash r {##1}}
\def\b##1{\realbackslash b {##1}}
\def\key##1{\realbackslash key {##1}}
\def\file##1{\realbackslash file {##1}}
\def\kbd##1{\realbackslash kbd {##1}}
% These are redefined because @smartitalic wouldn't work inside xdef.
\def\i##1{\realbackslash i {##1}}
\def\cite##1{\realbackslash cite {##1}}
\def\var##1{\realbackslash var {##1}}
\def\emph##1{\realbackslash emph {##1}}
\def\dfn##1{\realbackslash dfn {##1}}
}

\newcount\absseclevel % used to calculate proper heading level
\newcount\secbase\secbase=0 % @raise/lowersections modify this count

% @raisesections: treat @section as chapter, @subsection as section, etc.
\def\raisesections{\global\advance\secbase by -1}
\let\up=\raisesections % original BFox name

% @lowersections: treat @chapter as section, @section as subsection, etc.
\def\lowersections{\global\advance\secbase by 1}
\let\down=\lowersections % original BFox name

% Choose a numbered-heading macro
% #1 is heading level if unmodified by @raisesections or @lowersections
% #2 is text for heading
\def\numhead#1#2{\absseclevel=\secbase\advance\absseclevel by #1
\ifcase\absseclevel
\chapterzzz{#2}
\or
\seczzz{#2}
\or
\numberedsubseczzz{#2}
\or
\numberedsubsubseczzz{#2}
\else
\ifnum \absseclevel<0
\chapterzzz{#2}
\else
\numberedsubsubseczzz{#2}
\fi
\fi
}

% like \numhead, but chooses appendix heading levels
\def\apphead#1#2{\absseclevel=\secbase\advance\absseclevel by #1
\ifcase\absseclevel
\appendixzzz{#2}
\or
\appendixsectionzzz{#2}
\or
\appendixsubseczzz{#2}
\or
\appendixsubsubseczzz{#2}
\else
\ifnum \absseclevel<0
\appendixzzz{#2}
\else
\appendixsubsubseczzz{#2}
\fi
\fi
}

% like \numhead, but chooses numberless heading levels
\def\unnmhead#1#2{\absseclevel=\secbase\advance\absseclevel by #1
\ifcase\absseclevel
\unnumberedzzz{#2}
\or
\unnumberedseczzz{#2}
\or
\unnumberedsubseczzz{#2}
\or
\unnumberedsubsubseczzz{#2}
\else
\ifnum \absseclevel<0
\unnumberedzzz{#2}
\else
\unnumberedsubsubseczzz{#2}
\fi
\fi
}


\def\thischaptername{No Chapter Title}
\outer\def\chapter{\parsearg\chapteryyy}
\def\chapteryyy #1{\numhead0{#1}} % normally numhead0 calls chapterzzz
\def\chapterzzz #1{\seccheck{chapter}%
\secno=0 \subsecno=0 \subsubsecno=0
\global\advance \chapno by 1 \message{Chapter \the\chapno}%
\chapmacro {#1}{\the\chapno}%
\gdef\thissection{#1}%
\gdef\thischaptername{#1}%
% We don't substitute the actual chapter name into \thischapter
% because we don't want its macros evaluated now.
\xdef\thischapter{Chapter \the\chapno: \noexpand\thischaptername}%
{\chapternofonts%
\edef\temp{{\realbackslash chapentry {#1}{\the\chapno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\donoderef %
\global\let\section = \numberedsec
\global\let\subsection = \numberedsubsec
\global\let\subsubsection = \numberedsubsubsec
}}

\outer\def\appendix{\parsearg\appendixyyy}
\def\appendixyyy #1{\apphead0{#1}} % normally apphead0 calls appendixzzz
\def\appendixzzz #1{\seccheck{appendix}%
\secno=0 \subsecno=0 \subsubsecno=0
\global\advance \appendixno by 1 \message{Appendix \appendixletter}%
\chapmacro {#1}{Appendix \appendixletter}%
\gdef\thissection{#1}%
\gdef\thischaptername{#1}%
\xdef\thischapter{Appendix \appendixletter: \noexpand\thischaptername}%
{\chapternofonts%
\edef\temp{{\realbackslash chapentry
{#1}{Appendix \appendixletter}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\appendixnoderef %
\global\let\section = \appendixsec
\global\let\subsection = \appendixsubsec
\global\let\subsubsection = \appendixsubsubsec
}}

\outer\def\top{\parsearg\unnumberedyyy}
\outer\def\unnumbered{\parsearg\unnumberedyyy}
\def\unnumberedyyy #1{\unnmhead0{#1}} % normally unnmhead0 calls unnumberedzzz
\def\unnumberedzzz #1{\seccheck{unnumbered}%
\secno=0 \subsecno=0 \subsubsecno=0
%
% This used to be simply \message{#1}, but TeX fully expands the
% argument to \message. Therefore, if #1 contained @-commands, TeX
% expanded them. For example, in `@unnumbered The @cite{Book}', TeX
% expanded @cite (which turns out to cause errors because \cite is meant
% to be executed, not expanded).
%
% Anyway, we don't want the fully-expanded definition of @cite to appear
% as a result of the \message, we just want `@cite' itself. We use
% \the to achieve this: TeX expands \the only once,
% simply yielding the contents of the .
\toks0 = {#1}\message{(\the\toks0)}%
%
\unnumbchapmacro {#1}%
\gdef\thischapter{#1}\gdef\thissection{#1}%
{\chapternofonts%
\edef\temp{{\realbackslash unnumbchapentry {#1}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\unnumbnoderef %
\global\let\section = \unnumberedsec
\global\let\subsection = \unnumberedsubsec
\global\let\subsubsection = \unnumberedsubsubsec
}}

\outer\def\numberedsec{\parsearg\secyyy}
\def\secyyy #1{\numhead1{#1}} % normally calls seczzz
\def\seczzz #1{\seccheck{section}%
\subsecno=0 \subsubsecno=0 \global\advance \secno by 1 %
\gdef\thissection{#1}\secheading {#1}{\the\chapno}{\the\secno}%
{\chapternofonts%
\edef\temp{{\realbackslash secentry %
{#1}{\the\chapno}{\the\secno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\donoderef %
\penalty 10000 %
}}

\outer\def\appenixsection{\parsearg\appendixsecyyy}
\outer\def\appendixsec{\parsearg\appendixsecyyy}
\def\appendixsecyyy #1{\apphead1{#1}} % normally calls appendixsectionzzz
\def\appendixsectionzzz #1{\seccheck{appendixsection}%
\subsecno=0 \subsubsecno=0 \global\advance \secno by 1 %
\gdef\thissection{#1}\secheading {#1}{\appendixletter}{\the\secno}%
{\chapternofonts%
\edef\temp{{\realbackslash secentry %
{#1}{\appendixletter}{\the\secno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\appendixnoderef %
\penalty 10000 %
}}

\outer\def\unnumberedsec{\parsearg\unnumberedsecyyy}
\def\unnumberedsecyyy #1{\unnmhead1{#1}} % normally calls unnumberedseczzz
\def\unnumberedseczzz #1{\seccheck{unnumberedsec}%
\plainsecheading {#1}\gdef\thissection{#1}%
{\chapternofonts%
\edef\temp{{\realbackslash unnumbsecentry{#1}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\unnumbnoderef %
\penalty 10000 %
}}

\outer\def\numberedsubsec{\parsearg\numberedsubsecyyy}
\def\numberedsubsecyyy #1{\numhead2{#1}} % normally calls numberedsubseczzz
\def\numberedsubseczzz #1{\seccheck{subsection}%
\gdef\thissection{#1}\subsubsecno=0 \global\advance \subsecno by 1 %
\subsecheading {#1}{\the\chapno}{\the\secno}{\the\subsecno}%
{\chapternofonts%
\edef\temp{{\realbackslash subsecentry %
{#1}{\the\chapno}{\the\secno}{\the\subsecno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\donoderef %
\penalty 10000 %
}}

\outer\def\appendixsubsec{\parsearg\appendixsubsecyyy}
\def\appendixsubsecyyy #1{\apphead2{#1}} % normally calls appendixsubseczzz
\def\appendixsubseczzz #1{\seccheck{appendixsubsec}%
\gdef\thissection{#1}\subsubsecno=0 \global\advance \subsecno by 1 %
\subsecheading {#1}{\appendixletter}{\the\secno}{\the\subsecno}%
{\chapternofonts%
\edef\temp{{\realbackslash subsecentry %
{#1}{\appendixletter}{\the\secno}{\the\subsecno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\appendixnoderef %
\penalty 10000 %
}}

\outer\def\unnumberedsubsec{\parsearg\unnumberedsubsecyyy}
\def\unnumberedsubsecyyy #1{\unnmhead2{#1}} %normally calls unnumberedsubseczzz
\def\unnumberedsubseczzz #1{\seccheck{unnumberedsubsec}%
\plainsecheading {#1}\gdef\thissection{#1}%
{\chapternofonts%
\edef\temp{{\realbackslash unnumbsubsecentry{#1}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\unnumbnoderef %
\penalty 10000 %
}}

\outer\def\numberedsubsubsec{\parsearg\numberedsubsubsecyyy}
\def\numberedsubsubsecyyy #1{\numhead3{#1}} % normally numberedsubsubseczzz
\def\numberedsubsubseczzz #1{\seccheck{subsubsection}%
\gdef\thissection{#1}\global\advance \subsubsecno by 1 %
\subsubsecheading {#1}
{\the\chapno}{\the\secno}{\the\subsecno}{\the\subsubsecno}%
{\chapternofonts%
\edef\temp{{\realbackslash subsubsecentry %
{#1}
{\the\chapno}{\the\secno}{\the\subsecno}{\the\subsubsecno}
{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\donoderef %
\penalty 10000 %
}}

\outer\def\appendixsubsubsec{\parsearg\appendixsubsubsecyyy}
\def\appendixsubsubsecyyy #1{\apphead3{#1}} % normally appendixsubsubseczzz
\def\appendixsubsubseczzz #1{\seccheck{appendixsubsubsec}%
\gdef\thissection{#1}\global\advance \subsubsecno by 1 %
\subsubsecheading {#1}
{\appendixletter}{\the\secno}{\the\subsecno}{\the\subsubsecno}%
{\chapternofonts%
\edef\temp{{\realbackslash subsubsecentry{#1}%
{\appendixletter}
{\the\secno}{\the\subsecno}{\the\subsubsecno}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\appendixnoderef %
\penalty 10000 %
}}

\outer\def\unnumberedsubsubsec{\parsearg\unnumberedsubsubsecyyy}
\def\unnumberedsubsubsecyyy #1{\unnmhead3{#1}} %normally unnumberedsubsubseczzz
\def\unnumberedsubsubseczzz #1{\seccheck{unnumberedsubsubsec}%
\plainsecheading {#1}\gdef\thissection{#1}%
{\chapternofonts%
\edef\temp{{\realbackslash unnumbsubsubsecentry{#1}{\noexpand\folio}}}%
\escapechar=`\\%
\write \contentsfile \temp %
\unnumbnoderef %
\penalty 10000 %
}}

% These are variants which are not "outer", so they can appear in @ifinfo.
% Actually, they should now be obsolete; ordinary section commands should work.
\def\infotop{\parsearg\unnumberedzzz}
\def\infounnumbered{\parsearg\unnumberedzzz}
\def\infounnumberedsec{\parsearg\unnumberedseczzz}
\def\infounnumberedsubsec{\parsearg\unnumberedsubseczzz}
\def\infounnumberedsubsubsec{\parsearg\unnumberedsubsubseczzz}

\def\infoappendix{\parsearg\appendixzzz}
\def\infoappendixsec{\parsearg\appendixseczzz}
\def\infoappendixsubsec{\parsearg\appendixsubseczzz}
\def\infoappendixsubsubsec{\parsearg\appendixsubsubseczzz}

\def\infochapter{\parsearg\chapterzzz}
\def\infosection{\parsearg\sectionzzz}
\def\infosubsection{\parsearg\subsectionzzz}
\def\infosubsubsection{\parsearg\subsubsectionzzz}

% These macros control what the section commands do, according
% to what kind of chapter we are in (ordinary, appendix, or unnumbered).
% Define them by default for a numbered chapter.
\global\let\section = \numberedsec
\global\let\subsection = \numberedsubsec
\global\let\subsubsection = \numberedsubsubsec

% Define @majorheading, @heading and @subheading

% NOTE on use of \vbox for chapter headings, section headings, and
% such:
% 1) We use \vbox rather than the earlier \line to permit
% overlong headings to fold.
% 2) \hyphenpenalty is set to 10000 because hyphenation in a
% heading is obnoxious; this forbids it.
% 3) Likewise, headings look best if no \parindent is used, and
% if justification is not attempted. Hence \raggedright.


\def\majorheading{\parsearg\majorheadingzzz}
\def\majorheadingzzz #1{%
{\advance\chapheadingskip by 10pt \chapbreak }%
{\chapfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}\bigskip \par\penalty 200}

\def\chapheading{\parsearg\chapheadingzzz}
\def\chapheadingzzz #1{\chapbreak %
{\chapfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}\bigskip \par\penalty 200}

\def\heading{\parsearg\secheadingi}

\def\subheading{\parsearg\subsecheadingi}

\def\subsubheading{\parsearg\subsubsecheadingi}

% These macros generate a chapter, section, etc. heading only
% (including whitespace, linebreaking, etc. around it),
% given all the information in convenient, parsed form.

%%% Args are the skip and penalty (usually negative)
\def\dobreak#1#2{\par\ifdim\lastskip<#1\removelastskip\penalty#2\vskip#1\fi}

\def\setchapterstyle #1 {\csname CHAPF#1\endcsname}

%%% Define plain chapter starts, and page on/off switching for it
% Parameter controlling skip before chapter headings (if needed)

\newskip \chapheadingskip \chapheadingskip = 30pt plus 8pt minus 4pt

\def\chapbreak{\dobreak \chapheadingskip {-4000}}
\def\chappager{\par\vfill\supereject}
\def\chapoddpage{\chappager \ifodd\pageno \else \hbox to 0pt{} \chappager\fi}

\def\setchapternewpage #1 {\csname CHAPPAG#1\endcsname}

\def\CHAPPAGoff{
\global\let\pchapsepmacro=\chapbreak
\global\let\pagealignmacro=\chappager}

\def\CHAPPAGon{
\global\let\pchapsepmacro=\chappager
\global\let\pagealignmacro=\chappager
\global\def\HEADINGSon{\HEADINGSsingle}}

\def\CHAPPAGodd{
\global\let\pchapsepmacro=\chapoddpage
\global\let\pagealignmacro=\chapoddpage
\global\def\HEADINGSon{\HEADINGSdouble}}

\CHAPPAGon

\def\CHAPFplain{
\global\let\chapmacro=\chfplain
\global\let\unnumbchapmacro=\unnchfplain}

\def\chfplain #1#2{%
\pchapsepmacro
{%
\chapfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #2\enspace #1}%
}%
\bigskip
\penalty5000
}

\def\unnchfplain #1{%
\pchapsepmacro %
{\chapfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}\bigskip \par\penalty 10000 %
}
\CHAPFplain % The default

\def\unnchfopen #1{%
\chapoddpage {\chapfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}\bigskip \par\penalty 10000 %
}

\def\chfopen #1#2{\chapoddpage {\chapfonts
\vbox to 3in{\vfil \hbox to\hsize{\hfil #2} \hbox to\hsize{\hfil #1} \vfil}}%
\par\penalty 5000 %
}

\def\CHAPFopen{
\global\let\chapmacro=\chfopen
\global\let\unnumbchapmacro=\unnchfopen}

% Parameter controlling skip before section headings.

\newskip \subsecheadingskip \subsecheadingskip = 17pt plus 8pt minus 4pt
\def\subsecheadingbreak{\dobreak \subsecheadingskip {-500}}

\newskip \secheadingskip \secheadingskip = 21pt plus 8pt minus 4pt
\def\secheadingbreak{\dobreak \secheadingskip {-1000}}

% @paragraphindent is defined for the Info formatting commands only.
\let\paragraphindent=\comment

% Section fonts are the base font at magstep2, which produces
% a size a bit more than 14 points in the default situation.

\def\secheading #1#2#3{\secheadingi {#2.#3\enspace #1}}
\def\plainsecheading #1{\secheadingi {#1}}
\def\secheadingi #1{{\advance \secheadingskip by \parskip %
\secheadingbreak}%
{\secfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}%
\ifdim \parskip<10pt \kern 10pt\kern -\parskip\fi \penalty 10000 }


% Subsection fonts are the base font at magstep1,
% which produces a size of 12 points.

\def\subsecheading #1#2#3#4{\subsecheadingi {#2.#3.#4\enspace #1}}
\def\subsecheadingi #1{{\advance \subsecheadingskip by \parskip %
\subsecheadingbreak}%
{\subsecfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}%
\ifdim \parskip<10pt \kern 10pt\kern -\parskip\fi \penalty 10000 }

\def\subsubsecfonts{\subsecfonts} % Maybe this should change:
% Perhaps make sssec fonts scaled
% magstep half
\def\subsubsecheading #1#2#3#4#5{\subsubsecheadingi {#2.#3.#4.#5\enspace #1}}
\def\subsubsecheadingi #1{{\advance \subsecheadingskip by \parskip %
\subsecheadingbreak}%
{\subsubsecfonts \vbox{\hyphenpenalty=10000\tolerance=5000
\parindent=0pt\raggedright
\rm #1\hfill}}%
\ifdim \parskip<10pt \kern 10pt\kern -\parskip\fi \penalty 10000}


\message{toc printing,}

% Finish up the main text and prepare to read what we've written
% to \contentsfile.

\newskip\contentsrightmargin \contentsrightmargin=1in
\def\startcontents#1{%
\pagealignmacro
\immediate\closeout \contentsfile
\ifnum \pageno>0
\pageno = -1 % Request roman numbered pages.
\fi
% Don't need to put `Contents' or `Short Contents' in the headline.
% It is abundantly clear what they are.
\unnumbchapmacro{#1}\def\thischapter{}%
\begingroup % Set up to handle contents files properly.
\catcode`\\=0 \catcode`\{=1 \catcode`\}=2 \catcode`\@=11
\raggedbottom % Worry more about breakpoints than the bottom.
\advance\hsize by -\contentsrightmargin % Don't use the full line length.
}


% Normal (long) toc.
\outer\def\contents{%
\startcontents{Table of Contents}%
\input \jobname.toc
\endgroup
\vfill \eject
}

% And just the chapters.
\outer\def\summarycontents{%
\startcontents{Short Contents}%
%
\let\chapentry = \shortchapentry
\let\unnumbchapentry = \shortunnumberedentry
% We want a true roman here for the page numbers.
\secfonts
\let\rm=\shortcontrm \let\bf=\shortcontbf \let\sl=\shortcontsl
\rm
\advance\baselineskip by 1pt % Open it up a little.
\def\secentry ##1##2##3##4{}
\def\unnumbsecentry ##1##2{}
\def\subsecentry ##1##2##3##4##5{}
\def\unnumbsubsecentry ##1##2{}
\def\subsubsecentry ##1##2##3##4##5##6{}
\def\unnumbsubsubsecentry ##1##2{}
\input \jobname.toc
\endgroup
\vfill \eject
}
\let\shortcontents = \summarycontents

% These macros generate individual entries in the table of contents.
% The first argument is the chapter or section name.
% The last argument is the page number.
% The arguments in between are the chapter number, section number, ...

% Chapter-level things, for both the long and short contents.
\def\chapentry#1#2#3{\dochapentry{#2\labelspace#1}{#3}}

% See comments in \dochapentry re vbox and related settings
\def\shortchapentry#1#2#3{%
\tocentry{\shortchaplabel{#2}\labelspace #1}{\doshortpageno{#3}}%
}

% Typeset the label for a chapter or appendix for the short contents.
% The arg is, e.g. `Appendix A' for an appendix, or `3' for a chapter.
% We could simplify the code here by writing out an \appendixentry
% command in the toc file for appendices, instead of using \chapentry
% for both, but it doesn't seem worth it.
\setbox0 = \hbox{\shortcontrm Appendix }
\newdimen\shortappendixwidth \shortappendixwidth = \wd0

\def\shortchaplabel#1{%
% We typeset #1 in a box of constant width, regardless of the text of
% #1, so the chapter titles will come out aligned.
\setbox0 = \hbox{#1}%
\dimen0 = \ifdim\wd0 > \shortappendixwidth \shortappendixwidth \else 0pt \fi
%
% This space should be plenty, since a single number is .5em, and the
% widest letter (M) is 1em, at least in the Computer Modern fonts.
% (This space doesn't include the extra space that gets added after
% the label; that gets put in in \shortchapentry above.)
\advance\dimen0 by 1.1em
\hbox to \dimen0{#1\hfil}%
}

\def\unnumbchapentry#1#2{\dochapentry{#1}{#2}}
\def\shortunnumberedentry#1#2{\tocentry{#1}{\doshortpageno{#2}}}

% Sections.
\def\secentry#1#2#3#4{\dosecentry{#2.#3\labelspace#1}{#4}}
\def\unnumbsecentry#1#2{\dosecentry{#1}{#2}}

% Subsections.
\def\subsecentry#1#2#3#4#5{\dosubsecentry{#2.#3.#4\labelspace#1}{#5}}
\def\unnumbsubsecentry#1#2{\dosubsecentry{#1}{#2}}

% And subsubsections.
\def\subsubsecentry#1#2#3#4#5#6{%
\dosubsubsecentry{#2.#3.#4.#5\labelspace#1}{#6}}
\def\unnumbsubsubsecentry#1#2{\dosubsubsecentry{#1}{#2}}


% This parameter controls the indentation of the various levels.
\newdimen\tocindent \tocindent = 3pc

% Now for the actual typesetting. In all these, #1 is the text and #2 is the
% page number.
%
% If the toc has to be broken over pages, we would want to be at chapters
% if at all possible; hence the \penalty.
\def\dochapentry#1#2{%
\penalty-300 \vskip\baselineskip
\begingroup
\chapentryfonts
\tocentry{#1}{\dopageno{#2}}%
\endgroup
\nobreak\vskip .25\baselineskip
}

\def\dosecentry#1#2{\begingroup
\secentryfonts \leftskip=\tocindent
\tocentry{#1}{\dopageno{#2}}%
\endgroup}

\def\dosubsecentry#1#2{\begingroup
\subsecentryfonts \leftskip=2\tocindent
\tocentry{#1}{\dopageno{#2}}%
\endgroup}

\def\dosubsubsecentry#1#2{\begingroup
\subsubsecentryfonts \leftskip=3\tocindent
\tocentry{#1}{\dopageno{#2}}%
\endgroup}

% Final typesetting of a toc entry; we use the same \entry macro as for
% the index entries, but we want to suppress hyphenation here. (We
% can't do that in the \entry macro, since index entries might consist
% of hyphenated-identifiers-that-do-not-fit-on-a-line-and-nothing-else.)
%
\def\tocentry#1#2{\begingroup
\hyphenpenalty = 10000
\entry{#1}{#2}%
\endgroup}

% Space between chapter (or whatever) number and the title.
\def\labelspace{\hskip1em \relax}

\def\dopageno#1{{\rm #1}}
\def\doshortpageno#1{{\rm #1}}

\def\chapentryfonts{\secfonts \rm}
\def\secentryfonts{\textfonts}
\let\subsecentryfonts = \textfonts
\let\subsubsecentryfonts = \textfonts


\message{environments,}

% Since these characters are used in examples, it should be an even number of
% \tt widths. Each \tt character is 1en, so two makes it 1em.
% Furthermore, these definitions must come after we define our fonts.
\newbox\dblarrowbox \newbox\longdblarrowbox
\newbox\pushcharbox \newbox\bullbox
\newbox\equivbox \newbox\errorbox

\let\ptexequiv = \equiv

%{\tentt
%\global\setbox\dblarrowbox = \hbox to 1em{\hfil$\Rightarrow$\hfil}
%\global\setbox\longdblarrowbox = \hbox to 1em{\hfil$\mapsto$\hfil}
%\global\setbox\pushcharbox = \hbox to 1em{\hfil$\dashv$\hfil}
%\global\setbox\equivbox = \hbox to 1em{\hfil$\ptexequiv$\hfil}
% Adapted from the manmac format (p.420 of TeXbook)
%\global\setbox\bullbox = \hbox to 1em{\kern.15em\vrule height .75ex width .85ex
% depth .1ex\hfil}
%}

\def\point{$\star$}

\def\result{\leavevmode\raise.15ex\hbox to 1em{\hfil$\Rightarrow$\hfil}}
\def\expansion{\leavevmode\raise.1ex\hbox to 1em{\hfil$\mapsto$\hfil}}
\def\print{\leavevmode\lower.1ex\hbox to 1em{\hfil$\dashv$\hfil}}

\def\equiv{\leavevmode\lower.1ex\hbox to 1em{\hfil$\ptexequiv$\hfil}}

% Adapted from the TeXbook's \boxit.
{\tentt \global\dimen0 = 3em}% Width of the box.
\dimen2 = .55pt % Thickness of rules
% The text. (`r' is open on the right, `e' somewhat less so on the left.)
\setbox0 = \hbox{\kern-.75pt \tensf error\kern-1.5pt}

\global\setbox\errorbox=\hbox to \dimen0{\hfil
\hsize = \dimen0 \advance\hsize by -5.8pt % Space to left+right.
\advance\hsize by -2\dimen2 % Rules.
\vbox{
\hrule height\dimen2
\hbox{\vrule width\dimen2 \kern3pt % Space to left of text.
\vtop{\kern2.4pt \box0 \kern2.4pt}% Space above/below.
\kern3pt\vrule width\dimen2}% Space to right.
\hrule height\dimen2}
\hfil}

% The @error{} command.
\def\error{\leavevmode\lower.7ex\copy\errorbox}

% @tex ... @end tex escapes into raw Tex temporarily.
% One exception: @ is still an escape character, so that @end tex works.
% But \@ or @@ will get a plain tex @ character.

\def\tex{\begingroup
\catcode `\\=0 \catcode `\{=1 \catcode `\}=2
\catcode `\$=3 \catcode `\&=4 \catcode `\#=6
\catcode `\^=7 \catcode `\_=8 \catcode `\~=13 \let~=\tie
\catcode `\%=14
\catcode 43=12
\catcode`\"=12
\catcode`\==12
\catcode`\|=12
\catcode`\<=12
\catcode`\>=12
\escapechar=`\\
%
\let\{=\ptexlbrace
\let\}=\ptexrbrace
\let\.=\ptexdot
\let\*=\ptexstar
\let\dots=\ptexdots
\def\@{@}%
\let\bullet=\ptexbullet
\let\b=\ptexb \let\c=\ptexc \let\i=\ptexi \let\t=\ptext \let\l=\ptexl
\let\L=\ptexL
%
\let\Etex=\endgroup}

% Define @lisp ... @endlisp.
% @lisp does a \begingroup so it can rebind things,
% including the definition of @endlisp (which normally is erroneous).

% Amount to narrow the margins by for @lisp.
\newskip\lispnarrowing \lispnarrowing=0.4in

% This is the definition that ^^M gets inside @lisp, @example, and other
% such environments. \null is better than a space, since it doesn't
% have any width.
\def\lisppar{\null\endgraf}

% Make each space character in the input produce a normal interword
% space in the output. Don't allow a line break at this space, as this
% is used only in environments like @example, where each line of input
% should produce a line of output anyway.
%
{\obeyspaces %
\gdef\sepspaces{\obeyspaces\let =\tie}}

% Define \obeyedspace to be our active space, whatever it is. This is
% for use in \parsearg.
{\sepspaces %
\global\let\obeyedspace= }

% This space is always present above and below environments.
\newskip\envskipamount \envskipamount = 0pt

% Make spacing and below environment symmetrical. We use \parskip here
% to help in doing that, since in @example-like environments \parskip
% is reset to zero; thus the \afterenvbreak inserts no space -- but the
% start of the next paragraph will insert \parskip
%
\def\aboveenvbreak{{\advance\envskipamount by \parskip
\endgraf \ifdim\lastskip<\envskipamount
\removelastskip \penalty-50 \vskip\envskipamount \fi}}

\let\afterenvbreak = \aboveenvbreak

% \nonarrowing is a flag. If "set", @lisp etc don't narrow margins.
\let\nonarrowing=\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \cartouche: draw rectangle w/rounded corners around argument
\font\circle=lcircle10
\newdimen\circthick
\newdimen\cartouter\newdimen\cartinner
\newskip\normbskip\newskip\normpskip\newskip\normlskip
\circthick=\fontdimen8\circle
%
\def\ctl{{\circle\char'013\hskip -6pt}}% 6pt from pl file: 1/2charwidth
\def\ctr{{\hskip 6pt\circle\char'010}}
\def\cbl{{\circle\char'012\hskip -6pt}}
\def\cbr{{\hskip 6pt\circle\char'011}}
\def\carttop{\hbox to \cartouter{\hskip\lskip
\ctl\leaders\hrule height\circthick\hfil\ctr
\hskip\rskip}}
\def\cartbot{\hbox to \cartouter{\hskip\lskip
\cbl\leaders\hrule height\circthick\hfil\cbr
\hskip\rskip}}
%
\newskip\lskip\newskip\rskip

\long\def\cartouche{%
\begingroup
\lskip=\leftskip \rskip=\rightskip
\leftskip=0pt\rightskip=0pt %we want these *outside*.
\cartinner=\hsize \advance\cartinner by-\lskip
\advance\cartinner by-\rskip
\cartouter=\hsize
\advance\cartouter by 18pt % allow for 3pt kerns on either
% side, and for 6pt waste from
% each corner char
\normbskip=\baselineskip \normpskip=\parskip \normlskip=\lineskip
% Flag to tell @lisp, etc., not to narrow margin.
\let\nonarrowing=\comment
\vbox\bgroup
\baselineskip=0pt\parskip=0pt\lineskip=0pt
\carttop
\hbox\bgroup
\hskip\lskip
\vrule\kern3pt
\vbox\bgroup
\hsize=\cartinner
\kern3pt
\begingroup
\baselineskip=\normbskip
\lineskip=\normlskip
\parskip=\normpskip
\vskip -\parskip
\def\Ecartouche{%
\endgroup
\kern3pt
\egroup
\kern3pt\vrule
\hskip\rskip
\egroup
\cartbot
\egroup
\endgroup
}}


% This macro is called at the beginning of all the @example variants,
% inside a group.
\def\nonfillstart{%
\aboveenvbreak
\inENV % This group ends at the end of the body
\hfuzz = 12pt % Don't be fussy
\sepspaces % Make spaces be word-separators rather than space tokens.
\singlespace
\let\par = \lisppar % don't ignore blank lines
\obeylines % each line of input is a line of output
\parskip = 0pt
\parindent = 0pt
\emergencystretch = 0pt % don't try to avoid overfull boxes
% @cartouche defines \nonarrowing to inhibit narrowing
% at next level down.
\ifx\nonarrowing\relax
\advance \leftskip by \lispnarrowing
\exdentamount=\lispnarrowing
\let\exdent=\nofillexdent
\let\nonarrowing=\relax
\fi
}

% To ending an @example-like environment, we first end the paragraph
% (via \afterenvbreak's vertical glue), and then the group. That way we
% keep the zero \parskip that the environments set -- \parskip glue
% will be inserted at the beginning of the next paragraph in the
% document, after the environment.
%
\def\nonfillfinish{\afterenvbreak\endgroup}%

% This macro is
\def\lisp{\begingroup
\nonfillstart
\let\Elisp = \nonfillfinish
\tt
\rawbackslash % have \ input char produce \ char from current font
\gobble
}

% Define the \E... control sequence only if we are inside the
% environment, so the error checking in \end will work.
%
% We must call \lisp last in the definition, since it reads the
% return following the @example (or whatever) command.
%
\def\example{\begingroup \def\Eexample{\nonfillfinish\endgroup}\lisp}
\def\smallexample{\begingroup \def\Esmallexample{\nonfillfinish\endgroup}\lisp}
\def\smalllisp{\begingroup \def\Esmalllisp{\nonfillfinish\endgroup}\lisp}

% @smallexample and @smalllisp. This is not used unless the @smallbook
% command is given. Originally contributed by Pavel@xerox.
%
\def\smalllispx{\begingroup
\nonfillstart
\let\Esmalllisp = \nonfillfinish
\let\Esmallexample = \nonfillfinish
%
% Smaller interline space and fonts for small examples.
\baselineskip 10pt
\indexfonts \tt
\rawbackslash % output the \ character from the current font
\gobble
}

% This is @display; same as @lisp except use roman font.
%
\def\display{\begingroup
\nonfillstart
\let\Edisplay = \nonfillfinish
\gobble
}

% This is @format; same as @display except don't narrow margins.
%
\def\format{\begingroup
\let\nonarrowing = t
\nonfillstart
\let\Eformat = \nonfillfinish
\gobble
}

% @flushleft (same as @format) and @flushright.
%
\def\flushleft{\begingroup
\let\nonarrowing = t
\nonfillstart
\let\Eflushleft = \nonfillfinish
\gobble
}
\def\flushright{\begingroup
\let\nonarrowing = t
\nonfillstart
\let\Eflushright = \nonfillfinish
\advance\leftskip by 0pt plus 1fill
\gobble}

% @quotation does normal linebreaking and narrows the margins.
%
\def\quotation{%
\begingroup\inENV %This group ends at the end of the @quotation body
{\parskip=0pt % because we will skip by \parskip too, later
\aboveenvbreak}%
\singlespace
\parindent=0pt
\let\Equotation = \nonfillfinish
% @cartouche defines \nonarrowing to inhibit narrowing
% at next level down.
\ifx\nonarrowing\relax
\advance \leftskip by \lispnarrowing
\advance \rightskip by \lispnarrowing
\exdentamount=\lispnarrowing
\let\nonarrowing=\relax
\fi}

\message{defuns,}
% Define formatter for defuns
% First, allow user to change definition object font (\df) internally
\def\setdeffont #1 {\csname DEF#1\endcsname}

\newskip\defbodyindent \defbodyindent=.4in
\newskip\defargsindent \defargsindent=50pt
\newskip\deftypemargin \deftypemargin=12pt
\newskip\deflastargmargin \deflastargmargin=18pt

\newcount\parencount
% define \functionparens, which makes ( and ) and & do special things.
% \functionparens affects the group it is contained in.
\def\activeparens{%
\catcode`\(=\active \catcode`\)=\active \catcode`\&=\active
\catcode`\[=\active \catcode`\]=\active}

% Make control sequences which act like normal parenthesis chars.
\let\lparen = ( \let\rparen = )

{\activeparens % Now, smart parens don't turn on until &foo (see \amprm)

% Be sure that we always have a definition for `(', etc. For example,
% if the fn name has parens in it, \boldbrax will not be in effect yet,
% so TeX would otherwise complain about undefined control sequence.
\global\let(=\lparen \global\let)=\rparen
\global\let[=\lbrack \global\let]=\rbrack

\gdef\functionparens{\boldbrax\let&=\amprm\parencount=0 }
\gdef\boldbrax{\let(=\opnr\let)=\clnr\let[=\lbrb\let]=\rbrb}

% Definitions of (, ) and & used in args for functions.
% This is the definition of ( outside of all parentheses.
\gdef\oprm#1 {{\rm\char`\(}#1 \bf \let(=\opnested %
\global\advance\parencount by 1 }
%
% This is the definition of ( when already inside a level of parens.
\gdef\opnested{\char`\(\global\advance\parencount by 1 }
%
\gdef\clrm{% Print a paren in roman if it is taking us back to depth of 0.
% also in that case restore the outer-level definition of (.
\ifnum \parencount=1 {\rm \char `\)}\sl \let(=\oprm \else \char `\) \fi
\global\advance \parencount by -1 }
% If we encounter &foo, then turn on ()-hacking afterwards
\gdef\amprm#1 {{\rm\}\let(=\oprm \let)=\clrm\ }
%
\gdef\normalparens{\boldbrax\let&=\ampnr}
} % End of definition inside \activeparens
%% These parens (in \boldbrax) actually are a little bolder than the
%% contained text. This is especially needed for [ and ]
\def\opnr{{\sf\char`\(}} \def\clnr{{\sf\char`\)}} \def\ampnr{\&}
\def\lbrb{{\bf\char`\[}} \def\rbrb{{\bf\char`\]}}

% First, defname, which formats the header line itself.
% #1 should be the function name.
% #2 should be the type of definition, such as "Function".

\def\defname #1#2{%
% Get the values of \leftskip and \rightskip as they were
% outside the @def...
\dimen2=\leftskip
\advance\dimen2 by -\defbodyindent
\dimen3=\rightskip
\advance\dimen3 by -\defbodyindent
\noindent %
\setbox0=\hbox{\hskip \deflastargmargin{\rm #2}\hskip \deftypemargin}%
\dimen0=\hsize \advance \dimen0 by -\wd0 % compute size for first line
\dimen1=\hsize \advance \dimen1 by -\defargsindent %size for continuations
\parshape 2 0in \dimen0 \defargsindent \dimen1 %
% Now output arg 2 ("Function" or some such)
% ending at \deftypemargin from the right margin,
% but stuck inside a box of width 0 so it does not interfere with linebreaking
{% Adjust \hsize to exclude the ambient margins,
% so that \rightline will obey them.
\advance \hsize by -\dimen2 \advance \hsize by -\dimen3
\rlap{\rightline{{\rm #2}\hskip \deftypemargin}}}%
% Make all lines underfull and no complaints:
\tolerance=10000 \hbadness=10000
\advance\leftskip by -\defbodyindent
\exdentamount=\defbodyindent
{\df #1}\enskip % Generate function name
}

% Actually process the body of a definition
% #1 should be the terminating control sequence, such as \Edefun.
% #2 should be the "another name" control sequence, such as \defunx.
% #3 should be the control sequence that actually processes the header,
% such as \defunheader.

\def\defparsebody #1#2#3{\begingroup\inENV% Environment for definitionbody
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2{\begingroup\obeylines\activeparens\spacesplit#3}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup %
\catcode 61=\active % 61 is `='
\obeylines\activeparens\spacesplit#3}

\def\defmethparsebody #1#2#3#4 {\begingroup\inENV %
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2##1 {\begingroup\obeylines\activeparens\spacesplit{#3{##1}}}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup\obeylines\activeparens\spacesplit{#3{#4}}}

\def\defopparsebody #1#2#3#4#5 {\begingroup\inENV %
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2##1 ##2 {\def#4{##1}%
\begingroup\obeylines\activeparens\spacesplit{#3{##2}}}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup\obeylines\activeparens\spacesplit{#3{#5}}}

% These parsing functions are similar to the preceding ones
% except that they do not make parens into active characters.
% These are used for "variables" since they have no arguments.

\def\defvarparsebody #1#2#3{\begingroup\inENV% Environment for definitionbody
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2{\begingroup\obeylines\spacesplit#3}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup %
\catcode 61=\active %
\obeylines\spacesplit#3}

% This is used for \def{tp,vr}parsebody. It could probably be used for
% some of the others, too, with some judicious conditionals.
%
\def\parsebodycommon#1#2#3{%
\begingroup\inENV %
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2##1 {\begingroup\obeylines\spacesplit{#3{##1}}}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup\obeylines
}

\def\defvrparsebody#1#2#3#4 {%
\parsebodycommon{#1}{#2}{#3}%
\spacesplit{#3{#4}}%
}

% This loses on `@deftp {Data Type} {struct termios}' -- it thinks the
% type is just `struct', because we lose the braces in `{struct
% termios}' when \spacesplit reads its undelimited argument. Sigh.
% \let\deftpparsebody=\defvrparsebody
%
% So, to get around this, we put \empty in with the type name. That
% way, TeX won't find exactly `{...}' as an undelimited argument, and
% won't strip off the braces.
%
\def\deftpparsebody #1#2#3#4 {%
\parsebodycommon{#1}{#2}{#3}%
\spacesplit{\parsetpheaderline{#3{#4}}}\empty
}

% Fine, but then we have to eventually remove the \empty *and* the
% braces (if any). That's what this does, putting the result in \tptemp.
%
\def\removeemptybraces\empty#1\relax{\def\tptemp{#1}}%

% After \spacesplit has done its work, this is called -- #1 is the final
% thing to call, #2 the type name (which starts with \empty), and #3
% (which might be empty) the arguments.
%
\def\parsetpheaderline#1#2#3{%
\removeemptybraces#2\relax
#1{\tptemp}{#3}%
}%

\def\defopvarparsebody #1#2#3#4#5 {\begingroup\inENV %
\medbreak %
% Define the end token that this defining construct specifies
% so that it will exit this group.
\def#1{\endgraf\endgroup\medbreak}%
\def#2##1 ##2 {\def#4{##1}%
\begingroup\obeylines\spacesplit{#3{##2}}}%
\parindent=0in
\advance\leftskip by \defbodyindent \advance \rightskip by \defbodyindent
\exdentamount=\defbodyindent
\begingroup\obeylines\spacesplit{#3{#5}}}

% Split up #2 at the first space token.
% call #1 with two arguments:
% the first is all of #2 before the space token,
% the second is all of #2 after that space token.
% If #2 contains no space token, all of it is passed as the first arg
% and the second is passed as empty.

{\obeylines
\gdef\spacesplit#1#2^^M{\endgroup\spacesplitfoo{#1}#2 \relax\spacesplitfoo}%
\long\gdef\spacesplitfoo#1#2 #3#4\spacesplitfoo{%
\ifx\relax #3%
#1{#2}{}\else #1{#2}{#3#4}\fi}}

% So much for the things common to all kinds of definitions.

% Define @defun.

% First, define the processing that is wanted for arguments of \defun
% Use this to expand the args and terminate the paragraph they make up

\def\defunargs #1{\functionparens \sl
% Expand, preventing hyphenation at `-' chars.
% Note that groups don't affect changes in \hyphenchar.
\hyphenchar\tensl=0
#1%
\hyphenchar\tensl=45
\ifnum\parencount=0 \else \errmessage{unbalanced parens in @def arguments}\fi%
\interlinepenalty=10000
\advance\rightskip by 0pt plus 1fil
\endgraf\penalty 10000\vskip -\parskip\penalty 10000%
}

\def\deftypefunargs #1{%
% Expand, preventing hyphenation at `-' chars.
% Note that groups don't affect changes in \hyphenchar.
\functionparens
\code{#1}%
\interlinepenalty=10000
\advance\rightskip by 0pt plus 1fil
\endgraf\penalty 10000\vskip -\parskip\penalty 10000%
}

% Do complete processing of one @defun or @defunx line already parsed.

% @deffn Command forward-char nchars

\def\deffn{\defmethparsebody\Edeffn\deffnx\deffnheader}

\def\deffnheader #1#2#3{\doind {fn}{\code{#2}}%
\begingroup\defname {#2}{#1}\defunargs{#3}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% @defun == @deffn Function

\def\defun{\defparsebody\Edefun\defunx\defunheader}

\def\defunheader #1#2{\doind {fn}{\code{#1}}% Make entry in function index
\begingroup\defname {#1}{Function}%
\defunargs {#2}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% @deftypefun int foobar (int @var{foo}, float @var{bar})

\def\deftypefun{\defparsebody\Edeftypefun\deftypefunx\deftypefunheader}

% #1 is the data type. #2 is the name and args.
\def\deftypefunheader #1#2{\deftypefunheaderx{#1}#2 \relax}
% #1 is the data type, #2 the name, #3 the args.
\def\deftypefunheaderx #1#2 #3\relax{%
\doind {fn}{\code{#2}}% Make entry in function index
\begingroup\defname {\code{#1} #2}{Function}%
\deftypefunargs {#3}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% @deftypefn {Library Function} int foobar (int @var{foo}, float @var{bar})

\def\deftypefn{\defmethparsebody\Edeftypefn\deftypefnx\deftypefnheader}

% #1 is the classification. #2 is the data type. #3 is the name and args.
\def\deftypefnheader #1#2#3{\deftypefnheaderx{#1}{#2}#3 \relax}
% #1 is the classification, #2 the data type, #3 the name, #4 the args.
\def\deftypefnheaderx #1#2#3 #4\relax{%
\doind {fn}{\code{#3}}% Make entry in function index
\begingroup
\normalparens % notably, turn off `&' magic, which prevents
% at least some C++ text from working
\defname {\code{#2} #3}{#1}%
\deftypefunargs {#4}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% @defmac == @deffn Macro

\def\defmac{\defparsebody\Edefmac\defmacx\defmacheader}

\def\defmacheader #1#2{\doind {fn}{\code{#1}}% Make entry in function index
\begingroup\defname {#1}{Macro}%
\defunargs {#2}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% @defspec == @deffn Special Form

\def\defspec{\defparsebody\Edefspec\defspecx\defspecheader}

\def\defspecheader #1#2{\doind {fn}{\code{#1}}% Make entry in function index
\begingroup\defname {#1}{Special Form}%
\defunargs {#2}\endgroup %
\catcode 61=\other % Turn off change made in \defparsebody
}

% This definition is run if you use @defunx
% anywhere other than immediately after a @defun or @defunx.

\def\deffnx #1 {\errmessage{@deffnx in invalid context}}
\def\defunx #1 {\errmessage{@defunx in invalid context}}
\def\defmacx #1 {\errmessage{@defmacx in invalid context}}
\def\defspecx #1 {\errmessage{@defspecx in invalid context}}
\def\deftypefnx #1 {\errmessage{@deftypefnx in invalid context}}
\def\deftypeunx #1 {\errmessage{@deftypeunx in invalid context}}

% @defmethod, and so on

% @defop {Funny Method} foo-class frobnicate argument

\def\defop #1 {\def\defoptype{#1}%
\defopparsebody\Edefop\defopx\defopheader\defoptype}

\def\defopheader #1#2#3{%
\dosubind {fn}{\code{#2}}{on #1}% Make entry in function index
\begingroup\defname {#2}{\defoptype{} on #1}%
\defunargs {#3}\endgroup %
}

% @defmethod == @defop Method

\def\defmethod{\defmethparsebody\Edefmethod\defmethodx\defmethodheader}

\def\defmethodheader #1#2#3{%
\dosubind {fn}{\code{#2}}{on #1}% entry in function index
\begingroup\defname {#2}{Method on #1}%
\defunargs {#3}\endgroup %
}

% @defcv {Class Option} foo-class foo-flag

\def\defcv #1 {\def\defcvtype{#1}%
\defopvarparsebody\Edefcv\defcvx\defcvarheader\defcvtype}

\def\defcvarheader #1#2#3{%
\dosubind {vr}{\code{#2}}{of #1}% Make entry in var index
\begingroup\defname {#2}{\defcvtype{} of #1}%
\defvarargs {#3}\endgroup %
}

% @defivar == @defcv {Instance Variable}

\def\defivar{\defvrparsebody\Edefivar\defivarx\defivarheader}

\def\defivarheader #1#2#3{%
\dosubind {vr}{\code{#2}}{of #1}% Make entry in var index
\begingroup\defname {#2}{Instance Variable of #1}%
\defvarargs {#3}\endgroup %
}

% These definitions are run if you use @defmethodx, etc.,
% anywhere other than immediately after a @defmethod, etc.

\def\defopx #1 {\errmessage{@defopx in invalid context}}
\def\defmethodx #1 {\errmessage{@defmethodx in invalid context}}
\def\defcvx #1 {\errmessage{@defcvx in invalid context}}
\def\defivarx #1 {\errmessage{@defivarx in invalid context}}

% Now @defvar

% First, define the processing that is wanted for arguments of @defvar.
% This is actually simple: just print them in roman.
% This must expand the args and terminate the paragraph they make up
\def\defvarargs #1{\normalparens #1%
\interlinepenalty=10000
\endgraf\penalty 10000\vskip -\parskip\penalty 10000}

% @defvr Counter foo-count

\def\defvr{\defvrparsebody\Edefvr\defvrx\defvrheader}

\def\defvrheader #1#2#3{\doind {vr}{\code{#2}}%
\begingroup\defname {#2}{#1}\defvarargs{#3}\endgroup}

% @defvar == @defvr Variable

\def\defvar{\defvarparsebody\Edefvar\defvarx\defvarheader}

\def\defvarheader #1#2{\doind {vr}{\code{#1}}% Make entry in var index
\begingroup\defname {#1}{Variable}%
\defvarargs {#2}\endgroup %
}

% @defopt == @defvr {User Option}

\def\defopt{\defvarparsebody\Edefopt\defoptx\defoptheader}

\def\defoptheader #1#2{\doind {vr}{\code{#1}}% Make entry in var index
\begingroup\defname {#1}{User Option}%
\defvarargs {#2}\endgroup %
}

% @deftypevar int foobar

\def\deftypevar{\defvarparsebody\Edeftypevar\deftypevarx\deftypevarheader}

% #1 is the data type. #2 is the name.
\def\deftypevarheader #1#2{%
\doind {vr}{\code{#2}}% Make entry in variables index
\begingroup\defname {\code{#1} #2}{Variable}%
\interlinepenalty=10000
\endgraf\penalty 10000\vskip -\parskip\penalty 10000
\endgroup}

% @deftypevr {Global Flag} int enable

\def\deftypevr{\defvrparsebody\Edeftypevr\deftypevrx\deftypevrheader}

\def\deftypevrheader #1#2#3{\doind {vr}{\code{#3}}%
\begingroup\defname {\code{#2} #3}{#1}
\interlinepenalty=10000
\endgraf\penalty 10000\vskip -\parskip\penalty 10000
\endgroup}

% This definition is run if you use @defvarx
% anywhere other than immediately after a @defvar or @defvarx.

\def\defvrx #1 {\errmessage{@defvrx in invalid context}}
\def\defvarx #1 {\errmessage{@defvarx in invalid context}}
\def\defoptx #1 {\errmessage{@defoptx in invalid context}}
\def\deftypevarx #1 {\errmessage{@deftypevarx in invalid context}}
\def\deftypevrx #1 {\errmessage{@deftypevrx in invalid context}}

% Now define @deftp
% Args are printed in bold, a slight difference from @defvar.

\def\deftpargs #1{\bf \defvarargs{#1}}

% @deftp Class window height width ...

\def\deftp{\deftpparsebody\Edeftp\deftpx\deftpheader}

\def\deftpheader #1#2#3{\doind {tp}{\code{#2}}%
\begingroup\defname {#2}{#1}\deftpargs{#3}\endgroup}

% This definition is run if you use @deftpx, etc
% anywhere other than immediately after a @deftp, etc.

\def\deftpx #1 {\errmessage{@deftpx in invalid context}}

\message{cross reference,}
% Define cross-reference macros
\newwrite \auxfile

\newif\ifhavexrefs % True if xref values are known.
\newif\ifwarnedxrefs % True if we warned once that they aren't known.

% \setref{foo} defines a cross-reference point named foo.

\def\setref#1{%
\dosetq{#1-title}{Ytitle}%
\dosetq{#1-pg}{Ypagenumber}%
\dosetq{#1-snt}{Ysectionnumberandtype}}

\def\unnumbsetref#1{%
\dosetq{#1-title}{Ytitle}%
\dosetq{#1-pg}{Ypagenumber}%
\dosetq{#1-snt}{Ynothing}}

\def\appendixsetref#1{%
\dosetq{#1-title}{Ytitle}%
\dosetq{#1-pg}{Ypagenumber}%
\dosetq{#1-snt}{Yappendixletterandtype}}

% \xref, \pxref, and \ref generate cross-references to specified points.
% For \xrefX, #1 is the node name, #2 the name of the Info
% cross-reference, #3 the printed node name, #4 the name of the Info
% file, #5 the name of the printed manual. All but the node name can be
% omitted.
%
\def\pxref#1{see \xrefX[#1,,,,,,,]}
\def\xref#1{See \xrefX[#1,,,,,,,]}
\def\ref#1{\xrefX[#1,,,,,,,]}
\def\xrefX[#1,#2,#3,#4,#5,#6]{\begingroup%
\def\printedmanual{\ignorespaces #5}%
\def\printednodename{\ignorespaces #3}%
%
\setbox1=\hbox{\printedmanual}%
\setbox0=\hbox{\printednodename}%
\ifdim \wd0=0pt%
% No printed node name was explicitly given.
\ifx SETxref-automatic-section-title %
% This line should make the actual chapter or section title appear inside
% the square brackets. Use the real section title if we have it.
\ifdim \wd1>0pt%
% It is in another manual, so we don't have it.
\def\printednodename{\ignorespaces #1} \else%
% We know the real title if we have the xref values.
\ifhavexrefs \def\printednodename{\refx{#1-title}}%
% Otherwise just copy the Info node name.
\else \def\printednodename{\ignorespaces #1} \fi%
\fi\def\printednodename{#1-title}%
\else% This line just uses the node name.
\def\printednodename{\ignorespaces #1}%
\fi% ends \ifx SETxref-automatic-section-title
\fi% ends \ifdim \wd0
%
%
% If we use \unhbox0 and \unhbox1 to print the node names, TeX does
% not insert empty discretionaries after hyphens, which means that it
% will not find a line break at a hyphen in a node names. Since some
% manuals are best written with fairly long node names, containing
% hyphens, this is a loss. Therefore, we simply give the text of
% the node name again, so it is as if TeX is seeing it for the first
% time.
\ifdim \wd1>0pt
section ``\printednodename'' in \cite{\printedmanual}%
\else%
\turnoffactive%
\refx{#1-snt}{} [\printednodename], page\tie\refx{#1-pg}{}%
\fi
\endgroup}

% \dosetq is the interface for calls from other macros

% Use \turnoffactive so that punctuation chars such as underscore
% work in node names.
\def\dosetq #1#2{{\let\folio=0 \turnoffactive%
\edef\next{\write\auxfile{\internalsetq {#1}{#2}}}%
\next}}

% \internalsetq {foo}{page} expands into
% CHARACTERS 'xrdef {foo}{...expansion of \Ypage...}
% When the aux file is read, ' is the escape character

\def\internalsetq #1#2{'xrdef {#1}{\csname #2\endcsname}}

% Things to be expanded by \internalsetq

\def\Ypagenumber{\folio}

\def\Ytitle{\thissection}

\def\Ynothing{}

\def\Ysectionnumberandtype{%
\ifnum\secno=0 Chapter\xreftie\the\chapno %
\else \ifnum \subsecno=0 Section\xreftie\the\chapno.\the\secno %
\else \ifnum \subsubsecno=0 %
Section\xreftie\the\chapno.\the\secno.\the\subsecno %
\else %
Section\xreftie\the\chapno.\the\secno.\the\subsecno.\the\subsubsecno %
\fi \fi \fi }

\def\Yappendixletterandtype{%
\ifnum\secno=0 Appendix\xreftie'char\the\appendixno{}%
\else \ifnum \subsecno=0 Section\xreftie'char\the\appendixno.\the\secno %
\else \ifnum \subsubsecno=0 %
Section\xreftie'char\the\appendixno.\the\secno.\the\subsecno %
\else %
Section\xreftie'char\the\appendixno.\the\secno.\the\subsecno.\the\subsubsecno %
\fi \fi \fi }

\gdef\xreftie{'tie}

% Use TeX 3.0's \inputlineno to get the line number, for better error
% messages, but if we're using an old version of TeX, don't do anything.
%
\ifx\inputlineno\thisisundefined
\let\linenumber = \empty % Non-3.0.
\else
\def\linenumber{\the\inputlineno:\space}
\fi

% Define \refx{NAME}{SUFFIX} to reference a cross-reference string named NAME.
% If its value is nonempty, SUFFIX is output afterward.

\def\refx#1#2{%
\expandafter\ifx\csname X#1\endcsname\relax
% If not defined, say something at least.
$\langle$un\-de\-fined$\rangle$%
\ifhavexrefs
\message{\linenumber Undefined cross reference `#1'.}%
\else
\ifwarnedxrefs\else
\global\warnedxrefstrue
\message{Cross reference values unknown; you must run TeX again.}%
\fi
\fi
\else
% It's defined, so just use it.
\csname X#1\endcsname
\fi
#2% Output the suffix in any case.
}

% Read the last existing aux file, if any. No error if none exists.

% This is the macro invoked by entries in the aux file.
\def\xrdef #1#2{
{\catcode`\'=\other\expandafter \gdef \csname X#1\endcsname {#2}}}

\def\readauxfile{%
\begingroup
\catcode `\^^@=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\^^C=\other
\catcode `\^^D=\other
\catcode `\^^E=\other
\catcode `\^^F=\other
\catcode `\^^G=\other
\catcode `\^^H=\other
\catcode `\ =\other
\catcode `\^^L=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode `\=\other
\catcode 26=\other
\catcode `\^^[=\other
\catcode `\^^\=\other
\catcode `\^^]=\other
\catcode `\^^^=\other
\catcode `\^^_=\other
\catcode `\@=\other
\catcode `\^=\other
\catcode `\~=\other
\catcode `\[=\other
\catcode `\]=\other
\catcode`\"=\other
\catcode`\_=\other
\catcode`\|=\other
\catcode`\<=\other
\catcode`\>=\other
\catcode `\$=\other
\catcode `\#=\other
\catcode `\&=\other
% `\+ does not work, so use 43.
\catcode 43=\other
% the aux file uses ' as the escape.
% Turn off \ as an escape so we do not lose on
% entries which were dumped with control sequences in their names.
% For example, 'xrdef {$\leq $-fun}{page ...} made by @defun ^^
% Reference to such entries still does not work the way one would wish,
% but at least they do not bomb out when the aux file is read in.
\catcode `\{=1 \catcode `\}=2
\catcode `\%=\other
\catcode `\'=0
\catcode `\\=\other
\openin 1 \jobname.aux
\ifeof 1 \else \closein 1 \input \jobname.aux \global\havexrefstrue
\global\warnedobstrue
\fi
% Open the new aux file. Tex will close it automatically at exit.
\openout \auxfile=\jobname.aux
\endgroup}


% Footnotes.

\newcount \footnoteno

% The trailing space in the following definition for supereject is
% vital for proper filling; pages come out unaligned when you do a
% pagealignmacro call if that space before the closing brace is
% removed.
\def\supereject{\par\penalty -20000\footnoteno =0 }

% @footnotestyle is meaningful for info output only..
\let\footnotestyle=\comment

\let\ptexfootnote=\footnote

{\catcode `\@=11
%
% Auto-number footnotes. Otherwise like plain.
\gdef\footnote{%
\global\advance\footnoteno by \@ne
\edef\thisfootno{$^{\the\footnoteno}$}%
%
% In case the footnote comes at the end of a sentence, preserve the
% extra spacing after we do the footnote number.
\let\@sf\empty
\ifhmode\edef\@sf{\spacefactor\the\spacefactor}\/\fi
%
% Remove inadvertent blank space before typesetting the footnote number.
\unskip
\thisfootno\@sf
\footnotezzz
}%

% Don't bother with the trickery in plain.tex to not require the
% footnote text as a parameter. Our footnotes don't need to be so general.
%
\long\gdef\footnotezzz#1{\insert\footins{%
% We want to typeset this text as a normal paragraph, even if the
% footnote reference occurs in (for example) a display environment.
% So reset some parameters.
\interlinepenalty\interfootnotelinepenalty
\splittopskip\ht\strutbox % top baseline for broken footnotes
\splitmaxdepth\dp\strutbox
\floatingpenalty\@MM
\leftskip\z@skip
\rightskip\z@skip
\spaceskip\z@skip
\xspaceskip\z@skip
\parindent\defaultparindent
%
% Hang the footnote text off the number.
\hang
\textindent{\thisfootno}%
%
% Don't crash into the line above the footnote text. Since this
% expands into a box, it must come within the paragraph, lest it
% provide a place where TeX can split the footnote.
\footstrut
#1\strut}%
}

}%end \catcode `\@=11

% Set the baselineskip to #1, and the lineskip and strut size
% correspondingly. There is no deep meaning behind these magic numbers
% used as factors; they just match (closely enough) what Knuth defined.
%
\def\lineskipfactor{.08333}
\def\strutheightpercent{.70833}
\def\strutdepthpercent {.29167}
%
\def\setleading#1{%
\normalbaselineskip = #1\relax
\normallineskip = \lineskipfactor\normalbaselineskip
\normalbaselines
\setbox\strutbox =\hbox{%
\vrule width0pt height\strutheightpercent\baselineskip
depth \strutdepthpercent \baselineskip
}%
}

% @| inserts a changebar to the left of the current line. It should
% surround any changed text. This approach does *not* work if the
% change spans more than two lines of output. To handle that, we would
% have adopt a much more difficult approach (putting marks into the main
% vertical list for the beginning and end of each change).
%
\def\|{%
% \vadjust can only be used in horizontal mode.
\leavevmode
%
% Append this vertical mode material after the current line in the output.
\vadjust{%
% We want to insert a rule with the height and depth of the current
% leading; that is exactly what \strutbox is supposed to record.
\vskip-\baselineskip
%
% \vadjust-items are inserted at the left edge of the type. So
% the \llap here moves out into the left-hand margin.
\llap{%
%
% For a thicker or thinner bar, change the `1pt'.
\vrule height\baselineskip width1pt
%
% This is the space between the bar and the text.
\hskip 12pt
}%
}%
}

% For a final copy, take out the rectangles
% that mark overfull boxes (in case you have decided
% that the text looks ok even though it passes the margin).
%
\def\finalout{\overfullrule=0pt}


% End of control word definitions.

\message{and turning on texinfo input format.}

\def\openindices{%
\newindex{cp}%
\newcodeindex{fn}%
\newcodeindex{vr}%
\newcodeindex{tp}%
\newcodeindex{ky}%
\newcodeindex{pg}%
}

% Set some numeric style parameters, for 8.5 x 11 format.

%\hsize = 6.5in
\newdimen\defaultparindent \defaultparindent = 15pt
\parindent = \defaultparindent
\parskip 18pt plus 1pt
\setleading{15pt}
\advance\topskip by 1.2cm

% Prevent underfull vbox error messages.
\vbadness=10000

% Following George Bush, just get rid of widows and orphans.
\widowpenalty=10000
\clubpenalty=10000

% Use TeX 3.0's \emergencystretch to help line breaking, but if we're
% using an old version of TeX, don't do anything. We want the amount of
% stretch added to depend on the line length, hence the dependence on
% \hsize. This makes it come to about 9pt for the 8.5x11 format.
%
\ifx\emergencystretch\thisisundefined
% Allow us to assign to \emergencystretch anyway.
\def\emergencystretch{\dimen0}%
\else
\emergencystretch = \hsize
\divide\emergencystretch by 45
\fi

% Use @smallbook to reset parameters for 7x9.5 format (or else 7x9.25)
\def\smallbook{

% These values for secheadingskip and subsecheadingskip are
% experiments. RJC 7 Aug 1992
\global\secheadingskip = 17pt plus 6pt minus 3pt
\global\subsecheadingskip = 14pt plus 6pt minus 3pt

\global\lispnarrowing = 0.3in
\setleading{12pt}
\advance\topskip by -1cm
\global\parskip 3pt plus 1pt
\global\hsize = 5in
\global\vsize=7.5in
\global\tolerance=700
\global\hfuzz=1pt
\global\contentsrightmargin=0pt

\global\pagewidth=\hsize
\global\pageheight=\vsize

\global\let\smalllisp=\smalllispx
\global\let\smallexample=\smalllispx
\global\def\Esmallexample{\Esmalllisp}
}

% Use @afourpaper to print on European A4 paper.
\def\afourpaper{
\global\tolerance=700
\global\hfuzz=1pt
\setleading{12pt}
\global\parskip 15pt plus 1pt

\global\vsize= 53\baselineskip
\advance\vsize by \topskip
%\global\hsize= 5.85in % A4 wide 10pt
\global\hsize= 6.5in
\global\outerhsize=\hsize
\global\advance\outerhsize by 0.5in
\global\outervsize=\vsize
\global\advance\outervsize by 0.6in

\global\pagewidth=\hsize
\global\pageheight=\vsize
}

% Define macros to output various characters with catcode for normal text.
\catcode`\"=\other
\catcode`\~=\other
\catcode`\^=\other
\catcode`\_=\other
\catcode`\|=\other
\catcode`\<=\other
\catcode`\>=\other
\catcode`\+=\other
\def\normaldoublequote{"}
\def\normaltilde{~}
\def\normalcaret{^}
\def\normalunderscore{_}
\def\normalverticalbar{|}
\def\normalless{<}
\def\normalgreater{>}
\def\normalplus{+}

% This macro is used to make a character print one way in ttfont
% where it can probably just be output, and another way in other fonts,
% where something hairier probably needs to be done.
%
% #1 is what to print if we are indeed using \tt; #2 is what to print
% otherwise. Since all the Computer Modern typewriter fonts have zero
% interword stretch (and shrink), and it is reasonable to expect all
% typewriter fonts to have this, we can check that font parameter.
%
\def\ifusingtt#1#2{\ifdim \fontdimen3\the\font=0pt #1\else #2\fi}

% Turn off all special characters except @
% (and those which the user can use as if they were ordinary).
% Most of these we simply print from the \tt font, but for some, we can
% use math or other variants that look better in normal text.

\catcode`\"=\active
\def\activedoublequote{{\tt \char '042}}
\let"=\activedoublequote
\catcode`\~=\active
\def~{{\tt \char '176}}
\chardef\hat=`\^
\catcode`\^=\active
\def^{{\tt \hat}}

\catcode`\_=\active
\def_{\ifusingtt\normalunderscore\_}
% Subroutine for the previous macro.
\def\_{\lvvmode \kern.06em \vbox{\hrule width.3em height.1ex}}

% \lvvmode is equivalent in function to \leavevmode.
% Using \leavevmode runs into trouble when written out to
% an index file due to the expansion of \leavevmode into ``\unhbox
% \voidb@x'' ---which looks to TeX like ``\unhbox \voidb\x'' due to our
% magic tricks with @.
\def\lvvmode{\vbox to 0pt{}}

\catcode`\|=\active
\def|{{\tt \char '174}}
\chardef \less=`\<
\catcode`\<=\active
\def<{{\tt \less}}
\chardef \gtr=`\>
\catcode`\>=\active
\def>{{\tt \gtr}}
\catcode`\+=\active
\def+{{\tt \char 43}}
%\catcode 27=\active
%\def^^[{$\diamondsuit$}

% Used sometimes to turn off (effectively) the active characters
% even after parsing them.
\def\turnoffactive{\let"=\normaldoublequote
\let~=\normaltilde
\let^=\normalcaret
\let_=\normalunderscore
\let|=\normalverticalbar
\let<=\normalless
\let>=\normalgreater
\let+=\normalplus}

% Set up an active definition for =, but don't enable it most of the time.
{\catcode`\==\active
\global\def={{\tt \char 61}}}

\catcode`\@=0

% \rawbackslashxx output one backslash character in current font
\global\chardef\rawbackslashxx=`\\
%{\catcode`\\=\other
%@gdef@rawbackslashxx{\}}

% \rawbackslash redefines \ as input to do \rawbackslashxx.
{\catcode`\\=\active
@gdef@rawbackslash{@let\=@rawbackslashxx }}

% \normalbackslash outputs one backslash in fixed width font.
\def\normalbackslash{{\tt\rawbackslashxx}}

% Say @foo, not \foo, in error messages.
\escapechar=`\@

% \catcode 17=0 % Define control-q
\catcode`\\=\active

% If a .fmt file is being used, we don't want the `\input texinfo' to show up.
% That is what \eatinput is for; after that, the `\' should revert to printing
% a backslash.
%
@gdef@eatinput input texinfo{@fixbackslash}
@global@let\ = @eatinput

% On the other hand, perhaps the file did not have a `\input texinfo'. Then
% the first `\{ in the file would cause an error. This macro tries to fix
% that, assuming it is called before the first `\' could plausibly occur.
%
@gdef@fixbackslash{@ifx\@eatinput @let\ = @normalbackslash @fi}

%% These look ok in all fonts, so just make them not special. The @rm below
%% makes sure that the current font starts out as the newly loaded cmr10
@catcode`@$=@other @catcode`@%=@other @catcode`@&=@other @catcode`@#=@other

@textfonts
@rm

@c Local variables:
@c page-delimiter: "^\\\\message"
@c End:
bison-1.22/getopt.c100666 21641 1 52075 5432543736 13546 0ustar friedmandaemon/* Getopt for GNU.
NOTE: getopt is now part of the C library, so if you don't know what
"Keep this file name-space clean" means, talk to [email protected]
before changing it!

Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifdef HAVE_CONFIG_H
/* We use instead of "config.h" so that a compilation
using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
(which it would do because getopt.c was found in $srcdir). */
#include
#endif

#ifndef __STDC__
/* This is a separate conditional since some stdc systems
reject `defined (const)'. */
#ifndef const
#define const
#endif
#endif

/* This tells Alpha OSF/1 not to define a getopt prototype in . */
#ifndef _NO_PROTO
#define _NO_PROTO
#endif

#include

/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C
Library, but also included in many other GNU distributions. Compiling
and linking in this code is a waste when using the GNU C library
(especially if it is a shared library). Rather than having every GNU
program understand `configure --with-gnu-libc' and omit the object files,
it is simpler to just do this in the source for each such file. */

#if defined (_LIBC) || !defined (__GNU_LIBRARY__)


/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
/* Don't include stdlib.h for non-GNU C libraries because some of them
contain conflicting prototypes for getopt. */
#include
#endif /* GNU C library. */

/* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
long-named option. Because this is not POSIX.2 compliant, it is
being phased out. */
/* #define GETOPT_COMPAT */

/* This version of `getopt' appears to the caller like standard Unix `getopt'
but it behaves differently for the user, since it allows the user
to intersperse the options with the other arguments.

As `getopt' works, it permutes the elements of ARGV so that,
when it is done, all the options precede everything else. Thus
all application programs are extended to handle flexible argument order.

Setting the environment variable POSIXLY_CORRECT disables permutation.
Then the behavior is completely standard.

GNU application programs can use a third alternative mode in which
they can distinguish the relative order of options and other arguments. */

#include "getopt.h"

/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
the argument value is returned here.
Also, when `ordering' is RETURN_IN_ORDER,
each non-option ARGV-element is returned here. */

char *optarg = 0;

/* Index in ARGV of the next element to be scanned.
This is used for communication to and from the caller
and for communication between successive calls to `getopt'.

On entry to `getopt', zero means this is the first call; initialize.

When `getopt' returns EOF, this is the index of the first of the
non-option elements that the caller should itself scan.

Otherwise, `optind' communicates from one call to the next
how much of ARGV has been scanned so far. */

/* XXX 1003.2 says this must be 1 before any call. */
int optind = 0;

/* The next char to be scanned in the option-element
in which the last option character we returned was found.
This allows us to pick up the scan where we left off.

If this is zero, or a null string, it means resume the scan
by advancing to the next ARGV-element. */

static char *nextchar;

/* Callers store zero here to inhibit the error message
for unrecognized options. */

int opterr = 1;

/* Set to an option character which was unrecognized.
This must be initialized on some systems to avoid linking in the
system's own getopt implementation. */

int optopt = '?';

/* Describe how to deal with options that follow non-option ARGV-elements.

If the caller did not specify anything,
the default is REQUIRE_ORDER if the environment variable
POSIXLY_CORRECT is defined, PERMUTE otherwise.

REQUIRE_ORDER means don't recognize them as options;
stop option processing when the first non-option is seen.
This is what Unix does.
This mode of operation is selected by either setting the environment
variable POSIXLY_CORRECT, or using `+' as the first character
of the list of option characters.

PERMUTE is the default. We permute the contents of ARGV as we scan,
so that eventually all the non-options are at the end. This allows options
to be given in any order, even with programs that were not written to
expect this.

RETURN_IN_ORDER is an option available to programs that were written
to expect options and other ARGV-elements in any order and that care about
the ordering of the two. We describe each non-option ARGV-element
as if it were the argument of an option with character code 1.
Using `-' as the first character of the list of option characters
selects this mode of operation.

The special argument `--' forces an end of option-scanning regardless
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
`--' can cause `getopt' to return EOF with `optind' != ARGC. */

static enum
{
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
} ordering;

#ifdef __GNU_LIBRARY__
/* We want to avoid inclusion of string.h with non-GNU libraries
because there are many ways it can cause trouble.
On some systems, it contains special magic macros that don't work
in GCC. */
#include
#define my_index strchr
#else

/* Avoid depending on library functions or files
whose names are inconsistent. */

char *getenv ();

static char *
my_index (str, chr)
const char *str;
int chr;
{
while (*str)
{
if (*str == chr)
return (char *) str;
str++;
}
return 0;
}

/* If using GCC, we can safely declare strlen this way.
If not using GCC, it is ok not to declare it.
(Supposedly there are some machines where it might get a warning,
but changing this conditional to __STDC__ is too risky.) */
#ifdef __GNUC__
#ifdef IN_GCC
#include "gstddef.h"
#else
#include
#endif
extern size_t strlen (const char *);
#endif

#endif /* GNU C library. */

/* Handle permutation of arguments. */

/* Describe the part of ARGV that contains non-options that have
been skipped. `first_nonopt' is the index in ARGV of the first of them;
`last_nonopt' is the index after the last of them. */

static int first_nonopt;
static int last_nonopt;

/* Exchange two adjacent subsequences of ARGV.
One subsequence is elements [first_nonopt,last_nonopt)
which contains all the non-options that have been skipped so far.
The other is elements [last_nonopt,optind), which contains all
the options processed since those non-options were skipped.

`first_nonopt' and `last_nonopt' are relocated so that they describe
the new indices of the non-options in ARGV after they are moved. */

static void
exchange (argv)
char **argv;
{
int bottom = first_nonopt;
int middle = last_nonopt;
int top = optind;
char *tem;

/* Exchange the shorter segment with the far end of the longer segment.
That puts the shorter segment into the right place.
It leaves the longer segment in the right place overall,
but it consists of two parts that need to be swapped next. */

while (top > middle && middle > bottom)
{
if (top - middle > middle - bottom)
{
/* Bottom segment is the short one. */
int len = middle - bottom;
register int i;

/* Swap it with the top part of the top segment. */
for (i = 0; i < len; i++)
{
tem = argv[bottom + i];
argv[bottom + i] = argv[top - (middle - bottom) + i];
argv[top - (middle - bottom) + i] = tem;
}
/* Exclude the moved bottom segment from further swapping. */
top -= len;
}
else
{
/* Top segment is the short one. */
int len = top - middle;
register int i;

/* Swap it with the bottom part of the bottom segment. */
for (i = 0; i < len; i++)
{
tem = argv[bottom + i];
argv[bottom + i] = argv[middle + i];
argv[middle + i] = tem;
}
/* Exclude the moved top segment from further swapping. */
bottom += len;
}
}

/* Update records for the slots the non-options now occupy. */

first_nonopt += (optind - last_nonopt);
last_nonopt = optind;
}

/* Scan elements of ARGV (whose length is ARGC) for option characters
given in OPTSTRING.

If an element of ARGV starts with '-', and is not exactly "-" or "--",
then it is an option element. The characters of this element
(aside from the initial '-') are option characters. If `getopt'
is called repeatedly, it returns successively each of the option characters
from each of the option elements.

If `getopt' finds another option character, it returns that character,
updating `optind' and `nextchar' so that the next call to `getopt' can
resume the scan with the following option character or ARGV-element.

If there are no more option characters, `getopt' returns `EOF'.
Then `optind' is the index in ARGV of the first ARGV-element
that is not an option. (The ARGV-elements have been permuted
so that those that are not options now come last.)

OPTSTRING is a string containing the legitimate option characters.
If an option character is seen that is not listed in OPTSTRING,
return '?' after printing an error message. If you set `opterr' to
zero, the error message is suppressed but we still return '?'.

If a char in OPTSTRING is followed by a colon, that means it wants an arg,
so the following text in the same ARGV-element, or the text of the following
ARGV-element, is returned in `optarg'. Two colons mean an option that
wants an optional arg; if there is text in the current ARGV-element,
it is returned in `optarg', otherwise `optarg' is set to zero.

If OPTSTRING starts with `-' or `+', it requests different methods of
handling the non-option ARGV-elements.
See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.

Long-named options begin with `--' instead of `-'.
Their names may be abbreviated as long as the abbreviation is unique
or is an exact match for some defined option. If they have an
argument, it follows the option name in the same ARGV-element, separated
from the option name by a `=', or else the in next ARGV-element.
When `getopt' finds a long-named option, it returns 0 if that option's
`flag' field is nonzero, the value of the option's `val' field
if the `flag' field is zero.

The elements of ARGV aren't really const, because we permute them.
But we pretend they're const in the prototype to be compatible
with other systems.

LONGOPTS is a vector of `struct option' terminated by an
element containing a name which is zero.

LONGIND returns the index in LONGOPT of the long-named option found.
It is only valid when a long-named option has been found by the most
recent call.

If LONG_ONLY is nonzero, '-' as well as '--' can introduce
long-named options. */

int
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
int argc;
char *const *argv;
const char *optstring;
const struct option *longopts;
int *longind;
int long_only;
{
int option_index;

optarg = 0;

/* Initialize the internal data when the first call is made.
Start processing options with ARGV-element 1 (since ARGV-element 0
is the program name); the sequence of previously skipped
non-option ARGV-elements is empty. */

if (optind == 0)
{
first_nonopt = last_nonopt = optind = 1;

nextchar = NULL;

/* Determine how to handle the ordering of options and nonoptions. */

if (optstring[0] == '-')
{
ordering = RETURN_IN_ORDER;
++optstring;
}
else if (optstring[0] == '+')
{
ordering = REQUIRE_ORDER;
++optstring;
}
else if (getenv ("POSIXLY_CORRECT") != NULL)
ordering = REQUIRE_ORDER;
else
ordering = PERMUTE;
}

if (nextchar == NULL || *nextchar == '\0')
{
if (ordering == PERMUTE)
{
/* If we have just processed some options following some non-options,
exchange them so that the options come first. */

if (first_nonopt != last_nonopt && last_nonopt != optind)
exchange ((char **) argv);
else if (last_nonopt != optind)
first_nonopt = optind;

/* Now skip any additional non-options
and extend the range of non-options previously skipped. */

while (optind < argc
&& (argv[optind][0] != '-' || argv[optind][1] == '\0')
#ifdef GETOPT_COMPAT
&& (longopts == NULL
|| argv[optind][0] != '+' || argv[optind][1] == '\0')
#endif /* GETOPT_COMPAT */
)
optind++;
last_nonopt = optind;
}

/* Special ARGV-element `--' means premature end of options.
Skip it like a null option,
then exchange with previous non-options as if it were an option,
then skip everything else like a non-option. */

if (optind != argc && !strcmp (argv[optind], "--"))
{
optind++;

if (first_nonopt != last_nonopt && last_nonopt != optind)
exchange ((char **) argv);
else if (first_nonopt == last_nonopt)
first_nonopt = optind;
last_nonopt = argc;

optind = argc;
}

/* If we have done all the ARGV-elements, stop the scan
and back over any non-options that we skipped and permuted. */

if (optind == argc)
{
/* Set the next-arg-index to point at the non-options
that we previously skipped, so the caller will digest them. */
if (first_nonopt != last_nonopt)
optind = first_nonopt;
return EOF;
}

/* If we have come to a non-option and did not permute it,
either stop the scan or describe it to the caller and pass it by. */

if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
#ifdef GETOPT_COMPAT
&& (longopts == NULL
|| argv[optind][0] != '+' || argv[optind][1] == '\0')
#endif /* GETOPT_COMPAT */
)
{
if (ordering == REQUIRE_ORDER)
return EOF;
optarg = argv[optind++];
return 1;
}

/* We have found another option-ARGV-element.
Start decoding its characters. */

nextchar = (argv[optind] + 1
+ (longopts != NULL && argv[optind][1] == '-'));
}

if (longopts != NULL
&& ((argv[optind][0] == '-'
&& (argv[optind][1] == '-' || long_only))
#ifdef GETOPT_COMPAT
|| argv[optind][0] == '+'
#endif /* GETOPT_COMPAT */
))
{
const struct option *p;
char *s = nextchar;
int exact = 0;
int ambig = 0;
const struct option *pfound = NULL;
int indfound;

while (*s && *s != '=')
s++;

/* Test all options for either exact match or abbreviated matches. */
for (p = longopts, option_index = 0; p->name;
p++, option_index++)
if (!strncmp (p->name, nextchar, s - nextchar))
{
if (s - nextchar == strlen (p->name))
{
/* Exact match found. */
pfound = p;
indfound = option_index;
exact = 1;
break;
}
else if (pfound == NULL)
{
/* First nonexact match found. */
pfound = p;
indfound = option_index;
}
else
/* Second nonexact match found. */
ambig = 1;
}

if (ambig && !exact)
{
if (opterr)
fprintf (stderr, "%s: option `%s' is ambiguous\n",
argv[0], argv[optind]);
nextchar += strlen (nextchar);
optind++;
return '?';
}

if (pfound != NULL)
{
option_index = indfound;
optind++;
if (*s)
{
/* Don't test has_arg with >, because some C compilers don't
allow it to be used on enums. */
if (pfound->has_arg)
optarg = s + 1;
else
{
if (opterr)
{
if (argv[optind - 1][1] == '-')
/* --option */
fprintf (stderr,
"%s: option `--%s' doesn't allow an argument\n",
argv[0], pfound->name);
else
/* +option or -option */
fprintf (stderr,
"%s: option `%c%s' doesn't allow an argument\n",
argv[0], argv[optind - 1][0], pfound->name);
}
nextchar += strlen (nextchar);
return '?';
}
}
else if (pfound->has_arg == 1)
{
if (optind < argc)
optarg = argv[optind++];
else
{
if (opterr)
fprintf (stderr, "%s: option `%s' requires an argument\n",
argv[0], argv[optind - 1]);
nextchar += strlen (nextchar);
return optstring[0] == ':' ? ':' : '?';
}
}
nextchar += strlen (nextchar);
if (longind != NULL)
*longind = option_index;
if (pfound->flag)
{
*(pfound->flag) = pfound->val;
return 0;
}
return pfound->val;
}
/* Can't find it as a long option. If this is not getopt_long_only,
or the option starts with '--' or is not a valid short
option, then it's an error.
Otherwise interpret it as a short option. */
if (!long_only || argv[optind][1] == '-'
#ifdef GETOPT_COMPAT
|| argv[optind][0] == '+'
#endif /* GETOPT_COMPAT */
|| my_index (optstring, *nextchar) == NULL)
{
if (opterr)
{
if (argv[optind][1] == '-')
/* --option */
fprintf (stderr, "%s: unrecognized option `--%s'\n",
argv[0], nextchar);
else
/* +option or -option */
fprintf (stderr, "%s: unrecognized option `%c%s'\n",
argv[0], argv[optind][0], nextchar);
}
nextchar = (char *) "";
optind++;
return '?';
}
}

/* Look at and handle the next option-character. */

{
char c = *nextchar++;
char *temp = my_index (optstring, c);

/* Increment `optind' when we start to process its last character. */
if (*nextchar == '\0')
++optind;

if (temp == NULL || c == ':')
{
if (opterr)
{
#if 0
if (c < 040 || c >= 0177)
fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
argv[0], c);
else
fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
#else
/* 1003.2 specifies the format of this message. */
fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
#endif
}
optopt = c;
return '?';
}
if (temp[1] == ':')
{
if (temp[2] == ':')
{
/* This is an option that accepts an argument optionally. */
if (*nextchar != '\0')
{
optarg = nextchar;
optind++;
}
else
optarg = 0;
nextchar = NULL;
}
else
{
/* This is an option that requires an argument. */
if (*nextchar != '\0')
{
optarg = nextchar;
/* If we end this ARGV-element by taking the rest as an arg,
we must advance to the next element now. */
optind++;
}
else if (optind == argc)
{
if (opterr)
{
#if 0
fprintf (stderr, "%s: option `-%c' requires an argument\n",
argv[0], c);
#else
/* 1003.2 specifies the format of this message. */
fprintf (stderr, "%s: option requires an argument -- %c\n",
argv[0], c);
#endif
}
optopt = c;
if (optstring[0] == ':')
c = ':';
else
c = '?';
}
else
/* We already incremented `optind' once;
increment it again when taking next ARGV-elt as argument. */
optarg = argv[optind++];
nextchar = NULL;
}
}
return c;
}
}

int
getopt (argc, argv, optstring)
int argc;
char *const *argv;
const char *optstring;
{
return _getopt_internal (argc, argv, optstring,
(const struct option *) 0,
(int *) 0,
0);
}

#endif /* _LIBC or not __GNU_LIBRARY__. */

#ifdef TEST

/* Compile with -DTEST to make an executable for use in testing
the above definition of `getopt'. */

int
main (argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;

while (1)
{
int this_option_optind = optind ? optind : 1;

c = getopt (argc, argv, "abc:d:0123456789");
if (c == EOF)
break;

switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;

case 'a':
printf ("option a\n");
break;

case 'b':
printf ("option b\n");
break;

case 'c':
printf ("option c with value `%s'\n", optarg);
break;

case '?':
break;

default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}

if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}

exit (0);
}

#endif /* TEST */
bison-1.22/getopt.h100666 21641 1 10474 5372534261 13544 0ustar friedmandaemon/* Declarations for getopt.
Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifndef _GETOPT_H
#define _GETOPT_H 1

#ifdef __cplusplus
extern "C" {
#endif

/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
the argument value is returned here.
Also, when `ordering' is RETURN_IN_ORDER,
each non-option ARGV-element is returned here. */

extern char *optarg;

/* Index in ARGV of the next element to be scanned.
This is used for communication to and from the caller
and for communication between successive calls to `getopt'.

On entry to `getopt', zero means this is the first call; initialize.

When `getopt' returns EOF, this is the index of the first of the
non-option elements that the caller should itself scan.

Otherwise, `optind' communicates from one call to the next
how much of ARGV has been scanned so far. */

extern int optind;

/* Callers store zero here to inhibit the error message `getopt' prints
for unrecognized options. */

extern int opterr;

/* Set to an option character which was unrecognized. */

extern int optopt;

/* Describe the long-named options requested by the application.
The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
of `struct option' terminated by an element containing a name which is
zero.

The field `has_arg' is:
no_argument (or 0) if the option does not take an argument,
required_argument (or 1) if the option requires an argument,
optional_argument (or 2) if the option takes an optional argument.

If the field `flag' is not NULL, it points to a variable that is set
to the value given in the field `val' when the option is found, but
left unchanged if the option is not found.

To have a long-named option do something other than set an `int' to
a compiled-in constant, such as set a value from `optarg', set the
option's `flag' field to zero and its `val' field to a nonzero
value (the equivalent single-letter option character, if there is
one). For long options that have a zero `flag' field, `getopt'
returns the contents of the `val' field. */

struct option
{
#if __STDC__
const char *name;
#else
char *name;
#endif
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};

/* Names for the values of the `has_arg' field of `struct option'. */

#define no_argument 0
#define required_argument 1
#define optional_argument 2

#if __STDC__
#if defined(__GNU_LIBRARY__)
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
extern int getopt (int argc, char *const *argv, const char *shortopts);
#else /* not __GNU_LIBRARY__ */
extern int getopt ();
#endif /* not __GNU_LIBRARY__ */
extern int getopt_long (int argc, char *const *argv, const char *shortopts,
const struct option *longopts, int *longind);
extern int getopt_long_only (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind);

/* Internal only. Users should not call this directly. */
extern int _getopt_internal (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind,
int long_only);
#else /* not __STDC__ */
extern int getopt ();
extern int getopt_long ();
extern int getopt_long_only ();

extern int _getopt_internal ();
#endif /* not __STDC__ */

#ifdef __cplusplus
}
#endif

#endif /* _GETOPT_H */
bison-1.22/getopt1.c100666 21641 1 10520 5432543734 13612 0ustar friedmandaemon/* getopt_long and getopt_long_only entry points for GNU getopt.
Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifdef HAVE_CONFIG_H
/* We use instead of "config.h" so that a compilation
using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
(which it would do because getopt1.c was found in $srcdir). */
#include
#endif

#include "getopt.h"

#ifndef __STDC__
/* This is a separate conditional since some stdc systems
reject `defined (const)'. */
#ifndef const
#define const
#endif
#endif

#include

/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C
Library, but also included in many other GNU distributions. Compiling
and linking in this code is a waste when using the GNU C library
(especially if it is a shared library). Rather than having every GNU
program understand `configure --with-gnu-libc' and omit the object files,
it is simpler to just do this in the source for each such file. */

#if defined (_LIBC) || !defined (__GNU_LIBRARY__)


/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
#include
#else
char *getenv ();
#endif

#ifndef NULL
#define NULL 0
#endif

int
getopt_long (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}

/* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */

int
getopt_long_only (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}


#endif /* _LIBC or not __GNU_LIBRARY__. */

#ifdef TEST

#include

int
main (argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;

while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 0, 0, 0},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};

c = getopt_long (argc, argv, "abc:d:0123456789",
long_options, &option_index);
if (c == EOF)
break;

switch (c)
{
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;

case 'a':
printf ("option a\n");
break;

case 'b':
printf ("option b\n");
break;

case 'c':
printf ("option c with value `%s'\n", optarg);
break;

case 'd':
printf ("option d with value `%s'\n", optarg);
break;

case '?':
break;

default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}

if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}

exit (0);
}

#endif /* TEST */
bison-1.22/alloca.c100666 21641 1 33212 5433755641 13470 0ustar friedmandaemon/* alloca.c -- allocate automatically reclaimed memory
(Mostly) portable public-domain implementation -- D A Gwyn

This implementation of the PWB library alloca function,
which is used to allocate space off the run-time stack so
that it is automatically reclaimed upon procedure exit,
was inspired by discussions with J. Q. Johnson of Cornell.
J.Otto Tennant contributed the Cray support.

There are some preprocessor constants that can
be defined when compiling for your specific system, for
improved efficiency; however, the defaults should be okay.

The general concept of this implementation is to keep
track of all alloca-allocated blocks, and reclaim any
that are found to be deeper in the stack than the current
invocation. This heuristic does not reclaim storage as
soon as it becomes invalid, but it will do so eventually.

As a special case, alloca(0) reclaims storage without
allocating any. It is a good idea to use alloca(0) in
your main control loop, etc. to force garbage collection. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* If compiling with GCC 2, this file's not needed. */
#if !defined (__GNUC__) || __GNUC__ < 2

/* If someone has defined alloca as a macro,
there must be some other way alloca is supposed to work. */
#ifndef alloca

#ifdef emacs
#ifdef static
/* actually, only want this if static is defined as ""
-- this is for usg, in which emacs must undefine static
in order to make unexec workable
*/
#ifndef STACK_DIRECTION
you
lose
-- must know STACK_DIRECTION at compile-time
#endif /* STACK_DIRECTION undefined */
#endif /* static */
#endif /* emacs */

/* If your stack is a linked list of frames, you have to
provide an "address metric" ADDRESS_FUNCTION macro. */

#if defined (CRAY) && defined (CRAY_STACKSEG_END)
long i00afunc ();
#define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
#else
#define ADDRESS_FUNCTION(arg) &(arg)
#endif

#if __STDC__
typedef void *pointer;
#else
typedef char *pointer;
#endif

#define NULL 0

/* Different portions of Emacs need to call different versions of
malloc. The Emacs executable needs alloca to call xmalloc, because
ordinary malloc isn't protected from input signals. On the other
hand, the utilities in lib-src need alloca to call malloc; some of
them are very simple, and don't have an xmalloc routine.

Non-Emacs programs expect this to call use xmalloc.

Callers below should use malloc. */

#ifndef emacs
#define malloc xmalloc
#endif
extern pointer malloc ();

/* Define STACK_DIRECTION if you know the direction of stack
growth for your system; otherwise it will be automatically
deduced at run-time.

STACK_DIRECTION > 0 => grows toward higher addresses
STACK_DIRECTION < 0 => grows toward lower addresses
STACK_DIRECTION = 0 => direction of growth unknown */

#ifndef STACK_DIRECTION
#define STACK_DIRECTION 0 /* Direction unknown. */
#endif

#if STACK_DIRECTION != 0

#define STACK_DIR STACK_DIRECTION /* Known at compile-time. */

#else /* STACK_DIRECTION == 0; need run-time code. */

static int stack_dir; /* 1 or -1 once known. */
#define STACK_DIR stack_dir

static void
find_stack_direction ()
{
static char *addr = NULL; /* Address of first `dummy', once known. */
auto char dummy; /* To get stack address. */

if (addr == NULL)
{ /* Initial entry. */
addr = ADDRESS_FUNCTION (dummy);

find_stack_direction (); /* Recurse once. */
}
else
{
/* Second entry. */
if (ADDRESS_FUNCTION (dummy) > addr)
stack_dir = 1; /* Stack grew upward. */
else
stack_dir = -1; /* Stack grew downward. */
}
}

#endif /* STACK_DIRECTION == 0 */

/* An "alloca header" is used to:
(a) chain together all alloca'ed blocks;
(b) keep track of stack depth.

It is very important that sizeof(header) agree with malloc
alignment chunk size. The following default should work okay. */

#ifndef ALIGN_SIZE
#define ALIGN_SIZE sizeof(double)
#endif

typedef union hdr
{
char align[ALIGN_SIZE]; /* To force sizeof(header). */
struct
{
union hdr *next; /* For chaining headers. */
char *deep; /* For stack depth measure. */
} h;
} header;

static header *last_alloca_header = NULL; /* -> last alloca header. */

/* Return a pointer to at least SIZE bytes of storage,
which will be automatically reclaimed upon exit from
the procedure that called alloca. Originally, this space
was supposed to be taken from the current stack frame of the
caller, but that method cannot be made to work for some
implementations of C, for example under Gould's UTX/32. */

pointer
alloca (size)
unsigned size;
{
auto char probe; /* Probes stack depth: */
register char *depth = ADDRESS_FUNCTION (probe);

#if STACK_DIRECTION == 0
if (STACK_DIR == 0) /* Unknown growth direction. */
find_stack_direction ();
#endif

/* Reclaim garbage, defined as all alloca'd storage that
was allocated from deeper in the stack than currently. */

{
register header *hp; /* Traverses linked list. */

for (hp = last_alloca_header; hp != NULL;)
if ((STACK_DIR > 0 && hp->h.deep > depth)
|| (STACK_DIR < 0 && hp->h.deep < depth))
{
register header *np = hp->h.next;

free ((pointer) hp); /* Collect garbage. */

hp = np; /* -> next header. */
}
else
break; /* Rest are not deeper. */

last_alloca_header = hp; /* -> last valid storage. */
}

if (size == 0)
return NULL; /* No allocation required. */

/* Allocate combined header + user data storage. */

{
register pointer new = malloc (sizeof (header) + size);
/* Address of header. */

((header *) new)->h.next = last_alloca_header;
((header *) new)->h.deep = depth;

last_alloca_header = (header *) new;

/* User storage begins just after header. */

return (pointer) ((char *) new + sizeof (header));
}
}

#if defined (CRAY) && defined (CRAY_STACKSEG_END)

#ifdef DEBUG_I00AFUNC
#include
#endif

#ifndef CRAY_STACK
#define CRAY_STACK
#ifndef CRAY2
/* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
struct stack_control_header
{
long shgrow:32; /* Number of times stack has grown. */
long shaseg:32; /* Size of increments to stack. */
long shhwm:32; /* High water mark of stack. */
long shsize:32; /* Current size of stack (all segments). */
};

/* The stack segment linkage control information occurs at
the high-address end of a stack segment. (The stack
grows from low addresses to high addresses.) The initial
part of the stack segment linkage control information is
0200 (octal) words. This provides for register storage
for the routine which overflows the stack. */

struct stack_segment_linkage
{
long ss[0200]; /* 0200 overflow words. */
long sssize:32; /* Number of words in this segment. */
long ssbase:32; /* Offset to stack base. */
long:32;
long sspseg:32; /* Offset to linkage control of previous
segment of stack. */
long:32;
long sstcpt:32; /* Pointer to task common address block. */
long sscsnm; /* Private control structure number for
microtasking. */
long ssusr1; /* Reserved for user. */
long ssusr2; /* Reserved for user. */
long sstpid; /* Process ID for pid based multi-tasking. */
long ssgvup; /* Pointer to multitasking thread giveup. */
long sscray[7]; /* Reserved for Cray Research. */
long ssa0;
long ssa1;
long ssa2;
long ssa3;
long ssa4;
long ssa5;
long ssa6;
long ssa7;
long sss0;
long sss1;
long sss2;
long sss3;
long sss4;
long sss5;
long sss6;
long sss7;
};

#else /* CRAY2 */
/* The following structure defines the vector of words
returned by the STKSTAT library routine. */
struct stk_stat
{
long now; /* Current total stack size. */
long maxc; /* Amount of contiguous space which would
be required to satisfy the maximum
stack demand to date. */
long high_water; /* Stack high-water mark. */
long overflows; /* Number of stack overflow ($STKOFEN) calls. */
long hits; /* Number of internal buffer hits. */
long extends; /* Number of block extensions. */
long stko_mallocs; /* Block allocations by $STKOFEN. */
long underflows; /* Number of stack underflow calls ($STKRETN). */
long stko_free; /* Number of deallocations by $STKRETN. */
long stkm_free; /* Number of deallocations by $STKMRET. */
long segments; /* Current number of stack segments. */
long maxs; /* Maximum number of stack segments so far. */
long pad_size; /* Stack pad size. */
long current_address; /* Current stack segment address. */
long current_size; /* Current stack segment size. This
number is actually corrupted by STKSTAT to
include the fifteen word trailer area. */
long initial_address; /* Address of initial segment. */
long initial_size; /* Size of initial segment. */
};

/* The following structure describes the data structure which trails
any stack segment. I think that the description in 'asdef' is
out of date. I only describe the parts that I am sure about. */

struct stk_trailer
{
long this_address; /* Address of this block. */
long this_size; /* Size of this block (does not include
this trailer). */
long unknown2;
long unknown3;
long link; /* Address of trailer block of previous
segment. */
long unknown5;
long unknown6;
long unknown7;
long unknown8;
long unknown9;
long unknown10;
long unknown11;
long unknown12;
long unknown13;
long unknown14;
};

#endif /* CRAY2 */
#endif /* not CRAY_STACK */

#ifdef CRAY2
/* Determine a "stack measure" for an arbitrary ADDRESS.
I doubt that "lint" will like this much. */

static long
i00afunc (long *address)
{
struct stk_stat status;
struct stk_trailer *trailer;
long *block, size;
long result = 0;

/* We want to iterate through all of the segments. The first
step is to get the stack status structure. We could do this
more quickly and more directly, perhaps, by referencing the
$LM00 common block, but I know that this works. */

STKSTAT (&status);

/* Set up the iteration. */

trailer = (struct stk_trailer *) (status.current_address
+ status.current_size
- 15);

/* There must be at least one stack segment. Therefore it is
a fatal error if "trailer" is null. */

if (trailer == 0)
abort ();

/* Discard segments that do not contain our argument address. */

while (trailer != 0)
{
block = (long *) trailer->this_address;
size = trailer->this_size;
if (block == 0 || size == 0)
abort ();
trailer = (struct stk_trailer *) trailer->link;
if ((block <= address) && (address < (block + size)))
break;
}

/* Set the result to the offset in this segment and add the sizes
of all predecessor segments. */

result = address - block;

if (trailer == 0)
{
return result;
}

do
{
if (trailer->this_size <= 0)
abort ();
result += trailer->this_size;
trailer = (struct stk_trailer *) trailer->link;
}
while (trailer != 0);

/* We are done. Note that if you present a bogus address (one
not in any segment), you will get a different number back, formed
from subtracting the address of the first block. This is probably
not what you want. */

return (result);
}

#else /* not CRAY2 */
/* Stack address function for a CRAY-1, CRAY X-MP, or CRAY Y-MP.
Determine the number of the cell within the stack,
given the address of the cell. The purpose of this
routine is to linearize, in some sense, stack addresses
for alloca. */

static long
i00afunc (long address)
{
long stkl = 0;

long size, pseg, this_segment, stack;
long result = 0;

struct stack_segment_linkage *ssptr;

/* Register B67 contains the address of the end of the
current stack segment. If you (as a subprogram) store
your registers on the stack and find that you are past
the contents of B67, you have overflowed the segment.

B67 also points to the stack segment linkage control
area, which is what we are really interested in. */

stkl = CRAY_STACKSEG_END ();
ssptr = (struct stack_segment_linkage *) stkl;

/* If one subtracts 'size' from the end of the segment,
one has the address of the first word of the segment.

If this is not the first segment, 'pseg' will be
nonzero. */

pseg = ssptr->sspseg;
size = ssptr->sssize;

this_segment = stkl - size;

/* It is possible that calling this routine itself caused
a stack overflow. Discard stack segments which do not
contain the target address. */

while (!(this_segment <= address && address <= stkl))
{
#ifdef DEBUG_I00AFUNC
fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
#endif
if (pseg == 0)
break;
stkl = stkl - pseg;
ssptr = (struct stack_segment_linkage *) stkl;
size = ssptr->sssize;
pseg = ssptr->sspseg;
this_segment = stkl - size;
}

result = address - this_segment;

/* If you subtract pseg from the current end of the stack,
you get the address of the previous stack segment's end.
This seems a little convoluted to me, but I'll bet you save
a cycle somewhere. */

while (pseg != 0)
{
#ifdef DEBUG_I00AFUNC
fprintf (stderr, "%011o %011o\n", pseg, size);
#endif
stkl = stkl - pseg;
ssptr = (struct stack_segment_linkage *) stkl;
size = ssptr->sssize;
pseg = ssptr->sspseg;
result += size;
}
return (result);
}

#endif /* not CRAY2 */
#endif /* CRAY */

#endif /* no alloca */
#endif /* not GCC version 2 */
bison-1.22/mkinstalldirs100777 21641 1 1153 5432727042 14647 0ustar friedmandaemon#!/bin/sh
# Make directory hierarchy.
# Written by Noah Friedman
# Public domain.

defaultIFS='
'
IFS="${IFS-${defaultIFS}}"

errstatus=0

for file in ${1+"$@"} ; do
oIFS="${IFS}"
# Some sh's can't handle IFS=/ for some reason.
IFS='%'
set - `echo ${file} | sed -e 's@/@%@g' -e 's@^%@/@'`
IFS="${oIFS}"

pathcomp=''

for d in ${1+"$@"} ; do
pathcomp="${pathcomp}${d}"

if test ! -d "${pathcomp}"; then
echo "mkdir $pathcomp" 1>&2
mkdir "${pathcomp}" || errstatus=$?
fi

pathcomp="${pathcomp}/"
done
done

exit $errstatus

# eof


  3 Responses to “Category : C Source Code
Archive   : BISON122.ZIP
Filename : BISON122.TAR

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/