#!/bin/sh
######################################################################
# This script allows one to create a parser using
# LambdaYacc, created by Chuck Liang (liang@cs.umn.edu),
# from the command prompt without having to manually enter
# commands in Teyjus.  It takes the place of these
# steps:
#        tjcc [grammar name]
#        tjlink [grammar name]
#        tjsim [grammar name]
#        genparser "[grammar name]" "[parser name]" (in Teyjus)
#        tjcc [parser name]
#        tjlink [parser name]
# 
# Created 1/2002 by Kamal Aboul-Hosn
#                   kxa153@psu.edu
######################################################################

# Compile lyacc files
tjcc blists
tjcc control
tjcc lambdayacc
tjcc lyaccshares
tjcc maps
tjcc string
tjcc tokenizer
tjcc parser


# Test for proper arguments
if [ -z "$1" ]; then 
    echo usage: $0 grammar parser [-fc]
    exit
fi
if [ -z "$2" ]; then 
    echo usage: $0 grammar parser [-fc]
    exit
fi

if [ "$3" = '-fc' ]; then
    cp $1.mod $1.mod.b
    
    tjcc $1
    tjlink $1

    if [ $? = 0 ]; then
        tjsim --solve "generate_fc $1,halt." $1
    fi
fi

# Compile grammar file
echo "Compiling Grammar"
cat $1.sig | grep "type $2 o." > /dev/null
if [ $? != 0 ]; then
    echo "type $2 o." >> $1.sig
fi

tjcc $1
tjlink $1

if [ $? = 0 ]; then

    # Add name of parser to grammar's .sig file
    echo "Creating Parser"
    cat $1.sig | grep "type $2 o." > /dev/null
    # Execute grammar code, create parser
    rm -f $2.mod
    rm -f $2.sig
    if [ "$3" = '-fc' ]; then
        mv $1.mod $1.mod.b2
        mv $1.mod.b $1.mod
    fi
    tjsim --batch --solve "genparser \"$1\" \"$2\",!." $1

    echo "Parser written to $2.mod.  Compiling"
    if [ $? = 0 ]; then
	# Compile parser
        if [ "$3" = '-fc' ]; then
            mv $1.mod $1.mod.b
            mv $1.mod.b2 $1.mod
        fi
        tjcc $2
        tjlink $2

        if [ $? = 0 ]; then
            echo Parser compiled.  Use \"tjsim $2\" to run
        fi
        if [ "$3" = '-fc' ]; then
            mv $1.mod.b $1.mod
        fi
    fi
fi

