ArticleslgStudy

computer science

GNU Bison

GNU Bison is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand GNU Bison rather than just read about it. In short: GNU Bison, commonly known as Bison, is a parser generator that is part of the GNU Project. Bison reads a specification in Bison syntax (described as "machine-readable BNF"), warns about any parsing ambiguities, and generates a parser that reads sequences of tokens and decides whether the sequence conforms to the syntax specified by the grammar.

GNU Bison — main illustration
GNU Bison — illustration

Key takeaways

  • GNU Bison belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect GNU Bison to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of GNU Bison from memory before moving on to harder problems.

Reference excerpt

GNU Bison, commonly known as Bison, is a parser generator that is part of the GNU Project. Bison reads a specification in Bison syntax (described as "machine-readable BNF"), warns about any parsing ambiguities, and generates a parser that reads sequences of tokens and decides whether the sequence conforms to the syntax specified by the grammar. The generated parsers are portable: they do not require any specific compilers. Bison by default generates LALR(1) parsers but it can also generate canonical LR, IELR(1) and GLR parsers. In POSIX mode, Bison is compatible with Yacc, but also has several extensions over this earlier program, including

Generation of counterexamples for conflicts Location tracking (e.g., file, line, column) Rich and internationalizable syntax error messages in the generated parsers Customizable syntax error generation, Reentrant parsers Push parsers, with autocompletion Support for named references Several types of reports (graphical, XML) on the generated parser Support for several programming languages (C, C++, D, or Java) 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 1985. Later, in 1989, Robert Corbett released 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.

Features

Counterexample generation One delicate issue with LR parser generators is the resolution of conflicts (shift/reduce and reduce/reduce conflicts). With many LR parser generators, resolving conflicts requires the analysis of the parser automaton, which demands some expertise from the user. To aid the user in understanding conflicts more intuitively, Bison can instead automatically generate counterexamples. For ambiguous grammars, Bison often can even produce counterexamples that show the grammar is ambiguous. For instance, on a grammar suffering from the infamous dangling else problem, Bison reports

doc/if-then-else.y: warning: shift/reduce conflict on token "else" [-Wcounterexamples] Example: "if" expr "then" "if" expr "then" stmt • "else" stmt Shift derivation if_stmt ↳ "if" expr "then" stmt ↳ if_stmt ↳ "if" expr "then" stmt • "else" stmt Example: "if" expr "then" "if" expr "then" stmt • "else" stmt Reduce derivation if_stmt ↳ "if" expr "then" stmt "else" stmt ↳ if_stmt ↳ "if" expr "then" stmt •

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.

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

License 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 copyright questions.

A GPL-compatible license 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 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 do not have the generated files in their version control systems. The files are only generated when making a release. Some licenses, 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. This makes it difficult to determine if a project "uses" Bison-specific source code or not. In many cases, the "use" of Bison could trivially be replaced by the equivalent use of Yacc or one of its other derivatives. Bison has features not found in Yacc, so some projects can truly be said to "use" Bison, since Yacc would not 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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with GNU Bison

Start with the simplest possible case. Write down what GNU Bison claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to GNU Bison before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about GNU Bison ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of GNU Bison

In research
GNU Bison appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses GNU Bison in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
GNU Bison is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiling tools, Cross-platform software, GNU Project software, so understanding it makes those chapters shorter.
In everyday life
Look for GNU Bison outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “GNU Bison” →

How to study GNU Bison in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what GNU Bison means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain GNU Bison out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is GNU Bison in simple terms?

GNU Bison, commonly known as Bison, is a parser generator that is part of the GNU Project. Bison reads a specification in Bison syntax (described as "machine-readable BNF"), warns about any parsing ambiguities, and generates a parser that reads sequences of tokens and decides whether the sequence c…

Why does GNU Bison matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study GNU Bison?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on GNU Bison.

Tags

  • Compiling tools
  • Cross-platform software
  • GNU Project software
  • Parser generators

Keep exploring