http://qs1969.pair.com?node_id=11127191

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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.