We don't bite newbies here... much | |
PerlMonks |
Re^3: First steps with Marpa::R2 and BNFby duelafn (Parson) |
on Jan 17, 2021 at 19:04 UTC ( [id://11127040]=note: print w/replies, xml ) | Need Help?? |
Late responding, so you may have some of this figured out, but... default ::first: First is just a subroutine which returns the result of the first token. That is: sub ::first { return $_[1] }. Thus, Dice_Expression returns whatever Dice_Expression1 returns which returns whatever Simple_Dice returns which happens to be our "$self". After posting, I decided that if I were doing it, I would probably avoid using the default action and instead do something like:
The advantage being that this is explicit and gives a nice hook for modifying the final result just before returning it. Optionals For the most part, yes, I spell it out. The "*" syntax has a big limitation: The RHS alternative must consist of a single RHS primary. which means only "NAME ::= ITEM*" rules. You can't have multiple things on the right hand side. So, it would look like:
The star has to move to its own rule by itself (and then we renamed the token rule). Of course, that rule won't do what you want since it doesn't limit the number of "Die_Modifier_Comp_Toke". You could instead, use an empty rule to achieve a 0-or-1 match:
That seems to work, but requires "None" to be a "::=" rule, a "~" rule throws an error. I'm not sure if that means it is an abuse of syntax to do that or not, but if it works and doesn't seem to slow down the parsing, I'd say go for it. It will leave an undef in the Die_Modifier_Comp slot. Good Day,
In Section
Seekers of Perl Wisdom
|
|