3

I'm using menhir to generate a parser and right now, the parser.mli file that it generated from my parser.mly file looks like this:

(* The type of tokens. *)

type token = 
  (* ... huge ADT definition goes here ... *)

(* This exception is raised by the monolithic API functions. *)

exception Error

(* The monolithic API. *)

val start: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> Types.ast

Is there a way to include more stuff in my parser's interface? In particular, I would like to be able to also export the datatype for my AST (which is currently in a separate Types module) and some functions that work with the token datatype (for example, a function to convert them back to strings).

I tried putting some Ocaml code after the %% in parser.mly but while that code shows up in parser.ml none of the functions I declared appear in parser.mli.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 1
    Usually you would put the AST type and the utility functions in their own module, e.g. `Syntax`. – pdexter Jul 21 '16 at 21:00
  • I'm getting a circular-dependency problem if I do that. the Parser module depends on the ast types defined on the Syntax module and the Syntax module depends on the token type defined inside Parser – hugomg Jul 21 '16 at 21:59
  • The token type should probably be defined in the lexer. – pdexter Jul 21 '16 at 22:00
  • I would love to be able to define the tokens in the lexer but I don't know to tell menhir to use a custom token type (instead of one that was generated by %token declarations) – hugomg Jul 21 '16 at 22:05
  • 1
    Oh wait, sorry I got that backwards (wasn't in front of a computer). Why does your Syntax module depend on the token type? If you remove that dependency then you should be fine. For example in one project I have a `string_of_token` function in the lexer. – pdexter Jul 21 '16 at 22:08
  • Putting string_of_token there would solve the circular problem but its kind of weird that I can put additional functions ins the Lexer module but not in the Parser module, isn't it? – hugomg Jul 21 '16 at 22:22
  • Yeah. I'm no master of menhir though, so hopefully somebody else can offer a better solution. – pdexter Jul 21 '16 at 22:23
  • Related question: https://stackoverflow.com/q/40514349 – Arto Bendiken Apr 12 '18 at 17:30

0 Answers0