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 | ||
32 | sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" } | |
33 | ||
34 | use warnings; | |
35 | use strict; | |
36 | ||
37 | my $bison = 'bison'; | |
38 | ||
39 | if (@ARGV >= 2 and $ARGV[0] eq '-b') { | |
40 | shift; | |
41 | $bison = shift; | |
42 | } | |
43 | ||
44 | my $y_file = shift || 'perly.y'; | |
45 | ||
46 | usage unless @ARGV==0 && $y_file =~ /\.y$/; | |
47 | ||
48 | (my $h_file = $y_file) =~ s/\.y$/.h/; | |
49 | (my $act_file = $y_file) =~ s/\.y$/.act/; | |
50 | (my $tab_file = $y_file) =~ s/\.y$/.tab/; | |
51 | (my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/; | |
52 | (my $tmph_file = $y_file) =~ s/\.y$/tmp.h/; | |
53 | ||
54 | # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive | |
55 | ||
56 | die "$0: must be run on an ASCII system\n" unless ord 'A' == 65; | |
57 | ||
58 | # check for correct version number. The constraints are: | |
59 | # * must be >= 1.24 to avoid licensing issues. | |
60 | # * it must generate the yystos[] table. Version 1.28 doesn't generate | |
61 | # this; 1.35+ does | |
62 | # * Must produce output which is extractable by the regexes below | |
63 | # * Must produce the right values. | |
64 | # These last two contstraints may well be met by earlier versions, but | |
65 | # I simply haven't tested them yet. If it works for you, then modify | |
66 | # the test below to allow that version too. DAPM Feb 04. | |
67 | ||
68 | my $version = `$bison -V`; | |
20515881 | 69 | unless ($version =~ /\b(1\.875[a-z]?|2\.[01])\b/) { die <<EOF; } |
0de566d7 | 70 | |
2080282f | 71 | You have the wrong version of bison in your path; currently 1.875 |
20515881 RGS |
72 | 2.0 or 2.1 is required. Try installing |
73 | http://ftp.gnu.org/gnu/bison/bison-2.1.tar.gz | |
0de566d7 DM |
74 | or similar. Your bison identifies itself as: |
75 | ||
76 | $version | |
77 | EOF | |
78 | ||
79 | # creates $tmpc_file and $tmph_file | |
80 | my_system("$bison -d -o $tmpc_file $y_file"); | |
81 | ||
82 | open CTMPFILE, $tmpc_file or die "Can't open $tmpc_file: $!\n"; | |
83 | my $clines; | |
84 | { local $/; $clines = <CTMPFILE>; } | |
85 | die "failed to read $tmpc_file: length mismatch\n" | |
86 | unless length $clines == -s $tmpc_file; | |
87 | close CTMPFILE; | |
88 | ||
89 | my ($actlines, $tablines) = extract($clines); | |
90 | ||
0539ab63 DM |
91 | $tablines .= make_opval_tab($y_file, $tablines); |
92 | ||
0de566d7 DM |
93 | chmod 0644, $act_file; |
94 | open ACTFILE, ">$act_file" or die "can't open $act_file: $!\n"; | |
95 | print ACTFILE $actlines; | |
96 | close ACTFILE; | |
97 | chmod 0444, $act_file; | |
98 | ||
99 | chmod 0644, $tab_file; | |
100 | open TABFILE, ">$tab_file" or die "can't open $tab_file: $!\n"; | |
101 | print TABFILE $tablines; | |
102 | close TABFILE; | |
103 | chmod 0444, $tab_file; | |
104 | ||
105 | unlink $tmpc_file; | |
106 | ||
107 | # Wrap PERL_CORE round the symbol definitions. Also, the | |
96f4e226 SH |
108 | # C<#line 30 "perly.y"> confuses the Win32 resource compiler and the |
109 | # C<#line 188 "perlytmp.h"> gets picked up by make depend, so remove them. | |
0de566d7 DM |
110 | |
111 | open TMPH_FILE, $tmph_file or die "Can't open $tmph_file: $!\n"; | |
112 | chmod 0644, $h_file; | |
113 | open H_FILE, ">$h_file" or die "Can't open $h_file: $!\n"; | |
114 | my $endcore_done = 0; | |
115 | while (<TMPH_FILE>) { | |
116 | print H_FILE "#ifdef PERL_CORE\n" if $. == 1; | |
117 | if (!$endcore_done and /YYSTYPE_IS_DECLARED/) { | |
118 | print H_FILE "#endif /* PERL_CORE */\n"; | |
119 | $endcore_done = 1; | |
120 | } | |
96f4e226 | 121 | next if /^#line \d+ ".*"/; |
0de566d7 DM |
122 | print H_FILE $_; |
123 | } | |
124 | close TMPH_FILE; | |
125 | close H_FILE; | |
126 | chmod 0444, $h_file; | |
127 | unlink $tmph_file; | |
128 | ||
129 | print "rebuilt: $h_file $tab_file $act_file\n"; | |
130 | ||
131 | exit 0; | |
132 | ||
133 | ||
134 | sub extract { | |
135 | my $clines = shift; | |
136 | my $tablines; | |
137 | my $actlines; | |
138 | ||
139 | $clines =~ m@ | |
140 | (?: | |
141 | ^/* YYFINAL[^\n]+\n #optional comment | |
142 | )? | |
143 | \# \s* define \s* YYFINAL # first #define | |
144 | .*? # other defines + most tables | |
145 | yystos\[\]\s*= # start of last table | |
146 | .*? | |
147 | }\s*; # end of last table | |
148 | @xms | |
149 | or die "Can't extract tables from $tmpc_file\n"; | |
150 | $tablines = $&; | |
151 | ||
152 | ||
153 | $clines =~ m@ | |
154 | switch \s* \( \s* \w+ \s* \) \s* { \s* | |
155 | ( | |
156 | case \s* \d+ \s* : \s* | |
2ade6388 | 157 | \#line [^\n]+"\Q$y_file\E" |
0de566d7 DM |
158 | .*? |
159 | ) | |
160 | } | |
161 | \s* | |
162 | ( \s* /\* .*? \*/ \s* )* # optional C-comments | |
163 | \s* | |
164 | ( | |
165 | \#line[^\n]+\.c" | |
166 | | | |
167 | \#line[^\n]+\.simple" | |
168 | ) | |
169 | @xms | |
170 | or die "Can't extract actions from $tmpc_file\n"; | |
171 | $actlines = $1; | |
172 | ||
173 | return $actlines. "\n", $tablines. "\n"; | |
174 | } | |
175 | ||
0539ab63 DM |
176 | # read a .y file and extract a list of all the token names and |
177 | # non-terminal names that are declared to be of type opval | |
178 | # then scan the string $tablines for the table yytname which gives | |
179 | # the token index of each token/non-terminal, then use this to | |
180 | # create a new table, indexed by token number, which indicates | |
181 | # whether that token is of type opval. | |
182 | # | |
183 | # ie given | |
184 | # %token <opval> A B | |
185 | # %type <opval> C D | |
186 | # | |
187 | # and yytname[] = { "A" "B", "C", "D", "E", "F" }; | |
188 | # | |
189 | # then return | |
190 | # static const int yy_is_opval[] = { 1, 1, 1, 1, 0, 0 } | |
191 | ||
192 | sub make_opval_tab { | |
193 | my ($y_file, $tablines) = @_; | |
194 | my %tokens; | |
195 | open my $fh, '<', $y_file or die "Can't open $y_file: $!\n"; | |
196 | while (<$fh>) { | |
197 | next unless s/^%(token|type)\s+<opval>\s+//; | |
198 | $tokens{$_} =1 for (split ' ', $_); | |
199 | } | |
200 | ||
201 | $tablines =~ /^\Qstatic const char *const yytname[] =\E\n | |
202 | {\n | |
203 | (.*?) | |
204 | ^}; | |
205 | /xsm | |
206 | or die "Can't extract yytname[] from table string\n"; | |
207 | my $fields = $1; | |
208 | $fields =~ s/"([^"]+)"/$tokens{$1}||0/ge; | |
209 | return | |
210 | "/* which symbols are of type opval */\n" . | |
211 | "static const int yy_is_opval[] =\n{\n" . $fields . "\n};\n"; | |
212 | } | |
213 | ||
214 | ||
0de566d7 DM |
215 | sub my_system { |
216 | system(@_); | |
217 | if ($? == -1) { | |
218 | die "failed to execute comamnd '@_': $!\n"; | |
219 | } | |
220 | elsif ($? & 127) { | |
221 | die sprintf "command '@_' died with signal %d\n", | |
222 | ($? & 127); | |
223 | } | |
224 | elsif ($? >> 8) { | |
225 | die sprintf "command '@_' exited with value %d\n", $? >> 8; | |
226 | } | |
227 | } |