Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "François Pottier" <francois.pottier@inria.fr>
To: "Richard W.M. Jones" <rich@annexia.org>,
	caml-list@inria.fr, Yann.Regis-Gianas@inria.fr
Subject: Re: [Caml-list] Implementing include "file" statement in menhir
Date: Tue, 21 Jan 2020 09:55:30 +0100	[thread overview]
Message-ID: <05031ab6-1846-26da-d7cb-50218d60a20a@inria.fr> (raw)
In-Reply-To: <20200121064845.GM27889@rich.annexia.org>


Hello,

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 
grammatical
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
effects.)

If on the contrary you wish to consider "include <foo.h>" as a valid 
statement
(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 
abstract
syntax tree and defer reading included files to a separate "include
resolution" pass (which is allowed to invoke the parser). This approach 
should
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
francois.pottier@inria.fr
http://gallium.inria.fr/~fpottier/

  parent reply	other threads:[~2020-01-21  8:55 UTC|newest]

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:03 ` Jocelyn Sérot

Reply instructions:

You may reply publicly 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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=05031ab6-1846-26da-d7cb-50218d60a20a@inria.fr \
    --to=francois.pottier@inria.fr \
    --cc=Yann.Regis-Gianas@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox