Skip to content

Instantly share code, notes, and snippets.

@jeffreykegler
Created December 19, 2017 02:59
Show Gist options
  • Save jeffreykegler/6dd71354652fb0ba3ef124eea6877fe6 to your computer and use it in GitHub Desktop.
Save jeffreykegler/6dd71354652fb0ba3ef124eea6877fe6 to your computer and use it in GitHub Desktop.
Marpa::R2 ranking longest high versus shortest high
#!perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Marpa::R2;
sub My_Actions::dwim {
shift;
return $_[0] if scalar @_ == 1;
return join '', '(', @_, ')';
}
# This grammar tries to be equivalent to a PEG or Regex with ordered choice:
# List: Item*
# Item: VAR = VAR // VAR = // VAR
# In order to force that "order", this grammar specifies "rank"s.
use constant BNF => <<'BNF';
:default ::= action => My_Actions::dwim
:discard ~ ws; ws ~ [\s]+
List ::= Item3 rank => 6
List ::= Item2 rank => 5
List ::= Item1 rank => 4
List ::= List Item3 rank => 3
List ::= List Item2 rank => 2
List ::= List Item1 rank => 1
Item3 ::= VAR '=' VAR rank => 3
Item2 ::= VAR '=' rank => 2
Item1 ::= VAR rank => 1
VAR ~ [\w]+
BNF
local $Data::Dumper::Terse = 3;
my $g = Marpa::R2::Scanless::G->new({ source => \BNF });
sub all_parses {
my ($input) = @_;
my $r = Marpa::R2::Scanless::R->new({ grammar => $g, ranking_method => 'high_rule_only' });
$r->read(\$input);
# say "Earley set: $_\n", $r->show_progress($_) for 1 .. 6;
say "Input: $input";
my $i = 0;
while (my $value_ref = $r->value()) {
$i++;
say "Parse $i: ", Data::Dumper::Dumper($value_ref);
}
return $i;
}
all_parses("a = b");
all_parses("a = b c");
all_parses("a = b c = d");
all_parses("a = b c = d e");
all_parses("a = b c = d e =");
all_parses("a = b c = d e = f");
#!perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Marpa::R2;
sub My_Actions::dwim {
shift;
return $_[0] if scalar @_ == 1;
return join '', '(', @_, ')';
}
# This grammar tries to be equivalent to a PEG or Regex with ordered choice:
# List: Item*
# Item: VAR = VAR // VAR = // VAR
# In order to force that "order", this grammar specifies "rank"s.
use constant BNF => <<'BNF';
:default ::= action => My_Actions::dwim
:discard ~ ws; ws ~ [\s]+
List ::= Item3 rank => 1
List ::= Item2 rank => 2
List ::= Item1 rank => 3
List ::= List Item3 rank => 4
List ::= List Item2 rank => 5
List ::= List Item1 rank => 6
Item3 ::= VAR '=' VAR rank => 1
Item2 ::= VAR '=' rank => 2
Item1 ::= VAR rank => 3
VAR ~ [\w]+
BNF
local $Data::Dumper::Terse = 3;
my $g = Marpa::R2::Scanless::G->new({ source => \BNF });
sub all_parses {
my ($input) = @_;
my $r = Marpa::R2::Scanless::R->new({ grammar => $g, ranking_method => 'high_rule_only' });
$r->read(\$input);
# say "Earley set: $_\n", $r->show_progress($_) for 1 .. 6;
say "Input: $input";
my $i = 0;
while (my $value_ref = $r->value()) {
$i++;
say "Parse $i: ", Data::Dumper::Dumper($value_ref);
}
return $i;
}
all_parses("a = b");
all_parses("a = b c");
all_parses("a = b c = d");
all_parses("a = b c = d e");
all_parses("a = b c = d e =");
all_parses("a = b c = d e = f");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment