# Any parser \*for\* regular expressions?

**URL:** <https://rubytalk.org/t/any-parser-for-regular-expressions/64897>\
**Category:** ruby-talk\
**Created:** [16 February 2012 17:53 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897 "2012-02-16T17:53:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [16 February 2012 17:53 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897/1 "2012-02-16T17:53:21Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<seq\>  
&nbsp;&nbsp;&nbsp;&nbsp;/ | \  
\<bol\> | \<eol\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<alt\>  
&nbsp;&nbsp;&nbsp;/ | \  
foo \<\*\> qux  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<alt\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ \  
&nbsp;&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Alex\_Chaffee](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@Alex\_Chaffee](https://rubytalk.org/u/Alex_Chaffee)\
**Post date:** [16 February 2012 18:11 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897/2 "2012-02-16T18:11:51Z")

</div>

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](http://www.cs.sfu.ca/~cameron/Teaching/384/99-3/regexp-plg.html)  
via [Regex BNF Grammar - Stack Overflow](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.

> **[GitHub - nathansobo/treetop: A Ruby-based parsing DSL based on parsing...](https://github.com/nathansobo/treetop)**
>
> A Ruby-based parsing DSL based on parsing expression grammars. - GitHub - nathansobo/treetop: A Ruby-based parsing DSL based on parsing expression grammars.

[http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html](http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html)  
[http://groups.google.com/group/treetop-dev](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/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Alex Chaffee - alex@stinky.com  
> [http://alexchaffee.com](http://alexchaffee.com)  
> [http://twitter.com/alexch](http://twitter.com/alexch)

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [16 February 2012 19:01 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897/3 "2012-02-16T19:01:06Z")

</div>

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](https://github.com/ammar/regexp_parser)

[regexp\_parser | RubyGems.org | your community gem host](https://rubygems.org/gems/regexp_parser)

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

&nbsp;&nbsp;[GitHub - ammar/meta\_re: An experimental regular expression "preprocessor"](https://github.com/ammar/meta_re)

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?

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [16 February 2012 19:43 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897/4 "2012-02-16T19:43:37Z")

</div>

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

[http://www.rubyquiz.com/quiz143.html](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!)

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [16 February 2012 19:22 UTC](https://rubytalk.org/t/any-parser-for-regular-expressions/64897/5 "2012-02-16T19:22:12Z")

</div>

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](https://github.com/ammar/regexp_parser)

That looks like just the job, thank you!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
