Created
December 18, 2017 00:20
-
-
Save jeffreykegler/0a589188553530c5a54d794c4136c271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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