Paul W Bennett > Grammar-Marpa-2.000 > Grammar::Marpa

Download:
Grammar-Marpa-2.000.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 2.000   Source  

NAME ^

Grammar::Marpa - Overload wrapper around Marpa::R2::Scanless

VERSION ^

Version 2.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->new({ source => $dsl, semantics_package => 'M');

    '1 + 2 * 3' =~ $g;

    say $^R; # '7'

DESCRIPTION ^

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.

CONSTRUCTORS ^

Grammar::Marpa->new()

PREFERRED: Create a new grammar object, based on the provided arguments, which take one of the following forms:

GRAMMAR, PACKAGE

Specify the source EBNF and the Semantics Package. If no Semantics Package is provided, the Semantics must provided by the calling package.

ARGS

Hash or hashref containing arguments for both the Grammar and the Recognizer objects that are contained inside this object. Valid keys include (inter alia):

bless_package
semantics_package
trace_file_handle
trace_terminals
trace_values

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.

Grammar::Marpa(GRAMMAR, PACKAGE)

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.

USAGE ^

Parsing

Once you have a grammar object, you can parse a string by performing either of

    $string =~ $grammar;

    $value = $grammar->parse($string);

Getting the result

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.

DIFFERENCES FROM v1.000 ^

The previous version of this module would Carp::confess() if no parse was found.

The current version simply returns implicit undef.

syntax highlighting: