Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found