Last active
August 29, 2015 14:10
-
-
Save rns/d4f96e19c740b1dcb92f to your computer and use it in GitHub Desktop.
This file contains 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
$VAR1 = \[ | |
'S', | |
[ | |
'Id', | |
[ | |
'Identifier', | |
'ABC' | |
] | |
] | |
]; | |
$VAR1 = \[ | |
'S', | |
[ | |
'Id', | |
[ | |
'Identifier', | |
'ABC' | |
] | |
], | |
[ | |
'NumLiteral', | |
'42' | |
] | |
]; | |
$VAR1 = \[ | |
'S', | |
[ | |
'AND keyword', | |
'AND' | |
], | |
[ | |
'NumLiteral', | |
'1' | |
] | |
]; | |
This file contains 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
#!/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