From: "David Allsopp" <dra-news@metastack.com>
To: <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Matching start of input in lexer created with ocamllex
Date: Fri, 6 Apr 2007 08:40:51 +0100 [thread overview]
Message-ID: <011c01c7781e$e74a80c0$6a7ba8c0@treble> (raw)
In-Reply-To: <20070405205804.90509BC76@yquem.inria.fr>
> Hi,
>
> I'd like to match the beginning of input (or beginning of line) in my
> lexer. Is there an easy way to do that?
>
> I have a lexer that looks something like this (simplified):
>
> rule initial = parse
> | '!' [' ' '\t']* "for" { FOR (current_loc ()) }
> | ident as id { IDENT (id, current_loc ()) }
> | '!' { BANG (current_loc ()) }
>
> The !for token should only be matched at the beginning of a
> line/input. However, in the above lexer, there's nothing that
> prevents !for from being matched in the middle of an input string.
> This causes a problem: An input string containing !forbidXyz will be
> lexed FOR, IDENT "bidXyz". I'd like to lex it as BANG, IDENT
> "forbidXyz".
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).
HTH,
David
next parent reply other threads:[~2007-04-06 7:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20070405205804.90509BC76@yquem.inria.fr>
2007-04-06 7:40 ` David Allsopp [this message]
2007-04-06 20:14 ` Janne Hellsten
2007-04-05 14:37 Janne Hellsten
2007-04-05 19:55 ` [Caml-list] " skaller
2007-04-05 20:58 ` Janne Hellsten
2007-04-06 5:52 ` skaller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='011c01c7781e$e74a80c0$6a7ba8c0@treble' \
--to=dra-news@metastack.com \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox