Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Handling include files using ocamllex
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-02 (13:19)
From: skaller <skaller@u...>
Subject: RE: [SPAM?][Caml-list] Handling include files using ocamllex
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 <skaller at users dot sf dot net>
Felix, successor to C++: