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
[Caml-list] start conditions in ocamllex
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Kenneth Knowles <kknowles@u...>
Subject: Re: [Caml-list] start conditions in ocamllex
> > Has anyone thought of implementing start conditions for ocamllex?

> Can't you get the same effect with multiple entrypoints? Though this syntax
> is probably cleaner.

I disagree; I think different entry points are cleaner.  In some recent release
they added the ability to pass your own parameter to the rules, and so I
maintain state by passing a reference to the state.  I don't think the below
code is thread safe, but it could easily be made so by keeping the state ref
external to the entire lexer:

type state_t = StartCondition1 | StartCondition2

rule entrypoint1 state_ref = parse
	'a' { state_ref := StartCondition2; A_TOKEN }

and entrypoint2 state_ref = parse
	'a' { state_ref := StartCondition1; B_TOKEN }

let next_token start_start =
	let state_ref = ref start_state in
	(fun lexbuf -> match !state_ref with
		| StartCondition1 -> entrypoint1 state_ref lexbuf
		| StartCondition2 -> entrypoint2 state_ref lexbuf

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: