1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #!/usr/bin/perl
use strict; use warnings;
use Parse::RecDescent;
my $grammar = q {
line: statement(s /, */) /\n?/ { $return = [ @{$item[1]} ] }
statement: expr(s) { $return = join '', @{$item[1]} }
expr: notcomma paren(?) { $return = @{$item[2]} ? $item[1].$item[2][0] : $item[1] }
paren: '(' expr(s? /,/) ')' { $return = '('.(join ',', @{$item[2]}).')' }
notcomma: /[^,()\n]+/
};
my $text = "B.TOTO, C.TA_TA, SUBSTR(SUBSTR(A.TUTU,1,4),1,4)!!'-'!!SUBSTR(A.TETE,5,2)!!'-'!!SUBSTR(A.TYTY,7,1),B.TITI\n";
my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n";
defined( my $ret = $parser->line($text) ) or die "Bad text!\n";
__END__ |
Partager