int2sym.pl
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl
# Copyright 2010-2012 Microsoft Corporation Johns Hopkins University (Author: Daniel Povey)
# Apache 2.0.
undef $field_begin;
undef $field_end;
if ($ARGV[0] eq "-f") {
shift @ARGV;
$field_spec = shift @ARGV;
if ($field_spec =~ m/^\d+$/) {
$field_begin = $field_spec - 1; $field_end = $field_spec - 1;
}
if ($field_spec =~ m/^(\d*)[-:](\d*)/) { # accept e.g. 1:10 as a courtesty (properly, 1-10)
if ($1 ne "") {
$field_begin = $1 - 1; # Change to zero-based indexing.
}
if ($2 ne "") {
$field_end = $2 - 1; # Change to zero-based indexing.
}
}
if (!defined $field_begin && !defined $field_end) {
die "Bad argument to -f option: $field_spec";
}
}
$symtab = shift @ARGV;
if(!defined $symtab) {
print STDERR "Usage: sym2int.pl [options] symtab [input] > output\n" .
"options: [-f (<field>|<field_start>-<field-end>)]\n" .
"e.g.: -f 2, or -f 3-4\n";
exit(1);
}
open(F, "<$symtab") || die "Error opening symbol table file $symtab";
while(<F>) {
@A = split(" ", $_);
@A == 2 || die "bad line in symbol table file: $_";
$int2sym{$A[1]} = $A[0];
}
sub int2sym {
my $a = shift @_;
my $pos = shift @_;
if($a !~ m:^\d+$:) { # not all digits..
$pos1 = $pos+1; # make it one-based.
die "int2sym.pl: found noninteger token $a [in position $pos1]\n";
}
$s = $int2sym{$a};
if(!defined ($s)) {
die "int2sym.pl: integer $a not in symbol table $symtab.";
}
return $s;
}
$error = 0;
while (<>) {
@A = split(" ", $_);
for ($pos = 0; $pos <= $#A; $pos++) {
$a = $A[$pos];
if ( (!defined $field_begin || $pos >= $field_begin)
&& (!defined $field_end || $pos <= $field_end)) {
$a = int2sym($a, $pos);
}
print $a . " ";
}
print "\n";
}