#!/usr/bin/perl

#
# Checks if there were shift/reduce conflicts and generates an error
# if the maximum number is exceeded.
#

$Y_OUTPUT = "parse_y.output";

$shift_max = 1;
$reduce_max = 0;

#
# Check if yacc failed (left a temp file laying around)
#

sub fatal {
    local($msg) = @_;
    print STDERR $msg;
    unlink <parse_y.tab.[ch]>;
    unlink <parse_yacc.tmp>;
    exit 1;
}

-e "parse_yacc.tmp" && &fatal("Invalid yacc grammar.\n");

open(Y_OUTPUT, $Y_OUTPUT) || die "Could not open $Y_OUTPUT";

($_ = <Y_OUTPUT>) || die "Could not find correct line in $Y_OUTPUT"
    until ($shift,$reduce) =
	m:([0-9]+) shift/reduce, ([0-9]+) reduce/reduce conflicts reported:;

$shift > $shift_max &&
    &fatal("Too many shift/reduce conflicts: grammar ambiguous.\n");
$reduce > $reduce_max &&
    &fatal("Too many reduce/reduce conflicts: grammar ambiguous.\n");

exit 0;
