Skip to content

Latest commit

 

History

History
83 lines (66 loc) · 2.28 KB

File metadata and controls

83 lines (66 loc) · 2.28 KB

Parser Generator Frontend: Bison

This frontend describes the context free grammar of a parser as a GNU Bison grammar document.

The following functionality is currently supported:

  • %token
  • %left
  • %right
  • %nonassoc
  • %precedence
  • rules
  • %prec
  • %start

Any not supported functionality is ignored.

The GNU Bison grammar parser is tested against a set of well known GNU Bison grammar files for several programming languages, to make sure that it works correctly. The well known grammar files include GNU Bison, GCC C, GCC Objective C, GCC C++, GCC Java, Go and more. See thee testdata directory in the repository root for the full set of grammars.

Example

The GNU Bison input looks like this:

%token NUMBER
%token LPAREN "("
%token RPAREN ")"

%right UMINUS
%left  PLUS MINUS
%left  STAR SLASH
%nonassoc LT GT
%precedence NOT

%start expr

%%

expr
  : expr PLUS  expr
  | expr MINUS expr
  | expr STAR  expr
  | expr SLASH expr
  | expr LT    expr
  | expr GT    expr
  | NOT expr   %prec NOT
  | MINUS expr %prec UMINUS
  | LPAREN expr RPAREN
  | NUMBER
  ;

stmts
  : %empty
  | stmts stmt
  ;

stmt
  : expr
  ;

See the official GNU Bison documentation for details on the grammar syntax.

Benchmarks

goos: linux
goarch: amd64
pkg: golr/internal/parsergen/frontend/bison
cpu: Intel(R) Core(TM) i9-14900K
BenchmarkToGrammar/GNU_Bison_3.8.2-32                                699           1610186 ns/op          473257 B/op       6235 allocs/op
BenchmarkToGrammar/GCC_2.95.3_C-32                                  1020           3314797 ns/op         1169322 B/op      17970 allocs/op
BenchmarkToGrammar/GCC_2.95.3_Objective_C-32                         223           5782654 ns/op         1506658 B/op      23352 allocs/op
BenchmarkToGrammar/GCC_3.3.6_C++-32                                  126           9164394 ns/op         2619912 B/op      41781 allocs/op
BenchmarkToGrammar/GCC_4.2.4_Java-32                                 159           7745372 ns/op         2420908 B/op      24190 allocs/op
BenchmarkToGrammar/Go_1.5.4-32                                       288           3487969 ns/op         1029408 B/op      16399 allocs/op
PASS
ok      golr/internal/parsergen/frontend/bison  9.202s