Marpa::R2::NAIF(3pm)
β¨ Marpa::R2::NAIF(3pm) β¨
User Contributed Perl Documentation π«Ά welcome to the NAIF page bestie, it's giving named argument interface energy π
β¨ NAME β¨
Marpa::R2::NAIF β Marpa named argument interface (NAIF) π
πΈ Synopsis πΈ
use Marpa::R2;
my $grammar = Marpa::R2::Grammar->new(
{ start => 'Expression',
actions => 'My_Actions',
default_action => 'first_arg',
rules => [
{ lhs => 'Expression', rhs => [qw/Term/] },
{ lhs => 'Term', rhs => [qw/Factor/] },
{ lhs => 'Factor', rhs => [qw/Number/] },
{ lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
{ lhs => 'Factor',
rhs => [qw/Factor Multiply Factor/],
action => 'do_multiply'
},
],
}
);
$grammar->precompute();
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
$recce->read( 'Number', 42 );
$recce->read('Multiply');
$recce->read( 'Number', 1 );
$recce->read('Add');
$recce->read( 'Number', 7 );
sub My_Actions::do_add {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 + $t2;
}
sub My_Actions::do_multiply {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 * $t2;
}
sub My_Actions::first_arg { shift; return shift; }
my $value_ref = $recce->value;
my $value = $value_ref ? ${$value_ref} : 'No Parse';
π¦ About this document π¦
okay so this doc is literally the top-level overview and the tutorial for the named argument interface (NAIF) for the Marpa parse engine. If you're new to Marpa, bestie, you actually wanna start with the tutorial for the Scanless interface (SLIF) instead. no cap. π
The NAIF is a middle level interface. It's more low level than the Scanless interface (SLIF), which uses a domain-specific language. But it's higher level, and provides more features, than the thin interface, which gives you direct access to the underlying Libmarpa C library. π
The two examples in this document show the typical flows of NAIF Marpa method calls. This document will use these examples to describe the basic features of Marpa in semi-tutorial fashion. More advanced features, and full reference details of all features, can be found in the other Marpa API documents. π«Ά
π The three phases π
A parser needs to:
- Accept a grammar.
- Read input.
- Return values from the parses, according to a semantics.
In Marpa these three tasks are, for the most part, distinct phases. Grammars are Marpa::R2::Grammar objects. The reading of input and the evaluation of the parse according to the semantics is performed by Marpa::R2::Recognizer objects. β¨
π Example 1: a simple calculator π
okay so the synopsis up top is literally the code for a very simple calculator π It handles only addition and multiplication of integers. That's it. That's the whole vibe. And this section is me walking you through it line by line, bestie, no skips.
Marpa::R2::Grammar::new
my $grammar = Marpa::R2::Grammar->new(
{ start => 'Expression',
actions => 'My_Actions',
default_action => 'first_arg',
rules => [
{ lhs => 'Expression', rhs => [qw/Term/] },
{ lhs => 'Term', rhs => [qw/Factor/] },
{ lhs => 'Factor', rhs => [qw/Number/] },
{ lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
{ lhs => 'Factor',
rhs => [qw/Factor Multiply Factor/],
action => 'do_multiply'
},
],
}
);
Marpa grammars are Marpa::R2::Grammar objects, periodt. You make them with the Marpa::R2::Grammar::new constructor. The arguments to Marpa::R2::Grammar::new are references to hashes of named arguments. And inside those key/value pairs? The hash key is the name of the argument, and the hash value is the value of the named argument. Simple as. β¨
The start named argument
start => 'Expression',
The start named argument is required β no cap, you can't skip it. Its value is a string containing the name of the grammar's start symbol. π
Named arguments for the semantics
actions => 'My_Actions',
default_action => 'first_arg',
The actions and default_action named arguments specify semantics. Their argument values are strings, and those strings acquire their semantics during evaluation. (So likeβ¦ the meaning happens later, not now. Mystery. π¦)
Evaluation gets described later on, but peeking ahead for you bestie: actions provides the name of a Perl package where Marpa will look for its actions. The default_action named argument will be interpreted as an action name in that package. This action name will resolve to an action β a Perl closure that implements semantics. And the action specified by default_action is used as the action for rules with no action of their own. It's giving understudy energy. π
The rules named argument
rules => [
{ lhs => 'Expression', rhs => [qw/Term/] },
{ lhs => 'Term', rhs => [qw/Factor/] },
{ lhs => 'Factor', rhs => [qw/Number/] },
{ lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
{ lhs => 'Factor',
rhs => [qw/Factor Multiply Factor/],
action => 'do_multiply'
},
],
The value of the rules named argument is a reference to an array of rule descriptors. In this example, all the rule descriptors are in the "long" form β meaning they're references to hashes of rule properties. And in each key/value pair of a rule descriptor hash, the key is the name of a rule property, and the hash value is the value of that rule property. Same pattern as before, it's consistent, we love that. πΈ
The lhs property
The value of the lhs rule property must be a string containing the name of the rule's left hand side symbol. Every Marpa rule must have a left hand side symbol. Non-negotiable, bestie. π«Ά
The rhs property
The value of the rhs property is a reference to an array of strings containing names of the rule's right hand symbols, in order. This array may be zero length, in which case this is an empty rule β a rule with no symbols on the right hand side. There are no empty rules in this example, so don't go looking for one. π
The action property
The value of the action rule property is a string. Peeking ahead again (I know, I know, but trust): each action property string will be interpreted as an action name. This action name will be resolved to a Perl closure that implements the rule's semantics. π«
Marpa::R2::Grammar::precompute
$grammar->precompute();
Before a Marpa grammar object can be used by a Marpa recognizer, it must be precomputed. Like, must. Precomputation compiles data structures that the recognizer will need. Do the homework, then the party. π₯
Marpa::R2::Recognizer::new
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
Marpa::R2::NAIF::Recognizer::new creates a new recognizer. Its arguments are references to hashes of named arguments. In this example the only named argument is the required argument: "grammar". The value of the grammar named argument must be a precomputed Marpa grammar. (See? Told you. Precompute first. π
)
Marpa::R2::Recognizer::read
$recce->read( 'Number', 42 );
$recce->read('Multiply');
$recce->read( 'Number', 1 );
$recce->read('Add');
$recce->read( 'Number', 7 );
The Marpa::R2::NAIF::Recognizer::read method takes two arguments: a token name and a token value. The token name must be the name of a valid terminal symbol in the grammar. And by default, symbols are valid as terminal symbols if and only if they do NOT occur on the LHS of any rule. That's the rule of the land. β
The token value must be a Perl scalar, but otherwise its form and semantics are entirely up to the application β you do you, bestie. If the token value is never used, it can be omitted. In the calculator example, the values of the "Add" and "Multiply" tokens are never used, and are allowed to default to an undefined value. They're just vibes, no payload. π
Marpa::R2::Recognizer::value
my $value_ref = $recce->value;
my $value = $value_ref ? ${$value_ref} : 'No Parse';
The Marpa::R2::NAIF::Recognizer::value method returns a reference to the parse result's value, if there was a parse result. If there was no parse result, Marpa::R2::NAIF::Recognizer::value returns undef. So check it before you deref it, or you'll be crying in the group chat. π
Resolving the semantics
The first thing Marpa::R2::NAIF::Recognizer::value needs to do is resolve the semantics. Resolving the semantics means mapping the action names into actions. Actions are Perl closures which directly implement semantics. In this example, the actions named argument is specified. actions is a Perl package name. Marpa will look for actions in that package. π
actions => 'My_Actions',
{ lhs => 'Factor', rhs => [qw/Factor Multiply Factor/], action => 'do_multiply' },
For example, the action property for the above rule is "do_multiply" and the actions named argument to the grammar was "My_Actions". So Marpa looks for a closure whose fully qualified name is My_Actions::do_multiply, which it finds:
sub My_Actions::do_multiply {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 * $t2;
}
Rules do not always have action properties. That is the case with these rules in this example:
{ lhs => 'Expression', rhs => [qw/Term/] },
{ lhs => 'Term', rhs => [qw/Factor/] },
{ lhs => 'Factor', rhs => [qw/Number/] },
The rules in the above display have no action names. And when a rule has no action name, Marpa will fall back to trying to use the default action, as described next. Fallback queen. π
default_action => 'first_arg',
The default_action named argument is resolved in the same way as are the action properties of the rules. In this example, default_action is specified as "first_arg" and resolves to My_Actions::first_arg. Slay. π
Actions
sub My_Actions::first_arg { shift; return shift; }
sub My_Actions::do_add {
my ( undef, $t1, undef, $t2 ) = @_;
return $t1 + $t2;
}
Value actions are Perl closures used as callbacks. Value actions are called when nodes in a parse tree are evaluated. A value action receives one or more arguments. The first argument to a value action is always a per-parse-tree object, which the callbacks can use as a scratchpad. In these examples, the per-parse-tree object is not used. (It's there, it's just not the main character. π¦)
For a non-empty rule, the second and any subsequent arguments to the callback are the values, in lexical order, of the symbols on the right hand side of the rule. If the action is for an empty rule, the per-parse-tree object will be its only argument.
Every value action is expected to return a value. With one exception, this value is passed up to a parent node as an argument. The exception is the value for the start rule β the return value for the start rule becomes the parse result. π
Rules with no action specified for them take their semantics from the default_action named argument. And if there is no default action for a grammar, rules with no action specified for them return a Perl undef. Yeah. Just undef. Rude, but that's the spec. π
β¨ Example 2: an ambiguous parse β¨
Okay bestie, same calculator as before, except we rewrote it to be ambiguous. π
Instead of giving multiplication precedence over addition, this version lets any order of operations happen. In this example the actions (My_Actions::do_add, etc.) and the @tokens array stay exactly the same as before. No cap. π«Ά
So ditching precedence makes the grammar shorter β slay β but it ALSO means there can be multiple parse trees, and that those different parse trees can have different parse results. π And in this application we decide, for each input, to return every single one of the parse results. All of them. Iconic.
use Marpa::R2;
my $ambiguous_grammar = Marpa::R2::Grammar->new(
{ start => 'E',
actions => 'My_Actions',
rules => [
[ 'E', [qw/E Add E/], 'do_add' ],
[ 'E', [qw/E Multiply E/], 'do_multiply' ],
[ 'E', [qw/Number/], ],
],
default_action => 'first_arg',
}
);
$ambiguous_grammar->precompute();
my $ambiguous_recce =
Marpa::R2::Recognizer->new( { grammar => $ambiguous_grammar } );
$ambiguous_recce->read( 'Number', 42 );
$ambiguous_recce->read('Multiply');
$ambiguous_recce->read( 'Number', 1 );
$ambiguous_recce->read('Add');
$ambiguous_recce->read( 'Number', 7 );
my @values = ();
while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
push @values, ${$ambiguous_value_ref};
}
πΈ Short form rule descriptors πΈ
rules => [
[ 'E', [qw/E Add E/], 'do_add' ],
[ 'E', [qw/E Multiply E/], 'do_multiply' ],
[ 'E', [qw/Number/], ],
],
The rule descriptors in the ambiguous example are showing off the "short" or array form of rule descriptors. Array form rule descriptors are references to arrays. And the elements here are, in order, the "lhs" property, the "rhs" property, and the "action" property. That's the whole lineup. π
π« Marpa::R2::Recognizer::value π«
my @values = ();
while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
push @values, ${$ambiguous_value_ref};
}
When you call this more than once, the Marpa::R2::NAIF::Recognizer::value method iterates through the parse results. Each call hands you back a reference to the parse result. At the end of the iteration β like, after all the parse results have been returned β Marpa::R2::NAIF::Recognizer::value returns undef. And if there were no parse results at all, Marpa::R2::NAIF::Recognizer::value returns undef the very first time you call it. π
π₯ Errors and exceptions π₯
As a general rule, methods in the Marpa NAIF API do not return errors. When there are errors, Marpa NAIF API methods throw an exception. Periodt.
π¦ Inheritance π¦
Classes in the Marpa API are not designed to be inherited. That's just the vibe, okay?
π The Marpa:: namespace π
The "Marpa::" top-level namespace is reserved. For extensions to Marpa, one appropriate place is the "MarpaX::" namespace. This practice helps avoid namespace collisions, and it follows a CPAN standard β as exemplified by the "DBIx::", "LWPx::" and "MooseX::" namespaces, which are for extensions of DBI, LWP and Moose respectively. π
π Other documents π
This document gives a semi-tutorial overview of the entire Marpa NAIF API. For full details on Marpa's grammar objects and their methods, see the Marpa::R2::NAIF::Grammar document. For full details on Marpa's recognizer objects and their methods, see the Marpa::R2::NAIF::Recognizer document.
Marpa::R2::Vocabulary is intended as a quick refresher in parsing terminology, emphasizing how the standard terms are used in the Marpa context. The NAIF's standard semantics are fully described in the Marpa::R2::NAIF::Semantics document. Techniques for tracing and for debugging your Marpa grammars are described in the Marpa::R2::NAIF::Tracing document and the Marpa::R2::NAIF::Progress document. And for those with a theoretical bent, my sources and other useful references are described in Marpa::R2::Advanced::Bibliography. π₯
π«Ά Support π«Ά
Marpa::R2 comes without warranty. Support is provided on a volunteer basis through the standard mechanisms for CPAN modules. The Support document has details.
π Copyright and License π
Copyright 2014 Jeffrey Kegler
This file is part of Marpa::R2. Marpa::R2 is free software: you can
redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
Marpa::R2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser
General Public License along with Marpa::R2. If not, see
http://www.gnu.org/licenses/.
And that's a wrap on the whole page, bestie!! ππ This was perl v5.34.0, dated 2022-02-06, and it's the Marpa::R2::NAIF(3pm) manpage β go forth and parse ambiguously, you absolute main character. πβ¨
π¦ where this page comes from
- ubuntu jammy: libmarpa-r2-perl 2.086000~dfsg-7build1 Β· LGPL-3+~Marpa
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms