#!/usr/bin/perl -w use strict; #my $input = '[VP is [NP one NP] it [NP two NP] working VP]'; my $input = '[VP This stuff is [NP the left NP] [NP other thing NP] VP]'; #my $input = '[VP [NP This NP] [VP is [NP [NP the turning point NP] [PP to [NP the left NP] PP] NP] VP] . VP]'; #my $input = '[S [NP This NP] [VP is [NP [NP the turning point NP] ' . # '[PP to [NP the left NP] PP] NP] VP] . S]'; $input =~ s/^\[\w+\s*(.*?)\s*\w+\]$/$1\n/; my $bracket_count = 0; my @output; my $build_var; foreach (split(//, $input)) { if (/\[/) { if ($bracket_count == 0) {#DR $build_var =~ s/^\s*|\s*$//g;#dr push @output, $build_var if ($build_var ne '');#DR $build_var = '';#DR }#DR $bracket_count++; $build_var .= $_;#dr } elsif (/\]/) { $bracket_count--; $build_var .= $_;#dr if ($bracket_count == 0) {#DR $build_var =~ s/^\s*|\s*$//g;#dr push @output, $build_var if ($build_var ne '');#DR $build_var = '';#DR }#DR } elsif (/\n/) {#dr Should be the end $build_var =~ s/^\s*|\s*$//g;#dr push @output, $build_var if ($build_var ne '');#DR } else {#dr $build_var .= $_;#dr } } foreach (@output) { print '"', $_, '"', "\n"; }