The BNF Converter is a compiler construction tool generating a compiler front-end from a labeled BNF grammar. It is currently able to generate representations for several languages. Given a labeled BNF grammar BNFC generates:
- an abstract syntax implementation
- an Alex, JLex or Flex lexer generator file
- an Happy, CUP, or Bison parser generator file
- a pretty printer
- a LaTeX file containing a readable specification of the language
Further informations and sources can be found at:
http://www.cse.chalmers.se/research/group/Language-technology/BNFC/
Bnfc has to be installed in order to use bnfcCabal.
BnfcCabal is a cabal file creating tool for labeled BNF grammars of file extension *.cf. BnfcCabal runs bnfc on a given labeled grammar and automatically builds a cabal file containing all modules created by bnfc.
BnfcCabal can be installed using
cabal install bnfcCabal
Before using BnfcCabal ensure the executable bnfc
can be found in system PATH.
-
Create a labeled grammar like
mygrammar.cf
-
Launch
bnfcCabal mygrammar.cf
-
Type
cabal install
in order to install the bnfc modules
- Grammar
Examples/c.cf
is already existing - Change to directory
Examples
- Type
bnfcCabal c.cf
to build a cabal file for c grammar - To install grammar type
cabal install
See Section (3.3) how to use installed modules
We use a grammar named mygrammar
as a
running example in this section. After running bnfcCabal mygrammar.cf
the modules of a grammar mygrammar
can be found as submodules of
Language.Mygrammar
. In order to use the parser of
mygrammar
some modules have to be imported:
import Language.Mygrammar.Parmygrammar
import Language.Mygrammar.Absmygrammar
import Language.Mygrammar.ErrM
Module ErrM
contains a error monad to handle parse errors, whereas Absmygrammar
contains the abstract syntax.
The module Parmygrammar
contains lexer and parse functions. The lexer
function is always called myLexer
, but the parsing function is named
after the root of your grammar. Assume data type Myroot
is the root of
mygrammar
. The function
(pMyroot . myLexer) :: String -> Err Root
is the correct parsing function for data type Myroot
. Have a look at module
Language.Mygrammar.ErrM
to get an impression of the error monad.
-
[BUG] Module
Main
(in fileTestmygrammar
) is not being installled -
check whether link to bnfc binary is correct (preConf-hook)