Trisha Shetty (Editor)

GNU bison

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Operating system
  
Cross-platform

License
  
GPL (free software)

Type
  
Parser generator

Developer(s)
  
Robert Corbett, The GNU Project

Stable release
  
3.0.4 (January 23, 2015; 2 years ago (2015-01-23)) [±]

Website
  
www.gnu.org/software/bison/

GNU bison, commonly known as Bison, is a parser generator that is part of the GNU Project. Bison reads a specification of a context-free language, warns about any parsing ambiguities, and generates a parser (either in C, C++, or Java) which reads sequences of tokens and decides whether the sequence conforms to the syntax specified by the grammar. Bison by default generates LALR parsers but can also create GLR parsers.

Contents

In POSIX mode, Bison is compatible with yacc, but also has several extensions over this earlier program. flex, an automatic lexical analyser, is often used with Bison, to tokenise input data and provide Bison with tokens.

Bison was originally written by Robert Corbett in 1988. Later, in 1990, Robert Corbett wrote another parser generator named Berkeley Yacc. Bison was made Yacc-compatible by Richard Stallman.

Bison is free software and is available under the GNU General Public License, with an exception (discussed below) allowing its generated code to be used without triggering the copyleft requirements of the licence.

A complete reentrant parser example

The following example shows how to use Bison and flex to write a simple calculator program (only addition and multiplication) and a program for creating an abstract syntax tree. The next two files provide definition and implementation of the syntax tree functions.

The tokens needed by the Bison parser will be generated using flex.

Since the tokens are provided by flex we must provide the means to communicate between the parser and the lexer. The data type used for communication, YYSTYPE, is set using Bison's %union declaration.

Since in this sample we use the reentrant version of both flex and yacc we are forced to provide parameters for the yylex function, when called from yyparse. This is done through Bison's %lex-param and %parse-param declarations.

The code needed to obtain the syntax tree using the parser generated by Bison and the scanner generated by flex is the following.

A simple makefile to build the project is the following.

Reentrancy

Reentrancy is a feature which has been added to Bison and does not exist in Yacc.

Normally, Bison generates a parser which is not reentrant. In order to achieve reentrancy the declaration %define api.pure must be used. More details on Bison reentrancy can be found in the Bison manual.

Using Bison from other languages

Bison can only generate code for C, C++ and Java. For using the Bison generated parser from other languages a language binding tool such as SWIG can be used.

Licence and distribution of generated code

Because Bison generates source code that in turn gets added to the source code of other software projects, it raises some simple but interesting copyright questions.

A GPL-compatible licence is not required

The code generated by Bison includes significant amounts of code from the Bison project itself. The Bison package is distributed under the terms of the GNU General Public License (GPL) but an exception has been added so that the GPL does not apply to output.

Earlier releases of Bison stipulated that parts of its output were also licensed under the GPL, due to the inclusion of the yyparse() function from the original source code in the output.

Distribution of packages using Bison

Free software projects that use Bison may have a choice of whether to distribute the source code which their project feeds into Bison, or the resulting C code made output by Bison. Both are sufficient for a recipient to be able to compile the project's source code. However, distributing only the input carries the minor inconvenience that the recipients must have a compatible copy of Bison installed so that they can generate the necessary C code when compiling the project. And distributing only the C code in output, creates the problem of making it very difficult for the recipients to modify the parser since this code was written neither by a human nor for humans - its purpose is to be fed directly into a C compiler.

These problems can be avoided by distributing both the input files and the generated code. Most people will compile using the generated code, no different from any other software package, but anyone who wants to modify the parser component can modify the input files first and re-generate the generated files before compiling. Projects distributing both usually don't have the generated files in their revision control systems. The files are only generated when making a release.

Some licences, such as the GPL, require that the source code be in "the preferred form of the work for making modifications to it". GPL'd projects using Bison must thus distribute the files which are the input for Bison. Of course, they can also include the generated files.

Use

Because Bison was written as a replacement for Yacc, and is largely compatible, the code from a lot of projects using Bison could equally be fed into Yacc, so it's difficult to say that a project's source code "uses" Bison. In many cases, the "use" of Bison could be trivially replaced by the equivalent use of Yacc.

Bison does have features not found in Yacc, so some projects can be truly said to "use" Bison, since Yacc wouldn't suffice.

The following list is of projects which are known to "use" Bison in the looser sense, that they use free software development tools and distribute code which is intended to be fed into Bison or a Bison-compatible package.

  • The Ruby programming language (YARV)
  • The PHP programming language (Zend Parser)
  • GCC started out using Bison, but switched to a hand-written recursive-descent parser for C++ in 2004 (version 3.4), and for C and Objective-C in 2006 (version 4.1)
  • The Go programming language (GC)
  • Bash shell uses a yacc grammar for parsing the command input.
  • LilyPond
  • PostgreSQL
  • MySQL
  • GNU Octave uses a Bison-generated parser.
  • Perl 5 uses a Bison-generated parser starting in 5.10
  • References

    GNU bison Wikipedia