NAME
Grammar::Marpa - No-frills overload wrapper around Marpa::R2::Scanless
VERSION
Version 1.000
SYNOPSIS
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($dsl, 'M');
'1 + 2 * 3' =~ $g;
say $^R; # '7'
DESCRIPTION
This module provides a quick & dirty interface to the very basics of Marpa::R2's Scanless interface, including overloading the '=~' operator in a way that is only slightly inconvenient.
CONSTRUCTORS
Grammar::Marpa(GRAMMAR, PACKAGE)
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.
USAGE
'=~'
Once you have a grammar object, you can parse a string by performing
$string =~ $grammar;
$^R
Once you have parsed a string with a grammar object, the $^R variable will contain the value of the last parse.