This is a prototype for a simple tool to show the derivation of your source code from your grammar. A useful script might be:
#!/bin/sh file=$1 c- -d $file 2>&1 | decoder.plHere is the decoder.pl script. Feel free to improve this script and send it back to me with a descirption of the improvements.
#!/usr/bin/perl -w
# Given the debug out put of a bison parser this
# construct the derivation of a c- file using
# your grammar.
#
# USAGE: c- -d yourSource.c- 2>&1 | decoder.pl
#
@stack = ();
while (<>) {
chomp();
s/ \(\)//;
if (/^Shifting/) {
s/.*token //;
push @stack, $_;
print "Shift: -> ", $_, "\n";
print "Stack: ", join(' ', @stack), "\n";
}
if (/^Reducing/) {
s/[^,]*, //;
/(.*) ?-> (.*)/;
$from = $1;
$to = $2;
# if ($to !~ /^@/) {
print "Reduce: ", $_, "\n";
@s = split(/ /, $from);
$end = $#s+1;
for ($i=0; $i<$end; $i++) {
pop @stack;
}
push @stack, $to;
print "Stack: ", join(' ', @stack), "\n";
# }
}
if (/^Error: popping /) {
s/^Error: popping \w+ /Pop: /;
print $_, " -> \n";
pop @stack;
print "Stack: ", join(' ', @stack), "\n";
}
if (/^Error: discarding token /) {
s/^Error: discarding token /Toss: /;
print $_, "\n";
print "Stack: ", join(' ', @stack), "\n";
}
if (/\*\*/) {
print "$_\n";
}
}
| Robert Heckendorn | Last updated: |