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>
    / | \
<bol> | <eol>
     <alt>
   / | \
foo <*> qux
       >
     <alt>
     / \
   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.

···

--
Posted via http://www.ruby-forum.com/.

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 9:53 AM, Brian Candler <b.candler@pobox.com> wrote:

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:

&lt;seq&gt;

/ | \
<bol> | <eol>
<alt>
/ | \
foo <*> qux
>
<alt>
/ \
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.

--
Posted via http://www.ruby-forum.com/\.

--
Alex Chaffee - alex@stinky.com
http://alexchaffee.com
http://twitter.com/alexch

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

···

On Thu, Feb 16, 2012 at 7:53 PM, Brian Candler <b.candler@pobox.com> wrote:

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

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.

···

On Thu, Feb 16, 2012 at 6:53 PM, Brian Candler <b.candler@pobox.com> wrote:

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:

&lt;seq&gt;

/ | \
<bol> | <eol>
<alt>
/ | \
foo <*> qux
>
<alt>
/ \
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!)

Ammar Ali 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!

···

--
Posted via http://www.ruby-forum.com/\.