grizzley has asked for the wisdom of the Perl Monks concerning the following question:
Hi!
I am writing a script to parse IDL file and to generate graphviz file from it. I encountered following problem, which I isolated below:
Regexp tries to find 'module <module_name> { content };' text globally (//g) and if found, content is parsed recursively to create nested 'subgraph {}' sequences. Code below hangs. And if I remove innemost parens - it works good. Can anyone enlighten me what can be wrong? I am using this regexp (nested parens) for more than year in several scripts and it works (uhm, worked) perfectly since now.
With 'use warnings;' it prints 'Use of uninitialized value in pattern match (m//)', but it doesn't help me in any way to understand the problem.
Thank for any help.
#!perl $_=join"",<DATA>; sub parse { my ($text) = @_; $nw++; my $parens = qr{(?:[^{}]+?|\{(??{$parens})\})*}x; while($text =~ /module (\w+) \{ ($parens) \};?/gx) { ($name, $content)=($1, $2); # print "[$&]"; print ' 'x$nw, "subgraph $name\n"; print ' 'x$nw, "{\n"; parse($content); print ' 'x$nw, "}\n"; } $nw--; } print "digraph G\n{\n"; parse($_); print "}\n"; __DATA__ module abc { module def { module ghi { }; }; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: balanced parens regexp hangs
by moritz (Cardinal) on Mar 26, 2009 at 22:31 UTC | |
by grizzley (Chaplain) on Mar 27, 2009 at 08:32 UTC | |
by moritz (Cardinal) on Mar 27, 2009 at 09:07 UTC | |
by grizzley (Chaplain) on Mar 27, 2009 at 10:10 UTC | |
|
Re: balanced parens regexp hangs
by AnomalousMonk (Archbishop) on Mar 27, 2009 at 00:47 UTC | |
by ikegami (Patriarch) on Mar 27, 2009 at 01:01 UTC | |
|
Re: balanced parens regexp hangs
by Anonymous Monk on Mar 27, 2009 at 03:19 UTC |