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 http://stackoverflow.com/questions/265457/regex-grammar

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.

https://github.com/nathansobo/treetop
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.

https://github.com/ammar/regexp_parser

https://rubygems.org/gems/regexp_parser

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

https://github.com/ammar/meta_re

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.

https://github.com/ammar/regexp_parser

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs