a little parsing challenge ☺

Stefan Behnel stefan_ml at behnel.de
Mon Jul 18 03:24:21 EDT 2011


Rouslan Korneychuk, 18.07.2011 09:09:
> I don't know why, but I just had to try it (even though I don't usually use
> Perl and had to look up a lot of stuff). I came up with this:
>
> /(?|
> (\()(?&matched)([\}\]”›»】〉》」』]|$) |
> (\{)(?&matched)([\)\]”›»】〉》」』]|$) |
> (\[)(?&matched)([\)\}”›»】〉》」』]|$) |
> (“)(?&matched)([\)\}\]›»】〉》」』]|$) |
> (‹)(?&matched)([\)\}\]”»】〉》」』]|$) |
> («)(?&matched)([\)\}\]”›】〉》」』]|$) |
> (【)(?&matched)([\)\}\]”›»〉》」』]|$) |
> (〈)(?&matched)([\)\}\]”›»】》」』]|$) |
> (《)(?&matched)([\)\}\]”›»】〉」』]|$) |
> (「)(?&matched)([\)\}\]”›»】〉》』]|$) |
> (『)(?&matched)([\)\}\]”›»】〉》」]|$))
> (?(DEFINE)(?<matched>(?:
> \((?&matched)\) |
> \{(?&matched)\} |
> \[(?&matched)\] |
> “(?&matched)” |
> ‹(?&matched)› |
> «(?&matched)» |
> 【(?&matched)】 |
> 〈(?&matched)〉 |
> 《(?&matched)》 |
> 「(?&matched)」 |
> 『(?&matched)』 |
> [^\(\{\[“‹«【〈《「『\)\}\]”›»】〉》」』]++)*+))
> /sx;
>
> If the pattern matches, there is a mismatched bracket. $1 is set to the
> mismatched opening bracket. $-[1] is its location. $2 is the mismatched
> closing bracket or '' if the bracket was never closed. $-[2] is set to the
> location of the closing bracket or the end of the string if the bracket
> wasn't closed.
>
>
> I didn't write all that manually; it was generated with this:
>
> my @open = ('\(','\{','\[','“','‹','«','【','〈','《','「','『');
> my @close = ('\)','\}','\]','”','›','»','】','〉','》','」','』');
>
> '(?|'.join('|',map
> {'('.$open[$_].')(?&matched)(['.join('', at close[0..($_-1),($_+1)..$#close]).']|$)'}
> (0 .. $#open)).')(?(DEFINE)(?<matched>(?:'.join('|',map
> {$open[$_].'(?&matched)'.$close[$_]} (0 ..
> $#open)).'|[^'.join('', at open, at close).']++)*+))'


That's solid Perl. Both the code generator and the generated code are 
unreadable. Well done!

Stefan




More information about the Python-list mailing list