Skip to content

Instantly share code, notes, and snippets.

@jeffreykegler
Created December 19, 2017 03:13
Show Gist options
  • Save jeffreykegler/099d09480b50977701bcf8268576785c to your computer and use it in GitHub Desktop.
Save jeffreykegler/099d09480b50977701bcf8268576785c to your computer and use it in GitHub Desktop.
Example of how naive simplifying of Marpa::R2 Grammar breaks rule ranking
#!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 ::= Item
List ::= List Item
Item ::= Item3 | Item2 | Item1
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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment