bison
Generate parsers from grammar descriptions
TLDR
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; }
;
%%
FEATURES
- LALR(1) parser generation- GLR parsing support- C++, Java output- Location tracking- Error recovery- Precedence declarations- Semantic actions
WORKFLOW
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
INSTALL
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.
