A compiler front-end generator.
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.
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.
Installation via stack (recommended)
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
BNFC versions in major Stackage LTS snapshots:
| GHC | LTS | BNFC |
|---|---|---|
| 8.8.4 | 16.31 | 2.8.4 |
| 8.10.7 | 18.28 | 2.9.4 |
| 9.0.2 | 19.33 | 2.9.4 |
| 9.2.8 | 20.26 | 2.9.4.1 |
| 9.4.8 | 21.25 | 2.9.5 |
| 9.6.7 | 22.44 | 2.9.5 |
| 9.8.4 | 23.27 | 2.9.5 |
| 9.10.2 | 24.3 | 2.9.5 |
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-9.12.yaml
(replace 9.12 with your GHC major 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):
In a fresh directory, prepare a grammar file
Sum.cfwith the following content:EInt. Exp ::= Integer; EPlus. Exp ::= Exp "+" Integer;Build a parser (in Haskell) with bnfc:
bnfc -d -m Sum.cf && makeThe
makestep needs the Haskell compiler GHC, the lexer generator alex and the parser generator happy (all included in the GHC installation).Inspect the generated files in directory
Sum.Test the parser.
echo "1 + 2 + 3" | Sum/Test
Try the C-family backends. (The prerequisites, GNU C(++) compiler (
gcc/g++), lexer generatorflexand parser generatorbison, 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/TestSumTry the other backends:
Option Backend --javaRequires Java, JLex or JFlex, and CUP. --java-antlrRequires ANTLR. --ocamlRequires OCaml, ocamllexandocamlyacc.--ocaml-menhirUses menhir instead of ocamlyacc.--agdaProduces Agda bindings to the parser generated for Haskell. --pygmentsProduces 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 [email protected].
For current limitations of bnfc, or to report a new bug, please consult our issue tracker.
Contribute
- Issue Tracker: https://github.com/BNFC/bnfc/issues
- Source Code: https://github.com/BNFC/bnfc
- Haskell coding style guide: https://github.com/andreasabel/haskell-style-guide/
- Some pull request etiquette:
- Document, document, document! (See style guide)
- Include test cases that cover your feature.
- Include changelog entry.
- More etiquette: E.g. https://gist.github.com/mikepea/863f63d6e37281e329f8
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:
- NASA's OGMA tool uses LBNF for its grammars, e.g. for a subset of C 99.
In teaching:
- Course Programming Language Technology at Chalmers / Gothenburg University.