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 c5ff346549e7 (Postfix) with ESMTPS id 089515D5 for ; Tue, 21 Jan 2020 08:55:37 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.70,345,1574118000"; d="scan'208";a="432326664" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 21 Jan 2020 09:55:36 +0100 Received: by sympa.inria.fr (Postfix, from userid 20132) id B9B8F7F345; Tue, 21 Jan 2020 09:55:36 +0100 (CET) Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id CD90A7F30F for ; Tue, 21 Jan 2020 09:55:31 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.70,345,1574118000"; d="scan'208";a="336499172" Received: from bou78-2-82-240-46-163.fbx.proxad.net (HELO MP-41019.local) ([82.240.46.163]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/AES128-SHA; 21 Jan 2020 09:55:31 +0100 To: "Richard W.M. Jones" , caml-list@inria.fr, Yann.Regis-Gianas@inria.fr References: <20200121064845.GM27889@rich.annexia.org> From: =?UTF-8?Q?Fran=c3=a7ois_Pottier?= Message-ID: <05031ab6-1846-26da-d7cb-50218d60a20a@inria.fr> Date: Tue, 21 Jan 2020 09:55:30 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: <20200121064845.GM27889@rich.annexia.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] Implementing include "file" statement in menhir Reply-To: =?UTF-8?Q?Fran=c3=a7ois_Pottier?= X-Loop: caml-list@inria.fr X-Sequence: 17961 Errors-to: caml-list-owner@inria.fr Precedence: list Precedence: bulk Sender: caml-list-request@inria.fr X-no-archive: yes List-Id: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 " 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/