caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Pous <Damien.Pous@ens-lyon.fr>
To: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] Calling parser from inside a parser rule
Date: Mon, 20 Sep 2004 10:52:39 +0200	[thread overview]
Message-ID: <20040920105239.0c00bd5d@localhost.localdomain> (raw)
In-Reply-To: <auto-000000597458@appliedminds.com>

hi,

why don't you recurse before the parser level ?

lexer.mll:
<<
{ type token = [`Print | `Arf | `Eof] }

let blank = [' ' '\r' '\t' '\n']*
let word = [ 'a'-'z' '.' ]*

rule token = parse
  | blank   { token lexbuf }
  | "#include" blank '"' (word as s) '"' { `Include s }
  | "print" { `Print }
  | "arf"   { `Arf }
      (* ... *) 
  | eof     { `Eof }
>>

reclexer.mll
<<
let token lexbuf: unit -> Lexer.token =
  let acc = ref [lexbuf] in
  let rec aux () = match !acc with
    | [] -> `Eof
    | (x::q) as l -> 
	match Lexer.token x with
	  | `Include s -> acc := (Lexing.from_channel (open_in s))::l; aux()
	  | `Eof -> acc := q; aux()
	  | #Lexer.token as t -> t
  in aux
>>

let t = token (Lexing.from_channel (open_in "toto"));;
t();;
t();;
...

of course you have to handle cyclic includes...

damien

On Sun, 19 Sep 2004 08:50:42 -0700
James Lamanna <jamesl@appliedminds.com> wrote:
> Essentially.
> I want to be able to recurse into my parser.

-------------------
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:[~2004-09-20  8:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-19 15:50 James Lamanna
2004-09-20  8:52 ` Damien Pous [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-09-17 22:22 James Lamanna

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=20040920105239.0c00bd5d@localhost.localdomain \
    --to=damien.pous@ens-lyon.fr \
    --cc=caml-list@pauillac.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;
as well as URLs for NNTP newsgroup(s).