Questions tagged [bnfc]

BNFC is the BNF Converter compiler construction tool which takes an annotated BNF (Backus-Naur Form) grammar and creates a parser from it.

The BNF Converter project is a tool similar to Yacc and ANTLR in that it takes a grammar specification written in BNF notation and generates a compiler that recognizes the language specified.

The Github site says:

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#, F#, 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

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

41 questions
5
votes
1 answer

Hooking up a build tool in Cabal (Haskell)

I was trying to use bnfc tool to generate a bunch of files, like lexer, parser, etc. for me. This works fine. Now I wanted to clean this up a bit by not having to manually compile the bnfc file and having it generate a number of files which clutter…
user5803465
  • 343
  • 2
  • 7
4
votes
1 answer

BNFC parser and bracket Mathematica like syntax

I played a bit with the BNF Converter and tried to re-engineer parts of the Mathematica language. My BNF had already about 150 lines and worked OK, until I noticed a very basic bug. Brackets [] in Mathematica are used for two different…
halirutan
  • 4,281
  • 18
  • 44
2
votes
0 answers

Solving "the lexer hack"/"typedef-name: identifier" problem in BNFC

I'm trying to parse CPP using BNFC and I've come to the "typedef-name: identifier" problem (https://en.wikipedia.org/wiki/The_lexer_hack). This is the spec for BNFC: https://bnfc.readthedocs.io/en/latest/. The problem comes up when a new type is…
Conor Quinn
  • 529
  • 4
  • 13
1
vote
0 answers

Does BNFC support the symbol '|'?

There is a simple example in BNFC's documentation. Char_a. Char ::= "a" ; Char_b. Char ::= "b" ; I try to rewrite it by using union | Char_a. Char ::= "a" | "b" ; but then in this case I got an error bnfc: user error (syntax error at line 1…
HobbitQia
  • 11
  • 1
1
vote
1 answer

How to resolve mismatch module name in haskell stack

I have some haskell modules generated which is named Par.hs in Bnfc/Par.hs The generated module is module Bnfc.Abs where in my library referencing this is module Lib ( someFunc ) where import Bnfc.Abs import Text.Printf and my…
1
vote
1 answer

Issue parsing a program with LBNF Grammar for C++

I'm trying to modify this grammar to be able to parse a C++ program and it's having a hiccup with the using statement, throwing the error syntax error at line 10 before using std::cin ; using PDefs. Program ::= [Def] ; DFun. Def ::= Type Id…
gyaretto
  • 21
  • 2
1
vote
1 answer

Trying to convert a VHDL BNF to a labeled BNF for BNFC

I am in the trying to translate a VHDL BNF defined here to a labeled BNF to use with BNFC After running bnfc vhdl93-bnf.cf the result is: bnfc: user error (syntax error at line 22 before { , element_association }) I am not sure what the error is.…
LambdaScientist
  • 435
  • 2
  • 13
1
vote
2 answers

How to write a grammar for hex ints in BNFC?

Here's a tiny grammar for hexadecimal integers. Numbers . Numbers ::= [HexInt]; separator HexInt " " ; token HexDigit ["0123456789abcdefABCDEF"] ; rules HexInt ::= "0x" [HexDigit] ; separator HexDigit "" ; It fails to parse "0xff", however,…
Mike Stay
  • 1,071
  • 8
  • 17
1
vote
2 answers

BNFC install error

I am trying to install BNFC on my Mac (Running OS Sierra). I have already installed the Haskell Platform for Mac (v.8.0.1) I have installed cabal then used that to install alex (v 3.2.1) and happy (v 1.19.5). I then ran the cabal install bnfc and…
Will
  • 139
  • 1
  • 8
1
vote
1 answer

Why parsing this program with BNFC fails?

Given following grammar: comment "/*" "*/" ; TInt. Type1 ::= "int" ; TBool. Type1 ::= "bool" ; coercions Type 1 ; BTrue. BExp ::= "true" ; BFalse. BExp ::= "false" ; EOr. Exp ::= Exp "||" Exp1 ; EAnd. Exp1 ::= Exp1 "&&" Exp2…
kitek
  • 117
  • 1
  • 13
1
vote
2 answers

How to do proper error handling in BNFC? (C++, Flex, Bison)

I'm making a compiler in BNFC and it's got to a stage where it already compiles some stuff and the code works on my device. But before shipping it, I want my compiler to return proper error messages when the user tries to compile an invalid…
Eduardo Wada
  • 2,606
  • 19
  • 31
1
vote
1 answer

BNF grammar for a simple c++ program example

So i am trying to write grammar for a simple c++ program. this is how the grammar looks like right now: PDefs. Program ::= [Def] ; terminator Def "" ; comment "//" ; comment "/*" "*/" ; comment "#" ; DFun. Def ::= Type Id "(" [Arg] ")" "{" [Stm] "}"…
user2854773
1
vote
1 answer

How to disable built-in rules?

How can I disable all BNFC built-in rules, like Ident, Integer or the spaces being used to separate tokens? I found them useless and annoying since they interfere with the parsers I'm trying to write. I already tried to re-define them but it seems…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
1
vote
1 answer

Is there a way to use whitespace in BNFC?

How do you use whitespace in a BNFC definition? For example, suppose I want to produce a parser for the lambda calculus where I allow a list of variables to be abstracted: \x y z.x z (y z) The "obvious" thing to do is use a labeled rule…
Jonathan Gallagher
  • 2,115
  • 2
  • 17
  • 31
1
vote
2 answers

Setting precedence levels in BNFC grammar

Background: I'm taking a class in software semantics, and we are supposed to create a small compiler and runtime for a toy language called while. We were given a code skeleton for Java, but we are allowed to use whatever language we want. I saw this…
ansjob
  • 175
  • 7
1
2 3