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.

In reply to Re: Cannot get Marpa::R2 to prioritise one rule over another by Discipulus
in thread Cannot get Marpa::R2 to prioritise one rule over another by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.