Skip to content

Instantly share code, notes, and snippets.

@rns
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rns/df918056abc656eb0d01 to your computer and use it in GitHub Desktop.
Save rns/df918056abc656eb0d01 to your computer and use it in GitHub Desktop.
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 ::= '0b' BinDigits | '0x' HexDigits
HexDigits ::= [\dA-Fa-f]+
BinDigits ::= [01]+
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my $input = <<EOI;
0b0101
0x0f01
EOI
my $r = Marpa::R2::Scanless::R->new( { grammar => $g } );
$r->read(\$input);
say Dumper ${ $r->value() };
@jddurand
Copy link

jddurand commented Sep 2, 2014

Output:

$VAR1 = [
          'Numbers',
          [
            'Number',
            '0b',
            [
              'BinDigits',
              '0',
              '1',
              '0',
              '1'
            ]
          ],
          [
            'Number',
            '0x',
            [
              'HexDigits',
              '0',
              'f',
              '0',
              '1'
            ]
          ]
        ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment