From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 2D909BC69 for ; Thu, 2 Aug 2007 15:19:50 +0200 (CEST) Received: from ipmail01.adl2.internode.on.net (ipmail01.adl2.internode.on.net [203.16.214.140]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l72DJlqP025013 for ; Thu, 2 Aug 2007 15:19:49 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJh0sUZ5LGJp/2dsb2JhbAAN X-IronPort-AV: E=Sophos;i="4.19,212,1183300200"; d="scan'208";a="166158171" Received: from ppp121-44-98-105.lns10.syd6.internode.on.net (HELO [192.168.1.201]) ([121.44.98.105]) by ipmail01.adl2.internode.on.net with ESMTP; 02 Aug 2007 22:49:41 +0930 Subject: RE: [SPAM?][Caml-list] Handling include files using ocamllex From: skaller To: Christoph Bauer Cc: Erik de Castro Lopo , caml-list@yquem.inria.fr In-Reply-To: References: <20070802200933.3621fb1a.mle+ocaml@mega-nerd.com> Content-Type: text/plain Date: Thu, 02 Aug 2007 23:19:30 +1000 Message-Id: <1186060770.23889.39.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 46B1D9F3.004 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocamllex:01 0200,:01 ocamllex:01 ocamlyacc:01 ocamlyacc:01 lexbuf:01 lexing:01 buffer:01 lexbuf:01 parser:01 ocaml:01 lexer:01 lexers:01 compilation:01 booleans:01 On Thu, 2007-08-02 at 12:29 +0200, Christoph Bauer wrote: > > Hi all, > > > > I doing some simple parsing with ocamllex and ocamlyacc and I > > need to be able to handle C style include files. > > > > I know how to do this in C with flex and bison, but I can't > > figure out how to do it with ocamllex and ocamlyacc. > > > > Anyone know how to do this? > > A solution could be to create an lexbuf from a function with > Lexing.from_function. > This function has to manage a stack of open channels and positions. It > has to > scan for "#include"-statements and copies instead of these statements > the contents > of the corresponding files into the buffer. > > Just an idea, I haven't done it yet. I recommend abandoning the idea of passing a lexbuf to a parser: make a dummy lexbuf and pass that to keep Ocamlyacc happy, but make sure you never use it. Instead, create an Ocaml class with a get_token method, and use the closure of that method over the class PLUS a dummy lexbuf. The class then manages the lexer state. A stack of Ocamllex lexers and lexbufs can be used. If you want to do conditional compilation, you also need a stack of booleans -- one stack per include file (to ensure conditions don't span file boundaries). -- John Skaller Felix, successor to C++: http://felix.sf.net