How do I find the lhs of the match rule in my action sub?

Showing 1-5 of 5 messages
How do I find the lhs of the match rule in my action sub? rns 9/5/12 2:27 AM
subj

Perhaps any chance to access the matched rule's name in the action_object constructor?

Thanks in advance for this and the great work in Marpa!
Re: How do I find the lhs of the match rule in my action sub? Jeffrey Kegler 9/5/12 3:41 AM
The action_object constructor is called before the parse begins, so
there is no matched rule at the point at which it is called.  It's main
purpose is to allow creation and initialization of a customized
"per-parse-tree" variable.  The per-parse-tree variable is passed to
each action as its first argument, and can be used as a scratch pad, one
that is "global" to the parse tree.

-- jeffrey
Re: How do I find the lhs of the match rule in my action sub? rns 9/6/12 7:51 AM
Thanks for the explanation, I didn't read the doc attentively enough, sorry.

Why I am asking: with the rule name prepended to the value array passed to the action sub, do_what_I_mean as described in [1] would do a better job by producing parse trees in the form 

- rule_name
  - value 1
  - value 2
  ...

as in

- noun_nominative_masculine_single
  - example

I'm developing a grammar with many rules (case/gender/number forms of nouns modified by adjectives that are in turn modified by adverbs) and this would be useful for testing. Or so I think.

I'd use omit the actions in the grammar and use AUTOLOAD in the actions package, but AFAIK, Marpa resolves the action subs' names at compile time and this cannot be changed.

Re: How do I find the lhs of the match rule in my action sub? Jeffrey Kegler 9/6/12 9:01 AM
I looked into adding this feature, and it turns out that I've already implemented it.  It's just not documented.

In any action,  $Marpa::R2::Context::rule is the ID of the current rule.  The rule ID's can be translated into symbols with the Grammar rule() method, which is documented.  There is also a $Marpa::R2::Context::grammar variable, which can be used to find the grammar for the action.

There's a test for it in the test suite, and the test is doing pretty close to what you describe.  It can be used as an example.

Why hadn't I documented it?  Whenever I document a feature, I add a synopsis and whenever I add a synopsis, I make sure the accuracy of the synopsis is automatically checked as part of the test suite.  My development/build setup ensures that the snippets in the POD are all accurate and all run correctly.  I don't regret this, because IMHO it means my documentation is unusually accurate given the size, complexity, and rate of evolution of Marpa.  But it certainly makes documenting features take much more time.

A second reason I left it undocumented is that whenever I document something, I am to one degree or another committed to it.  I may have been wondering whether I should have a more extensive "Context" interface, use callbacks instead of variables, change the names, etc. etc.

As of this email you may consider $Marpa::R2::Context::rule and $Marpa::R2::Context::grammar part of the documented interface for Marpa::R2, 2.018000.  I'll add them to the official documentation in the next indexed release.

Thanks, jeffrey kegler
Re: How do I find the lhs of the match rule in my action sub? rns 9/6/12 10:17 PM
Just tried it and it works great, thanks a lot.

On a related note, this allows keeping all action code in one place by using "when/given" on the matched rule's lhs. This is probably less efficient but more readable.

BTW, I use Marpa::R2 (2.018000) with perl 5.010 on сygwin under winxp sp3 and it works great!

Thanks again.