The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Marpa::R2 - Marpa Tutorial 2

Synopsis

    use Marpa::R2;

    my $dsl = <<'END_OF_DSL';
    :default ::= action => [name,values]
    lexeme default = latm => 1

    Expression ::= Term action => ::first
    Term ::=
          Factor action => ::first
        | Term '+' Term action => do_add
    Factor ::=
          Number action => ::first
        | Factor '*' Factor action => do_multiply
    Number ~ digits
    digits ~ [\d]+
    :discard ~ whitespace
    whitespace ~ [\s]+
    END_OF_DSL

    my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
    my $recce = Marpa::R2::Scanless::R->new(
        { grammar => $grammar, semantics_package => 'My_Actions' } );
    my $input = '42 * 1 + 7';
    $recce->read( \$input );

    my $value_ref = $recce->value;
    my $value = $value_ref ? ${$value_ref} : 'No Parse';

    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;
    }

UNDER CONSTRUCTION

This tutorial is under construction.

Description

Overview

This document contains a second tutorial of the Scanless interface (SLIF), which demonstrates a lower level of method calls. These lower level allow access to more of Marpa's features. As examples, users will use these lower level calls to use Marpa events; to examine the values of multiple parses of an ambiguous parse; and to get finer control of the response to Marpa errors.

This uses the same extremely simple calculator as the tutorial in the landing page. Must of the code is exactly the same in fact, and we will skip it, going straight to what is new.

Marpa::R2::Scanless::R::new

    my $recce = Marpa::R2::Scanless::R->new(
        { grammar => $grammar, semantics_package => 'My_Actions' } );

Marpa::R2::Scanless::R::new creates a new SLIF recognizer. Its arguments are references to hashes of named arguments. In this example the first named argument is the required argument: "grammar". The value of the grammar named argument must be a Marpa::R2 SLIF grammar.

The second argument is optional, but you will use it frequently. The "semantics_package" named argument tells Marpa in which Perl package to look for the closures implementing the semantics for this grammar. We will talk more about this below.

Marpa::R2::Scanless::R::read

    my $input = '42 * 1 + 7';
    $recce->read( \$input );

To parse a string, we use the Marpa::R2::Scanless::R::read() method. In its simplest form, as here, the Marpa::R2::Scanless::R::read() takes a reference to a string containing the input stream as its argument.

Marpa::R2::Scanless::R::value

    my $value_ref = $recce->value;
    my $value = $value_ref ? ${$value_ref} : 'No Parse';

The Marpa::R2::Scanless::R::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::Scanless::R::value() returns undef.

The value of the parse is exactly the same, and computed in exactly the same way, as in the previous tutorial.

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/.