Perhaps a full example will help:

use strict; use warnings; use Marpa::R2; my $dsl = <<'END_OF_DSL'; lexeme default = latm => 1 Dice_Expression ::= Simple_Dice action => ::first Simple_Dice ::= Rolls ('d') Sides action => do_simple_roll Rolls ~ digits Sides ~ digits digits ~ [\d]+ :discard ~ whiteSpace whiteSpace ~ [\s]+ END_OF_DSL package Actions; sub do_simple_roll { my (undef, $rolls, $sides) = @_; my @res = map {1 + int(rand($sides))} 1 .. $rolls; return \@res; } package main; my $grammar = Marpa::R2::Scanless::G->new({ source => \$dsl}); my $input = '2d6'; my $rolls = $grammar->parse(\$input, 'Actions'); print "Result: @$$rolls\n";

Note the :default ::= action => ::first. Actually we can omit the default line altogether and then change the Dice_Expression line to:

Dice_Expression ::= Simple_Dice action => ::first

with the same result. The action determines what is passed up to the next level. If there is no action and no explicit default action undef is passed back up (although that isn't what the documentation seems to say). The default action is used where there is no explicit action. Perhaps the key thing to understand is that the called sub gets a parameter in its argument list for each RHS primary (see RHS alternatives) unless the primary is hidden. RHS values are either constant strings or the LHS for a G1 rule, or a L0 symbol.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re^3: First steps with Marpa::R2 and BNF by GrandFather
in thread First steps with Marpa::R2 and BNF by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.