| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # regen_perly.pl, DAPM 12-Feb-04 |
| 4 | # |
| 5 | # Copyright (c) 2004, 2005, 2006, 2009, 2010, 2011 Larry Wall |
| 6 | # |
| 7 | # Given an input file perly.y, run bison on it and produce |
| 8 | # the following output files: |
| 9 | # |
| 10 | # perly.h standard bison header file with minor doctoring of |
| 11 | # #line directives plus adding a #ifdef PERL_CORE |
| 12 | # |
| 13 | # perly.tab the parser table C definitions extracted from the bison output |
| 14 | # plus an extra table generated by this script. |
| 15 | # |
| 16 | # perly.act the action case statements extracted from the bison output |
| 17 | # |
| 18 | # Note that perly.c is *not* regenerated - this is now a static file which |
| 19 | # is not dependent on perly.y any more. |
| 20 | # |
| 21 | # If a filename of the form foo.y is given on the command line, then |
| 22 | # this is used instead as the basename for all the files mentioned |
| 23 | # above. |
| 24 | # |
| 25 | # Note that temporary files of the form perlytmp.h and perlytmp.c are |
| 26 | # created and then deleted during this process |
| 27 | # |
| 28 | # Note also that this script is intended to be run on a UNIX system; |
| 29 | # it may work elsewhere but no specific attempt has been made to make it |
| 30 | # portable. |
| 31 | |
| 32 | use 5.006; |
| 33 | sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" } |
| 34 | |
| 35 | use warnings; |
| 36 | use strict; |
| 37 | |
| 38 | our $Verbose; |
| 39 | BEGIN { require './regen/regen_lib.pl'; } |
| 40 | |
| 41 | my $bison = 'bison'; |
| 42 | |
| 43 | if (@ARGV >= 2 and $ARGV[0] eq '-b') { |
| 44 | shift; |
| 45 | $bison = shift; |
| 46 | } |
| 47 | |
| 48 | my $y_file = shift || 'perly.y'; |
| 49 | |
| 50 | usage unless @ARGV==0 && $y_file =~ /\.y$/; |
| 51 | |
| 52 | (my $h_file = $y_file) =~ s/\.y$/.h/; |
| 53 | (my $act_file = $y_file) =~ s/\.y$/.act/; |
| 54 | (my $tab_file = $y_file) =~ s/\.y$/.tab/; |
| 55 | (my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/; |
| 56 | (my $tmph_file = $y_file) =~ s/\.y$/tmp.h/; |
| 57 | |
| 58 | # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive |
| 59 | |
| 60 | die "$0: must be run on an ASCII system\n" unless ord 'A' == 65; |
| 61 | |
| 62 | # check for correct version number. The constraints are: |
| 63 | # * must be >= 1.24 to avoid licensing issues. |
| 64 | # * it must generate the yystos[] table. Version 1.28 doesn't generate |
| 65 | # this; 1.35+ does |
| 66 | # * Must produce output which is extractable by the regexes below |
| 67 | # * Must produce the right values. |
| 68 | # These last two constraints may well be met by earlier versions, but |
| 69 | # I simply haven't tested them yet. If it works for you, then modify |
| 70 | # the test below to allow that version too. DAPM Feb 04. |
| 71 | |
| 72 | my $version = `$bison -V`; |
| 73 | unless ($version) { die <<EOF; } |
| 74 | Could not find a version of bison in your path. Please install bison. |
| 75 | EOF |
| 76 | |
| 77 | # Don't change this to add new bison versions without testing that the generated |
| 78 | # files actually work :-) Win32 in particular may not like them. :-( |
| 79 | unless ($version =~ /\b(2\.[4567]|3\.[0-7])\b/) { die <<EOF; } |
| 80 | |
| 81 | You have the wrong version of bison in your path; currently versions |
| 82 | 2.4-2.7 or 3.0-3.7 are known to work. Try installing |
| 83 | http://ftp.gnu.org/gnu/bison/bison-3.3.tar.gz |
| 84 | or similar. Your bison identifies itself as: |
| 85 | |
| 86 | $version |
| 87 | EOF |
| 88 | |
| 89 | # bison's version number, not the entire string, is most useful later on. |
| 90 | $version = $1; |
| 91 | |
| 92 | # creates $tmpc_file and $tmph_file |
| 93 | my_system("$bison -d -o $tmpc_file $y_file"); |
| 94 | |
| 95 | open my $ctmp_fh, '<', $tmpc_file or die "Can't open $tmpc_file: $!\n"; |
| 96 | my $clines; |
| 97 | { local $/; $clines = <$ctmp_fh>; } |
| 98 | die "failed to read $tmpc_file: length mismatch\n" |
| 99 | unless length $clines == -s $tmpc_file; |
| 100 | close $ctmp_fh; |
| 101 | |
| 102 | my ($actlines, $tablines) = extract($clines); |
| 103 | |
| 104 | our %tokens; |
| 105 | $tablines .= make_type_tab($y_file, $tablines); |
| 106 | |
| 107 | my ($act_fh, $tab_fh, $h_fh) = map { |
| 108 | open_new($_, '>', { by => $0, from => $y_file }); |
| 109 | } $act_file, $tab_file, $h_file; |
| 110 | |
| 111 | print $act_fh $actlines; |
| 112 | |
| 113 | print $tab_fh $tablines; |
| 114 | |
| 115 | unlink $tmpc_file; |
| 116 | |
| 117 | # Wrap PERL_CORE round the symbol definitions. Also, the |
| 118 | # C<#line 30 "perly.y"> confuses the Win32 resource compiler and the |
| 119 | # C<#line 188 "perlytmp.h"> gets picked up by make depend, so remove them. |
| 120 | |
| 121 | open my $tmph_fh, '<', $tmph_file or die "Can't open $tmph_file: $!\n"; |
| 122 | |
| 123 | # add integer-encoded #def of the bison version |
| 124 | |
| 125 | { |
| 126 | $version =~ /^(\d+)\.(\d+)/ |
| 127 | or die "Can't handle bison version format: '$version'"; |
| 128 | my ($v1,$v2) = ($1,$2); |
| 129 | die "Unexpectedly large bison version '$v1'" if $v1 > 99; |
| 130 | die "Unexpectedly large bison subversion '$v2'" if $v2 > 9999; |
| 131 | |
| 132 | printf $h_fh "#define PERL_BISON_VERSION %2d%04d\n\n", $v1, $v2; |
| 133 | } |
| 134 | |
| 135 | my $endcore_done = 0; |
| 136 | my $gather_tokens = 0; |
| 137 | my $tokens; |
| 138 | while (<$tmph_fh>) { |
| 139 | # bison 2.6 adds header guards, which break things because of where we |
| 140 | # insert #ifdef PERL_CORE, so strip them because they aren't important |
| 141 | next if /YY_PERLYTMP_H/; |
| 142 | |
| 143 | print $h_fh "#ifdef PERL_CORE\n" if $. == 1; |
| 144 | if (!$endcore_done and /YYSTYPE_IS_DECLARED/) { |
| 145 | print $h_fh <<h; |
| 146 | #ifdef PERL_IN_TOKE_C |
| 147 | static bool |
| 148 | S_is_opval_token(int type) { |
| 149 | switch (type) { |
| 150 | h |
| 151 | print $h_fh <<i for sort grep $tokens{$_} eq 'opval', keys %tokens; |
| 152 | case $_: |
| 153 | i |
| 154 | print $h_fh <<j; |
| 155 | return 1; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | #endif /* PERL_IN_TOKE_C */ |
| 160 | #endif /* PERL_CORE */ |
| 161 | j |
| 162 | $endcore_done = 1; |
| 163 | } |
| 164 | next if /^#line \d+ ".*"/; |
| 165 | if (!$gather_tokens) { |
| 166 | $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x; |
| 167 | } |
| 168 | else { |
| 169 | if (/^\# \s* endif/x) { # The #endif just after the end of the token enum |
| 170 | $gather_tokens = 0; |
| 171 | $_ .= "\n/* Tokens. */\n$tokens"; |
| 172 | } |
| 173 | else { |
| 174 | my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x; |
| 175 | $tokens .= "#define $tok $val\n" if $tok; |
| 176 | } |
| 177 | } |
| 178 | print $h_fh $_; |
| 179 | } |
| 180 | close $tmph_fh; |
| 181 | unlink $tmph_file; |
| 182 | |
| 183 | foreach ($act_fh, $tab_fh, $h_fh) { |
| 184 | read_only_bottom_close_and_rename($_, ['regen_perly.pl', $y_file]); |
| 185 | } |
| 186 | |
| 187 | exit 0; |
| 188 | |
| 189 | |
| 190 | # extract the symbol kinds, tables and actions from the generated .c file |
| 191 | |
| 192 | sub extract { |
| 193 | my $clines = shift; |
| 194 | my $tablines; |
| 195 | my $actlines; |
| 196 | |
| 197 | # extract the symbol kind table if it exists |
| 198 | $clines =~ m@ |
| 199 | (?: |
| 200 | ^/\* \s* Symbol \s+ kind\. \s* \*/\n |
| 201 | )? |
| 202 | enum \s+ yysymbol_kind_t \s* \{ |
| 203 | .*? |
| 204 | \} \s* ;\n |
| 205 | typedef \s+ enum \s+ \w+ \s+ \w+ ; \n+ |
| 206 | @xms |
| 207 | and $tablines .= $&; |
| 208 | |
| 209 | my $last_table = $version >= 3 ? 'yyr2' : 'yystos'; |
| 210 | $clines =~ m@ |
| 211 | (?: |
| 212 | ^/* YYFINAL[^\n]+\n #optional comment |
| 213 | )? |
| 214 | \# \s* define \s* YYFINAL # first #define |
| 215 | .*? # other defines + most tables |
| 216 | $last_table\[\]\s*= # start of last table |
| 217 | .*? |
| 218 | }\s*; # end of last table |
| 219 | @xms |
| 220 | or die "Can't extract tables from $tmpc_file\n"; |
| 221 | $tablines .= $&; |
| 222 | |
| 223 | |
| 224 | # extract all the cases in the big action switch statement |
| 225 | |
| 226 | $clines =~ m@ |
| 227 | switch \s* \( \s* yyn \s* \) \s* { \s* |
| 228 | ( .*? default: \s* break; \s* ) |
| 229 | } |
| 230 | @xms |
| 231 | or die "Can't extract actions from $tmpc_file\n"; |
| 232 | $actlines = $1; |
| 233 | |
| 234 | # Remove extraneous comments from bison 2.4 |
| 235 | $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx; |
| 236 | |
| 237 | # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them. |
| 238 | $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm; |
| 239 | |
| 240 | # convert yyvsp[nnn] into ps[nnn].val |
| 241 | |
| 242 | $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g |
| 243 | or die "Can't convert value stack name\n"; |
| 244 | |
| 245 | return $actlines. "\n", $tablines. "\n"; |
| 246 | } |
| 247 | |
| 248 | # Generate a table, yy_type_tab[], that specifies for each token, what |
| 249 | # type of value it holds. |
| 250 | # |
| 251 | # Read the .y file and extract a list of all the token names and |
| 252 | # non-terminal names; then scan the string $tablines for the table yytname, |
| 253 | # which gives the token index of each token/non-terminal; then use this to |
| 254 | # create yy_type_tab. |
| 255 | # |
| 256 | # ie given (in perly.y), |
| 257 | # |
| 258 | # %token <opval> A |
| 259 | # %token <ival> B |
| 260 | # %type <pval> C |
| 261 | # %type <opval> D |
| 262 | # |
| 263 | # and (in $tablines), |
| 264 | # |
| 265 | # yytname[] = { "A" "B", "C", "D", "E" }; |
| 266 | # |
| 267 | # then return |
| 268 | # |
| 269 | # typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes; |
| 270 | # |
| 271 | # static const toketypes yy_type_tab[] |
| 272 | # = { toketype_opval, toketype_ival, toketype_pval, |
| 273 | # toketype_opval, toketype_ival } |
| 274 | # |
| 275 | # where "E" has the default type. The default type is determined |
| 276 | # by the __DEFAULT__ comment next to the appropriate union member in |
| 277 | # perly.y |
| 278 | |
| 279 | sub make_type_tab { |
| 280 | my ($y_file, $tablines) = @_; |
| 281 | my %just_tokens; |
| 282 | my %tokens; |
| 283 | my %types; |
| 284 | my $default_token; |
| 285 | open my $fh, '<', $y_file or die "Can't open $y_file: $!\n"; |
| 286 | while (<$fh>) { |
| 287 | if (/(\$\d+)\s*=[^=]/) { |
| 288 | warn "$y_file:$.: dangerous assignment to $1: $_"; |
| 289 | } |
| 290 | |
| 291 | if (/__DEFAULT__/) { |
| 292 | m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x |
| 293 | or die "$y_file: can't parse __DEFAULT__ line: $_"; |
| 294 | die "$y_file: duplicate __DEFAULT__ line: $_" |
| 295 | if defined $default_token; |
| 296 | $default_token = $1; |
| 297 | next; |
| 298 | } |
| 299 | |
| 300 | next unless /^%(token|type)/; |
| 301 | s/^%((token)|type)\s+<(\w+)>\s+// |
| 302 | or die "$y_file: unparseable token/type line: $_"; |
| 303 | for (split ' ', $_) { |
| 304 | $tokens{$_} = $3; |
| 305 | if ($2) { |
| 306 | $just_tokens{$_} = $3; |
| 307 | } |
| 308 | } |
| 309 | $types{$3} = 1; |
| 310 | } |
| 311 | *tokens = \%just_tokens; # perly.h needs this |
| 312 | die "$y_file: no __DEFAULT__ token defined\n" unless $default_token; |
| 313 | $types{$default_token} = 1; |
| 314 | |
| 315 | $tablines =~ /^\Qstatic const char *const yytname[] =\E\n |
| 316 | \{\n |
| 317 | (.*?) |
| 318 | ^}; |
| 319 | /xsm |
| 320 | or die "Can't extract yytname[] from table string\n"; |
| 321 | my $fields = $1; |
| 322 | $fields =~ s{"((?:[^"\\]|\\.)+)"} |
| 323 | { "toketype_" . |
| 324 | (defined $tokens{$1} ? $tokens{$1} : $default_token) |
| 325 | }ge; |
| 326 | $fields =~ s/, \s* (?:0|YY_NULL|YY_NULLPTR) \s* $//x |
| 327 | or die "make_type_tab: couldn't delete trailing ',0'\n"; |
| 328 | |
| 329 | return |
| 330 | "\ntypedef enum {\n\t" |
| 331 | . join(", ", map "toketype_$_", sort keys %types) |
| 332 | . "\n} toketypes;\n\n" |
| 333 | . "/* type of each token/terminal */\n" |
| 334 | . "static const toketypes yy_type_tab[] =\n{\n" |
| 335 | . $fields |
| 336 | . "\n};\n"; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | sub my_system { |
| 341 | if ($Verbose) { |
| 342 | print "executing: @_\n"; |
| 343 | } |
| 344 | system(@_); |
| 345 | if ($? == -1) { |
| 346 | die "failed to execute command '@_': $!\n"; |
| 347 | } |
| 348 | elsif ($? & 127) { |
| 349 | die sprintf "command '@_' died with signal %d\n", |
| 350 | ($? & 127); |
| 351 | } |
| 352 | elsif ($? >> 8) { |
| 353 | die sprintf "command '@_' exited with value %d\n", $? >> 8; |
| 354 | } |
| 355 | } |