Skip to content

Instantly share code, notes, and snippets.

@rns
Last active August 29, 2015 14:10
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/d4f96e19c740b1dcb92f to your computer and use it in GitHub Desktop.
Save rns/d4f96e19c740b1dcb92f to your computer and use it in GitHub Desktop.
$VAR1 = \[
'S',
[
'Id',
[
'Identifier',
'ABC'
]
]
];
$VAR1 = \[
'S',
[
'Id',
[
'Identifier',
'ABC'
]
],
[
'NumLiteral',
'42'
]
];
$VAR1 = \[
'S',
[
'AND keyword',
'AND'
],
[
'NumLiteral',
'1'
]
];
#!/usr/bin/perl
use 5.010;
use warnings;
use strict;
use Marpa::R2;
my $grammar = 'Marpa::R2::Scanless::G'->new(
{ bless_package => 'main',
source => \( << '__GRAMMAR__'),
:default ::= action => [ name, value]
lexeme default = action => [ name, value] latm => 1
S ::= Id
| Id NumLiteral
| <AND keyword> NumLiteral
Id ::= Identifier
Identifier ~ IdentifierHeadChar IdentifierBody
IdentifierBody ~ IdentifierBodyChar*
IdentifierHeadChar ~ [a-zA-Z_]
IdentifierBodyChar ~ [a-zA-Z0-9_]
NumLiteral ~ [0-9]+
<AND keyword> ~ 'AND'
:lexeme ~ <AND keyword> priority => 1
:discard ~ whitespace
whitespace ~ [\s]+
__GRAMMAR__
});
use Data::Dumper;
for my $input ('ABC', 'ABC 42', 'AND 1') {
say Dumper $grammar->parse(\$input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment