Centre for Language Technology

Chalmers University of Technology and University of Gothenburg

What is the BNF Converter?

The BNF Converter is a compiler construction tool generating a compiler front-end from a Labelled BNF grammar. It is currently able to generate C, C++, C#, Haskell, Java, and OCaml, as well as XML representations.

Given a Labelled BNF grammar the tool produces:

  • an abstract syntax implementation
  • a case skeleton for the abstract syntax in the same language
  • an Alex, JLex, or Flex lexer generator file
  • a Happy, CUP, or Bison parser generator file
  • a pretty-printer as a Haskell/Java/C++/C module
  • a Latex file containing a readable specification of the language

Download

Install by Cabal from Hackage:

$ cabal install bnfc

Current development code (Source repository on GitHub)

$ git clone https://github.com/BNFC/bnfc

Resources

Documentation

Mailing list

Send questions, bug reports, usage news, etc, to our Google group.

Book

Book cover

Implementing Programming Languages, a compiler book using BNFC.

A small example

-- file Calc.bnfc
EAdd. Exp ::= Exp "+" Exp1 ;
ESub. Exp ::= Exp "-" Exp1 ;
EMul. Exp1 ::= Exp1 "*" Exp2 ;
EDiv. Exp1 ::= Exp1 "/" Exp2 ;
EInt. Exp2 ::= Integer ;
coercions Exp 2 ;

bnfc -haskell Calc.bnfc
bnfc -c       Calc.bnfc
bnfc -cpp     Calc.bnfc
bnfc -csharp  Calc.bnfc
bnfc -java    Calc.bnfc
bnfc -ocaml   Calc.bnfc
   

Large examples

A grammar of C. An example C file parsable in type Program.

A grammar of Alfa (For more details, see the Alfa home page.)

A grammar of LBNF. It is at the same time an example LBNF file, parsable in type Grammar. This grammar is used in the implementation of the BNF Converter.

A grammar of Java 1.1 developed by Mike Rainey

Authors

© Jonas Almström Duregård, Krasimir Angelov, Jean-Philippe Bernardy, Björn Bringert, Johan Broberg, Paul Callaghan, Grégoire Détrez, Markus Forsberg, Ola Frid, Peter Gammie, Thomas Hallgren, Patrik Jansson, Kristofer Johannisson, Antti-Juhani Kaijanaho, Ulf Norell, Michael Pellauer and Aarne Ranta 2002 - 2013.

License

Free software under GNU General Public License (GPL).