Anonymous subs in rule arrays/hashes?

Showing 1-3 of 3 messages
Anonymous subs in rule arrays/hashes? rns 9/8/12 4:53 AM
I was writing several action subs and had to copy and paste the rule descriptions to (1) have them before my eyes to figure out what to do in the action and (2) make the action subs  consistent with the rules (symbols <=> variable names). So _subj_ would make Marpa a bit easier to use for those with relatively many actions to write.

Like this:

[ 'lhs', [qw(sym1 sym2), sub { 
    shift;
    my ($sym1, $sym2) = @_;
    ...
}]

[ lhs => 'lhs', rhs => [qw(sym1 sym2 ...), action => sub { 
    shift;
    my ($sym1, $sym2) = @_;
    ...
}]

I understand that this had already been provided by closures option of Recognizer as 

{ $action_name => $code_ref } 

hash so unless I'm missing something closures hash can be generated automatically as 

{ "$lhs_action" => $code_ref_from_rule_decription }.

AFAIK, such thing is fairly standard e.g. FWIW, XML::Twig and others use this syntax.

What do you think? I'd volunteer to make a patch if there is nothing that makes the feature undesirable.
Re: Anonymous subs in rule arrays/hashes? Jeffrey Kegler 9/8/12 9:44 AM
In some of the logic, I need to determine if two semantics are "the same".  Comparing semantics is straightforward when they are in the form of actions names (Perl strings).  Alternatives, such as code references, quickly get me into matters of definition, and I found it easiest and safest to stick to action names.

Not to say that this matter can't be approached differently.  You can create your own interface to Marpa::R2, perhaps in the form of a DSL.  (I take this route myself.  Note that when I added precedence parsing to Marpa, I did not add it to the Marpa::R2 interface -- instead I wrote a DSL.)  Alternative interfaces to Marpa can go through the Marpa::R2::Thin interface, so they don't even need to pay a performance penalty -- they might in fast be faster than Marpa::R2.

-- jeffrey
Re: Anonymous subs in rule arrays/hashes? rns 9/8/12 9:55 PM
On Saturday, September 8, 2012 7:44:59 PM UTC+3, Jeffrey Kegler wrote:
In some of the logic, I need to determine if two semantics are "the same".  Comparing semantics is straightforward when they are in the form of actions names (Perl strings).  Alternatives, such as code references, quickly get me into matters of definition, and I found it easiest and safest to stick to action names.
This makes things clear. 

Not to say that this matter can't be approached differently.  You can create your own interface to Marpa::R2, perhaps in the form of a DSL.  (I take this route myself.  Note that when I added precedence parsing to Marpa, I did not add it to the Marpa::R2 interface -- instead I wrote a DSL.)  Alternative interfaces to Marpa can go through the Marpa::R2::Thin interface, so they don't even need to pay a performance penalty -- they might in fast be faster than Marpa::R2.
Sounds like a viable alternative. 

Thanks a lot for explaining.