BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
582 stars 161 forks source link

Error with Menhir 20211230 #407

Closed k4rtik closed 2 years ago

k4rtik commented 2 years ago

With the latest release of menhir that requires type information in the source or inference enabled:

The code back-end now needs type information. This means that either Menhir's type inference mechanism must be enabled (the easiest way of enabling it is to use Menhir via dune and to check that the dune-project file says (using menhir 2.0) or later) or the type of every nonterminal symbol must be explicitly given via a %type declaration.

The menhir backend for BNFC currently fails as follows:

❯ cat > Sum.cf
EInt.  Exp ::= Integer;
EPlus. Exp ::= Exp "+" Integer;
/tmp
❯ bnfc -m Sum.cf --ocaml-menhir &&  make
2 rules accepted

writing new file ./AbsSum.ml
writing new file ./LexSum.mll
writing new file ./ParSum.mly
writing new file ./SkelSum.ml
writing new file ./PrintSum.ml
writing new file ./ShowSum.ml
writing new file ./TestSum.ml
writing new file ./BNFC_Util.ml
writing new file ./Makefile
menhir ParSum.mly
File "ParSum.mly", line 14, characters 14-22:
Warning: the token TOK_Char is unused.
File "ParSum.mly", line 15, characters 15-25:
Warning: the token TOK_Double is unused.
File "ParSum.mly", line 13, characters 16-25:
Warning: the token TOK_Ident is unused.
File "ParSum.mly", line 17, characters 16-26:
Warning: the token TOK_String is unused.
Error: the code back-end requires the type of every nonterminal symbol to be
known. Please specify the type of every symbol via %type declarations, or
enable type inference (look up --infer in the manual).
Type inference is automatically enabled when Menhir is used via Dune,
provided the dune-project file says (using menhir 2.0) or later.
The types of the following nonterminal symbols are unknown:
exp
int
make: *** [all] Error 1
/tmp
andreasabel commented 2 years ago

A simple fix is to annotate each non-terminal with its ML-type in the generated parser description (.mly file). This is compatible with ocamlyacc.

andreasabel commented 2 years ago

PR #408 fixes the OP, but menhir-2021-12-30 has a regression in error handling, see https://gitlab.inria.fr/fpottier/menhir/-/issues/62.

k4rtik commented 2 years ago

Thanks, @andreasabel for fixing this issue so quickly. 🎉