Skip to content

regex library to implement "re" module #13

Closed

Description

@pfalcon

CPython uses (possibly patched) PCRE library http://www.pcre.org/ (BSD license). Nothing would preclude its usage in MicroPython, but:

  1. Worst-case performance is awful.
  2. Nobody would ever say PCRE is optimized for code size/memory usage.

So, PCRE would definitely fit "unix" port, to be completely compatible with CPython "re" module. But I'm not sure how usable it will be for MCU ports.

So, what I imagine is be able to support 2 (or more?) regex libraries - one PCRE for complete compatibility, another using other library(ies) for unbloatness, at the expense of reduced feature set. Note that "reduced" subset can live in a separate module (say, "remini").

This ticket would be a good place to discuss that and collect candidates for alternative re lib.

Activity

  1. pfalcon commented on Dec 30, 2013

    @pfalcon
    ContributorAuthor

    See dpgeorge#9 (comment) for some previous discussion.

  2. dpgeorge commented on Dec 30, 2013

    @dpgeorge
    Member

    I like regex, and it's useful, so I think it's definitely worth trying to get it on the MCU. If we go for a cutdown version, then it should be a strict subset of Python's regex, not a variation.

  3. hagenkaye commented on Dec 31, 2013

    @hagenkaye
    Contributor

    as an FYI, the musl c library contains the tre regex code.

    https://github.com/laurikari/tre

    As well, it contains some bug fixes that were never fixed in the original code. (according to the wiki at musl)

  4. Neon22 commented on Dec 31, 2013

    @Neon22
    Contributor

    Note that tre does not appear to have the following (which Python re does):

    Its not a terrible set of limitiations - providing the other aspects match the same of course.

  5. chipaca commented on Jan 2, 2014

    @chipaca
    Contributor

    Note Python does not use PCRE. The default "re" has not been PCRE-based since at least 2.2, and the PCRE implementation of "re" was only included for (AFAIK undocumented) backwards compatibility until 2.3.

  6. pfalcon commented on Apr 8, 2014

    @pfalcon
    ContributorAuthor

    FYI, I started writing FFI bindings for libpcre in https://github.com/micropython/micropython-lib/tree/master/re-pcre , with the idea to have more or less complete re module implementation for unix port.

  7. chipaca commented on Apr 8, 2014

    @chipaca
    Contributor

    Not sure how useful it'll be, but you can look at
    http://svn.python.org/projects/python/branches/release22-maint/Lib/pre.pyand
    the pcre* files in
    http://svn.python.org/projects/python/branches/release22-maint/Modules/ for
    inspiration.

    On Tue, Apr 8, 2014 at 11:20 PM, Paul Sokolovsky
    [email protected]:

    FYI, I started writing FFI bindings for libpcre in
    https://github.com/micropython/micropython-lib/tree/master/re-pcre , with
    the idea to have more or less complete re module implementation for unix
    port.

    Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-39908960
    .

    John Lenton ([email protected]) ::: http://chipaca.com

  8. pfalcon commented on Apr 12, 2014

    @pfalcon
    ContributorAuthor

    re-pcre now has enough gear to run json module from CPy.

  9. dpgeorge commented on Apr 12, 2014

    @dpgeorge
    Member

    You mean, json module copied from CPy can run under uPy, by utilising re-pcre, right?

  10. pfalcon commented on Apr 12, 2014

    @pfalcon
    ContributorAuthor

    Yep, the only change required was this: micropython/micropython-lib@a4833ed (CPy stdlib is full of such dirtiness). Ah, disclaimer: can run on trivial input ;-)

  11. pfalcon commented on Jun 24, 2014

    @pfalcon
    ContributorAuthor

    Ok, I had another pass on this. Let's remind ourselves that regex matching can be implemented either as "recursive-descent" executor with backtracking (essentially, a Turing machine), or NFA/DFA (non-deterministic and deterministic finite automaton). The latter offers some performance guarantees, but harder to understand (and thus modify to add more features), and usually needs some bunch of memory to represent compiled regex, the former is easier to understand/modify and add non-regular features (with modern regex'es full of them).

    Technically good contender for "backtracking" category: SLRE . Compiles to under 5K (x86, -Os). Problem spot: has "active" "license life" with several incarnations:

    @dpgeorge, do you envision any probs adding MIT version to codebase? (This is certainly better than situation we had with "stm" port, which contained potentially proprietary code.)

  12. pfalcon commented on Jun 24, 2014

    @pfalcon
    ContributorAuthor

    Example of FA-based contender: http://swtch.com/plan9port/unix/ . Messy code from outer space. Support captures, which was big progress for FA impl as http://swtch.com/~rsc/regexp/regexp1.html says. Doesn't support non-greedy quantifers. Compiles to ~7K.

    There're bunch of hacks which seem to add non-greedy's and stay unbloated (unlike TLE):

  13. pfalcon commented on Jun 24, 2014

    @pfalcon
    ContributorAuthor

    Generally, grepping for "regex" for github can lead to few interesting, if practical, finds, e.g. https://github.com/fy0/tinyre "A tiny python style regex engine implement.", with nice Chinese readme and comments.

  14. dpgeorge commented on Jul 4, 2014

    @dpgeorge
    Member

    do you envision any probs adding MIT version to codebase?

    No. There is no problem if it's MIT and we acknowledge the source of the code. Now with the extmod/ directory, we can land it there.

    (Of course, I would love to write my own re engine, but that's not a good use of time :)

    fy0/tinyre looks quite nice, and the license might even be MIT compatible.

  15. pfalcon commented on Jul 28, 2014

    @pfalcon
    ContributorAuthor

    FYI, I kinda coded a module for slre, but then next step is refactoring slre to have sane interface (re-refactoring, because it's used to have it such, but latest explicitly MIT-licensed version has screwed it up), optimize memory usage (it just allocates big statically-sized buffer for compiled regexp), etc, etc.

    All that seemed kinda boring, so I made another pass over solutions mentioned above. I looked at tinyre, and it actually compiles to small enough code too, but all the Chinese comments doesn't help, and I saw funny programming practices like infinite loop in case of error. So, previous assessment holds: nobody known what exactly is done there and why.

    Then I took a more detailed look at https://code.google.com/p/re1/ (I almost skipped it last time). And that's true gem - first of all, it's truly minimal code (but implements all important features like greedy vs non-greedy quantifiers and submatch captures), and then, it separates regexp compiler from execution backends, and then provides few alternative backends (essentially, tiny VMs). Backends size from 400 bytes on. Frontend is implemented using yacc though, so is big, but I already reimplemented parser in adhoc way.

    There's issue that backtracking (== requiring least explicit runtime state for execution, but potentially lot of stack space) backends don't have protection against known backtracking killers like (a*)*. I even tried to implement that protection, until I checked how slre deals with it, and it infinite-loops too. So, I guess with our aims, it should be good enough to have just switchable backends to deal with it.

  16. 3 remaining items

  17. pfalcon commented on Aug 16, 2014

    @pfalcon
    ContributorAuthor

    Depending on what you mean. To start talking about "CPython compliance", would need to implement named groups. And full "CPython compliance" is not achievable for finite-automaton based implementation (and re1's feature is completely replaceable backends, so FA featureset is a common bound).

  18. pfalcon commented on Oct 19, 2014

    @pfalcon
    ContributorAuthor

    Ok, "ure" module based on re1.5 as described above was merged (and actually went thru couple upgrade iterations adding more features).

  19. dpgeorge commented on Oct 19, 2014

    @dpgeorge
    Member

    Thanks @pfalcon, really great work!

  20. added a commit that references this issue on Jan 22, 2017
  21. added a commit that references this issue on Sep 1, 2018
  22. added a commit that references this issue on Oct 7, 2018
  23. added a commit that references this issue on Jul 28, 2020
  24. added a commit that references this issue on Aug 20, 2020
  25. added a commit that references this issue on Feb 14, 2021
  26. added a commit that references this issue on Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    rfcRequest for Comment

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions