caml-list - the Caml user's mailing list
 help / Atom feed
From: François Pottier <>
To: "Richard W.M. Jones" <>,,
Subject: Re: [Caml-list] Implementing include "file" statement in menhir
Date: Tue, 21 Jan 2020 09:55:30 +0100
Message-ID: <> (raw)
In-Reply-To: <>


On 21/01/2020 07:48, Richard W.M. Jones wrote:
> I'm writing a parser which needs to have a C-like include directive.
> I thought I'd have a go at writing an include statement in menhir, and
> I did come up with something which works but it's quite a large hack.

If it is OK to recognize and obey an "include" directive in every 
context (as opposed to only where a valid "stmt" is expected) then I would
suggest implementing support for "include" at the level of the lexer, so the
parser is entirely unaware of it. (I haven't tried it, though; it might take
some thought to come up with an approach that does not involve horrible side

If on the contrary you wish to consider "include <foo.h>" as a valid 
(and be able in principle to parse what follows without actually reading the
file foo.h) then I would suggest making Include a constructor in the 
syntax tree and defer reading included files to a separate "include
resolution" pass (which is allowed to invoke the parser). This approach 
be conceptually simpler. Disadvantages: 1- it should be slower by a constant
factor; 2- the include resolution pass requires writing a lot of boilerplate
traversal code (but this could be automated using the visitors package).

> How do you pass extra parameters to menhir's generated parser functions?

If you need to parameterize the entire parser, you can use %parameter 
(see the
manual, section 4.1.2).

If you need to parameterize only certain semantic actions, then you can let
these semantic actions return a closure, as in { fun my_parameter -> ... }

> Is there a nice way to export values into menhir's generated parser.mli
> file?

No. If you would like to write some code by hand and make it visible in
an .mli file, then you must write a separate module.

Hope this helps,

François Pottier

  parent reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-21  6:48 Richard W.M. Jones
2020-01-21  7:15 ` Yann Régis-Gianas
2020-01-21  8:55 ` François Pottier [this message]
2020-01-21 13:04 ` Jocelyn Sérot

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror

AGPL code for this site: git clone public-inbox