Skip to content

Instantly share code, notes, and snippets.

@jddurand
Created September 2, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jddurand/59d2f01a62820619d335 to your computer and use it in GitHub Desktop.
Save jddurand/59d2f01a62820619d335 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() };
@jddurand
Copy link
Author

jddurand commented Sep 2, 2014

Output:

Setting trace_terminals option
Lexer "L0" accepted lexeme L1c1-6 e1: BinDigits; value="0b0101"
Lexer "L0" discarded lexeme L1c7: whitespace
Lexer "L0" accepted lexeme L2c1-6 e2: HexDigits; value="0x0f01"
Lexer "L0" discarded lexeme L2c7: whitespace
$VAR1 = [
          'Numbers',
          [
            'Number',
            [
              'BinDigits',
              '0b0101'
            ]
          ],
          [
            'Number',
            [
              'HexDigits',
              '0x0f01'
            ]
          ]
        ];

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