Grammar::Marpa - Regexp-overloading wrapper around Marpa::R2::Scanless
Version 2.004
use Grammar::Marpa; my $dsl = <<'END_OF_DSL'; :default ::= action => ::first :start ::= Expression Expression ::= Term Term ::= Factor | Term '+' Term action => do_add Factor ::= Number | Factor '*' Factor action => do_multiply Number ~ digits digits ~ [\d]+ :discard ~ whitespace whitespace ~ [\s]+ END_OF_DSL sub M::do_add { my (undef, $t1, undef, $t2) = @_; return $t1 + $t2; } sub M::do_multiply { my (undef, $t1, undef, $t2) = @_; return $t1 * $t2; } my $g = Grammar::Marpa->new({ source => $dsl, semantics_package => 'M'); '1 + 2 * 3' =~ $g; say $^R; # '7'
This module provides a quick & dirty interface to Marpa::R2's Scanless interface, including overloading the '=~' operator in a way that is only slightly inconvenient.
PREFERRED: Create a new grammar object, based on the provided arguments, which take one of the following forms:
Specify the source EBNF and the Semantics Package. If no Semantics Package is provided, the Semantics must provided by the calling package.
Hash or hashref containing arguments for both the Grammar and the Recognizer objects that are contained inside this object. Valid keys include (inter alia):
See the appropriate documentation for Marpa::R2::Scanless to understand the meanings of those arguments.
You may also combine the GRAMMAR argument, the optional PACKAGE argument, and any additional options as a hash or hashref.
DEPRECATED: Create a new grammar object, based on a well-formed SLIF EBNF grammar (which must be provided as a scalar string) and using the specified package to provide the semantics of the grammar.
Once you have a grammar object, you can parse a string by performing either of
$string =~ $grammar; $value = $grammar->parse($string);
If you have parsed a string with the '=~' overload, the $^R variable will contain the value of the last parse.
If you parse a string usintg the parse() method, the return value will be the value of that parse.
The previous version of this module would Carp::confess() if no parse was found.
The current version simply returns implicit undef.
Artistic 2.0
To install Grammar::Marpa, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Grammar::Marpa
CPAN shell
perl -MCPAN -e shell install Grammar::Marpa
For more information on module installation, please visit the detailed CPAN module installation guide.