By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,522 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,522 IT Pros & Developers. It's quick & easy.

Regexp parser and generator

P: n/a
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
Nov 4 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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_Pattern object at 0x47b7a0>
>>re.compile("ab*c[xyz]", 128)
literal 97
max_repeat 0 65535
literal 98
literal 99
in
literal 120
literal 121
literal 122
<_sre.SRE_Pattern object at 0x371f90>

Skip
Nov 4 '08 #2

P: n/a
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.parse("a|b")
[('in', [('literal', 97), ('literal', 98)])]
Peter
Nov 4 '08 #3

P: n/a
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
Nov 5 '08 #4

P: n/a
On Nov 4, 9:56*pm, Paul McGuire <pt...@austin.rr.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
Nov 6 '08 #5

P: n/a
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.parse("a|b")

[('in', [('literal', 97), ('literal', 98)])]

Peter
Thanks, that's rather low level and undocumented but it does the work.

Best,
George
Nov 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.