Вот пример рекурсивного вызова через структуру каталогов и копирования резервного сценария формы файлов, который я записал.
sub copy_directory {
my ($source, $dest) = @_;
my $start = time;
# get the contents of the directory.
opendir(D, $source);
my @f = readdir(D);
closedir(D);
# recurse through the directory structure and copy files.
foreach my $file (@f) {
# Setup the full path to the source and dest files.
my $filename = $source . "\\" . $file;
my $destfile = $dest . "\\" . $file;
# get the file info for the 2 files.
my $sourceInfo = stat( $filename );
my $destInfo = stat( $destfile );
# make sure the destinatin directory exists.
mkdir( $dest, 0777 );
if ($file eq '.' || $file eq '..') {
} elsif (-d $filename) { # if it's a directory then recurse into it.
#print "entering $filename\n";
copy_directory($filename, $destfile);
} else {
# Only backup the file if it has been created/modified since the last backup
if( (not -e $destfile) || ($sourceInfo->mtime > $destInfo->mtime ) ) {
#print $filename . " -> " . $destfile . "\n";
copy( $filename, $destfile ) or print "Error copying $filename: $!\n";
}
}
}
print "$source copied in " . (time - $start) . " seconds.\n";
}
На самом деле, я почти уверен, что знаю, что не так, не видя грамматики.
Также необходимо объявить типы терминальных символов, поскольку они могут и часто возвращают семантическое значение . Подумайте о таких вещах, как ID
и НОМЕР
, у которых есть данные yyval
. Но yacc не имеет возможности узнать, просто ли это 'x'
или что-то еще, а действие по умолчанию в правиле: $$ = $ 1
Например, следующая грамматика прекрасно справляется с yacc, но попробуйте удалить еще один символ из типа %
ниже:
%union {
char *s;
}
%type <s> r1 r2 'x'
%%
r1: r2;
r2: 'x' { printf("%s\n", $1); };
you probably just need to define types for your tokens in the yacc file
%token<str> yident