#!/usr/bin/env perl use warnings; use strict; use Data::Dumper::Concise; use Term::ANSIColor qw(:constants); use Marpa::R2; package main; my $rules = <<'END_OF_GRAMMAR'; lexeme default = latm => 1 :default ::= action => [name,values] :start ::= ::= 'foo' (SP) | 'bar' (SP) | 'baz' (SP) ::= NUMBER ('.') NUMBER ('.') NUMBER ('.') NUMBER ::= NAMECH+ separator => DOT ::= | SP ~ [\s]+ DOT ~ '.' NAMECH ~ [^\s.:]+ NUMBER ~ [\d]+ END_OF_GRAMMAR my $input = <<'END_OF_INPUT'; foo 192.0.2.1 foo www.example.org bar 192.0.2.2 bar 3.2.0.192.in-arpa.net baz 192.0.2 baz flibble.example.com 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', semantics_package => 'main'}); eval { $recce->read(\$_ ) }; print(($@ ? (RED . "$@\n") : GREEN), $recce->show_progress(), "\n", Dumper($recce->value), "\n\n", RESET); } } #### \[ "entry", "foo", [ "hostaddr4", [ "ipv4", 192, 0, 2, 1, ], ], ] ... \[ "entry", "foo", [ "hostaddr4", [ "hostname", "www", "example", "org", ], ], ] ... #### ::= NUMBER ('.') NUMBER ('.') NUMBER ('.') NUMBER action => joindot ::= NAMECH+ separator => DOT action => joindot ... sub joindot { shift, join '.', @_ } #### \[ "entry", "foo", [ "hostaddr4", "192.0.2.1", ], ] ... \[ "entry", "foo", [ "hostaddr4", "www.example.org", ], ] #### ::= NUMBER ('.') NUMBER ('.') NUMBER ('.') NUMBER action => joinipv4 ::= NAMECH+ separator => DOT action => joinhostname ... sub joindot { join '.', @_ } sub joinipv4 { shift, ["ipv4", (joindot @_)] } sub joinhostname { shift, ["hostname", (joindot @_)] }