Subject: bug#76731: closed (Re: bug#76731: C-style comment regexp example in (info "(elisp)Rx Notation") is not correct)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 17 May 2025 10:23:02 +0000

Your bug report

#76731: C-style comment regexp example in (info "(elisp)Rx Notation") is not correct

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
76731: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=76731
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#76731: C-style comment regexp example in (info "(elisp)Rx Notation") is not correct
From: Mattias Engdegård <[email-masked]>
To: Yue Yi <[email-masked]>
Cc: Stefan Kangas <[email-masked]>, Stefan Monnier <[email-masked]>, [email-masked]
Date: Sat, 17 May 2025 12:21:52 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

16 maj 2025 kl. 17.12 skrev Yue Yi <[email-masked]>:

> I'm not an expert in regular expressions, but it seems that cases like C
> block comments are hard to handle without introducing
> backtracking.

I see no fundamental reason why they should be, as the C comment syntax can be parsed efficiently by a tiny state machine. The first "/*" encountered is always the beginning of the comment on matter what is found later, and the first "*/" after that is always the end. There is never any reason to go back and try a different parse.

Non-DFA regexp engines such as the one in Emacs need some hacks and/or carefully formulated regexps to avoid consuming stack space but that's a different matter. I still think we should be able to do better with either your or my regexps.

I kept your proposed fix instead of switching to a different example. The quoted-string case is simpler but the amount of backslashes detracted from the point of the exercise.

Fix pushed to master. Thank you again!

Subject: C-style comment regexp example in (info "(elisp)Rx Notation") is not correct
From: "Yue Yi" <[email-masked]>
To: "bug-gnu-emacs" <[email-masked]>
Date: Tue, 4 Mar 2025 11:58:36 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello Emacs, In Elisp Manual's Rx Notation section, we have -------------------------------------------------------------------    Here is an ‘rx’ regexp(1) that matches a block comment in the C programming language:      (rx "/*"                    ; Initial /*          (zero-or-more           (or (not "*")          ;  Either non-*,               (seq "*"           ;  or * followed by                    (not "/"))))  ;     non-/          (one-or-more "*")       ; At least one star,          "/")                    ; and the final / or, using shorter synonyms and written more compactly,      (rx "/*"          (* (| (not "*")                (: "*" (not "/"))))          (+ "*") "/") In conventional string syntax, it would be written      "/\\*\\(?:[^*]\\|\\*[^/]\\)*\\*+/" -------------------------------------------------------------------- Sadly, this regexp is not correct, as demonstated by this simple example: (Try M-x isearch-forward-regexp with /\*\(?:[^*]\|\*[^/]\)*\*+/) /***/ 123 /* anything else */ As you can see, the entire line above is highlighted by the search, meaning that the whole line has been matched. In fact, this issue occurs when the number of asterisks in /*(nstar)*/ is odd. The correct regular expression is: /\*\(?:[^*]\|\*+[^*/]\)*\*+/ The corresponding RX expression in the original document could be: (rx "/*"     (zero-or-more      (or (not "*") 	 (seq (one-or-more "*") 	      (not (or "*" "/")))))     (one-or-more "*")     "/") Or: (rx "/*"     (* (| (not "*") 	  (: (1+ "*") (not (or "*" "/")))))     (1+ "*") "/") BTW, using non-greedy `*?', the simplest way might be: (rx "/*"     (*? anything)     "*/") "/\\*[^z-a]*?\\*/" or "/\\*\\(?:.\\|\n\\)*?\\*/" Regards.