1

我正在使用 Perl 中的Parse::RecDescent解析器,我似乎在从中获取信息的时间最糟糕。网上现成的信息似乎没有重要的例子。

这是代码:

event_function: object_list ':' event_list ';'
        <defer:
        {       #item is a special character with Parse::Recdescent.
            print Dumper($item{object_list});
            $return = $item[1];
        }
        >
        | object_list ':' ';'
        <defer:
        { 
            print Dumper($item{object_list});
            $return = $item[1];
        }
        >

这是输出

PS W:\developers\paulnathan\rd_dir> perl parser.pl testfile
$VAR1 = 4;
$VAR1 = 8;
PS W:\developers\paulnathan\rd_dir>

输入文件解析正确。

stuff, stuff2: pre-operation event = {foo1, foo2};

应该输出以“stuff”、“stuff2”为键的散列。

想法?

编辑:

object_list : 
        object ',' object_list
        <defer:
        {

            my $retval = ();
            $retval = ::merge_hash_refs($item[1], $item[3]);

            $return = $retval;
        }
        >
        | object
        <defer:
        { 
            #print Dumper($item{object});
            $return = $item{object};
        }
        >       

    object : 
        '/' /[a-z0-9_][a-z0-9_]*/ '/' '...'
            <defer:
            {
                $::objects->{$item[2]} = "stuff";
                $return = $::objects;
            }
            >
        |  /[a-z0-9_][a-z0-9_]*/
            <defer:
            { 
                $::objects->{$item[1]} = "stuff";
                $return = $::objects;
            }
            >

edit2:Merge_hash_refs,以防万一。:-)

#takes two hash references.
sub merge_hash_refs {
    my($ref1, $ref2) = @_;
    my $retref = ();
    while( my ($k, $v) = each %$ref1 ) {
        $retref->{$k} = $v;
    }
    while( my ($k, $v) = each %$ref2 ) {
        $retref->{$k} = $v;
    }

    return $retref;
}
4

2 回答 2

6

如果将 a 添加use strict到脚本中,您将收到致命错误Can't use string ("1") as a HASH ref while "strict refs" in use at [the call to merge_hash_refs]<defer>看起来,指令创建的闭包导致 的内容@item成为匹配产品时的内容,而不是子规则最终返回的 hashrefs。删除<defer>指令给了我这个输出:

$VAR1 = {
          'stuff2' => 'stuff',
          'stuff' => 'stuff'
        };

当然,这会产生副作用,object即使更高级别的规则失败(包括回溯),成功的产生也会更新 $::object。我会这样写:

use strict;
use warnings;
use Parse::RecDescent;
use Data::Dumper;

my $parser = Parse::RecDescent->new(<<'EOT');
event_function: object_list ':' event_list(?) ';'
    {
        $return = $item[1];
    }

object_list : <leftop: object ',' object>
    {
        $return = { map { %$_ } @{$item[1]} };
    }

object : 
    '/' /[a-z0-9_][a-z0-9_]*/ '/' '...'
        {
            $return = { $item[2] => 'stuff' };
        }
    |  /[a-z0-9_][a-z0-9_]*/
        { 
            $return = { $item[1] => 'stuff' };
        }

# stub, don't know what this should be
event_list : /[^;]+/

EOT

my %object;

while (<DATA>) {
    my $x = $parser->event_function($_);

    next unless $x;

    # merge objects into master list
    while (my ($k, $v) = each %$x) {
        $object{$k} = $v;
    }
}

print Dumper \%object;

__DATA__
stuff, stuff2: pre-operation event = {foo1, foo2};
stuff3, stuff4: ;

输出是:

$VAR1 = {
          'stuff2' => 'stuff',
          'stuff3' => 'stuff',
          'stuff' => 'stuff',
          'stuff4' => 'stuff'
        };
于 2009-06-04T17:26:00.517 回答
1

可能不是您问题的答案,但是当您通过散列启动 each() 循环时,如果之前已在散列上使用了 each(),则它只是从迭代器指向的任何位置开始。为了安全起见,在 while 循环之前放置一个 void-context keys()(例如 keys(%$ref1);) 以重置迭代器。旧版本的 Data::Dumper 有一个可爱的小错误,有时让迭代器指向最后一个元素之后,使得哈希对于不安全的 while(...each...) 循环看起来是空的 :)

于 2009-06-04T17:26:53.900 回答