Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View Significant whitespace?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#!/usr/bin/perl
# 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/.
 
# Example for blog post
 
use 5.010;
use strict;
use warnings;
 
use Test::More tests => 2;
 
use Marpa::R2;
 
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
 
my $dsl_scrambled = <<'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
 
for my $source ( $dsl, $dsl_scrambled ) {
my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
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';
 
Test::More::is( $value, 49, 'Landing page synopsis value' );
} ## end for my $source ( $dsl, $dsl_scrambled )
 
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;
}
 
# vim: expandtab shiftwidth=4:

Line 51: { source => \$source }

Owner

@Patterner: first, my apologies for the extremely slow response. For some reason, I didn't get a notification on your comment, and just happened to see it by chance. As to your technical point, yes, of course, you're right. I've made the correction. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.