I have a Marpa::R2 parser that is attempting to differentiate between IP addresses and hostnames without a difference in leading keywords. The actual grammar I am using is complicated enough not to try and replicate it here, but a minimally-reproducable example of the same problem is below:

#!/usr/bin/env perl use warnings; use strict; use Data::Dumper; use Term::ANSIColor qw(:constants); use Marpa::R2; my $rules = <<'END_OF_GRAMMAR'; lexeme default = latm => 1 :default ::= action => [name,values] :start ::= <entry> <entry> ::= <op> (SP) <hostaddr4> <op> ::= 'add' | 'remove' <ipv4> ::= NUMBER ('.') NUMBER ('.') NUMBER ('.') NUMBER <hostname> ::= NAME <hostaddr4> ::= <ipv4> | <hostname> SP ~ [\s]+ NAME ~ [\S]+ NUMBER ~ [\d]+ END_OF_GRAMMAR my $input = <<'END_OF_INPUT'; add 192.0.2.1 add www.example.org 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 "\n\n$_\n"; my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar, ranking_method => 'rule' }); eval { $recce->read(\$_ ) }; print ($@ ? (RED . "$@\n") : GREEN); print $recce->show_progress(), "\n"; print Dumper($recce->value), "\n\n", RESET; } }

From what I can tell, Marpa always picks the <hostname> form of the grammar, even on lines that look more like IPs. I assume this is because the character class [\S]+ also includes the characters which make up an IP address.

So far, in my grammar definition, I've tried:

<hostaddr4> ::= <ipv4> | <hostname> <hostaddr4> ::= <ipv4> || <hostname> <hostaddr4> ::= <hostname> | <ipv4> <hostaddr4> ::= <hostname> || <ipv4> <hostaddr4> ::= <ipv4> rank => 2 | <hostname> rank => 1 <hostaddr4> ::= <ipv4> rank => 1 | <hostname> rank => 2 <hostaddr4> ::= <ipv4> rank => 1 <hostaddr4> ::= <hostname> rank => 2 <hostaddr4> ::= <hostname> rank => 1 <hostaddr4> ::= <ipv4> rank => 2

...and none seem to make a difference. They all yield the ['hostname', '192.0.2.1'] array.

The only thing that does it is removing the <hostname> alternate from <hostaddr4> (which does not match the grammar of the data I am parsing), and then the representation changes to ['ipv4', '192', '0', '2', '1']

Can anyone advise the correct approach in this (seemingly) simple case?

J.

In reply to 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.