




/* EPC Imp to C Translation Release 4 Version Apr 95 */


#include "imptoc.h"

int             i, j, k, ss, nbip;
char            st49[32], st50[32], st51[32];
char            header[128];
int             clett[1000 + 1];
int             symbol[3000 - 1299];
int             cc[300 + 1];
int             cnext, dnext, cnum, dnum, alt, def, asl, nidflag, eflag;
int             kk[1200 - 1000];
int             puse[1200 - 1000];
int             dword[1200 - 1000];
int             dlett[1000 + 1];
int             cword[200 + 1];
extern void     printstr();
void
readstring(int terminator)
{
	int             under;
	under = 0;
	j = 0;
nexts:
	i = fgetc(in_file);
	assert(i != EOF);
	/* putchar(i); */
	if (i == '%') {
		under = 128;
		goto nexts;
	}
	if (('A' <= i) && (i <= 'Z'))
		i += under;
	else
		under = 0;
	if (i == 32)
		goto nexts;	/* '_' */

	if (i == terminator) {
		cc[0] = j;
		return;
	}
	j++;
	cc[j] = i;
	goto nexts;
}
void 
record(int *word, int *lett, int *num, int *next)
{
	*num += 1;
	word[*num] = *next;
	for (i = 0; i <= cc[0]; i++) {
		lett[*next + i] = cc[i];
	}
	*next = (*next + cc[0]) + 1;
}
void 
lookup(int *word, int *lett, int first, int last, int insert)
{
	i = first;
	while (i <= last) {
		j = word[i];
		for (k = 0; k <= lett[j]; k++) {
			if (lett[j + k] != cc[k])
				goto nxt;
		}
		return;
nxt:
		i++;
	}
	if (insert == 0) {
		fprintf(out_file, "\n****************PHRASE NOT IN DICTIONARY***********   ");
		printstr(&cc[0], 0);
		nidflag++;
		return;
	}
	record(word, lett, &cnum, &cnext);
	/* INSERT INTO C DICTIONARY */
	i = cnum;
}
void
pcdict()
{
	int             j, k, l;
	fprintf(out_file, "%c%s", 12, " KEY TO LITERAL DICTIONARY (CLETT)\n");
	for (j = 1; j <= cnum; j++) {
		k = cword[j];
		fprintf(out_file, "%5d  ", k);
		printstr(&clett[0], k);
		{
			for (_imptempint = 1; _imptempint <= 17 - ss; _imptempint++)
				fprintf(out_file, " ");
		}
		if ((j & 3) == 0)
			fprintf(out_file, "\n");
	}
}
void
printstr(int *cc, int ptr)
{
	int             i, j, k, del;
	ss = cc[ptr];
	k = ss;
	del = 0;
	for (i = 1; i <= k; i++) {
		j = cc[ptr + i];
		if (del == 0 && j > 128) {
			ss++;
			del = 1;
			fprintf(out_file, "%c", '%');
		}
		fprintf(out_file, "%c", j & 127);
	}
}
void
pddict()
{
	int             j;
	fprintf(out_file, "\n\n\n\n KEY TO MAIN TABLE (SYMBOL)\n");
	for (j = 1001; j <= dnum; j++) {
		fprintf(out_file, "%5d  ", (j <= (1000 + nbip)) ? j : kk[j - 1001]);
		printstr(&dlett[0], dword[j - 1001]);
		{
			for (_imptempint = 1; _imptempint <= 17 - ss; _imptempint++)
				fprintf(out_file, " ");
		}
		if ((((j - 1000) & 3)) == 0)
			fprintf(out_file, "\n");
	}
}

int
main(int argc, char **argv)
{
	selectinput(1);
	selectoutput(2);
	strcpy(header, "! PRODUCED BY newps FROM ");
	fprintf(out_file, "%s", header);
	asl = 1300;
	cnext = 0;
	dnext = 0;
	cnum = 0;
	nidflag = 0;
	dnum = 1000;
	(void) fscanf(in_file, "%d", &nbip);
	for (i = 1001; i <= 1200; i++) {
		puse[i - 1001] = 0;
	}
nexts:
	i = fgetc(in_file);
	assert(i != EOF);
	/* putchar(i); */
	if (i == 'D') {
		/* 'D' */
		i = fgetc(in_file);
		assert(i != EOF);
		/* putchar(i); */
		readstring(')');
		record(&dword[-1001], &dlett[0], &dnum, &dnext);
		goto nexts;
	}
	if (i == 'P')
		goto phrs;
	if (i == 'E')
		goto eend;
	goto nexts;
phrs:
	fprintf(out_file, "\n\n%c", i);
	i = fgetc(in_file);
	assert(i != EOF);
	/* putchar(i); */
	fprintf(out_file, "%c", i);
	readstring(')');
	printstr(&cc[0], 0);
	fprintf(out_file, ")");
	lookup(&dword[-1001], &dlett[0], 1001 + nbip, dnum, 0);
	kk[i - 1001] = asl;
	def = asl;
	alt = asl + 1;
	asl += 2;
nextp:
	i = fgetc(in_file);
	assert(i != EOF);
	/* putchar(i); */
	fprintf(out_file, "%c", i);
	if (i == '(')
		goto biporphr;
	if (i == 39)
		goto lit;
	if (i == ',') {
		symbol[alt - 1300] = asl;
		alt = asl;
		asl++;
		goto nextp;
	}
	if (i == ';') {
		symbol[alt - 1300] = asl;
		symbol[def - 1300] = asl;
		goto nexts;
	}
	if (i == '0') {
		symbol[asl - 1300] = 1000;
		asl++;
		goto nextp;
	}
	if (i == '*') {
		symbol[asl - 1300] = 999;
		asl++;
		goto nextp;
	}
	goto nextp;
biporphr:
	readstring(')');
	printstr(&cc[0], 0);
	fprintf(out_file, ")");
	lookup(&dword[-1001], &dlett[0], 1001, dnum, 0);
	puse[i - 1001] = puse[i - 1001] + 1;
	symbol[asl - 1300] = i;
	asl++;
	goto nextp;
lit:
	readstring(39);
	printstr(&cc[0], 0);
	fprintf(out_file, "%c", 39);
	lookup(&cword[0], &clett[0], 1, cnum, 1);
	symbol[asl - 1300] = i;
	asl++;
	goto nextp;
eend:
	pcdict();
	pddict();
	for (i = 1300; i <= asl - 1; i++) {
		if ((1 <= symbol[i - 1300]) && (symbol[i - 1300] <= cnum))
			symbol[i - 1300] = cword[symbol[i - 1300]];
		if (((1001 + nbip) <= symbol[i - 1300]) && (symbol[i - 1300] <= dnum))
			symbol[i - 1300] = kk[symbol[i - 1300] - 1001];
	}
	ss = kk[dnum - 1001];
	fprintf(out_file, "\n");
	for (k = 1; k <= 2; k++) {
		if (k == 2)
			selectoutput(1);
		fprintf(out_file, "%s%s%2d)=", header, "\n%CONSTBYTEINTEGERARRAY CLETT(0:", cnext - 1);
		for (i = 0; i <= cnext - 1; i++) {
			fprintf(out_file, "%4d", clett[i]);
			j = ',';
			if (i == (cnext - 1))
				j = ';';
			fprintf(out_file, "%c", j);
			if ((i - ((i / 14) * 14)) == 0)
				fprintf(out_file, "%s", "\n");
		}
		fprintf(out_file, "\n%s%2d)=", "\n%CONSTINTEGERARRAY SYMBOL(1300:", asl - 1);
		for (i = 1300; i <= asl - 1; i++) {
			fprintf(out_file, "%6d", symbol[i - 1300]);
			j = ',';
			if (i == (asl - 1))
				j = ';';
			fprintf(out_file, "%c", j);
			if ((((i - 1299) - (((i - 1299) / 10) * 10))) == 1)
				fprintf(out_file, "\n");
		}
		fprintf(out_file, "\n%s%2d\n", "%CONSTINTEGER SS=", ss);
		if (nidflag != 0)
			return nidflag;
	}
	selectoutput(0);
	fprintf(out_file, "NO ERRORS\n");
	for (i = 1001; i <= dnum - 1; i++) {
		if (puse[i - 1001] == 0) {
			fprintf(out_file, "\nWARNING PHRASE NOT USED :");
			printstr(&dlett[0], dword[i - 1001]);
			fprintf(out_file, "\n");
		}
	}
	return 0;
}

/* end of automatic translation */
