Regexp parser and generator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Sakkis

    Regexp parser and generator

    Is there any package that parses regular expressions and returns an
    AST ? Something like:
    >>parse_rx(r' i (love|hate) h(is|er) (cat|dog)s?\s*! +')
    Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
    'dog'), Optional('s'), ZeroOrMore(r'\s '), OneOrMore('!'))

    Given such a structure, I want to create a generator that can generate
    all strings matched by this regexp. Obviously if the regexp contains a
    '*' or '+' the generator is infinite, and although it can be
    artificially constrained by, say, a maxdepth parameter, for now I'm
    interested in finite regexps only. It shouldn't be too hard to write
    one from scratch but just in case someone has already done it, so much
    the better.

    George
  • skip@pobox.com

    #2
    Re: Regexp parser and generator


    GeorgeIs there any package that parses regular expressions and returns
    Georgean AST ?

    Maybe not directly, but this might provide a starting point for building
    such a beast:
    >>import re
    >>re.compile( "[ab]", 128)
    in
    literal 97
    literal 98
    <_sre.SRE_Patte rn object at 0x47b7a0>
    >>re.compile("a b*c[xyz]", 128)
    literal 97
    max_repeat 0 65535
    literal 98
    literal 99
    in
    literal 120
    literal 121
    literal 122
    <_sre.SRE_Patte rn object at 0x371f90>

    Skip

    Comment

    • Peter Otten

      #3
      Re: Regexp parser and generator

      George Sakkis wrote:
      Is there any package that parses regular expressions and returns an
      AST ? Something like:
      >
      >>>parse_rx(r 'i (love|hate) h(is|er) (cat|dog)s?\s*! +')
      Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
      'dog'), Optional('s'), ZeroOrMore(r'\s '), OneOrMore('!'))
      Seen today, on planet python:
      >>import sre_parse
      >>sre_parse.par se("a|b")
      [('in', [('literal', 97), ('literal', 98)])]


      Peter

      Comment

      • Paul McGuire

        #4
        Re: Regexp parser and generator

        On Nov 4, 1:34 pm, George Sakkis <george.sak...@ gmail.comwrote:
        Is there any package that parses regular expressions and returns an
        AST ? Something like:
        >
        >parse_rx(r'i (love|hate) h(is|er) (cat|dog)s?\s*! +')
        >
        Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
        'dog'), Optional('s'), ZeroOrMore(r'\s '), OneOrMore('!'))
        >
        Given such a structure, I want to create a generator that can generate
        all strings matched by this regexp. Obviously if the regexp contains a
        '*' or '+' the generator is infinite, and although it can be
        artificially constrained by, say, a maxdepth parameter, for now I'm
        interested in finite regexps only. It shouldn't be too hard to write
        one from scratch but just in case someone has already done it, so much
        the better.
        >
        George
        Check out this pyparsing regex inverter: http://pyparsing.wikispaces.com/file/view/invRegex.py

        Here is what your example generates:
        i (love|hate) h(is|er) (cat|dog)s?
        Parse time: 0.17 seconds
        16
        i love his cat
        i love his cats
        i love his dog
        i love his dogs
        i love her cat
        i love her cats
        i love her dog
        i love her dogs
        i hate his cat
        i hate his cats
        i hate his dog
        i hate his dogs
        i hate her cat
        i hate her cats
        i hate her dog
        i hate her dogs

        -- Paul

        Comment

        • George Sakkis

          #5
          Re: Regexp parser and generator

          On Nov 4, 9:56 pm, Paul McGuire <pt...@austin.r r.comwrote:
          On Nov 4, 1:34 pm, George Sakkis <george.sak...@ gmail.comwrote:
          >
          >
          >
          Is there any package that parses regular expressions and returns an
          AST ? Something like:
          >
          >>parse_rx(r' i (love|hate) h(is|er) (cat|dog)s?\s*! +')
          >
          Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
          'dog'), Optional('s'), ZeroOrMore(r'\s '), OneOrMore('!'))
          >
          Given such a structure, I want to create a generator that can generate
          all strings matched by this regexp. Obviously if the regexp contains a
          '*' or '+' the generator is infinite, and although it can be
          artificially constrained by, say, a maxdepth parameter, for now I'm
          interested in finite regexps only. It shouldn't be too hard to write
          one from scratch but just in case someone has already done it, so much
          the better.
          >
          George
          >
          Check out this pyparsing regex inverter:http://pyparsing.wikispaces.com/file/view/invRegex.py
          >
          Neat, seems like a good excuse to look into pyparsing :)

          Best,
          George

          Comment

          • George Sakkis

            #6
            Re: Regexp parser and generator

            On Nov 4, 3:30 pm, Peter Otten <__pete...@web. dewrote:
            George Sakkis wrote:
            Is there any package that parses regular expressions and returns an
            AST ? Something like:
            >
            >>parse_rx(r' i (love|hate) h(is|er) (cat|dog)s?\s*! +')
            Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
            'dog'), Optional('s'), ZeroOrMore(r'\s '), OneOrMore('!'))
            >
            Seen today, on planet python:
            >
            >import sre_parse
            >sre_parse.pars e("a|b")
            >
            [('in', [('literal', 97), ('literal', 98)])]
            >
            Peter
            Thanks, that's rather low level and undocumented but it does the work.

            Best,
            George

            Comment

            Working...