Dear Shen,

 

the technique I typically use is to define a preprocessor, which sits in between the lexer and the parser. It provides the same interface to the parser as a lexer and takes your lexer at the other end. Your lexer also includes preprocessor tokens. The preprocessor usually just forwards tokens from the lexer to the parser, but it intercepts preprocessor tokens and then creates e.g. a new instance of the lexer to read from an include file or plays back token from a define. When the preprocessor receives an EOF token from an include file, it closes the include lexer and switches back to the main lexer. So typically the preprocessor maintains a stack of lexers. The advantage compared to messing around inside of the lexer is that it is clearer and more easily reused.

 

Best regards,

 

Michael

 

 

From: caml-list-request@inria.fr [mailto:caml-list-request@inria.fr] On Behalf Of ???
Sent: Wednesday, November 25, 2015 3:04 PM
To: caml-list
Subject: [Caml-list] [OCAML]:: how to switch buffer in ocamllex?

 

Dear all:

I am using ocamllex to write a lexer, the language I am analyzing have "include" mechanism that refer to another file.

So how can I open the refered file and switch to it in ocamllex?

Shen

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928