Discipulus has asked for the wisdom of the Perl Monks concerning the following question:
I've get off the rust over my hands with a toy project recently uploaded to CPAN. It is a dice roller system.
After ~100 lines of coding, in the above module I had a sudden desire to use Marpa::R2 to accomplish the task of parsing dice expressions. But I terminate my module without any grammar and it accept dice expressions like:
3d6 # simplest one 3d6+3 # with a result modifier 3d8r1 # reroll and discard any 1 3d8rlt3 # reroll and discard any lesser than 3 3d8rgt6 # reroll and discard any greater than 6 3d8rgt6+2 # reroll and discard any greater than 6 and add +2 to the f +inal result
..and so on. See the synopsis of my module for more examples.
Now I want to parse these expressions with Marpa::R2 and I produced cool code I'm proud of you can review at the end of the post.
Marpa and BNF in general has a lot of documentation and well.. I did not read it all :) It is damn complicated and the enormous amount of documentation produced by Marpa::R2 author is invaluable but also sparse and not precisely destinated to beginners. Or I'm dumb.
Anyway I have some questions and I'd like simple answers.
1 - Marpa uses regexes but..
RHS (Right Hand Symbol) are often like: digits ~ [\d]+ but it seems only character classes can be used. I tried with: something ~ [(?:this|or|that)]+ but it seems is not the correct usage.
What can be put at the end of a definition chain?
2 - Optional terms
In the below example Dice_with_modifier_x rule works but in the Dice_with_modifier_r I wanted to introduce optional terms: it must accept all these expressions: 3d6r1 3d6rgt4 3d6rlt3 so the r will be always present but followed by an optional gt or lt (greater than and lesser than).
When I pass 3d8r1 and 3d8rgt1 I get different sized lists ( as shown by dd by Data::Dump ):
# 3d8r1 modifier_r received: ({}, { die_type => "1d8", rolls => [8, 1, 7] }, " +r", 1) # 3d8rgt1 modifier_r received: ({}, { die_type => "1d8", rolls => [4, 8, 1] }, " +r", "gt", 1)
Should I work on the size of the list? In my head I'd like something Optional_Modifier_Comparison and if it is not present assume eq as default.
How must I treat optional eventually empty terms?
I have put Die_Modifier_Comp ~ 'gt' | 'lt' but wonder if this is the way. Anyway I tried Die_Modifier_Comp ~ 'gt' | 'lt' | '' and it dies.
3 - Returned structures
I put :default ::= action => [name,values] and I see that my subs where I return a hashref are modified and the actual return structure is \["Dice_Expression", { die_type => "1d4", rolls => [2] }] if I remove name from the default action it eliminates the "Dice_Expression" part. Anyway a reference to an arrayref is returned and then I have to write ugly things like: $$$new[1]->{rolls}->[0]
How can I use name in a profitable way? There is a way to simplify returned structures (well I can unwrap it at the begin of the sub..)?
Below my actual code.
use Marpa::R2; use Data::Dump; # resurces (along with ones on cpan): # http://marpa-guide.github.io/ # http://savage.net.au/Perl-modules/html/marpa.faq/faq.html # http://savage.net.au/Perl-modules/html/marpa.papers/ # https://github.com/choroba/marpa-enhanced-calculator # https://perlmaven.com/marpa-for-building-parsers # https://perlmaven.com/marpa-debugging my $dsl = <<'END_OF_DSL'; #:default ::= action => [values] :default ::= action => [name,values] lexeme default = latm => 1 Dice_Expression ::= Simple_Dice |Dice_with_modifier_x |Dice_with_modifier_r Dice_with_modifier_x ::= Simple_Dice 'x' Die_Modifier_Val action => mo +difier_x Dice_with_modifier_r ::= Simple_Dice 'r' Die_Modifier_Val action => mo +difier_r |Simple_Dice 'r' <Die_Modifier_Comp> Die_M +odifier_Val action => modifier_r Simple_Dice ::= Rolls 'd' Sides action => do_simple_roll Die_Modifier_Val ~ digits Die_Modifier_Comp ~ 'gt' | 'lt' Rolls ~ digits Sides ~ digits digits ~ [\d]+ :discard ~ whitespace whitespace ~ [\s]+ END_OF_DSL my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); my $input = $ARGV[0] // '6d4x1'; my $value_ref = $grammar->parse( \$input, 'My_Actions' ); print "\n\nFinal result: ";dd $value_ref; sub My_Actions::modifier_r{ print "modifier_r received: ";dd @_; } sub My_Actions::do_simple_roll { my ( undef, $rolls, undef, $sides ) = @_; print "do_simple_roll received: "; dd @_; my $res = []; map{ $die = 1+int(rand($sides)); print "\tRolled : $die\n"; push @$res, $die} 1..$rolls; my $return = { die_type => "1d$sides", rolls => $res}; print "do_simple_roll returning: "; dd $return; return $return; } sub My_Actions::modifier_x { my ( undef, $rolls_ref, $modifier, $modifier_val ) = @_; print "modifier_x received: "; dd @_; #dd ($rolls_ref,$modifier, $ +modifier_val ); my @descr = @{$rolls_ref->{rolls}}; # some roll need to be exploded while ( 0 < grep{ $_ =~ /^$modifier_val$/ }@descr ){ foreach my $roll( @descr ){ print "\tanalyzing: $roll\n"; if ( $roll == $modifier_val ){ $roll = $roll."x"; print "\t\texploding a die..\n"; my $new = $grammar->parse( \$rolls_ref->{die_type} +, 'My_Actions' ); print "\tdo_simple_roll returned: ";dd $new; push @descr, $$$new[1]->{rolls}->[0]; } } } my @numbers = map{ $_=~/(\d+)/; $1 }@descr; my $sum = 0; $sum += $_ for @numbers; my $return = { result => $sum, description => join ' ',@descr}; print "do_roll_with_die_modifier_x returning: "; dd $return; return $return; }
Thanks for reading
L*
PS January 16 2021 I published A dice roller system with Marpa::R2
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: First steps with Marpa::R2 and BNF
by duelafn (Parson) on Jan 13, 2021 at 15:01 UTC | |
by Discipulus (Canon) on Jan 15, 2021 at 12:55 UTC | |
by duelafn (Parson) on Jan 17, 2021 at 19:04 UTC | |
Re: First steps with Marpa::R2 and BNF
by choroba (Cardinal) on Jan 13, 2021 at 14:56 UTC | |
by Discipulus (Canon) on Jan 14, 2021 at 08:41 UTC | |
Re: First steps with Marpa::R2 and BNF
by GrandFather (Saint) on Jan 14, 2021 at 09:38 UTC | |
Re: First steps with Marpa::R2 and BNF
by GrandFather (Saint) on Jan 15, 2021 at 09:23 UTC | |
by Discipulus (Canon) on Jan 15, 2021 at 11:56 UTC | |
by GrandFather (Saint) on Jan 16, 2021 at 03:44 UTC | |
Re: First steps with Marpa::R2 and BNF
by GrandFather (Saint) on Jan 15, 2021 at 08:26 UTC |