In the cpan page of Marpa::R2, I understand the BNF (Backus-Naur Form), but I am quite lost with the action callbacks.
In this example below, I understand that the two, left and right members are passed to do_multiply
. I have no problem with that. The problem is that I cannot find any documentation of what are these arguments?
my $dsl = <<'END_OF_DSL';
:default ::= action => [name,values]
lexeme default = latm => 1
Calculator ::= Expression action => ::first
...
Term '*' Factor action => do_multiply
...
END_OF_DSL
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
sub do_multiply { $_[1] * $_[2] }
What are $_[0]
or even $_[3]
? Where is this documented? Even on the official marpa website I don't see any documentation.
In a different example, here an answer of chobora, we see that pair
refers to $_[2]
and $_[3]
:
Snippet of the BNF:
Hash ::= '(' Pairs ')' action => hash
Pairs ::= Pair+ action => pairs
Pair ::= '(' Key Value ')' action => pair
Key ::= String
Core code:
$recce->read(\$input);
print Dumper $recce->value;
sub hash { $_[2] }
sub pairs { shift; +{ map @$_, @_ } }
sub pair { [ @_[2, 3] ] } # What is 2 and 3?
sub itself { $_[1] }