xref: /titanic_50/usr/src/cmd/sgs/yacc/common/y4.c (revision 1dd08564e4a3aafe66b00aee6f222b0885346fe8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1dd08564Sab196087  * Common Development and Distribution License (the "License").
6*1dd08564Sab196087  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*1dd08564Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
31e29394bdSmike_s #include "dextern.h"
327c478bd9Sstevel@tonic-gate #define	NOMORE -1000
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static void gin(int);
357c478bd9Sstevel@tonic-gate static void stin(int);
367c478bd9Sstevel@tonic-gate static void osummary(void);
377c478bd9Sstevel@tonic-gate static void aoutput(void);
387c478bd9Sstevel@tonic-gate static void arout(wchar_t *, int *, int);
397c478bd9Sstevel@tonic-gate static int nxti(void);
407c478bd9Sstevel@tonic-gate static int gtnm(void);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static int *ggreed;
437c478bd9Sstevel@tonic-gate static int *pgo;
447c478bd9Sstevel@tonic-gate static int *yypgo;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int maxspr = 0;  /* maximum spread of any entry */
477c478bd9Sstevel@tonic-gate static int maxoff = 0;  /* maximum offset into an array */
487c478bd9Sstevel@tonic-gate int *optimmem;
497c478bd9Sstevel@tonic-gate static int *maxa;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int nxdb = 0;
527c478bd9Sstevel@tonic-gate static int adb = 0;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void
callopt()557c478bd9Sstevel@tonic-gate callopt()
567c478bd9Sstevel@tonic-gate {
57e29394bdSmike_s 	int i, *p, j, k, *q;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	ggreed = (int *) malloc(sizeof (int) * size);
607c478bd9Sstevel@tonic-gate 	pgo = (int *) malloc(sizeof (int) * size);
617c478bd9Sstevel@tonic-gate 	yypgo = &nontrst[0].tvalue;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/* read the arrays from tempfile and set parameters */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if ((finput = fopen(TEMPNAME, "r")) == NULL)
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
687c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
697c478bd9Sstevel@tonic-gate  *	tempfile can be translated as temporary file.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 		error(gettext(
727c478bd9Sstevel@tonic-gate 		    "optimizer cannot open tempfile"));
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	optimmem = tracemem;
757c478bd9Sstevel@tonic-gate 	pgo[0] = 0;
767c478bd9Sstevel@tonic-gate 	temp1[0] = 0;
777c478bd9Sstevel@tonic-gate 	nstate = 0;
787c478bd9Sstevel@tonic-gate 	nnonter = 0;
797c478bd9Sstevel@tonic-gate 	for (;;) {
807c478bd9Sstevel@tonic-gate 		switch (gtnm()) {
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		case L'\n':
837c478bd9Sstevel@tonic-gate 			temp1[++nstate] = (--optimmem) - tracemem;
847c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		case L',':
877c478bd9Sstevel@tonic-gate 			continue;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		case L'$':
907c478bd9Sstevel@tonic-gate 			break;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		default:
937c478bd9Sstevel@tonic-gate 			error("bad tempfile");
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		break;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	temp1[nstate] = yypgo[0] = (--optimmem) - tracemem;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	for (;;) {
1017c478bd9Sstevel@tonic-gate 		switch (gtnm()) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		case L'\n':
1047c478bd9Sstevel@tonic-gate 			yypgo[++nnonter] = optimmem-tracemem;
1057c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
1067c478bd9Sstevel@tonic-gate 		case L',':
1077c478bd9Sstevel@tonic-gate 			continue;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		case EOF:
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		default:
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
1157c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
1167c478bd9Sstevel@tonic-gate  *	tempfile can be translated as 'temporary file'.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate 			error(gettext(
1197c478bd9Sstevel@tonic-gate 			"bad tempfile"));
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		break;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	yypgo[nnonter--] = (--optimmem) - tracemem;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i) {
1277c478bd9Sstevel@tonic-gate 		k = 32000000;
1287c478bd9Sstevel@tonic-gate 		j = 0;
1297c478bd9Sstevel@tonic-gate 		q = tracemem + temp1[i+1];
1307c478bd9Sstevel@tonic-gate 		for (p = tracemem + temp1[i]; p < q; p += 2) {
1317c478bd9Sstevel@tonic-gate 			if (*p > j)
1327c478bd9Sstevel@tonic-gate 				j = *p;
1337c478bd9Sstevel@tonic-gate 			if (*p < k)
1347c478bd9Sstevel@tonic-gate 				k = *p;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		if (k <= j) {
1377c478bd9Sstevel@tonic-gate 			/*
1387c478bd9Sstevel@tonic-gate 			 * nontrivial situation
1397c478bd9Sstevel@tonic-gate 			 * temporarily, kill this for compatibility
1407c478bd9Sstevel@tonic-gate 			 */
1417c478bd9Sstevel@tonic-gate 			/* j -= k;  j is now the range */
1427c478bd9Sstevel@tonic-gate 			if (k > maxoff)
1437c478bd9Sstevel@tonic-gate 				maxoff = k;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		tystate[i] = (temp1[i+1] - temp1[i]) + 2*j;
1467c478bd9Sstevel@tonic-gate 		if (j > maxspr)
1477c478bd9Sstevel@tonic-gate 			maxspr = j;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* initialize ggreed table */
1517c478bd9Sstevel@tonic-gate 	for (i = 1; i <= nnonter; ++i) {
1527c478bd9Sstevel@tonic-gate 		ggreed[i] = 1;
1537c478bd9Sstevel@tonic-gate 		j = 0;
1547c478bd9Sstevel@tonic-gate 		/* minimum entry index is always 0 */
1557c478bd9Sstevel@tonic-gate 		q = tracemem + yypgo[i+1] -1;
1567c478bd9Sstevel@tonic-gate 		for (p = tracemem + yypgo[i]; p < q; p += 2) {
1577c478bd9Sstevel@tonic-gate 			ggreed[i] += 2;
1587c478bd9Sstevel@tonic-gate 			if (*p > j)
1597c478bd9Sstevel@tonic-gate 				j = *p;
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		ggreed[i] = ggreed[i] + 2*j;
1627c478bd9Sstevel@tonic-gate 		if (j > maxoff)
1637c478bd9Sstevel@tonic-gate 			maxoff = j;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* now, prepare to put the shift actions into the amem array */
1677c478bd9Sstevel@tonic-gate 	for (i = 0; i < new_actsize; ++i)
1687c478bd9Sstevel@tonic-gate 		amem[i] = 0;
1697c478bd9Sstevel@tonic-gate 	maxa = amem;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i) {
1727c478bd9Sstevel@tonic-gate 		if (tystate[i] == 0 && adb > 1)
1737c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "State %d: null\n", i);
1747c478bd9Sstevel@tonic-gate 		indgo[i] = YYFLAG1;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	while ((i = nxti()) != NOMORE) {
1787c478bd9Sstevel@tonic-gate 		if (i >= 0)
1797c478bd9Sstevel@tonic-gate 			stin(i);
1807c478bd9Sstevel@tonic-gate 		else
1817c478bd9Sstevel@tonic-gate 			gin(-i);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (adb > 2) { /* print a array */
1857c478bd9Sstevel@tonic-gate 		for (p = amem; p <= maxa; p += 10) {
186e29394bdSmike_s 			(void) fprintf(ftable, "%4" PRIdPTR "  ", p-amem);
1877c478bd9Sstevel@tonic-gate 			for (i = 0; i < 10; ++i)
1887c478bd9Sstevel@tonic-gate 				(void) fprintf(ftable, "%4d  ", p[i]);
1897c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "\n");
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	/* write out the output appropriate to the language */
1937c478bd9Sstevel@tonic-gate 	aoutput();
1947c478bd9Sstevel@tonic-gate 	osummary();
1957c478bd9Sstevel@tonic-gate 	ZAPFILE(TEMPNAME);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static void
gin(int i)199e29394bdSmike_s gin(int i)
2007c478bd9Sstevel@tonic-gate {
201e29394bdSmike_s 	int *r, *s, *q1, *q2;
2027c478bd9Sstevel@tonic-gate 	int *p;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* enter gotos on nonterminal i into array amem */
2057c478bd9Sstevel@tonic-gate 	ggreed[i] = 0;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	q2 = tracemem + yypgo[i+1] - 1;
2087c478bd9Sstevel@tonic-gate 	q1 = tracemem + yypgo[i];
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* now, find a place for it */
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* for( p=amem; p < &amem[new_actsize]; ++p ){ */
2137c478bd9Sstevel@tonic-gate 	p = amem;
2147c478bd9Sstevel@tonic-gate 	for (;;) {
2157c478bd9Sstevel@tonic-gate 		while (p >= &amem[new_actsize])
2167c478bd9Sstevel@tonic-gate 			exp_act(&p);
2177c478bd9Sstevel@tonic-gate 		if (*p)
2187c478bd9Sstevel@tonic-gate 			goto nextgp;
2197c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2207c478bd9Sstevel@tonic-gate 			s = p + *r + 1;
2217c478bd9Sstevel@tonic-gate 			/*
2227c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2237c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2247c478bd9Sstevel@tonic-gate 			 * expand it.
2257c478bd9Sstevel@tonic-gate 			 */
2267c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
2277c478bd9Sstevel@tonic-gate 				exp_act(&p);
2287c478bd9Sstevel@tonic-gate 				s = p + *r + 1;
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 			if (*s)
2317c478bd9Sstevel@tonic-gate 				goto nextgp;
2327c478bd9Sstevel@tonic-gate 			if (s > maxa) {
2337c478bd9Sstevel@tonic-gate 				while ((maxa = s) >= &amem[new_actsize])
2347c478bd9Sstevel@tonic-gate 					/* error( "amem array overflow" ); */
2357c478bd9Sstevel@tonic-gate 					exp_act(&p);
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 		/* we have found a spot */
2397c478bd9Sstevel@tonic-gate 		*p = *q2;
2407c478bd9Sstevel@tonic-gate 		if (p > maxa) {
2417c478bd9Sstevel@tonic-gate 			while ((maxa = p) >= &amem[new_actsize])
2427c478bd9Sstevel@tonic-gate 				/* error("amem array overflow"); */
2437c478bd9Sstevel@tonic-gate 				exp_act(&p);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2467c478bd9Sstevel@tonic-gate 			s = p + *r + 1;
2477c478bd9Sstevel@tonic-gate 			/*
2487c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2497c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2507c478bd9Sstevel@tonic-gate 			 * expand it.
2517c478bd9Sstevel@tonic-gate 			 */
2527c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
2537c478bd9Sstevel@tonic-gate 				exp_act(&p);
2547c478bd9Sstevel@tonic-gate 				s = p + *r + 1;
2557c478bd9Sstevel@tonic-gate 			}
2567c478bd9Sstevel@tonic-gate 			*s = r[1];
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		pgo[i] = p - amem;
2607c478bd9Sstevel@tonic-gate 		if (adb > 1)
2617c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable,
2627c478bd9Sstevel@tonic-gate 			    "Nonterminal %d, entry at %d\n", i, pgo[i]);
2637c478bd9Sstevel@tonic-gate 		goto nextgi;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		nextgp:
2667c478bd9Sstevel@tonic-gate 			++p;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	/* error( "cannot place goto %d\n", i ); */
2697c478bd9Sstevel@tonic-gate 	nextgi:;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static void
stin(int i)273e29394bdSmike_s stin(int i)
2747c478bd9Sstevel@tonic-gate {
275e29394bdSmike_s 	int *r, n, nn, flag, j, *q1, *q2;
2767c478bd9Sstevel@tonic-gate 	int *s;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	tystate[i] = 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* Enter state i into the amem array */
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	q2 = tracemem + temp1[i + 1];
2837c478bd9Sstevel@tonic-gate 	q1 = tracemem + temp1[i];
2847c478bd9Sstevel@tonic-gate 	/* Find an acceptable place */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	nn = -maxoff;
2877c478bd9Sstevel@tonic-gate 	more:
2887c478bd9Sstevel@tonic-gate 	for (n = nn; n < new_actsize; ++n) {
2897c478bd9Sstevel@tonic-gate 		flag = 0;
2907c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2917c478bd9Sstevel@tonic-gate 			s = *r + n + amem;
292e29394bdSmike_s 			if (s < amem)
2937c478bd9Sstevel@tonic-gate 				goto nextn;
2947c478bd9Sstevel@tonic-gate 			/*
2957c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2967c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2977c478bd9Sstevel@tonic-gate 			 * expand it.
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
3007c478bd9Sstevel@tonic-gate 				exp_act((int **)NULL);
3017c478bd9Sstevel@tonic-gate 				s = *r + n + amem;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			if (*s == 0)
3047c478bd9Sstevel@tonic-gate 				++flag;
3057c478bd9Sstevel@tonic-gate 			else if (*s != r[1])
3067c478bd9Sstevel@tonic-gate 				goto nextn;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * check that the position equals another
3117c478bd9Sstevel@tonic-gate 		 * only if the states are identical
3127c478bd9Sstevel@tonic-gate 		 */
3137c478bd9Sstevel@tonic-gate 		for (j = 0; j < nstate; ++j) {
3147c478bd9Sstevel@tonic-gate 			if (indgo[j] == n) {
3157c478bd9Sstevel@tonic-gate 				if (flag)
3167c478bd9Sstevel@tonic-gate 					/*
3177c478bd9Sstevel@tonic-gate 					 * we have some disagreement.
3187c478bd9Sstevel@tonic-gate 					 */
3197c478bd9Sstevel@tonic-gate 					goto nextn;
3207c478bd9Sstevel@tonic-gate 				if (temp1[j+1] + temp1[i] ==
3217c478bd9Sstevel@tonic-gate 				    temp1[j] + temp1[i+1]) {
3227c478bd9Sstevel@tonic-gate 					/* states are equal */
3237c478bd9Sstevel@tonic-gate 					indgo[i] = n;
3247c478bd9Sstevel@tonic-gate 					if (adb > 1)
3257c478bd9Sstevel@tonic-gate 						(void) fprintf(ftable,
3267c478bd9Sstevel@tonic-gate 						    "State %d: entry at"
3277c478bd9Sstevel@tonic-gate 						    " %d equals state %d\n",
3287c478bd9Sstevel@tonic-gate 						    i, n, j);
3297c478bd9Sstevel@tonic-gate 					return;
3307c478bd9Sstevel@tonic-gate 				}
3317c478bd9Sstevel@tonic-gate 				goto nextn;  /* we have some disagreement */
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
3367c478bd9Sstevel@tonic-gate 			while ((s = *r + n + amem) >= &amem[new_actsize]) {
3377c478bd9Sstevel@tonic-gate 				/*
3387c478bd9Sstevel@tonic-gate 				 * error( "out of space");
3397c478bd9Sstevel@tonic-gate 				 */
3407c478bd9Sstevel@tonic-gate 				exp_act((int **)NULL);
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 			if (s > maxa)
3437c478bd9Sstevel@tonic-gate 				maxa = s;
3447c478bd9Sstevel@tonic-gate 			if (*s != 0 && *s != r[1])
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
3477c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
3487c478bd9Sstevel@tonic-gate  *	Leave this untrasnlated. Yacc internal error.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate 				error(gettext(
3517c478bd9Sstevel@tonic-gate 				    "clobber of amem array, pos'n %d, by %d"),
3527c478bd9Sstevel@tonic-gate 				    s-amem, r[1]);
3537c478bd9Sstevel@tonic-gate 			*s = r[1];
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		indgo[i] = n;
3567c478bd9Sstevel@tonic-gate 		if (adb > 1)
3577c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable,
3587c478bd9Sstevel@tonic-gate 			    "State %d: entry at %d\n", i, indgo[i]);
3597c478bd9Sstevel@tonic-gate 		return;
3607c478bd9Sstevel@tonic-gate 		nextn:;
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/* error( "Error; failure to place state %d\n", i ); */
3647c478bd9Sstevel@tonic-gate 	exp_act((int **)NULL);
3657c478bd9Sstevel@tonic-gate 	nn = new_actsize - ACTSIZE;
3667c478bd9Sstevel@tonic-gate 	goto more;
3677c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
370e29394bdSmike_s static int
nxti()371e29394bdSmike_s nxti()
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	/* finds the next i */
374e29394bdSmike_s 	int i, max, maxi;
3757c478bd9Sstevel@tonic-gate 	max = 0;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	for (i = 1; i <= nnonter; ++i)
3787c478bd9Sstevel@tonic-gate 		if (ggreed[i] >= max) {
3797c478bd9Sstevel@tonic-gate 		max = ggreed[i];
3807c478bd9Sstevel@tonic-gate 		maxi = -i;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i)
3847c478bd9Sstevel@tonic-gate 		if (tystate[i] >= max) {
3857c478bd9Sstevel@tonic-gate 			max = tystate[i];
3867c478bd9Sstevel@tonic-gate 			maxi = i;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	if (nxdb)
3897c478bd9Sstevel@tonic-gate 		(void) fprintf(ftable, "nxti = %d, max = %d\n", maxi, max);
3907c478bd9Sstevel@tonic-gate 	if (max == 0)
3917c478bd9Sstevel@tonic-gate 		return (NOMORE);
3927c478bd9Sstevel@tonic-gate 	else
3937c478bd9Sstevel@tonic-gate 		return (maxi);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate static void
osummary()3977c478bd9Sstevel@tonic-gate osummary()
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	/* write summary */
400e29394bdSmike_s 	int i, *p;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (foutput == NULL)
4037c478bd9Sstevel@tonic-gate 		return;
4047c478bd9Sstevel@tonic-gate 	i = 0;
4057c478bd9Sstevel@tonic-gate 	for (p = maxa; p >= amem; --p) {
4067c478bd9Sstevel@tonic-gate 		if (*p == 0)
4077c478bd9Sstevel@tonic-gate 			++i;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
411e29394bdSmike_s 	    "Optimizer space used: input %" PRIdPTR
412e29394bdSmike_s 	    "/%d, output %" PRIdPTR "/%d\n",
4137c478bd9Sstevel@tonic-gate 	    optimmem-tracemem + 1, new_memsize, maxa-amem + 1, new_actsize);
4147c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
415e29394bdSmike_s 	    "%" PRIdPTR " table entries, %d zero\n", (maxa-amem) + 1, i);
4167c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
4177c478bd9Sstevel@tonic-gate 	    "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static void
aoutput()4227c478bd9Sstevel@tonic-gate aoutput()
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	/* this version is for C */
4257c478bd9Sstevel@tonic-gate 	/* write out the optimized parser */
4267c478bd9Sstevel@tonic-gate 
427e29394bdSmike_s 	(void) fprintf(ftable, "# define YYLAST %" PRIdPTR "\n", maxa-amem + 1);
4287c478bd9Sstevel@tonic-gate 	arout(L"yyact", amem, (maxa - amem) + 1);
4297c478bd9Sstevel@tonic-gate 	arout(L"yypact", indgo, nstate);
4307c478bd9Sstevel@tonic-gate 	arout(L"yypgo", pgo, nnonter + 1);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate static void
arout(s,v,n)4347c478bd9Sstevel@tonic-gate arout(s, v, n)
4357c478bd9Sstevel@tonic-gate wchar_t *s;
4367c478bd9Sstevel@tonic-gate int *v, n;
4377c478bd9Sstevel@tonic-gate {
438e29394bdSmike_s 	int i;
4397c478bd9Sstevel@tonic-gate 
440*1dd08564Sab196087 	(void) fprintf(ftable, WSFMT("static YYCONST yytabelem %ws[]={\n"), s);
4417c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ) {
4427c478bd9Sstevel@tonic-gate 		if (i % 10 == 0)
4437c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "\n");
4447c478bd9Sstevel@tonic-gate 		(void) fprintf(ftable, "%6d", v[i]);
4457c478bd9Sstevel@tonic-gate 		if (++i == n)
4467c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, " };\n");
4477c478bd9Sstevel@tonic-gate 		else
4487c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, ",");
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
452e29394bdSmike_s static int
gtnm()453e29394bdSmike_s gtnm()
4547c478bd9Sstevel@tonic-gate {
455e29394bdSmike_s 	int s, val, c;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* read and convert an integer from the standard input */
4587c478bd9Sstevel@tonic-gate 	/* return the terminating character */
4597c478bd9Sstevel@tonic-gate 	/* blanks, tabs, and newlines are ignored */
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	s = 1;
4627c478bd9Sstevel@tonic-gate 	val = 0;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	while ((c = getwc(finput)) != EOF) {
4657c478bd9Sstevel@tonic-gate 		if (iswdigit(c))
4667c478bd9Sstevel@tonic-gate 			val = val * 10 + c - L'0';
4677c478bd9Sstevel@tonic-gate 		else if (c == L'-')
4687c478bd9Sstevel@tonic-gate 			s = -1;
4697c478bd9Sstevel@tonic-gate 		else
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 	*optimmem++ = s*val;
4737c478bd9Sstevel@tonic-gate 	if (optimmem >= &tracemem[new_memsize])
4747c478bd9Sstevel@tonic-gate 		exp_mem(0);
4757c478bd9Sstevel@tonic-gate 	return (c);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate void
exp_act(ptr)4797c478bd9Sstevel@tonic-gate exp_act(ptr)
4807c478bd9Sstevel@tonic-gate int **ptr;
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	static int *actbase;
4837c478bd9Sstevel@tonic-gate 	int i;
4847c478bd9Sstevel@tonic-gate 	new_actsize += ACTSIZE;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	actbase = amem;
4877c478bd9Sstevel@tonic-gate 	amem = (int *) realloc((char *)amem, sizeof (int) * new_actsize);
4887c478bd9Sstevel@tonic-gate 	if (amem == NULL)
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
4917c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
4927c478bd9Sstevel@tonic-gate  *
4937c478bd9Sstevel@tonic-gate  *	You may just translate this as:
4947c478bd9Sstevel@tonic-gate  *	'Could not allocate internally used memory.'
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate 		error(gettext(
4977c478bd9Sstevel@tonic-gate 		"couldn't expand action table"));
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	for (i = new_actsize-ACTSIZE; i < new_actsize; ++i)
5007c478bd9Sstevel@tonic-gate 		amem[i] = 0;
5017c478bd9Sstevel@tonic-gate 	if (ptr != NULL)
5027c478bd9Sstevel@tonic-gate 		*ptr = *ptr - actbase + amem;
5037c478bd9Sstevel@tonic-gate 	if (memp >= amem)
5047c478bd9Sstevel@tonic-gate 		memp = memp - actbase + amem;
5057c478bd9Sstevel@tonic-gate 	if (maxa >= amem)
5067c478bd9Sstevel@tonic-gate 		maxa = maxa - actbase + amem;
5077c478bd9Sstevel@tonic-gate }
508