Skip to content

Instantly share code, notes, and snippets.

@whitten
Forked from jddurand/number.pl
Created March 20, 2016 05:26
Show Gist options
  • Save whitten/10eca74e462029730392 to your computer and use it in GitHub Desktop.
Save whitten/10eca74e462029730392 to your computer and use it in GitHub Desktop.
MintberryCruNCH question on Marpa IRC
use 5.010;
use strict;
use warnings;
use Data::Dumper;
use Marpa::R2;
my $g = Marpa::R2::Scanless::G->new( {
bless_package => 'main',
source => \(<<'END_OF_SOURCE'),
:default ::= action => [name, values]
lexeme default = action => [name, value]
Numbers ::= Number+
Number ::= BinDigits | HexDigits
HexDigits ~ '0' 'x':i _HexDigitsRight
BinDigits ~ '0' 'b':i _BinDigitsRight
_HexDigitsRight ~ [0-9A-Fa-f]+
_BinDigitsRight ~ [01]+
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my $input = <<EOI;
0b0101
0x0f01
EOI
my $r = Marpa::R2::Scanless::R->new( { grammar => $g, trace_terminals => 1 } );
eval {$r->read(\$input)} || die "Parse failure, progress report is:\n" . $r->show_progress;
say Dumper ${ $r->value() };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment