Created
January 28, 2015 15:37
-
-
Save rns/176632ab5b2652900871 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
[ | |
'S', | |
[ | |
'b' | |
], | |
[ | |
'A', | |
[ | |
'd' | |
] | |
], | |
[ | |
'c' | |
] | |
] |
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
use 5.010; | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
$Data::Dumper::Indent = 1; | |
$Data::Dumper::Terse = 1; | |
$Data::Dumper::Deepcopy = 1; | |
use Carp::Always; # force stack trace | |
use Marpa::R2; | |
my $g = Marpa::R2::Scanless::G->new( { source => \(<<'END_OF_SOURCE'), | |
:default ::= action => [ name, value] | |
lexeme default = action => [ value] latm => 1 | |
S ::= A a | |
S ::= b A c | |
S ::= d c | |
S ::= b d a | |
A ::= d | |
# terminals | |
a ~ 'a' b ~ 'b' c ~ 'c' d ~ 'd' | |
:discard ~ whitespace | |
whitespace ~ [\s]+ | |
END_OF_SOURCE | |
} ); | |
my $input = <<EOI; | |
bdc | |
EOI | |
my $r = Marpa::R2::Scanless::R->new( { | |
grammar => $g, | |
# trace_terminals => 1 | |
} ); | |
eval { $r->read(\$input) } || warn "$@\nProgress report is:\n" . $r->show_progress; | |
my $ast = $r->value; | |
unless (defined $ast){ | |
die "No parse"; | |
} | |
if ( $r->ambiguity_metric() > 1 ){ | |
# gather parses | |
my @asts; | |
my $v = $ast; | |
do { | |
push @asts, ${ $v }; | |
} until ( not $v = $r->value() ); | |
say "Ambiguous parse: ", $#asts + 1, " alternatives."; | |
for my $i (0..$#asts){ | |
say "# Alternative ", $i+1, ":\n", Dumper $asts[$i]; | |
} | |
} | |
else{ | |
say Dumper ${$ast}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment