#! /bin/sh -
#
# Usage:
#
#     ./bison2.sh /path/to/bison --args... source.y
#
# The Bison grammars are compatible with versions 2 and 3 of Bison,
# but the options changed syntax from 2 to 3.
#
# This prepares a version of source.y with the options regressed to v2
# style, then runs the given bison binary with that as source, and
# (using option -o) the same outputs as would otherwise have been generated.
#
# This file is part of Beastie <https://purl.org/nxg/dist/beastie>
# SPDX-FileCopyrightText: 2024 Norman Gray <https://nxg.me.uk>
# SPDX-License-Identifier: BSD-2-Clause

set -e

# gather into $cmd all of the arguments except the last
cmd=
while test $# -gt 1; do
    cmd="$cmd $1"
    shift
done

f=`basename $1 .y`
tmpfile=$f.y2

trap "rm -f $tmpfile" EXIT

# Prepare a version of the .y source with adjusted options,
# compatible with Bison v2.x.
sed -e 's/^%define.*verbose$/%error-verbose/' \
    -e 's/^%define.*pure$/%pure-parser/' \
    $f.y >$tmpfile

echo $cmd -o $f.tab.c $tmpfile
$cmd -o $f.tab.c $tmpfile
