From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id DF8B67F30F for ; Tue, 21 Jan 2020 14:04:15 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=jocelyn.serot@uca.fr; spf=Pass smtp.mailfrom=jocelyn.serot@uca.fr; spf=None smtp.helo=postmaster@mta02.udamail.fr Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of jocelyn.serot@uca.fr) identity=pra; client-ip=193.49.117.21; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jocelyn.serot@uca.fr"; x-sender="jocelyn.serot@uca.fr"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of jocelyn.serot@uca.fr designates 193.49.117.21 as permitted sender) identity=mailfrom; client-ip=193.49.117.21; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jocelyn.serot@uca.fr"; x-sender="jocelyn.serot@uca.fr"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mta02.udamail.fr) identity=helo; client-ip=193.49.117.21; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jocelyn.serot@uca.fr"; x-sender="postmaster@mta02.udamail.fr"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AsA7FHxexT12rX+qpC0ZgMhULlGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxcW7Yx7h7PlgxGXEQZ/co6odzbaP7+a5CSdcv97B6ClELMUXEUddyI?= =?us-ascii?q?0/pE8JOIa9E0r1LfrnPWQRPf9pcxtbxUy9KlVfA83kZlff8TWY5D8WHQjjZ0Iu?= =?us-ascii?q?frymUoHdgN6q2O+s5pbdfxtHhCanYbN1MR66sRjdutMZjId/Jas90AbFr3hHd+?= =?us-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3CGhHp1XfG9KEF6MEW27P7mDDtAnYQe2aupokzgF?= =?us-ascii?q?U7fpb446yVn6vwb/z/9jL/HI0iwer5PqktZvsb79jxY3oB5zBMmZwme6dOBumX?= =?us-ascii?q?9AEz0x1617sEo70VqZ1rRQnvdRU9JJsaAaGjwmPILRmrQpQ+v5XRjMK5LUEQ/7?= =?us-ascii?q?EOXjOik4S5cK+/FLY0t5HIzy3BXf1nLwRaIZnrmCBZhy/LiOhyGgdfY48G7P0e?= =?us-ascii?q?wat3djR8JOMWO8gasvpRXYDsjHiRfAzvr4ReEnxCfIsVy74y+WpkgIAhN7WOPL?= =?us-ascii?q?RyJHaw=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CjDgBn9iZehxV1McFlHgELHIUQA1MxK?= =?us-ascii?q?oQSiQOGFIIRg26WcgNUCQEDAQwjDAEBhEACghIcBwEENBMCEAEBBAEBAQIBAgM?= =?us-ascii?q?EARMBAQEKCwkIKYU+DII7KYJ7AQICASMEGQEBOAQLC0ICAlcGE4MmAYJbJAEKr?= =?us-ascii?q?TN/M4J/AQEFgkSDa3sDBoE4jBSCGoE4IIJMPoJkAgKCDoJjMoIKIqAkjzIHgUF?= =?us-ascii?q?7hz2PD4JHjCcni2KEX5JgjniDVYExOEwNgSF0TCoBgkE+EhgNiAEMDgmDUIE+i?= =?us-ascii?q?RZBATECjg8BAQ?= X-IPAS-Result: =?us-ascii?q?A0CjDgBn9iZehxV1McFlHgELHIUQA1MxKoQSiQOGFIIRg26?= =?us-ascii?q?WcgNUCQEDAQwjDAEBhEACghIcBwEENBMCEAEBBAEBAQIBAgMEARMBAQEKCwkIK?= =?us-ascii?q?YU+DII7KYJ7AQICASMEGQEBOAQLC0ICAlcGE4MmAYJbJAEKrTN/M4J/AQEFgkS?= =?us-ascii?q?Da3sDBoE4jBSCGoE4IIJMPoJkAgKCDoJjMoIKIqAkjzIHgUF7hz2PD4JHjCcni?= =?us-ascii?q?2KEX5JgjniDVYExOEwNgSF0TCoBgkE+EhgNiAEMDgmDUIE+iRZBATECjg8BAQ?= X-IronPort-AV: E=Sophos;i="5.70,346,1574118000"; d="scan'208,217";a="432380341" X-MGA-submission: =?us-ascii?q?MDENBKuFQ8ZcURyfEXhRQgjB/u4CcfKlCnoTJd?= =?us-ascii?q?DyLKw1HPSOmXsqz6DilVXMwGRya7qiQNnMqkJnk0tj3gUjRPasGluwKF?= =?us-ascii?q?zuPLzd/HNOdRfQvOFoLPAN23WWtZWTRHxQlZjK7h9hPL+FeFchlzKMfR?= =?us-ascii?q?IcCpX7f0q6XXAdodWgYuc20g=3D=3D?= Received: from mta02.udamail.fr ([193.49.117.21]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Jan 2020 14:03:34 +0100 Received: from mta02.udamail.fr (localhost.localdomain [127.0.0.1]) by mta02.udamail.fr (Postfix) with ESMTPS id 4827yF6z1PzBsDv for ; Tue, 21 Jan 2020 14:03:33 +0100 (CET) Received: from localhost (localhost.localdomain [127.0.0.1]) by mta02.udamail.fr (Postfix) with ESMTP id 4827yF6JkJzBsDr for ; Tue, 21 Jan 2020 14:03:33 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 mta02.udamail.fr 4827yF6JkJzBsDr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uca.fr; s=72FD7F0C-5358-11E8-A3B7-17E0B3BFED1B; t=1579611813; bh=kMiAGtTsV/kFLTDwlILWecO9QoEExcNb7cGybnhgTHg=; h=From:Message-Id:Mime-Version:Date:To; b=EAZroQ0EFhnIz2lXt4dJXX272TdxSml+9Q1DiKsxYBObxbYri0AAdkrKVfqB8M5uA OHVhnm4iaU9i+DouNMM4y3xqPZ0Lw5k3cYU9ki2puQ0TNEsc/7295QJ6DqR2+eLQF1 fSZ8W6xp4acgaDD5e9V0hDIQ5eVjycjkEto7pglc= X-Virus-Scanned: amavisd-new at mta02.udamail.fr Received: from mta02.udamail.fr ([127.0.0.1]) by localhost (mta02.udamail.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id UxKtuSBbGmEq for ; Tue, 21 Jan 2020 14:03:33 +0100 (CET) Received: from proxy01.udamail.fr (unknown [193.49.117.26]) by mta02.udamail.fr (Postfix) with ESMTPS id 4827yF5YpmzBsDw for ; Tue, 21 Jan 2020 14:03:33 +0100 (CET) Received: from localhost (localhost.localdomain [127.0.0.1]) by proxy01.udamail.fr (Postfix) with ESMTP id 4827yF50nmzBnBY for ; Tue, 21 Jan 2020 14:03:33 +0100 (CET) Received: from proxy01.udamail.fr ([127.0.0.1]) by localhost (proxy01.udamail.fr [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 55aXV6JrlZwJ for ; Tue, 21 Jan 2020 14:03:29 +0100 (CET) Received: from localhost (localhost.localdomain [127.0.0.1]) by proxy01.udamail.fr (Postfix) with ESMTP id 4827y91vkLzBnBw for ; Tue, 21 Jan 2020 14:03:29 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 proxy01.udamail.fr 4827y91vkLzBnBw DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uca.fr; s=72FD7F0C-5358-11E8-A3B7-17E0B3BFED1B; t=1579611809; bh=kMiAGtTsV/kFLTDwlILWecO9QoEExcNb7cGybnhgTHg=; h=From:Message-Id:Mime-Version:Date:To; b=WSG9RiwohhwvFbDSXanuVbUhOrsKowIysPX+3Cq0PcZAvixkF7x/SvsBpSp7Zc0/n 6rWeuCf3dGJjvaUDwP0mwkebh0DCHYyKun7Rq9BsV86JLzYuMRXD6+oOeUy8eGU7c3 aIDm6dAzDrGXiBL/OiGKqhbIjVaIcjCJxpq4Hi5E= X-Virus-Scanned: amavisd-new at proxy01.udamail.fr Received: from proxy01.udamail.fr ([127.0.0.1]) by localhost (proxy01.udamail.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 4P1BzCs-EOgH for ; Tue, 21 Jan 2020 14:03:29 +0100 (CET) Received: from [192.168.0.38] (unknown [88.124.8.59]) by proxy01.udamail.fr (Postfix) with ESMTPSA id 4827y872XZzBnBY for ; Tue, 21 Jan 2020 14:03:28 +0100 (CET) From: =?utf-8?Q?Jocelyn_S=C3=A9rot?= Content-Type: multipart/alternative; boundary="Apple-Mail=_7E2E5324-196F-4652-8855-CF98FA98C009" Message-Id: Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Date: Tue, 21 Jan 2020 14:03:28 +0100 References: <20200121064845.GM27889@rich.annexia.org> To: OCaML Mailing List In-Reply-To: <20200121064845.GM27889@rich.annexia.org> X-Mailer: Apple Mail (2.2104) Subject: Re: [Caml-list] Implementing include "file" statement in menhir --Apple-Mail=_7E2E5324-196F-4652-8855-CF98FA98C009 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, I had implemented a include mechanisms at the lexer level using ocamllex fo= r the Caph programming language. This is here (undocumented unfortunately) (related stuff starts at line 128= ): https://github.com/jserot/caph/blob/master/compiler/lexer.mll Just in case it can help. Jocelyn > Le 21 janv. 2020 =C3=A0 07:48, Richard W.M. Jones a = =C3=A9crit : >=20 > [Resend, apologies if you get this twice, but I sent it > earlier and that seems to have disappeared.] >=20 > I'm writing a parser which needs to have a C-like include directive. > There's an old thread on this describing a rather complicated way to > do this for ocamllex: > https://groups.google.com/forum/#!topic/fa.caml/_v_k4WTQV_Q >=20 > 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. > What I did is documented below, but I wonder if someone can think of a > simpler way to do this? Also two related questions: >=20 > How do you pass extra parameters to menhir's generated parser > functions? >=20 > Is there a nice way to export values into menhir's generated > parser.mli file? >=20 > ---- >=20 > The concept behind my include statement uses the following grammar: >=20 > %token INCLUDE > %token STRING > %start file > %% > file: list(stmt) ; >=20 > stmt: > | INCLUDE STRING > { > let filename =3D $2 in > let fp =3D open_in filename in > let lexbuf =3D Lexing.from_channel fp in > lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname =3D filena= me }; > (* Recursively call Parser.file: *) > file Lexer.read lexbuf; > close_in fp; > } > | ... other statements ... > ; >=20 >=20 > Unfortunately as written the above code cannot work because it > introduces a circular dependency between the Parser and the Lexer > modules (normally the Lexer module depends on the Parser, and so the > Parser cannot use any functions from the Lexer module). >=20 > To break the cycle we have to add: >=20 > %{ > let lexer_read =3D ref None > %} >=20 > and replace Lexer.read with: >=20 > let reader =3D > match !lexer_read with None -> assert false | Some r -> r in > file reader lexbuf; >=20 > Then to initialize lexer_read, we have to export it by doing this > hack: >=20 > menhir parser.mly > echo 'val lexer_read : (Lexing.lexbuf -> token) option ref' >> parser.mli >=20 > and we can set it from the main program. >=20 > Rich. --Apple-Mail=_7E2E5324-196F-4652-8855-CF98FA98C009 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hi,

I had implemented a include mechanism= s at the lexer level using ocamllex for the Caph programming language.=
This is here (undocumented unfortunately) (related st= uff starts at line 128):


Just in = case it can help.


Jocelyn

Le 21 janv= . 2020 =C3=A0 07:48, Richard W.M. Jones <rich@annexia.org> a =C3=A9crit :

[Resend, apologies if you get thi= s twice, but I sent it
earlier and that seems to have disappe= ared.]

I'm writing a parser which needs to hav= e a C-like include directive.
There's an old thread on this d= escribing a rather complicated way to
do this for ocamllex:https://groups.google.com/forum/#!topic/fa.caml/_v_k4W= TQV_Q

I thought I'd have a go at writing a= n include statement in menhir, and
I did come up with somethi= ng which works but it's quite a large hack.
What I did is doc= umented below, but I wonder if someone can think of a
simpler= way to do this?  Also two related questions:

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

Is there a nice way to expor= t values into menhir's generated
parser.mli file?

----

The concept behind my i= nclude statement uses the following grammar:

=  %token INCLUDE
 %token <string> STRING
 %start file
 %%
 f= ile: list(stmt) ;

 stmt:
=      | INCLUDE STRING
  &= nbsp;  {
       = ;let filename =3D $2 in
      =  let fp =3D open_in filename in
    = ;   let lexbuf =3D Lexing.from_channel fp in
=        lexbuf.lex_curr_p <- { lexbuf.= lex_curr_p with pos_fname =3D filename };
(* Recursively call Parser.fi= le: *)
       file Lexer.= read lexbuf;
       close= _in fp;
     }
 = ;    | ... other statements ...
 &n= bsp;   ;


Unfortu= nately as written the above code cannot work because it
intro= duces a circular dependency between the Parser and the Lexer
= modules (normally the Lexer module depends on the Parser, and so the
Parser cannot use any functions from the Lexer module).

To break the cycle we have to add:

 %{
 let lexer_read =3D ref None
 %}

and replace Lexer.read with:
 let reader =3D
 &nbs= p;  match !lexer_read with None -> assert false | Some r ->= r in
 file reader lexbuf;

Then to initialize lexer_read, we have to export it by doing this
hack:

 menhir parser.mly
 echo 'val lexer_read : (Lexing.lexbuf -> token) option ref'= >> parser.mli

and we can set it from th= e main program.

Rich.

= --Apple-Mail=_7E2E5324-196F-4652-8855-CF98FA98C009--