
kencl
User
Aug 4, 2003, 3:20 PM
Post #1 of 3
(365 views)
|
algorithm for nested parsing needed
|
Can't Post
|
|
Hi Folks, I could use a hand coming up with an algorithm for parsing nested "cells" which is not recursive. Here's an example source document which is to be parsed: ==== parse test ==== <!--Record: outer_record1 --><table border="2"> <!--Record: inner_record1--><tr><td><b>%%title%%</b></td></tr> <!--End_Record: inner_record1--> <tr><td>%%one%%, %%two%%, %%three%%</td></tr> <!--Record: inner_record2--><tr><td bgcolor="#FFEEDD">%%type%%</td></tr> <!--End_Record: inner_record2--> </table><!--End_Record: outer_record1 --> <ul><u>Features</u> <!--Record: outer_record2--> <li>%%one%%</li> <!--End_Record: outer_record2--> </ul> <!--Record: outer_record3 --><table border="2"> <tr><tr><b>Quantity</b> %%qty%%</td></tr> <!--Record: inner_record3--><tr><td>%%retail_price%%</td></tr><!--End_Record: inner_record3--><!--Record: inner_record4--><tr><td>%%discount_price%%</td></tr><!--End_Record: inner_record4--> </table><!--End_Record: outer_record3 --> ==== end of parse test ====
And here's what I'm using to parse out the records. As you can see, this only parses 1 level deep (the top or outer level). # set record containers # eg: <!--Record: inner_record--><tr><td bgcolor="%%bgcolor3%%">%%cell_content%%</td></tr><!--End_Record: inner_record--> my $placeholder_prefix = $arg_ref->{'placeholder_prefix'} || '%%'; my $placeholder_suffix = $arg_ref->{'placeholder_suffix'} || '%%'; my $record_start_prefix = $arg_ref->{'record_start_prefix'} || '<!--Record:'; my $record_start_suffix = $arg_ref->{'record_start_suffix'} || '-->'; my $record_end_prefix = $arg_ref->{'record_end_prefix'} || '<!--End_Record:'; my $record_end_suffix = $arg_ref->{'record_end_suffix'} || '-->'; # parse records my $record = qr/\Q$record_start_prefix\E\s*([a-zA-Z0-9_-]+?)\s*\Q$record_start_suffix\E(.*?)\Q$record_end_prefix\E\s*\1\s*\Q$record_end_suffix\E/s; while ($file =~ s/$record/$placeholder_prefix$1$placeholder_suffix/) { my ($record_placeholder, $record_content) = ($1, $2); $Templates{$arg_ref->{'template'}}{'records'}{$record_placeholder} = $record_content; } $Templates{$arg_ref->{'template'}}{'file'} = $file; It would be easy enough parse the inner records recursively, but I just can't get my head around how to do this iteratively Any thoughts on even a general approach to converting a recursive algorithm to an itterative one are appreciated :) PS Sorry for the horizontal scroll... >> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!
|