Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Cannot get Marpa::R2 to prioritise one rule over another

by Discipulus (Canon)
on Jan 21, 2021 at 07:57 UTC ( [id://11127195]=note: print w/replies, xml ) Need Help??


in reply to Cannot get Marpa::R2 to prioritise one rule over another

Hello J.

UPDATE I probably missed your point.. now I see the IP is never reachead but always treated as if it was and hostname. I'll think a bit more about it..

The problem seems to be fixed if you put NAME            ~ [\D]+ but you will fail again with a hostname like 42.perl.org Perhaps a more strict rule definition is needed to tell difference from ip and hostname.

original reply My help can be very limited because I still do not understand Marpa::R2 and I'm just moving my first, baby steps. I dont understand the rank nor the show_progress part (atm). So I reduced the example to something I know (removing the colors).

Is not the hostname coming from your :default        ::= action => [name,values] ? This is what proposed in the synopsis but I find it a bit misleading.

If you see A dice roller system with Marpa::R2 and its prequel First steps with Marpa::R2 and BNF you will see an anonymous hash is used and is populated during the parsing phase. Maybe you can use a pattern like this (then you can check the validity of an IP or of a valid hostname in distinct part of the code).

I ended with the following code that seems to produce the expected result

#!/usr/bin/env perl use warnings; use strict; use Data::Dumper; use Marpa::R2; my $rules = <<'END_OF_GRAMMAR'; lexeme default = latm => 1 :default ::= action => [values] <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 "\nPARSING: $_\n"; my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar, }); my $value_ref = $grammar->parse( \$_); print Dumper $$value_ref; } } __OUTPUT__ PARSING: add 192.0.2.1 $VAR1 = [ [ 'add' ], [ [ '192.0.2.1' ] ] ]; PARSING: add www.example.org $VAR1 = [ [ 'add' ], [ [ 'www.example.org' ] ] ]; PARSING: remove 192.0.2.2 $VAR1 = [ [ 'remove' ], [ [ '192.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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11127195]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-26 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found