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/
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
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 |
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
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.4.yaml
(replace 9.4
with your GHC major version, and if you want to build with
your installed GHC then add flag --system-ghc
).
-
Build a first parser in 5 min (Haskell backend):
-
In a fresh directory, prepare a grammar file
Sum.cf
with the following content:EInt. Exp ::= Integer; EPlus. Exp ::= Exp "+" Integer;
-
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). -
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 generatorflex
and 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/TestSum
-
Try the other backends:
Option Backend --java
Requires Java, JLex or JFlex, and CUP. --java-antlr
Requires ANTLR. --ocaml
Requires OCaml, ocamllex
andocamlyacc
.--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.
https://bnfc.readthedocs.org/en/latest/
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.
- 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
The project is licensed under the BSD 3-clause license.
BNFC versions until 2.8.4 released under the GNU General Public License.
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.