Browse thread
Re: [Caml-list] Matching start of input in lexer created with ocamllex
-
David Allsopp
- Janne Hellsten
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2007-04-06 (20:14) |
From: | Janne Hellsten <jjhellst@g...> |
Subject: | Re: [Caml-list] Matching start of input in lexer created with ocamllex |
On 4/6/07, David Allsopp <dra-news@metastack.com> wrote: > > I'd like to match the beginning of input (or beginning of line) in my > > lexer. Is there an easy way to do that? > Ocamllex doesn't have a notion for beginning of line. Three possible > solutions: > > 1. You can simulate it with a bool ref parameter to your lexer that gets set > to true by each rule to indicate that you're no longer at the beginning of a > line - the "!for" rule than raises Failure if it matches when this ref is > true. Slightly tedious for code maintenance... > 2. You use two lexers - one with the "!for" rule and one without and call > one lexer from the other (not very nice, because ocamllex doesn't support > code reuse between lexers so you'll be duplicating a lot of code). > 3. Pre-process the input to ocamllex to include a special character that > cannot appear in your text and place that at the beginning of the line (e.g. > one of the control characters in 0..31). Thanks, my program was already running its input through a preprocessor and I thus chose to use solution #3. I had already considered solution #2 but came to the same conclusion: it leads to code duplication. Janne