Hello again,
maybe a second attempt is better than first one. I had to specify what an hostname is in an ugly way but seems viable.
I'm going mad to understand why the dot . is passed in for IPs and not for hostnames! (because ip ends with an action?)
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dump;
use Marpa::R2;
my $rules = <<'END_OF_GRAMMAR';
lexeme default = latm => 1
:default ::= action => ::first
entry ::= op hostaddr4 action => dump_entry
op ::= 'add' action => add_op
| 'remove' action => add_op
hostaddr4 ::= hostname | ipv4
hostname ::= DOMAIN EXT action => add_hostname
| DOMAIN DOMAIN EXT action => add_hostname
| DOMAIN DOMAIN DOMAIN EXT action => add_hostname
DOMAIN ::= NAME '.'
NAME ~ [\d\w]+
EXT ~ 'org' | 'net'
ipv4 ::= NUMBER '.' NUMBER '.' NUMBER '.' NUMBER action =>
+add_ip
NUMBER ~ [\d]+
:discard ~ SP
SP ~ [\s]+
END_OF_GRAMMAR
my $input = <<'END_OF_INPUT';
add example.org
add www.perl.org
add 42.perl.net
add 192.0.2.1
remove 192.0.2.2
END_OF_INPUT
my $grammar = Marpa::R2::Scanless::G->new({source => \$rules});
for (split /^/m, $input) {
chomp;
if (length $_) {
print "\nPARSING: $_\n";
my $recce = Marpa::R2::Scanless::R->new({
grammar => $grammar,
});
my $value_ref = $grammar->parse( \$_, 'main');
}
}
sub dump_entry{
print "dump_entry received: "; dd shift @_;
}
sub add_op{
my $self = shift @_;
print "add_op received: "; dd @_;
$$self{operator} = join '',@_;
return $self;
}
sub add_ip{
my $self = shift @_;
print "add_ip received: "; dd @_;
$$self{type} = 'IP';
$$self{value} = join '',@_;
return $self;
}
sub add_hostname{
my $self = shift @_;
print "add_hostname received: "; dd @_;
$$self{type} = 'hostname';
$$self{value} = join '.',@_;
return $self;
}
__DATA__
PARSING: add example.org
add_op received: "add"
add_hostname received: ("example", "org")
dump_entry received: { operator => "add", type => "hostname", value =>
+ "example.org" }
PARSING: add www.perl.org
add_op received: "add"
add_hostname received: ("www", "perl", "org")
dump_entry received: { operator => "add", type => "hostname", value =>
+ "www.perl.org" }
PARSING: add 42.perl.net
add_op received: "add"
add_hostname received: (42, "perl", "net")
dump_entry received: { operator => "add", type => "hostname", value =>
+ "42.perl.net" }
PARSING: add 192.0.2.1
add_op received: "add"
add_ip received: (192, ".", 0, ".", 2, ".", 1)
dump_entry received: { operator => "add", type => "IP", value => "192.
+0.2.1" }
PARSING: remove 192.0.2.2
add_op received: "remove"
add_ip received: (192, ".", 0, ".", 2, ".", 2)
dump_entry received: { operator => "remove", type => "IP", value => "1
+92.0.2.2" }
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.