Re: a little parsing challenge ☺

Xah Lee xahlee at gmail.com
Thu Jul 21 15:23:58 CEST 2011


2011-07-21

On Jul 18, 12:09 am, Rouslan Korneychuk <rousl... at msn.com> wrote:
> 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).']++)*+))'

Thanks for the code.

are you willing to make it complete and standalone? i.e. i can run it
like this:

perl Rouslan_Korneychuk.pl dirPath

and it prints any file that has mismatched pair and line/column number
or the char position?

i'd do it myself but so far i tried 5 codes, 3 fixes, all failed. Not
a complain, but it does take time to gather the code, of different
langs by different people, properly document their authors and
original source urls, etc, and test it out on my envirenment. All
together in the past 3 days i spent perhaps a total of 4 hours running
several code and writing back etc and so far not one really worked.

i know perl well, but your code is a bit out of the ordinary ☺. If
past days have been good experience, i might dive in and study for
fun.

 Xah



More information about the Python-list mailing list