GrandFather has asked for the wisdom of the Perl Monks concerning the following question:

I have a test grammar that returns an array of hashes:

use Parse::RecDescent; use Data::Dump::Streamer; my $grammar = q{ test: expr(s) /^\Z/ {return $item{"expr(s)"};} expr: name '=' /\d+/ { $return = {$item{name} => $item[-1]}; 1; } name: /\w+/ }; my $parser = new Parse::RecDescent ($grammar); Dump ($parser->test ("x = 1\ny = 2\nz = 3"));

Prints:

$ARRAY1 = [ { x => 1 }, { y => 2 }, { z => 3 } ];

but I'd like a simple hash:

$HASH1 = { x => 1, y => 2, z => 3 };

For some reason my brain has frozen up and I just can't get the syntax right. What am I missing? (I'd rather Parse::RecDescent return the hash than fix it later.)


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re: How do I return a hash from Parse::RecDescent?
by ikegami (Patriarch) on Sep 22, 2006 at 05:23 UTC

    You convert an AoH into an H in P::RD the same way you would anywhere else. Convert each hash into a list, combine the lists, and create a hash from the combined list.

    test: expr(s) /^\Z/ { { map %$_, @{$item{"expr(s)"}} } }

    That's a lot of curlies X_X

    Update: Changed map { %$_ } to map %$_, to remove a pair of curlies.

Re: How do I return a hash from Parse::RecDescent?
by GrandFather (Saint) on Sep 22, 2006 at 05:24 UTC

    Hmm, here's one way:

    ... test: expr(s) /^\Z/ { my %hash; @hash{keys %$_} = values %$_ for @{$item{"expr(s)"}}; return \%hash; } ...

    Prints:

    $HASH1 = { x => 1, y => 2, z => 3 };

    Is there anything "better"?


    DWIM is Perl's answer to Gödel

      I know you just copied the OP, but return \%hash; should be just \%hash.

      His return $item{"expr(s)"} should be just $item{"expr(s)"}.

      P::RD actions (i.e. code blocks in curlies) are placed in a do, not a sub. Since return shouldn't be used to exit a do, return shouldn't be used to exit a P::RD action.

Re: How do I return a hash from Parse::RecDescent?
by monkfan (Curate) on Sep 22, 2006 at 05:21 UTC
    Gramps,

    With a post processing like this perhaps:
    #!/usr/bin/perl use Data::Dumper; $var = [ { x => 1 }, { y => 2 }, { z => 3 } ]; my %res; foreach my $v (@{$var}) { my ($k) = keys %$v; $res{$k} = $v->{$k}; } print Dumper \%res;
    I know this is too naive. I am sure some other smarter guys will make it into one liner.

    Regards,
    Edward
Re: How do I return a hash from Parse::RecDescent?
by Outaspace (Scribe) on Sep 22, 2006 at 11:35 UTC
    Hi,

    just use:
    use Parse::RecDescent; use Data::Dumper; my $Hash = {}; my $grammar = q{ test: expr(s) /^\Z/ expr: name '=' /\d+/ { main::AddToHash($item{name}, $item[-1]); } name: /\w+/ }; my $parser = new Parse::RecDescent ($grammar); $parser->test ("x = 1\ny = 2\nz = 3"); sub AddToHash { my $szVar = shift; $Hash->{$szVar} = shift; } print Dumper ($Hash);
    Humble,
    Andre

      That breaks the ability to easily PreCompile (which is rather major) because the user must provide AddHash and the user must be main::. Breaking encapsulation in this fashion also prevents more than one instance of the parser from being used at a time. Fix:

      use strict; use warnings; use Parse::RecDescent (); use Data::Dumper qw( Dumper ); my $grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; } test : <rulevar: local %data > | expr(s) /^\Z/ { \%data } expr : name '=' /\d+/ { $data{ $item{name} } = $item[-1]; 1 } name : /\w+/ __END_OF_GRAMMAR__ my $parser = new Parse::RecDescent($grammar) or die; my $data = $parser->test("x = 1 y = 2 z = 3") or die; print Dumper $data;

      ( Note: I prefer the solution I posted last night because it doesn't break if backtracking occurs like this one and this one's parent do. )

        Ok, but actually I would use this and maybe put the MyParser package to a different file (but not everybody like OO):
        package MyParser; use constant DEBUG => 1; use strict; use base qw(Parse::RecDescent); use Data::Dumper; sub new { print "MyParser->new(@_)\n" if (DEBUG); my ($class, $szGrammar, $szCode) = @_; my $this; eval { $this = $class->SUPER::new($szGrammar); }; if ($@) { print $@; $this = {}; bless $this, $class; } else { # Start parsing code $this->test($szCode); return $this; } } sub AddToHash { print "MyParser->AddToHash(@_)\n" if (DEBUG); my ($this, $szVar, $szValue) = @_; $this->{Result}->{$szVar} = $szValue; } sub GetResult { print "MyParser->GetResult(@_)\n" if (DEBUG); my ($this) = @_; return $this->{Result}; } sub DumpResult { print "MyParser->DumpResult(@_)\n" if (DEBUG); my ($this) = @_; print Dumper($this->{Result}); } package main; my $szGrammar = q{ test: expr(s) /^\Z/ expr: name '=' /\d+/ { $thisparser->AddToHash($item{name}, $item[-1]); } name: /\w+/ }; my $szCode = "x = 1\ny = 2\nz = 3"; my $hParser = MyParser->new($szGrammar, $szCode); $hParser->DumpResult();
        Andre