From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: regexp to match a sexp? Date: Sat, 29 Jul 2006 04:24:47 +0200 Organization: Informatimago Message-ID: <87ac6t172o.fsf@thalassa.informatimago.com> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1154140833 11660 80.91.229.2 (29 Jul 2006 02:40:33 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 29 Jul 2006 02:40:33 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jul 29 04:40:29 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1G6ekc-0005MA-HV for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Jul 2006 04:40:26 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G6ekb-0003N6-C9 for geh-help-gnu-emacs@m.gmane.org; Fri, 28 Jul 2006 22:40:25 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsserver.news.garr.it!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 82 Original-X-Trace: individual.net NdGfcS9Iba9OeaHvz5UiuwWJ/n37g8EeCfD8RgEy7FWMm0+5aG Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) Cancel-Lock: sha1:/SjeKEY8NB3on6yvA4UTKUeX/Gk= Original-Xref: shelby.stanford.edu gnu.emacs.help:140734 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:36359 Archived-At: "Drew Adams" 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 ::= "\([^"\\]*\|\\.\)*"\|[^"]* > 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