Debian Codegraph @

Parser/bisonpre

raw
1 #!/usr/bin/perl -w
2 # See copyright, etc in below POD section.
3 ######################################################################
4
5 require 5.006_001;
6 use Getopt::Long;
7 use IO::File;
8 use Pod::Usage;
9 use strict;
10 use vars qw($Debug $VERSION);
11
12 $VERSION = '3.482';
13
14 our $Self;
15
16 #======================================================================
17 # main
18
19 our $Opt_Debug;
20 our $Opt_Definitions;
21 our $Opt_File_Prefix;
22 our $Opt_Name_Prefix;
23 our $Opt_Output;
24 our $Opt_Token_Table;
25 our $Opt_Verbose;
26 our $Opt_Yacc = "bison";
27 our $Opt_Input;
28
29 autoflush STDOUT 1;
30 autoflush STDERR 1;
31 Getopt::Long::config("no_auto_abbrev");
32 if (! GetOptions (
33 # Local options
34 "help" => \&usage,
35 "version" => sub { print "Version $VERSION\n"; exit(0); },
36 "yacc=s" => \$Opt_Yacc,
37 # Passed to Bison
38 "t|debug" => sub { $Opt_Debug = 1; },
39 "b|file-prefix=s" => \$Opt_File_Prefix,
40 "d" => \$Opt_Definitions,
41 "k|token-table" => \$Opt_Token_Table,
42 "o=s" => \$Opt_Output,
43 "p|name-prefix=s" => \$Opt_Name_Prefix,
44 "v|verbose" => \$Opt_Verbose,
45 "<>" => \&parameter,
46 )) {
47 die "%Error: Bad usage, try 'bisonpre --help'\n";
48 }
49
50 $Opt_Input or die "bisonpre: %Error: input file not specified\n";
51 $Opt_Output or die "bisonpre: %Error: --o option is required\n";
52
53 process();
54
55 #----------------------------------------------------------------------
56
57 sub usage {
58 print "Version $VERSION\n";
59 pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1);
60 exit(1);
61 }
62
63 sub parameter {
64 my $param = shift;
65 if (!defined $Opt_Input) {
66 $Opt_Input = $param;
67 } else {
68 die "bisonpre: %Error: Unknown parameter: $param\n";
69 }
70 }
71
72 #######################################################################
73
74 sub process {
75 remove_outputs();
76
77 $Self->{bison_version} = bison_version_check();
78 my $supports_report = ($Self->{bison_version} >= 2.3);
79
80 clean_input($Opt_Input, tmp_prefix().".y");
81
82 # Run bison
83 my $command = ($Opt_Yacc
84 .($Opt_Debug?" -t":"")
85 .($Opt_Definitions?" -d":"")
86 .($Opt_Token_Table?" -k":"")
87 .($Opt_Verbose?" -v":"")
88 .(($Opt_Verbose && $supports_report)?" --report=itemset --report=lookahead":"")
89 # -p required for GLR parsers; they write to -p basename, not -o
90 .($Opt_Name_Prefix?" -p $Opt_Name_Prefix":"")
91 ." -b ".tmp_prefix()
92 ." -o ".tmp_prefix().".c"
93 ." ".tmp_prefix().".y" );
94
95 print " $command\n";
96 system $command;
97 my $status = $?;
98 if ($status != 0) {
99 remove_outputs();
100 my $v = bison_version_check();
101 die "bisonpre: %Error: $Opt_Yacc version $v run failed due to errors\n";
102 }
103
104 clean_output(tmp_prefix().".output",output_prefix().".output", 1,0);
105 warning_check(output_prefix().".output");
106
107 clean_output(tmp_prefix().".c", output_prefix().".c", 0,1);
108 clean_output(tmp_prefix().".h", output_prefix().".h", 0,1);
109 remove_tmp();
110 }
111
112 sub tmp_prefix {
113 return output_prefix()."_pretmp";
114 }
115
116 sub output_prefix {
117 my $o;
118 if ($Opt_Output) {
119 (my $o = $Opt_Output) =~ s!\.[^.]*$!!;
120 return $o;
121 } else {
122 return $Opt_File_Prefix.".tab";
123 }
124 }
125
126 sub remove_tmp {
127 unlink(tmp_prefix().".c"); # Ok if errors
128 unlink(tmp_prefix().".h"); # Ok if errors
129 unlink(tmp_prefix().".output"); # Ok if errors
130 }
131
132 sub remove_outputs {
133 remove_tmp();
134 unlink(output_prefix().".c"); # Ok if errors
135 unlink(output_prefix().".h"); # Ok if errors
136 # We don't remove .output file, as it's useful for debugging errors
137 }
138
139 sub bison_version_check {
140 my $v = `$Opt_Yacc --version`;
141 if ($v && $v =~ /([0-9]+\.[0-9]+)/) {
142 my $v = $1;
143 ($v >= 1.875) or die "bisonpre: %Error: '$Opt_Yacc' is version $v; version 1.875 or newer is required\n";
144 return $v;
145 } else {
146 die "bisonpre: %Error: '$Opt_Yacc' is not installed, or not working\n";
147 }
148 }
149
150 sub clean_output {
151 my $filename = shift;
152 my $outname = shift || $filename;
153 my $is_output = shift;
154 my $is_c = shift;
155 print " edit $filename $outname\n";
156
157 my $fh = IO::File->new("<$filename") or die "%Error: $! $filename\n";
158 my @lines = $fh->getlines;
159 $fh->close;
160
161 (my $basename = tmp_prefix().".") =~ s!.*/!!;
162 $basename = quotemeta($basename);
163 (my $newbase = $Opt_Input) =~ s!.*/!!;
164 $newbase =~ s/\.y/./;
165
166 if ($is_output) {
167 my %state_line; my $l=0;
168 foreach my $line (@lines) {
169 $l++;
170 # We add a colon so it's easy to search for the definition
171 $state_line{$1} = $l if $line =~ s/^state (\d+)\s*$/state $1:/;
172 }
173 my @out;
174 foreach my $line (@lines) {
175 if ($line =~ /^State (\d+) (conflicts)/) {
176 chomp $line;
177 $line .= " // line $state_line{$1}" if $state_line{$1};
178 $line .= "\n";
179 }
180 push @out, $line;
181 }
182 @lines = @out; @out = ();
183 }
184 if ($is_c) {
185 my %token_values;
186 my $in_en=0;
187 foreach my $line (@lines) {
188 $in_en=1 if $line =~ /enum\s+yytokentype/;
189 $in_en=0 if $line =~ /;/;
190 $token_values{$2} = $1 if $in_en && $line =~ /\b(\S+) = (\d+)/;
191 }
192 my @out;
193 foreach my $line (@lines) {
194 if ($line =~ /BISONPRE_TOKEN_NAMES/) {
195 push @out, $line;
196 foreach my $tv (sort keys %token_values) {
197 push @out, sprintf("\tcase %d: return \"%s\";\n",
198 $tv, $token_values{$tv});
199 }
200 next;
201 }
202 push @out, $line;
203 }
204 @lines = @out; @out = ();
205 }
206
207 $fh = IO::File->new(">$outname") or die "%Error: $! writing $outname\n";
208 foreach my $line (@lines) {
209 # Fix filename refs
210 $line =~ s!$basename!$newbase!g;
211 # Fix bison 2.3 and GCC 4.2.1
212 $line =~ s!\(YY_\("!(YY_((char*)"!g;
213 # Fix bison 2.3 glr-parser warning about yyerrorloc.YYTYPE::yydummy uninit
214 $line =~ s!(YYLTYPE yyerrloc;)!$1 yyerrloc.yydummy=0;/*bisonpre*/!g;
215 $fh->write($line);
216 }
217 $fh->close;
218 }
219
220 sub warning_check {
221 my $filename = shift;
222
223 my $fh = IO::File->new("<$filename") or die "%Error: $! $filename\n";
224 while (defined(my $line = $fh->getline)) {
225 if ($line =~ /(conflicts|warning:|^useless)/i) {
226 die "%Error: $filename:$.: $line\n";
227 }
228 }
229 $fh->close;
230 }
231
232 #######################################################################
233
234 sub clean_input {
235 my $filename = shift;
236 my $outname = shift || $filename; # Can == filename if desired
237 print " edit $filename $outname\n";
238
239 $Self->{filename} = $filename;
240 my $fh = IO::File->new("<$filename") or die "%Error: $! $filename\n";
241 my @lines = $fh->getlines;
242 $fh->close;
243
244 # Find "%tokens<type>:"
245 # Find "rule<type>:" and replace with just "rule:"
246 my %types;
247 my %rules; $Self->{rules} = \%rules;
248 my %tokens;
249 my $last_rule;
250 my $section = 1;
251 {
252 my @linesin = @lines; @lines=(); my $l=0;
253 foreach my $line (@linesin) {
254 $l++;
255 # ^/ to prevent comments from matching
256 $line =~ m!^[a-zA-Z0-9_<>]+:[^/]*[a-zA-Z]! and die "%Error: $filename:$l: Move text on rule line to next line: $line\n";
257 if ($line =~ /^%%/) {
258 $section++;
259 if ($section==2) { $last_rule = undef; }
260 }
261 elsif ($line =~ s/^([a-zA-Z0-9_]+)<(\S*)>:/$1:/) {
262 !$rules{$1}{name} or die "%Error: $filename:$l: Redeclaring '$1': $line\n";
263 $types{$2}{$1} = 1;
264 $rules{$1}{name} = $1;
265 $rules{$1}{type} = $2;
266 !$last_rule or die "%Error: $filename:$l: Unterminated previous rule\n";
267 $last_rule = $1;
268 } elsif ($line =~ /^([a-zA-Z0-9_]+):/) {
269 !$rules{$1}{name} or die "%Error: $filename:$l: Redeclaring '$1': $line\n";
270 $rules{$1}{name} = $1;
271 $rules{$1}{type} = "";
272 !$last_rule or die "%Error: $filename:$l: Unterminated previous rule\n";
273 $last_rule = $1;
274 }
275 push @lines, $line;
276 # Now clean the line and extract some more info
277 (my $cline = $line) =~ s/\/\/.*$/\n/;
278 (my $rline = $line) =~ s/\/\/.*$/\n/;
279 if ($cline =~ /^\s*;/) {
280 $last_rule or die "%Error: $filename:$l: Stray semicolon\n";
281 $last_rule = undef;
282 } elsif ($last_rule) {
283 $rules{$last_rule}{rules_and_productions} .= $cline;
284 }
285 if ($cline =~ /^%token\s*<(\S+)>\s*(\S+)/) {
286 !$tokens{$2} or die "%Error: $filename:$l: Redeclaring '$2': $line\n";
287 $tokens{$2} = $1;
288 }
289 foreach my $tok (split /[^a-zA-Z0-9_]+/, $cline) {
290 if ($last_rule && $tok=~/^[a-zA-Z]/) {
291 #print "TT $last_rule $tok\n";
292 $rules{$last_rule}{subrules}{$tok} = 1;
293 $rules{$tok}{parentrules}{$last_rule} = 1;
294 }
295 }
296 }
297 }
298
299 #use Data::Dumper; print Dumper(\%rules);
300
301 # Replace BISONPRE_VERSION(ver,,...) with expanded list
302 {
303 my @linesin = @lines; @lines=(); my $l=0;
304 foreach my $line (@linesin) {
305 $l++;
306 if ($line =~ /BISONPRE_VERSION/) {
307 # 1 3 4
308 ($line =~ /BISONPRE_VERSION\((\S+)\s*,\s*((\S+)\s*,)?\s*([^\),]+)\)\s*$/)
309 or die "%Error: $filename:$l: Bad form of BISONPRE_VERSION: $line\n";
310 my $ver=$1; my $ver_max=$3; my $cmd=$4;
311 if ($Self->{bison_version} >= $1
312 && (!$ver_max || $Self->{bison_version} <= $ver_max)) {
313 $line = $cmd."\n";
314 } else {
315 $line = "//NOP: $line";
316 }
317 }
318 push @lines, $line;
319 }
320 }
321
322 # Replace BISONPRE_NOT(type,...) with expanded list
323 {
324 my @linesin = @lines; @lines=(); my $l=0;
325 foreach my $line (@linesin) {
326 $l++;
327 if ($line =~ /BISONPRE_NOT/) {
328 ($line =~ s/BISONPRE_NOT\((\S+)\)\s*(\{[^}]+})\s*$//)
329 or die "%Error: $filename:$l: Bad form of BISONPRE_NOT: $line\n";
330 my $endtok = $1; my $action = $2;
331 my @endtoks = split(/,/, $endtok);
332 map { $tokens{$_} or die "%Error: $filename:$l: Can't find definition for token: $_\n"
333 } @endtoks;
334 # Push it all onto one line to avoid error messages changing
335 my $bar = "";
336 tok:
337 foreach my $tok (sort keys %tokens) {
338 foreach (@endtoks) {
339 next tok if $tok eq $_;
340 }
341 if ($endtok ne $tok) {
342 $line .= "\t$bar $tok $action";
343 $bar = "|";
344 }
345 }
346 $line .= "\n";
347 }
348 push @lines, $line;
349 }
350 }
351
352 # Replace BISONPRE_COPY(type,{code})
353 {
354 my @linesin = @lines; @lines=(); my $l=0;
355 foreach my $line (@linesin) {
356 $l++;
357 if ($line =~ /BISONPRE_COPY/) {
358 $line = _bisonpre_copy($line,$l,0);
359 }
360 push @lines, $line;
361 }
362 }
363
364 # Replace ~[x]~ - must be after BISONPRE_COPY expansion
365 {
366 my @linesin = @lines; @lines=(); my $l=0;
367 foreach my $line (@linesin) {
368 $l++;
369 $line =~ s/~[a-zA-Z0-9_]+~//g;
370 push @lines, $line;
371 }
372 }
373
374 # Find "BISONPRE_TYPES"
375 {
376 my @linesin = @lines; @lines=(); my $l=0;
377 my $needmore = 0;
378 foreach my $line (@linesin) {
379 $l++;
380 if ($line =~ m!//BISONPRE_TYPES!) {
381 push @lines, $line;
382 foreach my $type (sort keys %types) {
383 next if !$type;
384 my $line = "%type<$type>\t";
385 foreach my $rule (sort keys %{$types{$type}}) {
386 $line.=" ".$rule;
387 }
388 $line .= "\n";
389 push @lines, $line;
390 $needmore++
391 }
392 } elsif ($needmore) {
393 # Bison doesn't have a #line directive, so we need somewhere to insert into
394 $line =~ s!^\s*//.*$!!;
395 ($line =~ m/^\s*$/) or die "%Error: $filename:$l: Need $needmore more blank lines to insure line numbers are constant\n";
396 $needmore--;
397 } else {
398 push @lines, $line;
399 }
400 }
401 }
402
403 $fh = IO::File->new(">$outname") or die "%Error: $! writing $outname\n";
404 foreach my $line (@lines) {
405 $fh->write($line);
406 }
407 $fh->close;
408 }
409
410 sub _bisonpre_copy {
411 my $text = shift;
412 my $l = shift;
413 my $depth = shift;
414 while ($text =~ /BISONPRE_COPY/) {
415 ($text =~ s/BISONPRE_COPY(_ONCE)?\((\S+)\s*,\s*\{([^}]*)}\s*\)/{HERE}/)
416 or die "%Error: $Self->{filename}:$l: Bad form of BISONPRE_NOT: $text\n";
417 my $once = $1; my $rule = $2; my $code = $3;
418 $Self->{rules}{$rule} or die "%Error: $Self->{filename}:$l: Can't find definition for rule: $rule\n";
419 if ($depth > 0 && $once) {
420 # _ONCE means don't inherit
421 $text =~ s/\|[ \t]+{HERE}//; # Don't OR in nothing
422 $text =~ s/{HERE}//;
423 } else {
424 # Push it all onto one line to avoid error messages changing
425 my $insert = $Self->{rules}{$rule}{rules_and_productions};
426 $insert =~ s/^\S+://g; # Strip rule name
427 # Recurse so BISONPRE under B
428 #print "COPY $l code $code\n";
429 #print "COPY $l in $insert\n";
430 $_=$insert; eval("$code; \$_;"); $insert = $_;
431 #print "COPY $l out $insert\n";
432 while ($insert =~ s/[ \t\n]+\n/\n/go) {}
433 while ($insert =~ s/\n/ /go) {} # Optional - preserve line numbering
434 $text =~ s/{HERE}/$insert/;
435 }
436 $depth++;
437 }
438 return $text;
439 }
440
441 #######################################################################
442 __END__
443
444 =pod
445
446 =head1 NAME
447
448 bisonpre - Bison wrapper with pre and post processing
449
450 =head1 SYNOPSIS
451
452 bisonpre --yacc bison --debug --verbose --defines X.h -k $< -pX -o X.c
453
454
455 =head1 DESCRIPTION
456
457 Bisonpre is a wrapper for the Bison YACC replacement. Input to Bison is
458 preprocessed with substitution as described below under EXTENSIONS. Output
459 from Bison is checked for additional errors, and corrected to work around
460 various compile warnings.
461
462 =head1 EXTENSIONS
463
464 =over 4
465
466 =item //BISONPRE_TYPES
467
468 This is expanded into %type declarations.
469
470 =item ~[a-z]+~
471
472 Any text matching ~[a-z]+~ is removed. This allows optional text to be
473 used only when the rule containing the ~~ is used in a BISONPRE_COPY.
474
475 =item rule_label<type>:
476
477 This allows the label declaring a rule to also specify the type of the
478 rule. The type will be inserted where /*BISONPRE_TYPES*/ is encountered.
479
480 =item BISONPRE_COPY(rule, {code})
481
482 Copy the rules and productions from the specified rule, filter through the
483 Perl code provided in the {} and insert here into the output file.
484
485 =item BISONPRE_COPY_ONCE(rule, {code})
486
487 As with BISONPRE_COPY, but if called from underneath another BISONPRE_COPY
488 rule, ignore it.
489
490 =item BISONPRE_NOT(token[, token...])
491
492 Create a rule that matches every token except for those specified.
493
494 =item BISONPRE_VERSION(ver, cmd)
495
496 If the bison version is >= the specified version, include the given command.
497
498 =back
499
500 =head1 ARGUMENTS
501
502 =over 4
503
504 =item -b file-prefix
505 =item --file-prefix=file-prefix
506
507 Passed to bison.
508
509 Specify a prefix to use for all bison output file names. The names are
510 chosen as if the input file were named file-prefix.c.
511
512 =item -d
513
514 Passed to bison.
515
516 Write an extra output file containing macro definitions for the token type
517 names defined in the grammar and the semantic value type YYSTYPE, as well
518 as a few extern variable declarations. If the parser output file is named
519 name.c then this file is named name.h. This output file is essential if
520 you wish to put the definition of yylex in a separate source file, because
521 yylex needs to be able to refer to token type codes and the variable
522 yylval.
523
524 =item --help
525
526 Displays this message and program version and exits.
527
528 =item -k
529 =item --token-table
530
531 Passed to bison.
532
533 This switch causes the name.tab.c output to include a list of token names
534 in order by their token numbers; this is defined in the array yytname.
535 Also generated are #defines for YYNTOKENS, YYNNTS, YYNRULES, and YYNSTATES.
536
537 =item -t
538 =item --debug
539
540 Passed to bison.
541
542 In the parser file, define the macro YYDEBUG to 1 if it is not already
543 defined, so that the debugging facilities are compiled.
544
545 =item -v
546 =item --verbose
547
548 Passed to bison.
549
550 Write an extra output file containing verbose descriptions of the parser
551 states and what is done for each type of look-ahead token in that state.
552 This file also describes all the conflicts, both those resolved by operator
553 precedence and the unresolved ones. The file's name is made by removing
554 .tab.c or .c from the parser output file name, and adding .output instead.
555 Therefore, if the input file is foo.y, then the parser file is called
556 foo.tab.c by default. As a consequence, the verbose output file is called
557 foo.output.
558
559 =item --version
560
561 Print the version number and exit.
562
563 =item --yacc
564
565 Specify the name of the bison executable, defaults to "bison."
566
567 =back
568
569 =head1 DISTRIBUTION
570
571 This is part of the L<https://www.veripool.org/> free Verilog EDA software
572 tool suite. The latest version is available from CPAN and from
573 L<https://www.veripool.org/>.
574
575 Copyright 2008-2024 by Wilson Snyder. This package is free software; you
576 can redistribute it and/or modify it under the terms of either the GNU
577 Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.
578
579 This program is distributed in the hope that it will be useful, but WITHOUT
580 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
581 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
582 more details.
583
584 =head1 AUTHORS
585
586 Wilson Snyder <[email protected]>
587
588 =head1 SEE ALSO
589
590 C<bison>
591
592 =cut
593
594 ######################################################################
595 ### Local Variables:
596 ### compile-command: "./bisonpre "
597 ### End: