Using ocamlyacc with dune

I’ve recently tried to add a my_parser.mly file to my dune project. My dune file was as follows :

(include_subdirs unqualified)

(library
(flags -w +1..31+33..69+71+72)
(name my_library_lib)
(libraries str unix zarith)
)

I read at ocamlyacc - Dune documentation that the extra text should be (ocamlyacc my_parser).
I first tried to add that extra command inside the library stanza, as follows :

(include_subdirs unqualified)

(library
(flags -w +1..31+33..69+71+72)
(name my_library_lib)
(libraries str unix zarith)
(ocamlyacc my_parser)
)

But when I do that, dune build fails with the error message

7 | (ocamlyacc my_parser)
     ^^^^^^^^^
Error: Unknown field ocamlyacc

So I tried putting it outside library :

(include_subdirs unqualified)

(library
(flags -w +1..31+33..69+71+72)
(name my_library_lib)
(libraries str unix zarith)
)
(ocamlyacc my_parser)

But in that case,

dune build fails with the error message

8 | (ocamlyacc my_parser)
     ^^^^^^^^^
Error: No rule found for lib/my_parser.mly

What is the correct syntax then ?

The latter one is the correct syntax, but I think you should explain more about your project setup. The error message mentions a lib/ folder, but where does your dune file live?

Maybe you can even provide a small example to reproduce?

My dune setup is very basic. The main root directory contains a default dune-project file, and a lib subdirectory which contains the dune file which I have shown above. That’s it. There are several sub-directories and sub-sub-directories and so on in lib, but none of them contain a dune-related file.

The my_parser.mly file is in some sub-sub-(…) directory of lib (which are not mentioned in the error message, which makes sense because of the include_subdirs unqualified directive).

If so, I need to understand why dune gives me this No rule found message. By the way, putting “No rule found” or “no rule found error”
in the search field produces only unrelated results in the dune doc.

I’ll try …

Yes, so this is the issue. If you have ocamlyacc foo then dune expects foo.mly to be in the same folder as the dune file.

This is why the error message says that there’s no rule to build lib/my_parser.mly because there’s neither a source file there nor a rule that would create a source file in that location.

Thank you. And (if I don’t want to change the location of the mly file) the fix is ? To add the path
into the ocamlyacc directive ?

Give it a try but personally I would create a dune file with (ocamlyacc my_parser) in the same folder as the my_parser.mly if you want to preserve the directory layout that you have.

Indeed, my idea fails with the error message Error: “blah/blah/my_parser.mli” does not denote a file in the current directory. Your idea works though, thanks !