all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Finding C style comments using isearch-forward-regexp
@ 2003-02-19 13:40 Timur Aydin
  2003-02-19 14:15 ` Kai Großjohann
  2003-02-19 19:01 ` Kai Großjohann
  0 siblings, 2 replies; 13+ messages in thread
From: Timur Aydin @ 2003-02-19 13:40 UTC (permalink / raw)


Hi,

I am struggling to find C style comments using the
isearch-forward-regexp function (C-M-s). Here is the regexp that I am
using:

/\*\(.\|^J\)*\*/

Basically, I am telling emacs to first find the opening comment "/*",
then any number of any character or newline and then the closing
comment "*/".

However, immediately after typing the * after the closing parenthesis,
emacs tells me:

[(error stack overflow in regexp matcher)]

I have found a simpler regexp that causes this error. This regexp is
part of the one specified above and finds any character or newline any
number of times:

\(.\|^J\)*

Even though this is not a trivial regexp, it is fairly basic and I
cannot come up with an alternate syntax to achieve the same
effect. Please help...

-- 
Timur Aydin

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 13:40 Finding C style comments using isearch-forward-regexp Timur Aydin
@ 2003-02-19 14:15 ` Kai Großjohann
  2003-02-19 17:03   ` Timur Aydin
  2003-02-19 19:01 ` Kai Großjohann
  1 sibling, 1 reply; 13+ messages in thread
From: Kai Großjohann @ 2003-02-19 14:15 UTC (permalink / raw)


Timur Aydin <timuraydin@superonline.com> writes:

> I am struggling to find C style comments using the
> isearch-forward-regexp function (C-M-s). Here is the regexp that I am
> using:
>
> /\*\(.\|^J\)*\*/

Why with a regexp?  Why not use a command like forward-comment?

Or you could use font-lock and just search for the next character
which has comment face.
-- 
A turnip curses Elvis

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 14:15 ` Kai Großjohann
@ 2003-02-19 17:03   ` Timur Aydin
  2003-02-19 19:46     ` Stefan Monnier <foo@acm.com>
  0 siblings, 1 reply; 13+ messages in thread
From: Timur Aydin @ 2003-02-19 17:03 UTC (permalink / raw)


kai.grossjohann@uni-duisburg.de (Kai Großjohann) writes:

> Timur Aydin <timuraydin@superonline.com> writes:
>
>> I am struggling to find C style comments using the
>> isearch-forward-regexp function (C-M-s). Here is the regexp that I am
>> using:
>>
>> /\*\(.\|^J\)*\*/
>
> Why with a regexp?  Why not use a command like forward-comment?
>
> Or you could use font-lock and just search for the next character
> which has comment face.

Hmm, that seems like a workaround. But the mentioned regexp not
working indicates a bug in the emacs regexp handler, doesn't it?

-- 
Timur Aydin

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 13:40 Finding C style comments using isearch-forward-regexp Timur Aydin
  2003-02-19 14:15 ` Kai Großjohann
@ 2003-02-19 19:01 ` Kai Großjohann
  2003-02-19 19:49   ` Stefan Monnier <foo@acm.com>
  1 sibling, 1 reply; 13+ messages in thread
From: Kai Großjohann @ 2003-02-19 19:01 UTC (permalink / raw)


Timur Aydin <timuraydin@superonline.com> writes:

> I am struggling to find C style comments using the
> isearch-forward-regexp function (C-M-s). Here is the regexp that I am
> using:
>
> /\*\(.\|^J\)*\*/
>
> Basically, I am telling emacs to first find the opening comment "/*",
> then any number of any character or newline and then the closing
> comment "*/".
>
> However, immediately after typing the * after the closing parenthesis,
> emacs tells me:
>
> [(error stack overflow in regexp matcher)]

I created a buffer foo.c and entered the following:

int main(int argc, char** argv)
{
    /* A comment for testing. */
    printf("hello, world\n");
}

Then I went to the beginning of the buffer, entered C-M-s, the regexp
you cited, and it found the comment just nicely.

(Note that your regexp will find too much: if there are two comments,
then it will find everything from the start of the first to the end
of the second comment.)
-- 
A turnip curses Elvis

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 17:03   ` Timur Aydin
@ 2003-02-19 19:46     ` Stefan Monnier <foo@acm.com>
  2003-02-20  7:19       ` Kai Großjohann
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-19 19:46 UTC (permalink / raw)


>>>>> "Timur" == Timur Aydin <timuraydin@superonline.com> writes:
> Hmm, that seems like a workaround. But the mentioned regexp not
> working indicates a bug in the emacs regexp handler, doesn't it?

A shortcoming, yes, a bug, I don't think so: it's inherent to the
technique used to do the matching (i.e. backtracking).


        Stefan

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 19:01 ` Kai Großjohann
@ 2003-02-19 19:49   ` Stefan Monnier <foo@acm.com>
  0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-19 19:49 UTC (permalink / raw)


>>>>> "Kai" == Kai Großjohann <kai.grossjohann@uni-duisburg.de> writes:
> I created a buffer foo.c and entered the following:

> int main(int argc, char** argv)
> {
>     /* A comment for testing. */
>     printf("hello, world\n");
> }

> Then I went to the beginning of the buffer, entered C-M-s, the regexp
> you cited, and it found the comment just nicely.

It worked because your buffer was too small.  Make it larger
and you too will overflow your stack.


        Stefan

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-19 19:46     ` Stefan Monnier <foo@acm.com>
@ 2003-02-20  7:19       ` Kai Großjohann
  2003-02-20 20:35         ` Stefan Monnier <foo@acm.com>
  0 siblings, 1 reply; 13+ messages in thread
From: Kai Großjohann @ 2003-02-20  7:19 UTC (permalink / raw)


"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:

>>>>>> "Timur" == Timur Aydin <timuraydin@superonline.com> writes:
>> Hmm, that seems like a workaround. But the mentioned regexp not
>> working indicates a bug in the emacs regexp handler, doesn't it?
>
> A shortcoming, yes, a bug, I don't think so: it's inherent to the
> technique used to do the matching (i.e. backtracking).

Maybe one can work around it by constructing the regexp differently:
instead of allowing any character sequence between "/*" and "*/", one
restricts the possible sequences, with the following idea:

- any sequence of [^*] is allowed
- a * followed by [^/] is allowed

The above two can be repeated.

I think that avoids backtracking.
-- 
A preposition is not a good thing to end a sentence with.

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-20  7:19       ` Kai Großjohann
@ 2003-02-20 20:35         ` Stefan Monnier <foo@acm.com>
  2003-02-20 21:25           ` Kai Großjohann
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-20 20:35 UTC (permalink / raw)


> - any sequence of [^*] is allowed
> - a * followed by [^/] is allowed
> The above two can be repeated.
> I think that avoids backtracking.

It will avoid matching past the comment-end, but it doesn't prevent the
regexp routines from pushing stuff on the stack in case we need
to backtrack (the routines are not clever enough).


        Stefan

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-20 20:35         ` Stefan Monnier <foo@acm.com>
@ 2003-02-20 21:25           ` Kai Großjohann
  2003-02-20 23:52             ` Stefan Monnier <foo@acm.com>
  2003-02-21 14:00             ` Kevin Dziulko
  0 siblings, 2 replies; 13+ messages in thread
From: Kai Großjohann @ 2003-02-20 21:25 UTC (permalink / raw)


"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:

>> - any sequence of [^*] is allowed
>> - a * followed by [^/] is allowed
>> The above two can be repeated.
>> I think that avoids backtracking.
>
> It will avoid matching past the comment-end, but it doesn't prevent the
> regexp routines from pushing stuff on the stack in case we need
> to backtrack (the routines are not clever enough).

Hm.  Does that mean that even, say, .* on a file with a single very
long line will put too much stuff on the stack?

Is there a way to tell the routines to never backtrack?  I think
there is a way for Perl, and Perlish things went into the Emacs
regexp engine...
-- 
A preposition is not a good thing to end a sentence with.

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-20 21:25           ` Kai Großjohann
@ 2003-02-20 23:52             ` Stefan Monnier <foo@acm.com>
  2003-02-21 14:00             ` Kevin Dziulko
  1 sibling, 0 replies; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-20 23:52 UTC (permalink / raw)


>> It will avoid matching past the comment-end, but it doesn't prevent the
>> regexp routines from pushing stuff on the stack in case we need
>> to backtrack (the routines are not clever enough).
> Hm.  Does that mean that even, say, .* on a file with a single very
> long line will put too much stuff on the stack?

Depends.  ".*\n" or ".*$" will not because the regexp routines recognize
this case and don't push anything on the stack for it, but
".*(" will definitely push stuff and will thus lead to the same
error if the line is long enough.

> Is there a way to tell the routines to never backtrack?  I think
> there is a way for Perl, and Perlish things went into the Emacs
> regexp engine...

Perl has a way to throw away part of the stack frames (it's basically
equivalent to the `!' construct of Prolog, a.k.a. cut).
It's not implemented in Emacs.


        Stefan

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-20 21:25           ` Kai Großjohann
  2003-02-20 23:52             ` Stefan Monnier <foo@acm.com>
@ 2003-02-21 14:00             ` Kevin Dziulko
  1 sibling, 0 replies; 13+ messages in thread
From: Kevin Dziulko @ 2003-02-21 14:00 UTC (permalink / raw)


On Thu, 20 Feb 2003, Kai Großjohann wrote:

> "Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:
> 
> >> - any sequence of [^*] is allowed
> >> - a * followed by [^/] is allowed
> >> The above two can be repeated.
> >> I think that avoids backtracking.
> >
> > It will avoid matching past the comment-end, but it doesn't prevent the
> > regexp routines from pushing stuff on the stack in case we need
> > to backtrack (the routines are not clever enough).
> 
> Hm.  Does that mean that even, say, .* on a file with a single very
> long line will put too much stuff on the stack?
> 
> Is there a way to tell the routines to never backtrack?  I think
> there is a way for Perl, and Perlish things went into the Emacs
> regexp engine...
> 


The regexp /*[^\(*/\)]*/ will find C comments in a non-greedy fashion.
Does this help your stack problems?

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

* Re: Finding C style comments using isearch-forward-regexp
       [not found] <mailman.2229.1045836305.21513.help-gnu-emacs@gnu.org>
@ 2003-02-21 14:42 ` Stefan Monnier <foo@acm.com>
  2003-02-24 18:48   ` Kevin Dziulko
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-21 14:42 UTC (permalink / raw)


> The regexp /*[^\(*/\)]*/ will find C comments in a non-greedy fashion.
> Does this help your stack problems?

It will find any comment which has no *, no /, no (, no ), and no \ in
its text.  I don't think that's quite what you wanted, so I presume
you slightly misunderstand the way [...] works.  Check out the
manual again.


        Stefan

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

* Re: Finding C style comments using isearch-forward-regexp
  2003-02-21 14:42 ` Stefan Monnier <foo@acm.com>
@ 2003-02-24 18:48   ` Kevin Dziulko
  0 siblings, 0 replies; 13+ messages in thread
From: Kevin Dziulko @ 2003-02-24 18:48 UTC (permalink / raw)


On 21 Feb 2003, Stefan Monnier <foo@acm.com> wrote:

> > The regexp /*[^\(*/\)]*/ will find C comments in a non-greedy fashion.
> > Does this help your stack problems?
> 
> It will find any comment which has no *, no /, no (, no ), and no \ in
> its text.  I don't think that's quite what you wanted, so I presume
> you slightly misunderstand the way [...] works.  Check out the
> manual again.
> 

Try: /\*[^*]*\*+\([^/*][^*]*\*+\)*/

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

end of thread, other threads:[~2003-02-24 18:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-19 13:40 Finding C style comments using isearch-forward-regexp Timur Aydin
2003-02-19 14:15 ` Kai Großjohann
2003-02-19 17:03   ` Timur Aydin
2003-02-19 19:46     ` Stefan Monnier <foo@acm.com>
2003-02-20  7:19       ` Kai Großjohann
2003-02-20 20:35         ` Stefan Monnier <foo@acm.com>
2003-02-20 21:25           ` Kai Großjohann
2003-02-20 23:52             ` Stefan Monnier <foo@acm.com>
2003-02-21 14:00             ` Kevin Dziulko
2003-02-19 19:01 ` Kai Großjohann
2003-02-19 19:49   ` Stefan Monnier <foo@acm.com>
     [not found] <mailman.2229.1045836305.21513.help-gnu-emacs@gnu.org>
2003-02-21 14:42 ` Stefan Monnier <foo@acm.com>
2003-02-24 18:48   ` Kevin Dziulko

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.