LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

bison

Generate parsers from grammar descriptions

TLDR

Generate parser
$ bison [grammar.y]
copy
Generate with verbose output
$ bison -v [grammar.y]
copy
Specify output file
$ bison -o [parser.c] [grammar.y]
copy
Generate header file
$ bison -d [grammar.y]
copy

SYNOPSIS

bison [options] file

DESCRIPTION

bison is a general-purpose parser generator that converts grammar descriptions (in .y files) into C, C++, or Java parsers. It's compatible with yacc and used for building compilers, interpreters, and other language processors.The tool is a GNU replacement for yacc with additional features and better error handling.

PARAMETERS

-o, --output=file

Output file name
-d, --defines
Generate a header file with token definitions (also spelled --header)
-H, --header[=file]
Generate the token-definitions header, optionally at a given path
-v, --verbose
Create .output file with parser states
-t, --debug
Enable debug output in parser
-g, --graph
Generate VCG graph of parser
-r, --report=things
Generate report (state, itemset, lookahead)
-W, --warnings
Enable warnings
-l, --no-lines
Don't generate #line directives
-L, --language=lang
Target output language: c, c++, or java
-y, --yacc
Emulate POSIX yacc (default output names y.tab.c, y.tab.h)

GRAMMAR FILE FORMAT

$ %{
#include <stdio.h>
%}

%token NUMBER
%%

expr: NUMBER '+' NUMBER { $$ = $1 + $3; }
    ;

%%
copy

FEATURES

- LALR(1) parser generation- GLR parsing support- C++, Java output- Location tracking- Error recovery- Precedence declarations- Semantic actions

WORKFLOW

$ # Generate parser
bison -d calculator.y

# Compile with flex lexer
flex lexer.l
gcc lex.yy.c calculator.tab.c -o calculator

# View parser details
bison -v grammar.y
cat grammar.output
copy

INSTALL

sudo apt install bison
copy
sudo dnf install bison
copy
sudo pacman -S bison
copy
sudo apk add bison
copy
sudo zypper install bison
copy
brew install bison
copy
nix profile install nixpkgs#bison
copy

CAVEATS

Learning curve for grammar syntax. Shift/reduce and reduce/reduce conflicts possible. Generated code can be large. Error messages sometimes cryptic. Usually paired with flex for lexical analysis.

HISTORY

Bison was written by Robert Corbett in 1985 as a free replacement for Unix yacc. Richard Stallman made it yacc-compatible, and it became part of the GNU Project. It is now maintained by Akim Demaille and others.

SEE ALSO

flex(1), yacc(1), antlr(1)

RESOURCES

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid