action => ::lhs

33 views
Skip to first unread message

Cev Ing

unread,
Nov 26, 2015, 12:25:44 PM11/26/15
to marpa parser
I am wondering if it is possible to define a LHS action. I mean something like this:

<create table> ::=
    (CREATE TABLE) identifier ('(') <table element list> (')' ';')
    action => ::lhs

This should mean call the action with the name of the rule. Later on it would be possible to define a default action for all rules:

:default ::= action => ::lhs

I also thought to define it myself, but I can not find a place where I can define what gets passed to an action. I seems to me that it is always the value. If I would be able to pass also the LHS to the action, I could do the dispatching myself.

Is it possible anyhow or do I have to write the action name in every rule:

<create table> ::=
    (CREATE TABLE) identifier ('(') <table element list> (')' ';')
    action => create_table

The reason why I want to use the 'action' attribute instead of 'bless' is, that I want to build in one pass ready to use Perl objects. If I use the 'bless' approach, I have two passes. The first builds a bunch of blessed arrays, which define the AST and after that I have to traverse the AST to build my Perl objects. When I use the actions to define the Perl objects, I can avoid the AST creation and the second pass.

Ruslan Shvedov

unread,
Nov 26, 2015, 1:18:19 PM11/26/15
to marpa-...@googlegroups.com
You can (1) bless each RHS alternative to specific packages, all of which must define the same action sub, e.g. doit() as in https://metacpan.org/pod/distribution/Marpa-R2/pod/Scanless.pod#Synopsis or (2) use action adverb to set the action subs explicitly and set a single semantics package for them as in https://github.com/jeffreykegler/Marpa--R2/blob/master/cpan/t/sl_gsyn.t

In either case, actions subs will be called at the evaluation phase and their return values will be used to build the parse value without explicit AST traversal. 

Ron Savage has recently written a good article on how to use action subs to build parse value --http://savage.net.au/Ron/html/Understanding.Marpa-style.action.subs.parameters.html
 

--
You received this message because you are subscribed to the Google Groups "marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email to marpa-parser...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeffrey Kegler

unread,
Nov 26, 2015, 5:53:26 PM11/26/15
to Marpa Parser Mailing LIst
The LHS is available to an action via context variables.  So you could write your own dispatcher and either make it the default action or apply it selectively.

Cev Ing

unread,
Nov 27, 2015, 3:41:52 AM11/27/15
to marpa parser

Am Donnerstag, 26. November 2015 23:53:26 UTC+1 schrieb Jeffrey Kegler:
The LHS is available to an action via context variables.  So you could write your own dispatcher and either make it the default action or apply it selectively.

Thanks. This action helps me getting warm with Marpa actions:

sub lhs {
  my ($lhs) = map {
    $Marpa::R2::Context::slg->symbol_display_form($_)
  } $Marpa::R2::Context::slg->rule_expand($Marpa::R2::Context::rule);
  return $lhs;
}

sub dump_and_exit
{
  my ($self, @args) = @_;
  print lhs, "\n", Dumper (@args);
  exit;
}

I use it gradually from the top to the bottom of the grammar. After reaching the bottom I can go back to the top by replacing each dump_and_exit by some useful code. Together with the default action [name,values] I think I can finish my task pretty fast.

Durand Jean-Damien

unread,
Jan 3, 2016, 6:41:31 AM1/3/16
to marpa parser
Yes, nice alternative.
I am using this in a framework on any BNF to automatically fill structures on-demand -;

Ron Savage

unread,
Jan 3, 2016, 4:31:54 PM1/3/16
to marpa parser
The LHS is available to an action via context variables.  So you could write your own dispatcher and either make it the default action or apply it selectively.

 
Reply all
Reply to author
Forward
0 new messages