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
Custom lexer in Camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-06-25 (12:17)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Custom lexer in Camlp4
Excerpts from Jeremy Yallop's message of Wed Jun 25 13:53:02 +0200 2008:
> Nicolas Pouillard wrote:
> > Excerpts from Jeremy Yallop's message of Tue Jun 24 16:30:23 +0200 2008:
> >> Nicolas Pouillard wrote:
> >>> Excerpts from Jeremy Yallop's message of Tue Jun 24 15:03:19 +0200 2008:
> >>>> Is there a way to replace the default lexer in Camlp4?  I'd like to use 
> >>>> (an extended version of) one of the OCaml grammars, but with a different 
> >>>> lexer.
> >>> You can rebuild (I mean functor application), your own camlp4 by giving
> >>> another Lexer module. Basically you will have to copy/paste the short
> >>> Camlp4.PreCast module (or a part of), by substituting Camlp4.Struct.Lexer
> >>> by your Lexer implementation. That's fairly heavy but works.
> >> Ok, I've built a slightly modified clone of PreCast.  What's the best 
> >> way to persuade Camlp4 to use it?  The original PreCast seems fairly 
> >> hardwired, e.g. in the Register module.
> > 
> > Hum, you can't you have to build your own camlp4 driver :(
> Thanks, Nicolas.  That's useful to know.
> The solution I've adopted for now is to change the default quotation 
> expander to a function that just slots in my lexer before calling the 
> original expander.  This means that I can use my custom lexical syntax 
> inside the << >> delimiters and I can use those delimiters either around 
> the whole program or around a particular term.  I think this'll be fine 
> for my needs, although it would be nice to be able to customize the 
> lexer that's used for regular code as well.

You've  followed  a  reasonable  approach,  where one use quotations to embed a
"foreign"  language  in  the  sense that it even doesn't share the same lexical

However your right that changing the lexer should be doable.

Nicolas Pouillard aka Ertai