Skip to content

Instantly share code, notes, and snippets.

@rns
Created July 13, 2015 04:34
Show Gist options
  • Save rns/d0ad3dea7dbead99e64d to your computer and use it in GitHub Desktop.
Save rns/d0ad3dea7dbead99e64d to your computer and use it in GitHub Desktop.
use 5.010;
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
$Data::Dumper::Deepcopy = 1;
use Marpa::R2;
my $g = Marpa::R2::Scanless::G->new( { source => \(<<'END_OF_SOURCE'),
:default ::= action => [ name, value ]
lexeme default = action => [ name, value ] latm => 1
data ::= heading tables
heading ::= string
tables ::= table+
table ::= row+
row ::= level name ':' value
level ~ '+' | '++' | '+++' | '++++'
name ::= string
value ::= number
value ::= string
value ::=
string ~ [\w\& ]+
number ~ [\d]+
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my $input = <<EOI;
Soccer
+Team:US
++Shirt:Red & White Stripes
++Shorts:Blue
++Players:17
+++Active:11
++++Forward:2
++++Midfield:4
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:1
++++Midfied:2
++++Defense:3
++++Goalkeeper:
+Team:Mexico
++Shirt:Green
++Shorts:White
++Players:17
+++Active:11
++++Forward:3
++++Midfield:3
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:2
++++Midfield:1
++++Defense:2
++++Goalkeeper:1
EOI
my $r = Marpa::R2::Scanless::R->new( {
max_parses => 10000,
grammar => $g,
trace_terminals => 0,
trace_values => 0,
} );
eval {$r->read(\$input)} || warn "# Parse failure, progress report is:\n" . $r->show_progress;
# nearly endless without max_parses
while ( defined( my $value_ref = $r->value() ) ) {
warn Dumper ${ $value_ref };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment