Created
January 28, 2015 15:08
-
-
Save rns/85c6d18fb62065ae1617 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| Ambiguous parse: 5 alternatives. | |
| # Alternative 1: | |
| ['hex',['hex',['hex',['hex',['digit','0']],['hex',['digit','1']]],['hex',['letter','F']]],['hex',['letter','E']]] | |
| # Alternative 2: | |
| ['hex',['hex',['hex',['digit','0']],['hex',['hex',['digit','1']],['hex',['letter','F']]]],['hex',['letter','E']]] | |
| # Alternative 3: | |
| ['hex',['hex',['hex',['digit','0']],['hex',['digit','1']]],['hex',['hex',['letter','F']],['hex',['letter','E']]]] | |
| # Alternative 4: | |
| ['hex',['hex',['digit','0']],['hex',['hex',['hex',['digit','1']],['hex',['letter','F']]],['hex',['letter','E']]]] | |
| # Alternative 5: | |
| ['hex',['hex',['digit','0']],['hex',['hex',['digit','1']],['hex',['hex',['letter','F']],['hex',['letter','E']]]]] |
This file contains hidden or 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 = 0; | |
| $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 => [ name, value] latm => 1 | |
| #this is highly ambiguous: | |
| <hex> ::= <digit> | <letter> | <hex> <hex> | |
| #this is unambiguous: <hex> ::= <digit> | <letter> | <hex> <digit> | <hex> <letter> | |
| <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | |
| <letter> ::= 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | |
| :discard ~ whitespace | |
| whitespace ~ [\s]+ | |
| END_OF_SOURCE | |
| } ); | |
| my $input = <<EOI; | |
| 01FE | |
| EOI | |
| my $r = Marpa::R2::Scanless::R->new( { | |
| max_parses => 100, | |
| grammar => $g, | |
| } ); | |
| 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() ); | |
| # push @asts, ${ $v }; | |
| say "Ambiguous parse: ", $#asts + 1, " alternatives."; | |
| for my $i (0..$#asts){ | |
| say "# Alternative ", $i+1, ":\n", Dumper $asts[$i]; | |
| } | |
| } | |
| else{ | |
| say Dumper $ast; | |
| } |
This file contains hidden or 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
| ['hex',['hex',['hex',['hex',['digit','0']],['digit','1']],['letter','F']],['letter','E']] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment