emacs-devel
[Top][All Lists]
Advanced

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

Improving regexp-opt


From: Miguel V. S. Frasson
Subject: Improving regexp-opt
Date: Thu, 7 Feb 2019 14:41:04 -0200

Hi

I had an idea to improve regexp-opt. (I use Emacs 25.3.2).

In a regexp when you have a group with alternatives, sometimes all alternatives *finish* with one or more common atom regexps. You could take the common part out of the group and try to improve the remaining smaller group, splitting all strings that match and recurse regexp-opt.

Example 1: we read from regexp-opt.el:
;; One possible improvement would be to compile '("aa" "ab" "ba" "bb")
;; into "[ab][ab]" rather than "a[ab]\\|b[ab]".  I'm not sure it's worth
;; it but if someone knows how to do it without going through too many
;; contortions, I'm all ears.

Evaluating,
(regexp-opt '("aa" "ab"  "ba" "bb"))
 -> "\\(?:a[ab]\\|b[ab]\\)"

All alternatives finish with "[ab]", so it is equivalent to
  "\\(?:a\\|b\\)[ab]"

The remaining group is "\\(?:a\\|b\\)". We can further improve making a list of all strings that match it and recourse regexp-opt:
"\\(?:a\\|b\\)"
all-matchs = ("a" "b")
(regexp-opt '("a" "b")) -> "[ab]"

Finally join the two regexps: "[ab][ab]"
... and the wish of the developer is fulfilled :) 

Example 2:
(regexp-opt '("car" "cdr" "caar" "cadr" "cdar" "cddr"))
-> "\\(?:c\\(?:\\(?:a[ad]\\|d[ad]\\|[ad]\\)r\\)\\)"

First of all, there is an (apparently) unnecessary group around the result. 

Look the inner group of the result:
  "\\(?:a[ad]\\|d[ad]\\|[ad]\\)"
Notice that all alternatives finish with "[ad]", so this group is equivalent to
 "\\(?:a\\|d\\|\\)[ad]" 

The smaller group matches the strings ("a" "d" "") and
  (regexp-opt '("a" "d" "")) ->"[ad]?"
and the inner group is equivalent to 
 "[ad]?[ad]"

So,
  (regexp-opt '("car" "cdr" "caar" "cadr" "cdar" "cddr"))
could return (eliminating outer group) 
  "c[ad]?[ad]r"

Of course the splitting and recurse not always improve smaller group. For example, 
(regexp-opt '("master" "monster" "mister"))
-> "\\(?:m\\(?:\\(?:on\\|[ai]\\)ster\\)\\)"

It would be better (imo) eliminate unnecessary groups (those without "\\|") that the result was
  "m\\(?:on\\|[ai]\\)ster"

Cheers

Miguel Frasson


reply via email to

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