Debian Codegraph @

mkparse

raw
1 #!/usr/local/bin/perl
2
3 # $Id: mkparse,v 1.2 2002/03/25 07:39:46 gbarr Exp $
4
5 my($source,$dest) = @ARGV;
6
7 system qw(byacc -P),$source;
8
9 open(OUT, ">$dest") or die "Cannot open $dest: $!\n";
10
11 open(IN, "cpp y.tab.pl|");
12
13 %var = ();
14 %seen = ();
15 %state = ();
16
17 while(<IN>) {
18
19 # In perl the stack size is not a problem
20 next if /YYSTACKSIZE/;
21
22 next if /^# \d+ "(.*?)"/ and $1 ne "parser.y";
23
24 # Replace variables with constant subs
25 if(s/^\$([A-Z][A-Z_0-9]*)\s*=\s*(\d+)\s*;/sub const$1 () { $2 }/) {
26 $var{$1} = "const" . $1 . "()";
27 }
28 s/
29 (\$([A-Z][A-Z_0-9]*))
30 /
31 exists $var{$2} ? $var{$2} : $1
32 /xeg;
33
34 # Comment out the id line
35 s/^(?=\$yysccsid)/#/;
36
37 # Use my variables
38 s/^(\@yy\w+\s+=)/my $1/;
39
40 # Make yyparse return $$ for the top terminal
41 if (/^sub yyparse/ .. /^\}\s*#\s*yyparse/) {
42 s/\breturn\(1\)/return undef/;
43 s/\breturn\(0\)/return \$yyvs[\$yyvsp]/;
44 }
45
46 # Are we inside the switch statement ?
47 my $ln = /^\s+switch:/ .. /^\s+\$yyssp -= \$yym;/;
48
49 # Change all the if statements in the switch to labels
50 if ( $ln ) {
51 s/^if \(\$yyn == (\d+)\s*\)/State$1:/ and $state{"State$1"} = '';
52 }
53
54 # fix an uninit bug
55 s/^(\s*)(?=\$yycheck\[\$yyn\]\s+==)/$1\$yyn <= \$#yycheck && /;
56
57 print OUT;
58
59 # Print the goto for the switch
60 print OUT <<'ESQ' if $ln == 2;
61 my $label = "State$yyn";
62 goto $label if exists $yystate{$label};
63 last switch;
64 ESQ
65 }
66
67 # output table with names of labels that exist
68 my $states = '%yystate = (\'' . join("','",%state) . "');\n";
69 $states =~ s/(.{60,75},)/$1\n/g;
70 print OUT $states,"\n1;\n";
71
72 close OUT;
73 close IN;
74