Commit | Line | Data |
---|---|---|
0de566d7 DM |
1 | #!/usr/bin/perl |
2 | # | |
3 | # regen_perly.pl, DAPM 12-Feb-04 | |
4 | # | |
1d325971 | 5 | # Copyright (c) 2004, 2005 Larry Wall |
0de566d7 DM |
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 | # | |
15 | # perly.act the action case statements extracted from the bison output | |
16 | # | |
17 | # Note that perly.c is *not* regenerated - this is now a static file which | |
18 | # is not dependent on perly.y any more. | |
19 | # | |
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 | |
22 | # above. | |
23 | # | |
24 | # Note that temporary files of the form perlytmp.h and perlytmp.c are | |
25 | # created and then deleted during this process | |
26 | # | |
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 | |
29 | # portable. | |
30 | ||
31 | sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" } | |
32 | ||
33 | use warnings; | |
34 | use strict; | |
35 | ||
36 | my $bison = 'bison'; | |
37 | ||
38 | if (@ARGV >= 2 and $ARGV[0] eq '-b') { | |
39 | shift; | |
40 | $bison = shift; | |
41 | } | |
42 | ||
43 | my $y_file = shift || 'perly.y'; | |
44 | ||
45 | usage unless @ARGV==0 && $y_file =~ /\.y$/; | |
46 | ||
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/; | |
52 | ||
53 | # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive | |
54 | ||
55 | die "$0: must be run on an ASCII system\n" unless ord 'A' == 65; | |
56 | ||
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 | |
60 | # this; 1.35+ does | |
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. | |
66 | ||
67 | my $version = `$bison -V`; | |
20515881 | 68 | unless ($version =~ /\b(1\.875[a-z]?|2\.[01])\b/) { die <<EOF; } |
0de566d7 | 69 | |
2080282f | 70 | You have the wrong version of bison in your path; currently 1.875 |
20515881 RGS |
71 | 2.0 or 2.1 is required. Try installing |
72 | http://ftp.gnu.org/gnu/bison/bison-2.1.tar.gz | |
0de566d7 DM |
73 | or similar. Your bison identifies itself as: |
74 | ||
75 | $version | |
76 | EOF | |
77 | ||
78 | # creates $tmpc_file and $tmph_file | |
79 | my_system("$bison -d -o $tmpc_file $y_file"); | |
80 | ||
81 | open CTMPFILE, $tmpc_file or die "Can't open $tmpc_file: $!\n"; | |
82 | my $clines; | |
83 | { local $/; $clines = <CTMPFILE>; } | |
84 | die "failed to read $tmpc_file: length mismatch\n" | |
85 | unless length $clines == -s $tmpc_file; | |
86 | close CTMPFILE; | |
87 | ||
88 | my ($actlines, $tablines) = extract($clines); | |
89 | ||
90 | chmod 0644, $act_file; | |
91 | open ACTFILE, ">$act_file" or die "can't open $act_file: $!\n"; | |
92 | print ACTFILE $actlines; | |
93 | close ACTFILE; | |
94 | chmod 0444, $act_file; | |
95 | ||
96 | chmod 0644, $tab_file; | |
97 | open TABFILE, ">$tab_file" or die "can't open $tab_file: $!\n"; | |
98 | print TABFILE $tablines; | |
99 | close TABFILE; | |
100 | chmod 0444, $tab_file; | |
101 | ||
102 | unlink $tmpc_file; | |
103 | ||
104 | # Wrap PERL_CORE round the symbol definitions. Also, the | |
105 | # C<#line 123 "perlytmp.h"> gets picked up by make depend, so change it. | |
106 | ||
107 | open TMPH_FILE, $tmph_file or die "Can't open $tmph_file: $!\n"; | |
108 | chmod 0644, $h_file; | |
109 | open H_FILE, ">$h_file" or die "Can't open $h_file: $!\n"; | |
110 | my $endcore_done = 0; | |
111 | while (<TMPH_FILE>) { | |
112 | print H_FILE "#ifdef PERL_CORE\n" if $. == 1; | |
113 | if (!$endcore_done and /YYSTYPE_IS_DECLARED/) { | |
114 | print H_FILE "#endif /* PERL_CORE */\n"; | |
115 | $endcore_done = 1; | |
116 | } | |
117 | s/"perlytmp.h"/"perly.h"/; | |
118 | print H_FILE $_; | |
119 | } | |
120 | close TMPH_FILE; | |
121 | close H_FILE; | |
122 | chmod 0444, $h_file; | |
123 | unlink $tmph_file; | |
124 | ||
125 | print "rebuilt: $h_file $tab_file $act_file\n"; | |
126 | ||
127 | exit 0; | |
128 | ||
129 | ||
130 | sub extract { | |
131 | my $clines = shift; | |
132 | my $tablines; | |
133 | my $actlines; | |
134 | ||
135 | $clines =~ m@ | |
136 | (?: | |
137 | ^/* YYFINAL[^\n]+\n #optional comment | |
138 | )? | |
139 | \# \s* define \s* YYFINAL # first #define | |
140 | .*? # other defines + most tables | |
141 | yystos\[\]\s*= # start of last table | |
142 | .*? | |
143 | }\s*; # end of last table | |
144 | @xms | |
145 | or die "Can't extract tables from $tmpc_file\n"; | |
146 | $tablines = $&; | |
147 | ||
148 | ||
149 | $clines =~ m@ | |
150 | switch \s* \( \s* \w+ \s* \) \s* { \s* | |
151 | ( | |
152 | case \s* \d+ \s* : \s* | |
153 | \#line [^\n]+"perly\.y" | |
154 | .*? | |
155 | ) | |
156 | } | |
157 | \s* | |
158 | ( \s* /\* .*? \*/ \s* )* # optional C-comments | |
159 | \s* | |
160 | ( | |
161 | \#line[^\n]+\.c" | |
162 | | | |
163 | \#line[^\n]+\.simple" | |
164 | ) | |
165 | @xms | |
166 | or die "Can't extract actions from $tmpc_file\n"; | |
167 | $actlines = $1; | |
168 | ||
169 | return $actlines. "\n", $tablines. "\n"; | |
170 | } | |
171 | ||
0de566d7 DM |
172 | sub my_system { |
173 | system(@_); | |
174 | if ($? == -1) { | |
175 | die "failed to execute comamnd '@_': $!\n"; | |
176 | } | |
177 | elsif ($? & 127) { | |
178 | die sprintf "command '@_' died with signal %d\n", | |
179 | ($? & 127); | |
180 | } | |
181 | elsif ($? >> 8) { | |
182 | die sprintf "command '@_' exited with value %d\n", $? >> 8; | |
183 | } | |
184 | } |