1 This file attempts to describe the rules to use when hacking Bison.
2 Don't put this file into the distribution.
4 Everything related to the development of Bison is on Savannah:
5 http://savannah.gnu.org/projects/bison/.
8 Working from the Repository
9 ===========================
11 These notes intend to help people working on the checked-out sources. These
12 requirements do not apply when building from a distribution tarball.
16 We've opted to keep only the highest-level sources in the repository. This
17 eases our maintenance burden, (fewer merges etc.), but imposes more
18 requirements on anyone wishing to build from the just-checked-out sources.
19 For example, you have to use the latest stable versions of the maintainer
20 tools we depend upon, including:
22 - Autoconf <http://www.gnu.org/software/autoconf/>
23 - Automake <http://www.gnu.org/software/automake/>
24 - Flex <http://www.gnu.org/software/flex/>
25 - Gettext <http://www.gnu.org/software/gettext/>
26 - Gperf <http://www.gnu.org/software/gperf/>
27 - Graphviz <http://www.graphviz.org>
28 - Gzip <http://www.gnu.org/software/gzip/>
29 - Help2man <http://www.gnu.org/software/help2man/>
30 - Perl <http://www.cpan.org/>
31 - Rsync <http://samba.anu.edu.au/rsync/>
32 - Tar <http://www.gnu.org/software/tar/>
33 - Texinfo <http://www.gnu.org/software/texinfo/>
35 Valgrind <http://valgrind.org/> is also highly recommended, if it supports
38 If you're using a GNU/Linux distribution, the easiest way to install the
39 above packages depends on your system. The following shell command should
40 work for Debian-based systems such as Ubuntu:
42 sudo apt-get install \
43 autoconf automake autopoint flex gperf graphviz help2man texinfo valgrind
45 Bison is written using Bison grammars, so there are bootstrapping issues.
46 The bootstrap script attempts to discover when the C code generated from the
47 grammars is out of date, and to bootstrap with an out-of-date version of the
48 C code, but the process is not foolproof. Also, you may run into similar
49 problems yourself if you modify Bison.
51 Only building the initial full source tree will be a bit painful. Later,
52 after synchronizing from the repository a plain 'make' should be sufficient.
53 Note, however, that when gnulib is updated, running './bootstrap' again
58 Obviously, if you are reading these notes, you did manage to check out this
59 package from the repository. For the record, you will find all the relevant
60 information on http://savannah.gnu.org/git/?group=bison.
62 Bison uses Git submodules: subscriptions to other Git repositories. In
63 particular it uses gnulib, the GNU portability library. To ask Git to
64 perform the first checkout of the submodules, run
66 $ git submodule update --init
68 The next step is to get other files needed to build, which are extracted
69 from other source packages:
73 Bootstrapping updates the submodules to the versions registered in the
74 top-level directory. To change gnulib, first check out the version you want
75 in `gnulib`, then commit this change in Bison's repository, and finally run
78 If it fails with missing symbols (e.g., `error: possibly undefined macro:
79 AC_PROG_GNU_M4`), you are likely to have forgotten the submodule
80 initialization part. To recover from it, run `git reset --hard HEAD`, and
81 restart with the submodule initialization. Otherwise, there you are! Just
87 At this point, there should be no difference between your local copy, and
92 should output no difference.
98 If you have git at version 1.8.2 or later, the command
100 $ git submodule update --recursive --remote
102 will be useful for updating to the latest version of all submodules.
104 Under earlier versions, use of submodules make things somewhat different
105 because git does not yet support recursive operations: submodules must be
106 taken care of explicitly.
110 If you pull a newer version of a branch, say via `git pull`, you might
111 import requests for updated submodules. A simple `git diff` will reveal if
112 the current version of the submodule (i.e., the actual contents of the
113 gnulib directory) and the current request from the subscriber (i.e., the
114 reference of the version of gnulib that the Bison repository requests)
115 differ. To upgrade the submodules (i.e., to check out the version that is
116 actually requested by the subscriber, run `git submodule update`.
119 $ git submodule update
121 ### Updating a submodule
122 To update a submodule, say gnulib, do as follows:
124 Get the most recent version of the master branch from git.
128 $ git checkout -b master --track origin/master
130 Make sure Bison can live with that version of gnulib.
136 Register your changes.
140 For a suggestion of what gnulib commit might be stable enough for a formal
141 release, see the ChangeLog in the latest gnulib snapshot at
142 http://erislabs.net/ianb/projects/gnulib/.
144 The Autoconf files we use are currently:
146 - lib/m4sugar/m4sugar.m4
147 - lib/m4sugar/foreach.m4
149 These files don't change very often in Autoconf, so it should be relatively
150 straight-forward to examine the differences in order to decide whether to
155 Bison is self-hosted: its parser is generated by Bison. We don't force
156 ourselves to use the previous release of Bison, we use the current git
157 master for several reasons:
158 - dogfooding: let Bison be its first user
159 - monitoring: seeing the diff on the generated parsers with git is very
160 helpful, as it allows to see easily the impact of the changes on a real
163 If you are unlucky the generated files, src/parse-gram.[ch], may be older
164 than their source, src/parse-gram.y. And your current version of Bison
165 might not be able to grok it. In that case, first refresh the generated
168 $ touch src/parse-gram.[ch]
172 In case you wrecked your current copy of the parser, get back the previous
173 version, compile bison, then force it to recreate the files:
175 $ git checkout HEAD^ src/parse-gram.[ch]
177 $ touch src/parse-gram.y
184 ## If you incorporate a change from somebody on the net:
185 First, if it is a large change, you must make sure they have signed the
186 appropriate paperwork. Second, be sure to add their name and email address
189 ## If a change fixes a test, mention the test in the commit message.
192 If somebody reports a new bug, mention his name in the commit message and in
193 the test case you write. Put him into THANKS.
195 The correct response to most actual bugs is to write a new test case which
196 demonstrates the bug. Then fix the bug, re-run the test suite, and check
205 Which include serious bug fixes, must be mentioned in NEWS.
208 Only user visible strings are to be translated: error messages, bits of the
209 .output file etc. This excludes impossible error messages (comparable to
210 assert/abort), and all the --trace output which is meant for the maintainers
214 - "lookahead", not "look-ahead".
215 - "midrule", not "mid-rule".
216 - "nonterminal", not "variable" or "non-terminal" or "non terminal".
217 Abbreviated as "nterm".
218 - "shift/reduce" and "reduce/reduce", not "shift-reduce" or "shift reduce",
221 ## Syntax Highlighting
222 It's quite nice to be in C++ mode when editing lalr1.cc for instance.
223 However tools such as Emacs will be fooled by the fact that braces and
224 parens do not nest, as in `[[}]]`. As a consequence you might be misguided
225 by its visual pairing to parens. The m4-mode is safer. Unfortunately the
226 m4-mode is also fooled by `#` which is sees as a comment, stops pairing with
227 parens/brackets that are inside...
229 ## Implementation Notes
230 There are several places with interesting details about the implementation:
231 - [Understanding C parsers generated by GNU
232 Bison](https://www.cs.uic.edu/~spopuri/cparser.html) by Satya Kiran Popuri,
233 is a wonderful piece of work that explains the implementation of Bison,
234 - [src/gram.h](src/gram.h) documents the way the grammar is represented
235 - [src/tables.h](src/tables.h) documents the generated tables
236 - [data/README.md](data/README.md) contains details about the m4 implementation
239 Do not add horizontal tab characters to any file in Bison's repository
240 except where required. For example, do not use tabs to format C code.
241 However, make files, ChangeLog, and some regular expressions require tabs.
242 Also, test cases might need to contain tabs to check that Bison properly
243 processes tabs in its input.
245 Prefer `res` as the name of the local variable that will be "return"ed by
248 In writing arithmetic comparisons, use "<" and "<=" rather than ">" and ">="
249 (http://www.gelato.unsw.edu.au/archives/git/0505/4507.html).
252 Follow the GNU Coding Standards.
254 Don't reinvent the wheel: we use gnulib, which features many components.
255 Actually, Bison has legacy code that we should replace with gnulib modules
256 (e.g., many ad hoc implementations of lists).
259 The `#include` directives follow an order:
260 - first section for *.c files is `<config.h>`. Don't include it in header
262 - then, for *.c files, the corresponding *.h file
263 - then possibly the `"system.h"` header
264 - then the system headers.
265 Consider headers from `lib/` like system headers (i.e., `#include
266 <verify.h>`, not `#include "verify.h"`).
267 - then headers from src/ with double quotes (`#include "getargs.h"`).
269 Keep headers sorted alphabetically in each section.
272 files](https://www.gnu.org/software/gnulib/manual/html_node/Header-files.html)
273 and the [Implementation
274 files](https://www.gnu.org/software/gnulib/manual/html_node/Implementation-files.html#Implementation-files)
275 nodes of the gnulib documentation.
277 Some source files are in the build tree (e.g., `src/scan-gram.c` made from
278 `src/scan-gram.l`). For them to find the headers from `src/`, we actually
279 use `#include "src/getargs.h"` instead of `#include "getargs.h"`---that
280 saves us from additional `-I` flags.
283 We try to use the "typical" coding style for each language.
286 We indent the CPP directives this way:
296 Don't indent with leading spaces in the skeletons (it's OK in the grammar
297 files though, e.g., in `%code {...}` blocks).
299 On occasions, use `cppi -c` to see where we stand. We don't aim at full
300 correctness: depending `-d`, some bits can be in the *.c file, or the *.h
301 file within the double-inclusion cpp-guards. In that case, favor the case
302 of the *.h file, but don't waste time on this.
304 Don't hesitate to leave a comment on the `#endif` (e.g., `#endif /* FOO
305 */`), especially for long blocks.
307 There is no consistency on `! defined` vs. `!defined`. The day gnulib
308 decides, we'll follow them.
311 Follow the GNU Coding Standards.
313 The `glr.c` skeleton was implemented with `camlCase`. We are migrating it
314 to `snake_case`. Because we are gradually standardizing the code, it is
315 currently inconsistent.
317 Use `YYFOO` and `yyfoo` for entities that are exposed to the user. They are
318 part of our contract with the users wrt backward compatibility.
320 Use `YY_FOO` and `yy_foo` for private matters. Users should not use them,
321 we are free to change them without fear of backward compatibility issues.
323 Use `*_t` for types, especially for `yy*_t` in which case we shouldn't worry
324 about the C standard introducing such a name.
328 Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines).
329 The [Google ones](https://google.github.io/styleguide/cppguide.html) may be
332 Our enumerators, such as the kinds (symbol and token kinds), should be lower
333 case, but it was too late to follow that track for token kinds, and symbol
334 kind enumerators are made to be consistent with them.
336 Use `*_type` for type aliases. Use `foo_get()` and `foo_set(v)` for
337 accessors, or simply `foo()` and `foo(v)`.
339 Use the `yy` prefix for private stuff, but there's no need for it in the
340 public API. The `yy` prefix is already taken care of via the namespace.
343 We follow the [Java Code
344 Conventions](https://www.oracle.com/technetwork/java/codeconventions-150003.pdf)
345 and [Google Java Style
346 Guide](https://google.github.io/styleguide/javaguide.html). Unfortunately
347 at some point some GNU Coding Style was installed in Java, but it's an
348 error. So we should for instance stop putting spaces in function calls.
349 Because we are standardizing the code, it is currently inconsistent. Treat
350 acronyms as words: `YYLacStack`, not `YYLACStack`.
352 Use a 2-space indentation (Google) rather than 4 (Oracle).
354 Don't use the `yy` prefix for public members: `getExpectedTokens`, not
355 `yyexpectedTokens` or `yygetExpectedTokens`. Keep the `yy` prefix though
359 Imitate the style we use. Use `git log` to get sources of inspiration.
361 If the changes have a small impact on Bison's generated parser, embed these
362 changes in the commit itself. If the impact is large, first push all the
363 changes except those about src/parse-gram.[ch], and then another commit
364 named "regen" which is only about them.
367 Bison supports tracing of its various steps, via the `--trace` option.
368 Since it is not meant for the end user, it is not displayed by `bison
369 --help`, nor is it documented in the manual. Instead, run `bison
373 Use `@option` for options and options with their argument if they have no
374 space (e.g., `@option{-Dfoo=bar}`). However, use `@samp` elsewhere (e.g.,
382 Consume without moderation. It is composed of two kinds of tests: the
383 examples, and the main test suite.
386 In examples/, there is a number of ready-to-use examples (see
387 [examples/README.md](examples/README.md)). These examples have small test
388 suites run by `make check`. The test results are in local `*.log` files
389 (e.g., `$build/examples/c/calc/calc.log`).
391 ### The Main Test Suite
392 The main test suite, in tests/, is written on top of GNU Autotest, which is
393 part of Autoconf. Run `info autoconf 'Using Autotest'` to read the
394 documentation, not only about how to write tests, but also where are the
395 logs, how to read them etc.
397 The main test suite generates a log for each test (e.g.,
398 `$build/tests/testsuite.dir/004/testsuite.log` for test #4), and a main log
399 file in `$build/tests/testsuite.log`. The latter is meant for end users: it
400 contains lots of details that should help diagnosing issues, including build
401 issues. The per-test logs are more convenient when working locally.
404 To run just the main test suite, run `make check-local`.
406 The default is for make check-local to run all tests sequentially. This can
407 be very time consuming when checking repeatedly or on slower setups. This
408 can be sped up in two ways:
410 Using -j, in a make-like fashion, for example:
412 $ make check-local TESTSUITEFLAGS='-j8'
414 Actually, when using GNU Make, TESTSUITEFLAGS defaults to the -jN passed to
415 it, so you may simply run
417 $ make check-local -j8
419 Running only the tests of a certain category, as specified in the AT files
420 with AT_KEYWORDS([[category]]). Categories include:
421 - c++, for c++ parsers
422 - deprec, for tests concerning deprecated constructs.
423 - glr, for glr parsers
424 - java, for java parsers
425 - report, for automaton dumps
427 To get a list of all the tests (and their keywords for -k), run
429 $ ./tests/testsuite -l
431 To run a specific set of tests, use -k (for "keyword"). For example:
433 $ make check-local TESTSUITEFLAGS='-k c++'
435 Both can be combined.
437 $ make check-local TESTSUITEFLAGS='-j8 -k c++'
439 To rerun the tests that failed:
443 #### Updating the Expectations
444 Sometimes some changes have a large impact on the test suite (e.g., when we
445 added the `[-Wother]` part to all the warnings). Part of the update can be
446 done with a crude tool: `build-aux/update-test`.
448 Once you ran the test suite, and therefore have many `testsuite.log` files,
449 run `make update-tests`. Or, by hand, from the *source* tree:
451 $ ./build-aux/update-test $build/tests/testsuite.dir/*/testsuite.log
453 where `$build` would be your build tree. This will hopefully update most
454 tests. Re-run the test suite. It might be interesting to run `update-test`
455 again, since some early failures may stop latter tests from being run. Yet
456 at some point, you'll have to fix remaining issues by hand...
459 ## Running Java parsers
460 Use the `javaexec.sh` script. For instance to run the parser of test case
463 $ sh ./_build/javaexec.sh -cp ./_build/tests/testsuite.dir/504 Calc
466 Address sanitizer (ASAN) and undefined-behavior sanitizer (UBSAN) are very
467 useful. Here's one way to set them up with GCC 10 on Mac Ports
471 $ ./configure -C --enable-gcc-warnings \
472 CPPFLAGS='-isystem /opt/local/include' \
473 CC='gcc-mp-10 -fsanitize=address -fsanitize=undefined' \
475 CXX='g++-mp-10.0 -fsanitize=address -fsanitize=undefined' \
477 LDFLAGS='-L/opt/local/lib'
482 3. Generate debug symbols:
488 4. Run the tests with leak detection enabled
489 (`ASAN_OPTIONS=detect_leaks=1`). E.g. for counterexamples:
491 $ make check-local TESTSUITEFLAGS='-j5 -k cex' ASAN_OPTIONS=detect_leaks=1
494 5. You might need a suppression file. See
495 https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions.
496 With G++ on a Mac, you might need a suppression file (say `leak.supp`)
503 and pass the additional flags
504 `LSAN_OPTIONS=suppressions=$PWD/leak.supp,print_suppressions=0`
506 6. To run the debugger, you might want something like this:
509 UBSAN_OPTIONS=print_stacktrace=1 \
510 LSAN_OPTIONS=suppressions=$PWD/leak.supp,print_suppressions=0 \
511 ASAN_OPTIONS=detect_leaks=1 \
512 lldb -- ./_build/tests/testsuite.dir/712/glr-regr2a ./_build/tests/testsuite.dir/712/input1.txt
515 In lldb to set a break on ubsan, try `rbreak ^__ubsan_handle_`.
517 ## make maintainer-check-valgrind
518 This target uses valgrind both to check bison, and the generated parsers.
520 This is not mature on Mac OS X. First, Valgrind does support the way bison
521 calls m4, so Valgrind cannot be used to check bison on Mac OS X.
523 Second, there are many errors that come from the platform itself, not from
524 bison. build-aux/darwin11.4.0.valgrind addresses some of them.
526 Third, valgrind issues warnings such as:
528 --99312:0:syswrap- WARNING: Ignoring sigreturn( ..., UC_RESET_ALT_STACK );
530 which cause the test to fail uselessly. It is hard to ignore these errors
531 with a major overhaul of the way instrumentation is performed in the test
532 suite. So currently, do not try to run valgrind on Mac OS X.
535 Try to run the test suite with more severe conditions before a
538 - Configure the package with --enable-gcc-warnings, so that one checks that
539 1. Bison compiles cleanly, 2. the parsers it produces compile cleanly too.
541 - Maybe build with -DGNULIB_POSIXCHECK, which suggests gnulib modules that
542 can fix portability issues. See if you really want to pay attention to
543 its warnings; there's no need to obey blindly to it
544 (<http://lists.gnu.org/archive/html/bison-patches/2012-05/msg00057.html>).
546 - Check with `make syntax-check` if there are issues diagnosed by gnulib.
548 - run `make maintainer-check` which:
549 - runs `valgrind -q bison` to run Bison under Valgrind.
550 - runs the parsers under Valgrind.
551 - runs the test suite with G++ as C compiler...
553 - run `make maintainer-check-push`, which runs `make maintainer-check` while
554 activating the push implementation and its pull interface wrappers in many
555 test cases that were originally written to exercise only the pull
556 implementation. This makes certain the push implementation can perform
557 every task the pull implementation can.
559 - run `make maintainer-check-xml`, which runs `make maintainer-check` while
560 checking Bison's XML automaton report for every working grammar passed to
561 Bison in the test suite. The check just diffs the output of Bison's
562 included XSLT style sheets with the output of --report=all and --graph.
564 - running `make maintainer-check-release` takes care of running
565 maintainer-check, maintainer-check-push and maintainer-check-xml.
567 - Change tests/atlocal/CFLAGS to add your preferred options.
569 - Test with a very recent version of GCC for both C and C++. Testing with
570 older versions that are still in use is nice too.
573 To run tests on gnulib components (e.g., on bitset):
576 ./gnulib-tool --test bitset-tests
578 possibly within a specified environment:
580 CC='gcc-mp-8 -fsanitize=undefined' ./gnulib-tool --test bitset-tests
582 To be able to run the tests several times, and to use symlinks instead of
583 copies so that one can update the origin gnulib directory and immediately
584 re-run the tests, run:
586 ./gnulib-tool --symlink --create-test --dir=/tmp/gnutest bitset-tests
588 ./configure -C CC='gcc-mp-8 -fsanitize=undefined' CFLAGS='-ggdb'
594 Running old compilers is not very easy. Docker can be used for some of
595 them. Have a look at .travis.yml for setups. Move the tarball in `/tmp`
596 and run, for instance:
599 docker run -v /tmp:/tmp -it ubuntu:xenial
602 This way, the host and guest machines share `/tmp`.
609 apt-get install software-properties-common
610 apt-add-repository -y "ppa:ubuntu-toolchain-r/test"
612 apt-get install -y gcc-4.6 g++-4.6 m4 make
617 If you want to install several versions of Bison using GNU Stow, do
624 wget https://ftp.gnu.org/gnu/bison/bison-$v.tar.xz
625 tar xf bison-$v.tar.xz
627 ./configure --prefix /usr/local/stow/bison-$v
636 See the [README-release file](README-release), created when the package is
641 Copyright (C) 2002-2005, 2007-2015, 2018-2020 Free Software Foundation,
644 This file is part of GNU Bison.
646 This program is free software: you can redistribute it and/or modify
647 it under the terms of the GNU General Public License as published by
648 the Free Software Foundation, either version 3 of the License, or
649 (at your option) any later version.
651 This program is distributed in the hope that it will be useful,
652 but WITHOUT ANY WARRANTY; without even the implied warranty of
653 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
654 GNU General Public License for more details.
656 You should have received a copy of the GNU General Public License
657 along with this program. If not, see <http://www.gnu.org/licenses/>.
662 ispell-dictionary: "american"
665 LocalWords: Automake Autoconf Gettext Gzip Rsync Valgrind gnulib submodules
666 LocalWords: submodule init cd distcheck ChangeLog valgrind sigreturn sudo
667 LocalWords: UC gcc DGNULIB POSIXCHECK xml XSLT glr lalr README po runtime rc
668 LocalWords: gnupload gnupg gpg keyserver BDF ncftp filename clearsign cvs dir
669 LocalWords: symlinks vti html lt POSIX Cc'ed Graphviz Texinfo autoconf jN
670 LocalWords: automake autopoint graphviz texinfo PROG Wother parsers YYFOO
671 LocalWords: TESTSUITEFLAGS deprec struct gnulib's getopt config ggdb yyfoo
672 LocalWords: bitset fsanitize symlink CFLAGS MERCHANTABILITY ispell wrt YY
673 LocalWords: american Administrivia camlCase yy accessors namespace src hoc
674 LocalWords: getExpectedTokens yyexpectedTokens yygetExpectedTokens parens
675 LocalWords: regen dogfooding Autotest testsuite getargs CPP BAZ endif cppi
676 LocalWords: cpp javaexec cp Calc ASAN UBSAN CPPFLAGS isystem CXX cex Gperf
677 LocalWords: CXXFLAGS LDFLAGS dsymutil gperf