help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp to match a sexp?


From: Pascal Bourguignon
Subject: Re: regexp to match a sexp?
Date: Sat, 29 Jul 2006 04:24:47 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

"Drew Adams" <drew.adams@oracle.com> writes:
> This is a job description for a regexp.
>
> I'm looking for a regexp that will match (only) a sexp. 

In general, it's not possible.  Regexps cannot match recursive grammars.


> Ideally, by using
> syntax classes, it might be able to adapt to (some) other languages, but a
> regexp that worked for only Emacs-Lisp sexps would be a great start.
>
> For Lisp, for example, here are some considerations I can think of (are
> there others?):
>
> 1. It would need to match either a list sexp or an atomic sexp. The former
> would be something like \s(.*\s), but `.' would need to be refined here
> (how?). The latter would have to account for symbols, as in \s_+, but also
> arbitrary numbers and strings. (Is anything else a readable Emacs-Lisp
> atom?)

Are you excluding sublists?  With sublists excluded, it's possible,
but note that \s(.*\s) you cannot ensure that the opening bracket is
the corresponding character to the closing bracket. You'll have to write:

(ELEMENTS)\|\[ELEMENTS\]\|{ELEMENTS}\|<ELEMENTS>\|...

ELEMENTS ::= "\([^"\\]*\|\\.\)*"\|[^"]*


> 2. Strings and the possible escaping of `"' would be one headache that would
> need to be dealt with carefully, as always.

No, it's simplistic to deal with them in regexps. See above.


> 3. It would need to be effectively recursive or some approximation thereof,
> for example, with some limit placed on nesting. That is, it would need to
> allow for nested sexps.

Yes, that's why it's not possible with regexps. Regexps are not
recursive by definition.


> 4. It would need to deal properly with quoting, `''. Dealing with backquote
> syntax, ``', would be a plus.
>
> Can something like this be done in a reasonable way? What's a good regexp
> that you could use, e.g., to search for one or more sexps?
>
> I'm not looking for a way to search for or scan a sexp *without* using a
> regexp; I know there are ways to do that. I'm wondering what can be done
> *with* a regexp. IOW, imagine that all you have is `C-M-s' (but don't worry
> about the expression being too complex to type interactively).
>
> I'm less interested in hearing "it can't be done" than in attempts to do the
> job, even if in a rough way.

Well, since it's not possible, ita can't be done, but you can still go
thru the mirror and see if it's possible, since you prefer that.  Good bye.



Now, if your purpose is to _*PARSE*_ sexps  instead of using regexps,
then you can easily write a sexp parser.  This is one of the simpliest
grammar there is.  In emacs, of course you can use the provided sexp
parser, with functions such as: forward-sexp, backward-sexp,
(thing-at-point 'sexp), read-from-string, etc...

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay









reply via email to

[Prev in Thread] Current Thread [Next in Thread]