Yoann Padioleau <padiolea@cripure.irisa.fr> writes:
[...]
> I wrote:
>> Macros are certainly not the only difference I see between scsh and
>> other scripting languages. The regular-expressions expressed as
>> s-expressions is one feature I do not find in any other
>> regular-expression package, for example.
>
> And what it brings ? what is the advantage to write regexp as
> sexp over usual string syntax ?
It brings the fact that you work at a different level. To use
compilation terms, when you use a standard regexp package where
everything is encoded as strings, you work at the level of the
concrete syntax. With scsh's SRE (regexps expressed as s-expressions)
you work at the level of the abstract syntax.
In scsh, a string and a regexp are different things. With string-based
packages, regexps *are* strings, which are interpreted in a special
way when you pass them to regexp matching functions. With scsh,
regular expressions are represented as trees which makes them easy to
manipulate.
This means for example that with SREs you do not need to perform any
kind of processing to quote meta-characters in strings. If you want to
match empty OCaml comments, for example, you simply say
(rx "(**)")
i.e. you do not need to quote the meta-characters "(", ")" and "*"
like in Posix regexps, where the above would be written like that:
"\(\*\*\)"
This also means that you can do all kinds of neat things with regular
expressions, without having to write a parser for them first. For
example, defining a regular expression which extract in its first
submatch everything which follows some other regexp is as easy as:
(rx ,prefix-rx (submatch (* any)))
And this works even if the prefix-rx contains submatches itself,
because scsh is smart enough to remove them when a regexp is inserted
in a bigger one (there is, of course, a way to include them if you
really need them). Try doing that with a string-based regular
expression package, and you'll see very quickly why SREs are great.
I don't know if you're familiar with Emacs or not, but if you are,
you'll see that it contains a function (regexp-opt-depth) whose only
aim is to work around the problems raised when one constructs big
regular expressions out of small ones, and the small ones happen to
contain submatches.
And if you want more examples, you can find several in the scsh
manual.
Michel.
|