3 # regen_madly.pl, DAPM 12-Feb-04
5 # Copyright (c) 2004, 2005 Larry Wall
7 # Given an input file madly.y, run bison on it and produce
8 # the following output files:
10 # madly.h standard bison header file with minor doctoring of
11 # #line directives plus adding a #ifdef PERL_CORE
13 # madly.tab the parser table C definitions extracted from the bison output
15 # madly.act the action case statements extracted from the bison output
17 # Note that madly.c is *not* regenerated - this is now a static file which
18 # is not dependent on madly.y any more.
20 # If a filename of the form foo.y is given on the command line, then
21 # this is used instead as the basename for all the files mentioned
24 # Note that temporary files of the form madlytmp.h and madlytmp.c are
25 # created and then deleted during this process
27 # Note also that this script is intended to be run on a UNIX system;
28 # it may work elsewhere but no specific attempt has been made to make it
31 sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" }
38 if (@ARGV >= 2 and $ARGV[0] eq '-b') {
43 my $y_file = shift || 'madly.y';
45 usage unless @ARGV==0 && $y_file =~ /\.y$/;
47 (my $h_file = $y_file) =~ s/\.y$/.h/;
48 (my $act_file = $y_file) =~ s/\.y$/.act/;
49 (my $tab_file = $y_file) =~ s/\.y$/.tab/;
50 (my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/;
51 (my $tmph_file = $y_file) =~ s/\.y$/tmp.h/;
53 # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive
55 die "$0: must be run on an ASCII system\n" unless ord 'A' == 65;
57 # check for correct version number. The constraints are:
58 # * must be >= 1.24 to avoid licensing issues.
59 # * it must generate the yystos[] table. Version 1.28 doesn't generate
61 # * Must produce output which is extractable by the regexes below
62 # * Must produce the right values.
63 # These last two contstraints may well be met by earlier versions, but
64 # I simply haven't tested them yet. If it works for you, then modify
65 # the test below to allow that version too. DAPM Feb 04.
67 my $version = `$bison -V`;
68 unless ($version =~ /\b(1\.875[a-z]?|2\.[01])\b/) { die <<EOF; }
70 You have the wrong version of bison in your path; currently 1.875,
71 2.0 or 2.1 is required. Try installing
72 http://ftp.gnu.org/gnu/bison/bison-2.0.tar.gz
74 http://ftp.gnu.org/gnu/bison/bison-1.875.tar.bz2
75 or similar. Your bison identifies itself as:
80 # creates $tmpc_file and $tmph_file
81 my_system("$bison -d -o $tmpc_file $y_file");
83 open CTMPFILE, $tmpc_file or die "Can't open $tmpc_file: $!\n";
85 { local $/; $clines = <CTMPFILE>; }
86 die "failed to read $tmpc_file: length mismatch\n"
87 unless length $clines == -s $tmpc_file;
90 my ($actlines, $tablines) = extract($clines);
92 chmod 0644, $act_file;
93 open ACTFILE, ">$act_file" or die "can't open $act_file: $!\n";
94 print ACTFILE $actlines;
96 chmod 0444, $act_file;
98 chmod 0644, $tab_file;
99 open TABFILE, ">$tab_file" or die "can't open $tab_file: $!\n";
100 print TABFILE $tablines;
102 chmod 0444, $tab_file;
106 # Wrap PERL_CORE round the symbol definitions. Also, the
107 # C<#line 123 "madlytmp.h"> gets picked up by make depend, so change it.
109 open TMPH_FILE, $tmph_file or die "Can't open $tmph_file: $!\n";
111 open H_FILE, ">$h_file" or die "Can't open $h_file: $!\n";
112 my $endcore_done = 0;
113 while (<TMPH_FILE>) {
114 print H_FILE "#ifdef PERL_CORE\n" if $. == 1;
115 if (!$endcore_done and /YYSTYPE_IS_DECLARED/) {
116 print H_FILE "#endif /* PERL_CORE */\n";
119 s/"madlytmp.h"/"madly.h"/;
127 print "rebuilt: $h_file $tab_file $act_file\n";
139 ^/* YYFINAL[^\n]+\n #optional comment
141 \# \s* define \s* YYFINAL # first #define
142 .*? # other defines + most tables
143 yystos\[\]\s*= # start of last table
145 }\s*; # end of last table
147 or die "Can't extract tables from $tmpc_file\n";
152 switch \s* \( \s* \w+ \s* \) \s* { \s*
154 case \s* \d+ \s* : \s*
155 \#line [^\n]+"madly\.y"
160 ( \s* /\* .*? \*/ \s* )* # optional C-comments
165 \#line[^\n]+\.simple"
168 or die "Can't extract actions from $tmpc_file\n";
171 return $actlines. "\n", $tablines. "\n";
177 die "failed to execute comamnd '@_': $!\n";
180 die sprintf "command '@_' died with signal %d\n",
184 die sprintf "command '@_' exited with value %d\n", $? >> 8;