LinuxCommandLibrary

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 header file with token definitions
-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

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

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, later adopted by the GNU Project.

SEE ALSO

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

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community