Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: First steps with Marpa::R2 and BNF

by GrandFather (Saint)
on Jan 15, 2021 at 09:23 UTC ( #11126951=note: print w/replies, xml ) Need Help??


in reply to First steps with Marpa::R2 and BNF

3 - Returned structures

First: sorry for the piecemeal replies. I'm playing with your code and refreshing my Marpa as I go - it's been a while since I played with it.

You are getting a "bogus" array because that is the default action and you haven't specified actions for Dice_Expression ::= .... You can fix it by:

Dice_Expression ::= Simple_Dice action => ::first |Dice_with_modifier_x action => ::first |Dice_with_modifier_r action => ::first

which will cause just the hash ref to be returned from $grammar->parse(\$input, 'My_Actions'). See the semantics section in the Marpa::R2 documentation.

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

Replies are listed 'Best First'.
Re^2: First steps with Marpa::R2 and BNF
by Discipulus (Abbot) on Jan 15, 2021 at 11:56 UTC
    no sorry GrandFather but a big thanks

    Anyway i dont understand your reply. I removed :default ::= action => [name,values] and is this the root cause of the "bogus" array (it makes sense to know which name is passing around but I consider it misleading in the sysnopsis).

    I read in the doc you linked:

    > The "::first" action indicates that the value of a rule is to be the value of its first child, that is, the value corresponding to the first symbol of the rule's RHS.

    But I cannot see how your addition of action => ::first can fix it. Given I removed the :default ::= action I get the same results with or without ::first

    Dice_Expression ::= Simple_Dice |Dice_with_modifier_x |Dice_with_modifier_r do_simple_roll received: ({}, 4, "d", 6) Rolled : 4 Rolled : 3 Rolled : 6 Rolled : 2 do_simple_roll returning: { die_type => "1d6", rolls => [4, 3, 6, 2] } modifier_r received: ({}, { die_type => "1d6", rolls => [4, 3, 6, 2] } +, "r", 1) Dice_Expression ::= Simple_Dice action => ::first |Dice_with_modifier_x action => ::first |Dice_with_modifier_r action => ::first do_simple_roll received: ({}, 4, "d", 6) Rolled : 3 Rolled : 2 Rolled : 5 Rolled : 6 do_simple_roll returning: { die_type => "1d6", rolls => [3, 2, 5, 6] } modifier_r received: ({}, { die_type => "1d6", rolls => [3, 2, 5, 6] } +, "r", 1)

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11126951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2021-04-22 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?