Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ocamllex, regular expression syntax
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] ocamllex, regular expression syntax
Stefan Heimann wrote:

> Hi,
> 
> [sorry if this posting appears twice. I first submitted it with my
> news client. It seems not to appear on the mailing list and so I
> decided to post it again]
> 
> 
> I new to ocaml and today I played a little bit around with
> ocamllex. Now I'm wondering why ocamllex has this strange regular
> expression syntax. One has to quoted every character
> Regular expressions like this
> 
> "[^"\\]*(\\.[^"\\]*)*"
> 
> are not easy to read, but with the ocamllex syntax it is even more
> difficult:
> 
> '"'[^'"''\\']*('\\'_[^'"''\\']*)*'"'
> 
> (and harder to write).
> 
> Is this just for historical reason or is there a practical reason for
> this syntax? 


The ocamllex syntax is MUCH more readable
if you figure out how to use it correctly:

let bindigit = ['0'-'1']
let octdigit = ['0'-'7']
let digit = ['0'-'9']
let hexdigit = digit | ['A'-'F'] | ['a'-'f']


let bin_lit  = '0' ('b' | 'B') (underscore? bindigit) +
let oct_lit  = '0' ('o' | 'O') (underscore? octdigit) +
let dec_lit  = ('0' ('d' | 'D'))? digit (underscore? digit) *
let hex_lit  = '0' ('x' | 'X') (underscore? hexdigit)  +

The reason for quoting characters is now obvious:
ocamllex provides regular *definitions* not just
regular expressions, and they're infinitely superior;
its much better to use identifers for expressions,
than to embed them in strings like pcre

	"<alpha>*" // pcre
	alpha * // ocamllex

You'd be mad not to write your example like this:

let quote = '"'
let slosh = "\\"
let any = _
let nsq = [^'\\''"'] (* WEAK! *)

dquote nsq * ( any nsq * ) * dquote

which I can actually read :-)

the [] syntax is weak though, Felix does much better
(and regular definitions are built into the language
like patterns are in ocaml)

-- 
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850


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