Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CPAN module to parse nested parentheses?

Tags:

perl

cpan

Is there a CPAN module that can read a string like this:

"[[<asdf>, <foo>], (abc, def, ghi), ({'jkl'})]"

...and parse it into some sort of tree structure that's easy to walk and pretty-print?

like image 745
mike Avatar asked Dec 09 '08 21:12

mike


4 Answers

I think that you could build on top of Text::Balanced, which will do a lot of the heavy lifting for you. You'll still need to build a tree structure, though.

like image 151
Dave Rolsky Avatar answered Sep 28 '22 20:09

Dave Rolsky


Perl 5.10's regular expressions can handle balanced structures like that. See the (?PARNO) (?-PARNO) (?+PARNO) (?R) (?0) section in perlre.

like image 27
brian d foy Avatar answered Sep 28 '22 19:09

brian d foy


Perl 6 is going to have built-in facilities to help support this. In the interim, Text::Balanced (as mentioned by Dave Rolsky) is probably the module of choice. Note that it, too, was written by Damian Conway (as commended by Bill Karwin).

like image 44
Jonathan Leffler Avatar answered Sep 28 '22 19:09

Jonathan Leffler


Damian Conway's Parse::RecDescent

I haven't used this, but it's a good bet that a Perl module written by Damian Conway is worth using.

like image 36
Bill Karwin Avatar answered Sep 28 '22 19:09

Bill Karwin