Re: Text::Balanced v Parse::RecDescent
[email protected] (Andrew Savige) Tue, 3 Dec 2002 18:46:44 +1100 (EST)
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
En op 3 december 2002 sprak Damian Conway:
> -----cut----------cut----------cut----------cut----------cut-----
>
> use re 'eval';
>
> our $quoted = qr/ ' (?: \\. | [^'] )*? ' # Match 'str'
> | " (?: \\. | [^"] )*? " # Match "str"
> /x;
>
> our $element = qr/ (?: [^'"{,]+ # Match non-special
> characters
> | \\. # Match escaped
> anything
> | $quoted # Match quoted
> anything
> | (??{$nested}) # Match
> {...,...,...}
> )+
> /xs;
>
> our $nested = qr/ [{] # Match {
> (?: $element , )* # Match list of
> subelements
> $element? # Match last
> subelement
> [}] # Match }
> /x;
>
>
> $data = <DATA>;
>
> @fields = $data =~ m/ ( $element ) ,? /gx; # Capture elements
> repeatedly
>
> use Data::Dumper 'Dumper';
> print Dumper(@fields);
>
> __DATA__
> abc, ',def' "\"ab'c,}" xyz , fred IN { 1, "x}y",3 } x, 'z'
>
> -----cut----------cut----------cut----------cut----------cut-----
I like this elegant solution.
However, these recursive regexes seem to have a flawed implementation.
For example, with the following test data:
__DATA__
{1}, hello one two three
it seems to hang. Actually, it finishes eventually, taking 87.33
seconds on Linux Perl 5.6.1. Ditto for 5.6.0 and 5.8.0
(tested on both Windows and Linux). Seems very sensitive to the
length of the last field; shortening "hello one two three" to
"h o t t" and it finished in 0.4 seconds.
I also remember some very slow recursive regex solutions in a recent
Infix-to-RPN golf game, TPR(0,5a):
http://archive.develooper.com/[email protected]/msg01676.html
http://perlgolf.sourceforge.net/
/-\
http://www.yahoo.promo.com.au/hint/ - Yahoo! Hint Dropper
- Avoid getting hideous gifts this Christmas with Yahoo! Hint Dropper!