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 | |
0539ab63 | 14 | # plus an extra table generated by this script. |
0de566d7 DM |
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 | ||
e8fb9efb | 32 | use 5.006; |
0de566d7 DM |
33 | sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" } |
34 | ||
35 | use warnings; | |
36 | use strict; | |
37 | ||
2bd4012d | 38 | BEGIN { require 'regen/regen_lib.pl'; } |
a9718e07 | 39 | |
0de566d7 DM |
40 | my $bison = 'bison'; |
41 | ||
42 | if (@ARGV >= 2 and $ARGV[0] eq '-b') { | |
43 | shift; | |
44 | $bison = shift; | |
45 | } | |
46 | ||
47 | my $y_file = shift || 'perly.y'; | |
48 | ||
49 | usage unless @ARGV==0 && $y_file =~ /\.y$/; | |
50 | ||
51 | (my $h_file = $y_file) =~ s/\.y$/.h/; | |
52 | (my $act_file = $y_file) =~ s/\.y$/.act/; | |
53 | (my $tab_file = $y_file) =~ s/\.y$/.tab/; | |
54 | (my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/; | |
55 | (my $tmph_file = $y_file) =~ s/\.y$/tmp.h/; | |
56 | ||
57 | # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive | |
58 | ||
59 | die "$0: must be run on an ASCII system\n" unless ord 'A' == 65; | |
60 | ||
61 | # check for correct version number. The constraints are: | |
62 | # * must be >= 1.24 to avoid licensing issues. | |
63 | # * it must generate the yystos[] table. Version 1.28 doesn't generate | |
64 | # this; 1.35+ does | |
65 | # * Must produce output which is extractable by the regexes below | |
66 | # * Must produce the right values. | |
8abc1060 | 67 | # These last two constraints may well be met by earlier versions, but |
0de566d7 DM |
68 | # I simply haven't tested them yet. If it works for you, then modify |
69 | # the test below to allow that version too. DAPM Feb 04. | |
70 | ||
71 | my $version = `$bison -V`; | |
7b931d50 LB |
72 | unless ($version) { die <<EOF; } |
73 | Could not find a version of bison in your path. Please install bison. | |
74 | EOF | |
75 | ||
ce1534ab | 76 | unless ($version =~ /\b(1\.875[a-z]?|2\.[0134])\b/) { die <<EOF; } |
0de566d7 | 77 | |
2080282f | 78 | You have the wrong version of bison in your path; currently 1.875 |
aae7e7b3 VP |
79 | 2.0, 2.1, 2.3 or 2.4 is required. Try installing |
80 | http://ftp.gnu.org/gnu/bison/bison-2.4.1.tar.gz | |
0de566d7 DM |
81 | or similar. Your bison identifies itself as: |
82 | ||
83 | $version | |
84 | EOF | |
85 | ||
86 | # creates $tmpc_file and $tmph_file | |
87 | my_system("$bison -d -o $tmpc_file $y_file"); | |
88 | ||
e8fb9efb | 89 | open my $ctmp_fh, '<', $tmpc_file or die "Can't open $tmpc_file: $!\n"; |
0de566d7 | 90 | my $clines; |
e8fb9efb | 91 | { local $/; $clines = <$ctmp_fh>; } |
0de566d7 DM |
92 | die "failed to read $tmpc_file: length mismatch\n" |
93 | unless length $clines == -s $tmpc_file; | |
e8fb9efb | 94 | close $ctmp_fh; |
0de566d7 DM |
95 | |
96 | my ($actlines, $tablines) = extract($clines); | |
97 | ||
d5c6462e | 98 | $tablines .= make_type_tab($y_file, $tablines); |
0539ab63 | 99 | |
cc49830d NC |
100 | my ($act_fh, $tab_fh, $h_fh) = map { |
101 | open_new($_, '>', { by => $0, from => $y_file }); | |
102 | } $act_file, $tab_file, $h_file; | |
0de566d7 | 103 | |
cc49830d | 104 | print $act_fh $actlines; |
e8fb9efb | 105 | |
cc49830d | 106 | print $tab_fh $tablines; |
0de566d7 DM |
107 | |
108 | unlink $tmpc_file; | |
109 | ||
110 | # Wrap PERL_CORE round the symbol definitions. Also, the | |
96f4e226 SH |
111 | # C<#line 30 "perly.y"> confuses the Win32 resource compiler and the |
112 | # C<#line 188 "perlytmp.h"> gets picked up by make depend, so remove them. | |
0de566d7 | 113 | |
e8fb9efb | 114 | open my $tmph_fh, '<', $tmph_file or die "Can't open $tmph_file: $!\n"; |
e8fb9efb | 115 | |
0de566d7 | 116 | my $endcore_done = 0; |
ce1534ab VP |
117 | # Token macros need to be generated manually on bison 2.4 |
118 | my $gather_tokens = ($version =~ /\b2\.4\b/ ? undef : 0); | |
119 | my $tokens; | |
e8fb9efb NC |
120 | while (<$tmph_fh>) { |
121 | print $h_fh "#ifdef PERL_CORE\n" if $. == 1; | |
0de566d7 | 122 | if (!$endcore_done and /YYSTYPE_IS_DECLARED/) { |
e8fb9efb | 123 | print $h_fh "#endif /* PERL_CORE */\n"; |
0de566d7 DM |
124 | $endcore_done = 1; |
125 | } | |
96f4e226 | 126 | next if /^#line \d+ ".*"/; |
ce1534ab VP |
127 | if (not defined $gather_tokens) { |
128 | $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x; | |
129 | } | |
130 | elsif ($gather_tokens) { | |
131 | if (/^\# \s* endif/x) { # The #endif just after the end of the token enum | |
132 | $gather_tokens = 0; | |
133 | $_ .= "\n/* Tokens. */\n$tokens"; | |
134 | } | |
135 | else { | |
136 | my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x; | |
137 | $tokens .= "#define $tok $val\n" if $tok; | |
138 | } | |
139 | } | |
e8fb9efb | 140 | print $h_fh $_; |
0de566d7 | 141 | } |
e8fb9efb | 142 | close $tmph_fh; |
0de566d7 DM |
143 | unlink $tmph_file; |
144 | ||
c24c946d NC |
145 | foreach ($act_fh, $tab_fh, $h_fh) { |
146 | read_only_bottom_close_and_rename($_, ['regen_perly.pl', $y_file]); | |
147 | } | |
0de566d7 DM |
148 | |
149 | exit 0; | |
150 | ||
151 | ||
152 | sub extract { | |
153 | my $clines = shift; | |
154 | my $tablines; | |
155 | my $actlines; | |
156 | ||
157 | $clines =~ m@ | |
158 | (?: | |
159 | ^/* YYFINAL[^\n]+\n #optional comment | |
160 | )? | |
161 | \# \s* define \s* YYFINAL # first #define | |
162 | .*? # other defines + most tables | |
163 | yystos\[\]\s*= # start of last table | |
164 | .*? | |
165 | }\s*; # end of last table | |
166 | @xms | |
167 | or die "Can't extract tables from $tmpc_file\n"; | |
168 | $tablines = $&; | |
169 | ||
170 | ||
171 | $clines =~ m@ | |
172 | switch \s* \( \s* \w+ \s* \) \s* { \s* | |
173 | ( | |
ce1534ab VP |
174 | case \s* \d+ \s* : |
175 | \s* | |
176 | (?: \s* /\* .*? \*/ \s* )* # optional C-comments | |
177 | \s* | |
2ade6388 | 178 | \#line [^\n]+"\Q$y_file\E" |
0de566d7 DM |
179 | .*? |
180 | ) | |
181 | } | |
182 | \s* | |
ce1534ab | 183 | (?: \s* /\* .*? \*/ \s* )* # optional C-comments |
0de566d7 DM |
184 | \s* |
185 | ( | |
186 | \#line[^\n]+\.c" | |
187 | | | |
188 | \#line[^\n]+\.simple" | |
3797f23d DM |
189 | | |
190 | YY_SYMBOL_PRINT | |
0de566d7 DM |
191 | ) |
192 | @xms | |
193 | or die "Can't extract actions from $tmpc_file\n"; | |
194 | $actlines = $1; | |
195 | ||
ce1534ab VP |
196 | # Remove extraneous comments from bison 2.4 |
197 | $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx; | |
198 | ||
0d6f9730 DM |
199 | # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them. |
200 | $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm; | |
201 | ||
1654d593 DM |
202 | # convert yyvsp[nnn] into ps[nnn].val |
203 | ||
204 | $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g | |
205 | or die "Can't convert value stack name\n"; | |
206 | ||
0de566d7 DM |
207 | return $actlines. "\n", $tablines. "\n"; |
208 | } | |
209 | ||
d5c6462e DM |
210 | # Generate a table, yy_type_tab[], that specifies for each token, what |
211 | # type of value it holds. | |
0539ab63 | 212 | # |
d5c6462e DM |
213 | # Read the .y file and extract a list of all the token names and |
214 | # non-terminal names; then scan the string $tablines for the table yytname, | |
215 | # which gives the token index of each token/non-terminal; then use this to | |
216 | # create yy_type_tab. | |
0539ab63 | 217 | # |
d5c6462e DM |
218 | # ie given (in perly.y), |
219 | # | |
220 | # %token <opval> A | |
221 | # %token <ival> B | |
222 | # %type <pval> C | |
223 | # %type <opval> D | |
224 | # | |
225 | # and (in $tablines), | |
226 | # | |
227 | # yytname[] = { "A" "B", "C", "D", "E" }; | |
0539ab63 DM |
228 | # |
229 | # then return | |
d5c6462e DM |
230 | # |
231 | # typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes; | |
232 | # | |
233 | # static const toketypes yy_type_tab[] | |
234 | # = { toketype_opval, toketype_ival, toketype_pval, | |
235 | # toketype_opval, toketype_ival } | |
236 | # | |
237 | # where "E" has the default type. The default type is determined | |
238 | # by the __DEFAULT__ comment next to the appropriate union member in | |
239 | # perly.y | |
0539ab63 | 240 | |
d5c6462e | 241 | sub make_type_tab { |
0539ab63 DM |
242 | my ($y_file, $tablines) = @_; |
243 | my %tokens; | |
d5c6462e DM |
244 | my %types; |
245 | my $default_token; | |
0539ab63 DM |
246 | open my $fh, '<', $y_file or die "Can't open $y_file: $!\n"; |
247 | while (<$fh>) { | |
29522234 DM |
248 | if (/(\$\d+)\s*=/) { |
249 | warn "$y_file:$.: dangerous assignment to $1: $_"; | |
250 | } | |
251 | ||
d5c6462e DM |
252 | if (/__DEFAULT__/) { |
253 | m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x | |
254 | or die "$y_file: can't parse __DEFAULT__ line: $_"; | |
255 | die "$y_file: duplicate __DEFAULT__ line: $_" | |
256 | if defined $default_token; | |
257 | $default_token = $1; | |
258 | next; | |
259 | } | |
260 | ||
261 | next unless /^%(token|type)/; | |
262 | s/^%(token|type)\s+<(\w+)>\s+// | |
263 | or die "$y_file: unparseable token/type line: $_"; | |
264 | $tokens{$_} = $2 for (split ' ', $_); | |
265 | $types{$2} = 1; | |
0539ab63 | 266 | } |
d5c6462e DM |
267 | die "$y_file: no __DEFAULT__ token defined\n" unless $default_token; |
268 | $types{$default_token} = 1; | |
0539ab63 DM |
269 | |
270 | $tablines =~ /^\Qstatic const char *const yytname[] =\E\n | |
271 | {\n | |
272 | (.*?) | |
273 | ^}; | |
274 | /xsm | |
275 | or die "Can't extract yytname[] from table string\n"; | |
276 | my $fields = $1; | |
d5c6462e DM |
277 | $fields =~ s{"([^"]+)"} |
278 | { "toketype_" . | |
279 | (defined $tokens{$1} ? $tokens{$1} : $default_token) | |
280 | }ge; | |
281 | $fields =~ s/, \s* 0 \s* $//x | |
282 | or die "make_type_tab: couldn't delete trailing ',0'\n"; | |
283 | ||
0539ab63 | 284 | return |
d5c6462e DM |
285 | "\ntypedef enum {\n\t" |
286 | . join(", ", map "toketype_$_", sort keys %types) | |
287 | . "\n} toketypes;\n\n" | |
288 | . "/* type of each token/terminal */\n" | |
d5c6462e DM |
289 | . "static const toketypes yy_type_tab[] =\n{\n" |
290 | . $fields | |
291 | . "\n};\n"; | |
0539ab63 DM |
292 | } |
293 | ||
294 | ||
0de566d7 DM |
295 | sub my_system { |
296 | system(@_); | |
297 | if ($? == -1) { | |
d5c6462e | 298 | die "failed to execute command '@_': $!\n"; |
0de566d7 DM |
299 | } |
300 | elsif ($? & 127) { | |
301 | die sprintf "command '@_' died with signal %d\n", | |
302 | ($? & 127); | |
303 | } | |
304 | elsif ($? >> 8) { | |
305 | die sprintf "command '@_' exited with value %d\n", $? >> 8; | |
306 | } | |
307 | } |