ディレクティブ token

まんま

#!/usr/bin/env perl

use strict;
use Parse::RecDescent;
use Data::Dumper;

my $grammar =
q{
    lex   : token(s)                    { print Data::Dumper::Dumper($item[1]); }
    token :  /a\b/                      <token:INDEF>
          |  /the\b/                    <token:DEF>
          |  /fly\b/                    <token:NOUN,VERB>
          |  /[a-z]+/i { lc $item[1] }  <token:ALPHA>
          |  <error: Unknown token>
};

my $parse = new Parse::RecDescent ($grammar);

while (<>)
{
    defined $parse->lex($_) or print "Bad text!\n";
}

で、

a
$VAR1 = [
          {
            'text' => 'a',
            'type' => {
                        'INDEF' => 1
                      }
          }
        ];
the
$VAR1 = [
          {
            'text' => 'the',
            'type' => {
                        'DEF' => 1
                      }
          }
        ];
fly
$VAR1 = [
          {
            'text' => 'fly',
            'type' => {
                        'NOUN' => 1,
                        'VERB' => 2
                      }
          }
        ];
aa
$VAR1 = [
          {
            'text' => 'aa',
            'type' => {
                        'ALPHA' => 1
                      }
          }
        ];