http://www.perlmonks.org?node_id=894578


in reply to Re: Parse::RecDescent grammar definition
in thread Parse::RecDescent grammar definition

The second problem is that your print statement only belongs to the second rule, i.e. if Pre is matched, nothing is done at all.

Wow, there is something I am not getting here. I would expect %item to have what matches in the rule, so if the rule is "Pre | Post", %item would have either the "Pre" or the "Post" matching part.

Obviously I am getting all this wrong

citromatik

Replies are listed 'Best First'.
Re^3: Parse::RecDescent grammar definition
by jethro (Monsignor) on Mar 21, 2011 at 17:22 UTC

    You are right, %item has either the Pre or Post matching part. But only in one case does it print anything!

    Entry : Pre | Post { use Data::Dumper; print Dumper \%item;} # is equivalent to Entry : Pre Entry : Post { use Data::Dumper; print Dumper \%item; } # is equivalent to Entry : Post { use Data::Dumper; print Dumper \%item; } Entry : Pre # is NOT equivalent to Entry : Pre { use Data::Dumper; print Dumper \%item; } Entry : Post { use Data::Dumper; print Dumper \%item; }

    And the last example is what you want