Re: rebuilding a string from the autotree datastructure.
[email protected] ("Ron D. Smith") Tue, 15 Jun 2004 08:36:10 -0700
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
On Tuesday, Jun 15, 2004 David Holden said:
> Hello,
>
> I would like to rebuild a parsed string from the datastructure given by
> autotree.
>
> For example
>
>
> Given the date string "(2001)."
>
>
> using the following rules:-
>
> Date: lft_bracket Year YearLabel(?) rgt_bracket point
>
> Year: /\d{4}/
> YearLabel: /[a-z]/
> point: '.'
> lft_bracket: '('
> rgt_bracket: ')'
>
>
> autotree gives the following datastructure
>
> $VAR1 = bless( {
> 'Year' => bless( {
> '__VALUE__' => '2001'
> }, 'Year' ),
> '__RULE__' => 'Date',
> 'point' => bless( {
> '__VALUE__' => '.'
> }, 'point' ),
> 'lft_bracket' => bless( {
> '__VALUE__' => '('
> }, 'lft_bracket' ),
> 'rgt_bracket' => bless( {
> '__VALUE__' => ')'
> }, 'rgt_bracket' ),
> 'YearLabel(?)' => []
> }, 'Date' );
>
> I would like to be able to reconstruct the string "(2001)." from this
> structure the problem I have is that because it is a HASH it does not have
> order information, e.g. in the above "Year" key comes before "lft_bracket".
>
> Am I missing somethings?
No.
Basically what you want to do is "unparse" the data. The "unparser" will
need to know as much about the syntax as the parser.
autotree gives you a "best guess" as to what you need, but if you want to
have a structure that contains ordering information, you will need to take
control of the structure that gets created.
To do so, you do not need to abandon autotree:
use strict;
use vars qw($parser $text %top);
use Data::Dumper;
use Parse::RecDescent;
$RD_WARN = 1;
$RD_HINT = 1;
$RD_TRACE = 120;
use constant GRAMMAR => q(
<autotree>
Date: lft_bracket Year YearLabel(?) rgt_bracket point {\@item}
Year: /\d{4}/
YearLabel: /[a-z]/
point: '.'
lft_bracket: '('
rgt_bracket: ')'
);
$parser = Parse::RecDescent->new(GRAMMAR) or die 'Bad grammar';
$text .= $_ while (<DATA>);
print Dumper($parser->Date($text));
__DATA__
(2001).
>
> thanks in advance.
>
> Dave.
>
>
>
>
>
>
> --
> Dr. David Holden.
>
> Thanks in advance:-
> Please avoid sending me Word or PowerPoint attachments.
> See: <http://www.fsf.org/philosophy/no-word-attachments.html>
>
> Show me your papers..: http://www.no2id.net/index.html
>
> Public GPG key available on request.
> -------------------------------------------------------------
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226