Search code examples
ocamlparser-generatorocamlyacc

Using external type declarations with OCamlyacc


I have a type expr in an expr.ml file. In parser.mly (OCamlyacc file), I define the expr rule and give the type :

    %start expr
    %type <expr> expr

However, I get :

    File "parser.mli", line 34, characters 48-52:
    Error: Unbound type constructor expr

I tried adding

    %{
      open Expr
    %}

at the beginning of the .mly file but it still doesn't work. How may I define this expr type in an external file and use it as the return value of my rule? Thanks.


Solution

  • You need to qualify expr type with the module name. I.e., if it is defined in expression.ml (using type expr = ...) you should use

    %type <Expresssion.expr> main
    

    Note the capital E when using the module name.