Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Created January 3, 2013 12:13
Show Gist options
  • Save pstuifzand/4443047 to your computer and use it in GitHub Desktop.
Save pstuifzand/4443047 to your computer and use it in GitHub Desktop.
Another test program. The input string is a comma separated list of multi-digit integers. The output should be a `Dumper`ed array-ref. $VAR1 = [100,3,4]. Without the ":discard" rules I get an error about how it can only parse the first number "100" and stops at the ",".
use strict;
use Marpa::R2 2.038000;
use Data::Dumper;
my $grammar = Marpa::R2::Scanless::G->new(
{
action_object => 'My_Actions',
default_action => 'do_first_arg',
source => \(<<'END_OF_SOURCE'),
:start ::= Number
Number ::= number+ separator => comma action => do_list
number ~ [\d]+
comma ~ [,]
# This starts to work when I enable the 2 lines below
#:discard ~ whitespace
#whitespace ~ [\s]+
END_OF_SOURCE
}
);
sub my_parser {
my ( $grammar, $input_string ) = @_;
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
my $self = bless { grammar => $grammar }, 'My_Actions';
$self->{recce} = $recce;
local $My_Actions::SELF = $self;
$recce->read($input_string);
my $value_ref = $recce->value();
return ${$value_ref};
} ## end sub my_parser
my $value = my_parser($grammar, \"100,3,4");
print Dumper($value);
package My_Actions;
our $SELF;
sub new {return $SELF }
sub do_first_arg { shift; return shift; }
sub do_list { shift; return \@_; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment