/usr/bin/cg-strictify is in cg3 1.0.0~r12254-1ubuntu3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | #!/usr/bin/env perl
use warnings;
use strict;
use utf8;
BEGIN {
$| = 1;
binmode(STDIN, ':encoding(UTF-8)');
binmode(STDOUT, ':encoding(UTF-8)');
}
use open qw( :encoding(UTF-8) :std );
use Getopt::Long;
Getopt::Long::Configure('no_ignore_case');
my %opts = ();
GetOptions (\%opts,
'help|?',
'grammar|g=s',
'output|o',
'strip',
'secondary',
'regex',
'icase',
'baseforms',
'wordforms',
'all',
);
sub print_help {
print <<'XOUT';
Usage: cg-strictify [OPTIONS] <grammar>
Options:
-?, --help outputs this help
-g, --grammar the grammar to parse; defaults to first non-option argument
-o, --output outputs the whole grammar with STRICT-TAGS
--strip removes superfluous LISTs from the output grammar; implies -o
--secondary adds secondary tags (<...>) to strict list
--regex adds regular expression tags (/../r, <..>r, etc) to strict list
--icase adds case-insensitive tags to strict list
--baseforms adds baseform tags ("...") to strict list
--wordforms adds wordform tags ("<...>") to strict list
--all same as --strip --secondary --regex --icase --baseforms --wordforms
XOUT
}
if (defined $opts{'help'}) {
print_help();
exit(0);
}
if (!defined $opts{'grammar'}) {
if (!$ARGV[0]) {
print "Missing input grammar argument!\n\n";
print_help();
exit(-1);
}
$opts{'grammar'} = $ARGV[0];
}
if (! -s $opts{'grammar'}) {
print "Grammar is either missing or empty!\n";
exit(-1);
}
if (defined $opts{'strip'}) {
$opts{'output'} = 1;
}
if (defined $opts{'all'}) {
$opts{'secondary'} = 1;
$opts{'regex'} = 1;
$opts{'icase'} = 1;
$opts{'baseforms'} = 1;
$opts{'wordforms'} = 1;
}
my $tags = `vislcg3 --show-tags -g '$opts{grammar}' | LC_ALL=C sort`;
$tags =~ s@[\r\n]+@\n@g; # Normalize newlines
chomp($tags);
my @tags = split /\n/, $tags;
my $options = '';
if (defined $opts{'secondary'} || defined $opts{'regex'} || defined $opts{'icase'} || defined $opts{'baseforms'} || defined $opts{'wordforms'}) {
$options .= 'OPTIONS +=';
if (defined $opts{'secondary'}) {
$options .= ' strict-secondary';
}
if (defined $opts{'regex'}) {
$options .= ' strict-regex';
}
if (defined $opts{'icase'}) {
$options .= ' strict-icase';
}
if (defined $opts{'baseforms'}) {
$options .= ' strict-baseforms';
}
if (defined $opts{'wordforms'}) {
$options .= ' strict-wordforms';
}
$options .= " ;\n";
}
my $strict_tags = 'STRICT-TAGS +=';
my @strict = ();
my $lf = '';
foreach my $tag (@tags) {
if ($tag eq '*') {
next; # Marker for erasure, any, etc.
}
if ($tag eq '>>>') {
next; # Start of window marker
}
if ($tag eq '<<<') {
next; # End of window marker
}
if ($tag =~ m@^\^@) {
next; # Fail-fast tags
}
if ($tag =~ m@^VSTR:@) {
next; # Varstrings
}
if ($tag =~ m@^VAR:@) {
next; # Global variables
}
if ($tag =~ m@^_.+_$@) {
next; # Magic placeholders such as _TARGET_
}
if ($tag =~ m@dummy string@) {
next; # Internal placeholder for tag #0
}
if ($tag =~ m@[>"/]v$@) {
next;
}
if (! defined $opts{'secondary'} && $tag =~ m@^<.+>[riv]*$@) {
next;
}
if (! defined $opts{'regex'} && $tag =~ m@^/.+/[riv]*$@) {
next;
}
if (! defined $opts{'icase'} && $tag =~ m@[>"/]i$@) {
next;
}
if (! defined $opts{'baseforms'} && $tag =~ m@^"[^<].*"[riv]*$@) {
next;
}
if (! defined $opts{'wordforms'} && $tag =~ m@^"<.*>"[riv]*$@) {
next;
}
my $nlf = substr($tag, 0, 1);
if ($nlf =~ m@^\p{Lu}@) {
$nlf = 'A';
}
elsif ($nlf =~ m@^\p{Ll}@) {
$nlf = 'a';
}
elsif ($tag =~ m@^"<.+>"[riv]*$@) {
$nlf = 'W';
}
elsif ($tag =~ m@^".+"[riv]*$@) {
$nlf = 'B';
}
if ($lf && $lf ne $nlf) {
$strict_tags .= "\n";
}
$lf = $nlf;
$strict_tags .= " $tag";
push(@strict, $tag);
}
$strict_tags .= " ;\n";
if (! defined $opts{'output'}) {
print "Put these lines at the top of your grammar, and edit the STRICT-TAGS list by removing invalid tags:\n\n";
print $options;
print $strict_tags;
exit(0);
}
my $g = '';
{
local $/ = undef;
open(FH, '<'.$opts{'grammar'});
$g = <FH>;
close(FH);
}
if (defined $opts{'strip'}) {
print STDERR "Deleting unnecessary LISTs...\n";
foreach my $tag (@strict) {
if ($g =~ m@[\b\n][Ll][Ii][Ss][Tt]\s+\Q$tag\E\s*=\s*\Q$tag\E\s*;@) {
print STDERR "Deleting LIST $tag\n";
$g =~ s@[\b\n][Ll][Ii][Ss][Tt]\s+\Q$tag\E\s*=\s*\Q$tag\E\s*;@@;
}
}
}
print $options;
print $strict_tags;
print "\n";
print $g;
|