The BNF Converter is a compiler construction tool generating a compiler front-end from a Labelled BNF grammar. It was originally written to generate Haskell code, but can also be used for generating Agda, C, C++, Java, Ocaml and XML code. Given a Labelled BNF grammar the tool produces: an abstract syntax as a Haskell, Agda, C, C++, Ocaml module or Java package, a case skeleton for the abstract syntax in the same language, an Alex, Flex, JLex, JFlex, or ocamllex lexer generator file, a Happy, CUP, Bison, Antlr, ocamlyacc or menhir parser generator file, a pretty-printer as a Haskell, Agda, C, C++, Java, or Ocaml module, an XML representation, a LaTeX file containing a readable specification of the language.


Keywords
development, gpl, library, parsing, program, Propose Tags, Skip to Readme, , Index, Quick Jump, BNFC.Abs, BNFC.Backend.Agda, BNFC.Backend.Base, BNFC.Backend.C, BNFC.Backend.C.CFtoBisonC, BNFC.Backend.C.CFtoCAbs, BNFC.Backend.C.CFtoCPrinter, BNFC.Backend.C.CFtoCSkel, BNFC.Backend.C.CFtoFlexC, BNFC.Backend.C.Common, BNFC.Backend.C.RegToFlex, BNFC.Backend.CPP.Common, BNFC.Backend.CPP.Makefile, BNFC.Backend.CPP.Naming, BNFC.Backend.CPP.NoSTL, BNFC.Backend.CPP.NoSTL.CFtoCPPAbs, BNFC.Backend.CPP.PrettyPrinter, BNFC.Backend.CPP.STL, BNFC.Backend.CPP.STL.CFtoCVisitSkelSTL, BNFC.Backend.CPP.STL.CFtoSTLAbs, BNFC.Backend.CPP.STL.STLUtils, BNFC.Backend.Common, BNFC.Backend.Common.Makefile, BNFC.Backend.Common.NamedVariables, BNFC.Backend.Common.OOAbstract, BNFC.Backend.Common.StrUtils, BNFC.Backend.Haskell, BNFC.Backend.Haskell.CFtoAbstract, BNFC.Backend.Haskell.CFtoAlex3, BNFC.Backend.Haskell.CFtoHappy, BNFC.Backend.Haskell.CFtoLayout, BNFC.Backend.Haskell.CFtoPrinter, BNFC.Backend.Haskell.CFtoTemplate, BNFC.Backend.Haskell.HsOpts, BNFC.Backend.Haskell.MkErrM, BNFC.Backend.Haskell.Utils, BNFC.Backend.HaskellGADT, BNFC.Backend.HaskellGADT.CFtoAbstractGADT, BNFC.Backend.HaskellGADT.CFtoTemplateGADT, BNFC.Backend.HaskellGADT.HaskellGADTCommon, BNFC.Backend.Java, BNFC.Backend.Java.CFtoAbstractVisitor, BNFC.Backend.Java.CFtoAllVisitor, BNFC.Backend.Java.CFtoAntlr4Lexer, BNFC.Backend.Java.CFtoAntlr4Parser, BNFC.Backend.Java.CFtoComposVisitor, BNFC.Backend.Java.CFtoCup15, BNFC.Backend.Java.CFtoFoldVisitor, BNFC.Backend.Java.CFtoJLex15, BNFC.Backend.Java.CFtoJavaAbs15, BNFC.Backend.Java.CFtoJavaPrinter15, BNFC.Backend.Java.CFtoVisitSkel15, BNFC.Backend.Java.RegToAntlrLexer, BNFC.Backend.Java.RegToJLex, BNFC.Backend.Java.Utils, BNFC.Backend.Latex, BNFC.Backend.OCaml, BNFC.Backend.OCaml.CFtoOCamlAbs, BNFC.Backend.OCaml.CFtoOCamlLex, BNFC.Backend.OCaml.CFtoOCamlPrinter, BNFC.Backend.OCaml.CFtoOCamlShow, BNFC.Backend.OCaml.CFtoOCamlTemplate, BNFC.Backend.OCaml.CFtoOCamlTest, BNFC.Backend.OCaml.CFtoOCamlYacc, BNFC.Backend.OCaml.OCamlUtil, BNFC.Backend.Pygments, BNFC.Backend.Txt2Tag, BNFC.Backend.XML, BNFC.CF, BNFC.Check.EmptyTypes, BNFC.GetCF, BNFC.Lex, BNFC.Lexing, BNFC.License, BNFC.Options, BNFC.Par, BNFC.PrettyPrint, BNFC.Print, BNFC.Regex, BNFC.TypeChecker, BNFC.Utils, BNFC-2.9.5.tar.gz, browse, Package description, Package maintainers, AndreasAbel, GregoireDetrez, MarkusForsberg, ThomasHallgren, edit package information , 2.4.3, 2.5.0.1, 2.7.0.0, 2.7.0.2, 2.8, 2.8.2, 2.8.3, 2.8.3.1, 2.8.4, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.9.4, 2.9.4.1, 2.9.5, http://bnfc.digitalgrammars.com/, https://github.com/BNFC/bnfc/releases, stack, bnfc from stackage, GHC, Cabal, GHCup, bnfc from hackage, development version of bnfc, alex, happy, JLex, JFlex, CUP, ANTLR, OCaml, menhir, Agda, Pygments, https://bnfc.readthedocs.org/en/latest/, bnfc-dev@googlegroups.com, issue tracker, https://github.com/BNFC/bnfc/issues, https://github.com/BNFC/bnfc, https://github.com/andreasabel/haskell-style-guide/, https://gist.github.com/mikepea/863f63d6e37281e329f8, BSD 3-clause license, GNU General Public License, OGMA, subset of C 99, Programming Language Technology, abstract-syntax-tree, bnf, bnf-converter, bnfc, grammar-specification, lexer-generator, parser-generator, pretty-print
License
BSD-3-Clause
Install
cabal install BNFC-2.5.0.1

Documentation

Hackage version BNFC on Stackage Nightly Stackage LTS version Build status Documentation status

The BNF Converter

What is the BNF Converter?

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

Given a LBNF grammar the tool produces:

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

More information: http://bnfc.digitalgrammars.com/

Installation

Some binaries are available at https://github.com/BNFC/bnfc/releases. Installation from the Haskell sources is possible via stack or cabal.

You need a running installation of stack. To install and run the latest version of bnfc from stackage, enter at the command line:

  stack install BNFC
  bnfc --help

Installation via cabal

You need a running installation of a recent version of GHC and Cabal, most easily available via the GHCup. To install bnfc from hackage, enter at the command line:

  cabal install BNFC
  bnfc --help

Installing the development version

To install the development version of bnfc with the latest bugfixes (and regressions ;-)):

  git clone https://github.com/BNFC/bnfc.git
  cd bnfc/source

and then either

  cabal install

or

  stack install --stack-yaml stack-8.10.7.yaml

(replace 8.10.7 with your GHC version, and if you want to build with your installed GHC then add flag --system-ghc).

Mini tutorial

  • Build a first parser in 5 min (Haskell backend):

    1. In a fresh directory, prepare a grammar file Sum.cf with the following content:

      EInt.  Exp ::= Integer;
      EPlus. Exp ::= Exp "+" Integer;
      
    2. Build a parser (in Haskell) with bnfc:

      bnfc -d -m Sum.cf  &&  make
      

      The make step needs the Haskell compiler GHC, the lexer generator alex and the parser generator happy (all included in the GHC installation).

    3. Inspect the generated files in directory Sum.

    4. Test the parser.

      echo "1 + 2 + 3" | Sum/Test
      
  • Try the C-family backends. (The prerequisites, GNU C(++) compiler (gcc / g++), lexer generator flex and parser generator bison, are usually present):

    bnfc --c   -m -o sum-c   Sum.cf  &&  make -C sum-c    &&  echo "1 + 2 + 3" | sum-c/TestSum
    bnfc --cpp -m -o sum-cpp Sum.cf  &&  make -C sum-cpp  &&  echo "1 + 2 + 3" | sum-cpp/TestSum
    
  • Try the other backends:

    Option Backend
    --java Requires Java, JLex or JFlex, and CUP.
    --java-antlr Requires ANTLR.
    --ocaml Requires OCaml, ocamllex and ocamlyacc.
    --ocaml-menhir Uses menhir instead of ocamlyacc.
    --agda Produces Agda bindings to the parser generated for Haskell.
    --pygments Produces a lexer definition for the Python highlighting suite Pygments.

Documentation

https://bnfc.readthedocs.org/en/latest/

Support

You can discuss with us issues around bnfc on our mailing list bnfc-dev@googlegroups.com.

For current limitations of bnfc, or to report a new bug, please consult our issue tracker.

Contribute

License

The project is licensed under the BSD 3-clause license.

BNFC versions until 2.8.4 released under the GNU General Public License.

Example uses of the BNF Converter

In research:

In teaching: