caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] start conditions in ocamllex
@ 2004-02-25 18:07 Sean Proctor
  2004-02-25 19:00 ` Shawn Wagner
  0 siblings, 1 reply; 5+ messages in thread
From: Sean Proctor @ 2004-02-25 18:07 UTC (permalink / raw)
  To: caml-list

Hi,

Has anyone thought of implementing start conditions for ocamllex? I have some
problems that require keeping some state. They would be very simply solved
with start conditions. I don't know much about the internals of ocamllex, but
if the implementor of it thinks this is a good idea, I would be interested in
writing this.

I was thinking a good syntax would be something like:
regexp where condition

ex:
condition startcondition
rule entrypoint =
  parse 'a' { start startcondition; A_TOKEN }
    | 'a' where startcondition { finish startcondition; B_TOKEN }

so a string of a's would give alternating A_TOKEN and B_TOKEN.

Sean

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] start conditions in ocamllex
  2004-02-25 18:07 [Caml-list] start conditions in ocamllex Sean Proctor
@ 2004-02-25 19:00 ` Shawn Wagner
  2004-02-25 19:23   ` Kenneth Knowles
  0 siblings, 1 reply; 5+ messages in thread
From: Shawn Wagner @ 2004-02-25 19:00 UTC (permalink / raw)
  To: caml-list

On Wed, Feb 25, 2004 at 01:07:44PM -0500, Sean Proctor wrote:
> Hi,
> 
> Has anyone thought of implementing start conditions for ocamllex? I have some
> problems that require keeping some state. They would be very simply solved
> with start conditions. I don't know much about the internals of ocamllex, but
> if the implementor of it thinks this is a good idea, I would be interested in
> writing this.
> 
> I was thinking a good syntax would be something like:
> regexp where condition
> 
> ex:
> condition startcondition
> rule entrypoint =
>   parse 'a' { start startcondition; A_TOKEN }
>     | 'a' where startcondition { finish startcondition; B_TOKEN }
> 
> so a string of a's would give alternating A_TOKEN and B_TOKEN.

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

-- 
Shawn Wagner
shawnw@speakeasy.org

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] start conditions in ocamllex
  2004-02-25 19:00 ` Shawn Wagner
@ 2004-02-25 19:23   ` Kenneth Knowles
  2004-02-25 19:31     ` Shawn Wagner
  2004-02-26  2:54     ` [Caml-list] Plea for arguments to Ocamlyacc generated parsers skaller
  0 siblings, 2 replies; 5+ messages in thread
From: Kenneth Knowles @ 2004-02-25 19:23 UTC (permalink / raw)
  To: caml-list

> > 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 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] start conditions in ocamllex
  2004-02-25 19:23   ` Kenneth Knowles
@ 2004-02-25 19:31     ` Shawn Wagner
  2004-02-26  2:54     ` [Caml-list] Plea for arguments to Ocamlyacc generated parsers skaller
  1 sibling, 0 replies; 5+ messages in thread
From: Shawn Wagner @ 2004-02-25 19:31 UTC (permalink / raw)
  To: caml-list

On Wed, Feb 25, 2004 at 11:23:09AM -0800, Kenneth Knowles wrote:
> > > 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 wasn't aware you could do that. Makes it a lot nicer than what I was
thinking of, yup.

-- 
Shawn Wagner
shawnw@speakeasy.org

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Plea for arguments to Ocamlyacc generated parsers
  2004-02-25 19:23   ` Kenneth Knowles
  2004-02-25 19:31     ` Shawn Wagner
@ 2004-02-26  2:54     ` skaller
  1 sibling, 0 replies; 5+ messages in thread
From: skaller @ 2004-02-26  2:54 UTC (permalink / raw)
  To: caml-list

On Thu, 2004-02-26 at 06:23, Kenneth Knowles wrote:
> > > Has anyone thought of implementing start conditions for ocamllex?

I find it is useful to have a Ocaml class specifying lexer state
and passing objects thereof to the lexer. It has always
been possible to do this, but now the lexer makes the notation
easier (saves writing fun state -> ... for every match)

I sorely need the same ability to pass a state argument 
to the parser!!! As far as I know it can't be done at all
(without hacking the generated code).

Please could we have that? I'd sure like to be able
to parse C without using a global variable -- I have a strong
belief in writing re-entrant code which I'd hope all functional
programmers would share :D


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-02-26  2:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-25 18:07 [Caml-list] start conditions in ocamllex Sean Proctor
2004-02-25 19:00 ` Shawn Wagner
2004-02-25 19:23   ` Kenneth Knowles
2004-02-25 19:31     ` Shawn Wagner
2004-02-26  2:54     ` [Caml-list] Plea for arguments to Ocamlyacc generated parsers skaller

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).