Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use Marpa::R2;
# 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.
# This seems to fail because ranks are not evaluated left-to-right?
use constant BNF => <<'BNF';
:default ::= bless => ::lhs
:discard ~ ws; ws ~ [\s]+
List ::=
List ::= List2
List2 ::= List1 rank => 1
List2 ::= List1 Item
List1 ::= Item rank => 1
List1 ::= Item Item
Item ::= VAR '=' VAR
Item ::= VAR '='
Item ::= VAR
VAR ~ [\w]+
BNF
my $g = Marpa::R2::Scanless::G->new({ source => \BNF, bless_package => 'main' });
local $Data::Dumper::Terse = 1;
sub all_parses {
my ($input) = @_;
my $r = Marpa::R2::Scanless::R->new({ grammar => $g, ranking_method => 'high_rule_only' });
$r->read(\$input);
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");
# expected: List(Item(a = b))
# actual 1: List(Item(a = b))
# actual 2: List(Item(a =) Item(b))
all_parses("a = b c");
# expected: List(Item(a = b) Item(c))
# actual 1: List(Item(a = b) Item(c))
# actual 2: List(Item(a =) Item(b) Item(c))
all_parses("a = b c = d");
# expected: List(Item(a = b) Item(c = d))
# actual 1: List(Item(a = b) Item(c = d))
# actual 2: List(Item(a =) Item(b) Item(c = d)
# but not this one??? List(Item(a = b) Item(c =) Item(d)) ??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment