3 # regen_perly.pl, DAPM 12-Feb-04
5 # Copyright (c) 2004, 2005 Larry Wall
7 # Given an input file perly.y, run bison on it and produce
8 # the following output files:
10 # perly.h standard bison header file with minor doctoring of
11 # #line directives plus adding a #ifdef PERL_CORE
13 # perly.tab the parser table C definitions extracted from the bison output
14 # plus an extra table generated by this script.
16 # perly.act the action case statements extracted from the bison output
18 # Note that perly.c is *not* regenerated - this is now a static file which
19 # is not dependent on perly.y any more.
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
25 # Note that temporary files of the form perlytmp.h and perlytmp.c are
26 # created and then deleted during this process
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
32 sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" }
39 if (@ARGV >= 2 and $ARGV[0] eq '-b') {
44 my $y_file = shift || 'perly.y';
46 usage unless @ARGV==0 && $y_file =~ /\.y$/;
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/;
54 # the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive
56 die "$0: must be run on an ASCII system\n" unless ord 'A' == 65;
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
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.
68 my $version = `$bison -V`;
69 unless ($version =~ /\b(1\.875[a-z]?|2\.[013])\b/) { die <<EOF; }
71 You have the wrong version of bison in your path; currently 1.875
72 2.0, 2.1 or 2.3 is required. Try installing
73 http://ftp.gnu.org/gnu/bison/bison-2.1.tar.gz
74 or similar. Your bison identifies itself as:
79 # creates $tmpc_file and $tmph_file
80 my_system("$bison -d -o $tmpc_file $y_file");
82 open CTMPFILE, $tmpc_file or die "Can't open $tmpc_file: $!\n";
84 { local $/; $clines = <CTMPFILE>; }
85 die "failed to read $tmpc_file: length mismatch\n"
86 unless length $clines == -s $tmpc_file;
89 my ($actlines, $tablines) = extract($clines);
91 $tablines .= make_type_tab($y_file, $tablines);
93 chmod 0644, $act_file;
94 open ACTFILE, ">$act_file" or die "can't open $act_file: $!\n";
95 print ACTFILE $actlines;
97 chmod 0444, $act_file;
99 chmod 0644, $tab_file;
100 open TABFILE, ">$tab_file" or die "can't open $tab_file: $!\n";
101 print TABFILE $tablines;
103 chmod 0444, $tab_file;
107 # Wrap PERL_CORE round the symbol definitions. Also, the
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.
111 open TMPH_FILE, $tmph_file or die "Can't open $tmph_file: $!\n";
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";
121 next if /^#line \d+ ".*"/;
129 print "rebuilt: $h_file $tab_file $act_file\n";
141 ^/* YYFINAL[^\n]+\n #optional comment
143 \# \s* define \s* YYFINAL # first #define
144 .*? # other defines + most tables
145 yystos\[\]\s*= # start of last table
147 }\s*; # end of last table
149 or die "Can't extract tables from $tmpc_file\n";
154 switch \s* \( \s* \w+ \s* \) \s* { \s*
156 case \s* \d+ \s* : \s*
157 \#line [^\n]+"\Q$y_file\E"
162 ( \s* /\* .*? \*/ \s* )* # optional C-comments
167 \#line[^\n]+\.simple"
172 or die "Can't extract actions from $tmpc_file\n";
175 # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them.
176 $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm;
178 return $actlines. "\n", $tablines. "\n";
181 # Generate a table, yy_type_tab[], that specifies for each token, what
182 # type of value it holds.
184 # Read the .y file and extract a list of all the token names and
185 # non-terminal names; then scan the string $tablines for the table yytname,
186 # which gives the token index of each token/non-terminal; then use this to
187 # create yy_type_tab.
189 # ie given (in perly.y),
196 # and (in $tablines),
198 # yytname[] = { "A" "B", "C", "D", "E" };
202 # typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes;
204 # static const toketypes yy_type_tab[]
205 # = { toketype_opval, toketype_ival, toketype_pval,
206 # toketype_opval, toketype_ival }
208 # where "E" has the default type. The default type is determined
209 # by the __DEFAULT__ comment next to the appropriate union member in
213 my ($y_file, $tablines) = @_;
217 open my $fh, '<', $y_file or die "Can't open $y_file: $!\n";
220 m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x
221 or die "$y_file: can't parse __DEFAULT__ line: $_";
222 die "$y_file: duplicate __DEFAULT__ line: $_"
223 if defined $default_token;
228 next unless /^%(token|type)/;
229 s/^%(token|type)\s+<(\w+)>\s+//
230 or die "$y_file: unparseable token/type line: $_";
231 $tokens{$_} = $2 for (split ' ', $_);
234 die "$y_file: no __DEFAULT__ token defined\n" unless $default_token;
235 $types{$default_token} = 1;
237 $tablines =~ /^\Qstatic const char *const yytname[] =\E\n
242 or die "Can't extract yytname[] from table string\n";
244 $fields =~ s{"([^"]+)"}
246 (defined $tokens{$1} ? $tokens{$1} : $default_token)
248 $fields =~ s/, \s* 0 \s* $//x
249 or die "make_type_tab: couldn't delete trailing ',0'\n";
252 "\ntypedef enum {\n\t"
253 . join(", ", map "toketype_$_", sort keys %types)
254 . "\n} toketypes;\n\n"
255 . "/* type of each token/terminal */\n"
256 . "static const toketypes XXX;\n"
257 . "static const toketypes yy_type_tab[] =\n{\n"
266 die "failed to execute command '@_': $!\n";
269 die sprintf "command '@_' died with signal %d\n",
273 die sprintf "command '@_' exited with value %d\n", $? >> 8;