Parse :: RecDescent - 从中​​获取信息

时间:2009-06-04 16:21:31

标签: perl parse-recdescent

我正在使用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;
}

2 个答案:

答案 0 :(得分:6)

如果您在脚本中添加use strict,您将收到致命错误无法使用字符串(“1”)作为HASH参考,而在[来电时使用“严格参考”到merge_hash_refs] 。似乎<defer>指令创建的闭包导致@item的内容是生成匹配时的内容,而不是最终由子规则返回的hashref。删除<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'
        };

答案 1 :(得分:1)

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