Any parser *for* regular expressions?

Is there some existing code (preferably ruby) which can take a regular
expression and
build a parse tree from it? e.g. I want to turn

“^(foo|(bar|baz)*|qux)$”

into something like this:

 <seq>
/  |  \

|

/ |
foo <*> qux
|

/
bar baz

or some equivalent representation of the regular expression itself.

(The trouble is, when you google for ‘ruby regular expression parser’
you get all sorts of parsers written using regular expressions, not
parsers for regular expressions!)

Thanks,

Brian.

A better search term might be “regular expression grammar” since a
grammar is what a regular expression parser would require.

Here’s one I just found for Perl regexes:
http://www.cs.sfu.ca/~cameron/Teaching/384/99-3/regexp-plg.html
via Regex BNF Grammar - Stack Overflow

If I were you I’d use the Perl one as a starting point for writing a
Treetop grammar for Ruby regexes. Or ask around on the Treetop list
and see if someone’s already done that.

http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html
http://groups.google.com/group/treetop-dev

On Thu, Feb 16, 2012 at 7:53 PM, Brian C. [email protected]
wrote:

Is there some existing code (preferably ruby) which can take a regular
expression and build a parse tree from it?

I wrote one a while ago. It doesn’t exactly produce a tree like you
described, but it does provide outputs that I think can be easily used
to
create such a tree.

GitHub - ammar/regexp_parser: A regular expression parser library for Ruby

regexp_parser | RubyGems.org | your community gem host

I only have one public, and simple, project that uses it so far:

GitHub - ammar/meta_re: An experimental regular expression "preprocessor"

HTH,
Ammar

Ammar A. wrote in post #1047200:

I wrote one a while ago. It doesn’t exactly produce a tree like you
described, but it does provide outputs that I think can be easily used
to
create such a tree.

GitHub - ammar/regexp_parser: A regular expression parser library for Ruby

That looks like just the job, thank you!

On Thu, Feb 16, 2012 at 6:53 PM, Brian C. [email protected]
wrote:

|
(The trouble is, when you google for ‘ruby regular expression parser’
you get all sorts of parsers written using regular expressions, not
parsers for regular expressions!)

I don’t have a good answer for your problem, but this question
reminded me of an old rubyquiz I have fond memories of :slight_smile:

http://www.rubyquiz.com/quiz143.html

Jesus.