caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christian Lindig <lindig@cs.uni-sb.de>
To: "Rafael 'Dido' Sevilla" <dido@imperium.ph>
Cc: Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] backslashes in ocamllex
Date: Mon, 6 Oct 2003 09:28:53 +0200	[thread overview]
Message-ID: <20031006072853.GE1459@st> (raw)
In-Reply-To: <20031006013740.GA2149@imperium.ph>

On Mon, Oct 06, 2003 at 09:37:40AM +0800, Rafael 'Dido' Sevilla wrote:
> Now I'm stuck again.  I'm revising the lexical analyzer for my compiler
> to enable it to recognize escaped strings, with conventions different
> from OCaml's.  Currently, I'm using this regex:
> 
> '\'' ("\\\\"|"\\'"|[^'\''])* '\''
> 
> in an attempt to recognize strings that begin and end with single
> quotes, but may possibly include sequences like \' that represent
> escaped quotes, and '\\' that represent escaped backslashes.  -- 

As you discovered, you cannot recognize strings with a single regular
expression. You need a sub-lexer:

{
let get         = Lexing.lexeme
let getchar     = Lexing.lexeme_char
}

rule token = parse (* main lexer *)
    eof ->
  | ...
  | "'" -> string lexbuf (Buffer.create 80) (* use sub-lexer *)


and string = parse (* lexer for strings *)
    eof     -> { fun buf -> error "EOF in string" } 
  | '\\' _  -> { fun buf -> let c = getchar lexbuf 1 in
                            let k = match c with
                            | 'n'   -> '\n'
                            | 't'   -> '\t'
                            | .... 
                            in
                                ( Buffer.add_char buf k
                                ; string lexbuf buf
                                )
               }                 
  | _       -> { fun buf -> string lexbuf (Buffer.add_string (get lexbuf)
  | "'"     -> { fun buf -> Buffer.contents buf } (* return string *)

-- Christian

--
Christian Lindig         http://www.st.cs.uni-sb.de/~lindig/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-10-06  8:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-06  1:37 Rafael 'Dido' Sevilla
2003-10-06  7:28 ` Christian Lindig [this message]
2003-10-06  8:26   ` Alain.Frisch
2003-10-06  7:53 ` Jean-Christophe Filliatre

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=20031006072853.GE1459@st \
    --to=lindig@cs.uni-sb.de \
    --cc=caml-list@inria.fr \
    --cc=dido@imperium.ph \
    /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;
as well as URLs for NNTP newsgroup(s).