Parse::RecDescent始め
まだ、これを使うか決めてないんだけど、
実用Perlプログラミングに載ってたので、どんな感じか使ってみた。
use v5.14;
use strict;
use warnings;
use Parse::RecDescent;
my $parser = Parse::RecDescent->new( q{
Source : "(" Text Source(s?) ")" {
if ( @{$item[3]} ) {
$return = $item[2] . ' -> ' . $item[3][0];
}
else {
$return = $item[2];
}
}
Text : /[a-z]+/
} );
my $text = do { local $/; <DATA>; };
say $parser->Source( $text );
__DATA__
( aaa( bbb( ccc( ddd( eee( fff( ggg( hhh ) ) ) ) ) ) ) )
これを実行すると、こうなる。
$ perl aaa.pl
aaa -> bbb -> ccc -> ddd -> eee -> fff -> ggg -> hhh
こういうのをパースする予定はないんだけど、
こういうことも簡単に出来るって素敵デスね!
おしまい。
Leave a Comment