xref: /illumos-gate/usr/src/cmd/sgs/yacc/common/y4.c (revision caeaa751)
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
51dd08564Sab  * Common Development and Distribution License (the "License").
61dd08564Sab  * 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 /*
221dd08564Sab  * 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 
29e29394bdSmike_s #include "dextern.h"
307c478bd9Sstevel@tonic-gate #define	NOMORE -1000
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate static void gin(int);
337c478bd9Sstevel@tonic-gate static void stin(int);
347c478bd9Sstevel@tonic-gate static void osummary(void);
357c478bd9Sstevel@tonic-gate static void aoutput(void);
367c478bd9Sstevel@tonic-gate static void arout(wchar_t *, int *, int);
377c478bd9Sstevel@tonic-gate static int nxti(void);
387c478bd9Sstevel@tonic-gate static int gtnm(void);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int *ggreed;
417c478bd9Sstevel@tonic-gate static int *pgo;
427c478bd9Sstevel@tonic-gate static int *yypgo;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static int maxspr = 0;  /* maximum spread of any entry */
457c478bd9Sstevel@tonic-gate static int maxoff = 0;  /* maximum offset into an array */
467c478bd9Sstevel@tonic-gate int *optimmem;
477c478bd9Sstevel@tonic-gate static int *maxa;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int nxdb = 0;
507c478bd9Sstevel@tonic-gate static int adb = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate void
callopt(void)53a97db1b7SToomas Soome callopt(void)
547c478bd9Sstevel@tonic-gate {
55e29394bdSmike_s 	int i, *p, j, k, *q;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	ggreed = (int *) malloc(sizeof (int) * size);
587c478bd9Sstevel@tonic-gate 	pgo = (int *) malloc(sizeof (int) * size);
597c478bd9Sstevel@tonic-gate 	yypgo = &nontrst[0].tvalue;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* read the arrays from tempfile and set parameters */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if ((finput = fopen(TEMPNAME, "r")) == NULL)
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
667c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
677c478bd9Sstevel@tonic-gate  *	tempfile can be translated as temporary file.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 		error(gettext(
701dd08564Sab 		    "optimizer cannot open tempfile"));
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	optimmem = tracemem;
737c478bd9Sstevel@tonic-gate 	pgo[0] = 0;
747c478bd9Sstevel@tonic-gate 	temp1[0] = 0;
757c478bd9Sstevel@tonic-gate 	nstate = 0;
767c478bd9Sstevel@tonic-gate 	nnonter = 0;
777c478bd9Sstevel@tonic-gate 	for (;;) {
787c478bd9Sstevel@tonic-gate 		switch (gtnm()) {
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		case L'\n':
817c478bd9Sstevel@tonic-gate 			temp1[++nstate] = (--optimmem) - tracemem;
827c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		case L',':
857c478bd9Sstevel@tonic-gate 			continue;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 		case L'$':
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		default:
917c478bd9Sstevel@tonic-gate 			error("bad tempfile");
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		break;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	temp1[nstate] = yypgo[0] = (--optimmem) - tracemem;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	for (;;) {
997c478bd9Sstevel@tonic-gate 		switch (gtnm()) {
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		case L'\n':
1027c478bd9Sstevel@tonic-gate 			yypgo[++nnonter] = optimmem-tracemem;
1037c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
1047c478bd9Sstevel@tonic-gate 		case L',':
1057c478bd9Sstevel@tonic-gate 			continue;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		case EOF:
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		default:
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
1137c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
1147c478bd9Sstevel@tonic-gate  *	tempfile can be translated as 'temporary file'.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate 			error(gettext(
1177c478bd9Sstevel@tonic-gate 			"bad tempfile"));
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		break;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	yypgo[nnonter--] = (--optimmem) - tracemem;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i) {
1257c478bd9Sstevel@tonic-gate 		k = 32000000;
1267c478bd9Sstevel@tonic-gate 		j = 0;
1277c478bd9Sstevel@tonic-gate 		q = tracemem + temp1[i+1];
1287c478bd9Sstevel@tonic-gate 		for (p = tracemem + temp1[i]; p < q; p += 2) {
1297c478bd9Sstevel@tonic-gate 			if (*p > j)
1307c478bd9Sstevel@tonic-gate 				j = *p;
1317c478bd9Sstevel@tonic-gate 			if (*p < k)
1327c478bd9Sstevel@tonic-gate 				k = *p;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		if (k <= j) {
1357c478bd9Sstevel@tonic-gate 			/*
1367c478bd9Sstevel@tonic-gate 			 * nontrivial situation
1377c478bd9Sstevel@tonic-gate 			 * temporarily, kill this for compatibility
1387c478bd9Sstevel@tonic-gate 			 */
1397c478bd9Sstevel@tonic-gate 			/* j -= k;  j is now the range */
1407c478bd9Sstevel@tonic-gate 			if (k > maxoff)
1417c478bd9Sstevel@tonic-gate 				maxoff = k;
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		tystate[i] = (temp1[i+1] - temp1[i]) + 2*j;
1447c478bd9Sstevel@tonic-gate 		if (j > maxspr)
1457c478bd9Sstevel@tonic-gate 			maxspr = j;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* initialize ggreed table */
1497c478bd9Sstevel@tonic-gate 	for (i = 1; i <= nnonter; ++i) {
1507c478bd9Sstevel@tonic-gate 		ggreed[i] = 1;
1517c478bd9Sstevel@tonic-gate 		j = 0;
1527c478bd9Sstevel@tonic-gate 		/* minimum entry index is always 0 */
1537c478bd9Sstevel@tonic-gate 		q = tracemem + yypgo[i+1] -1;
1547c478bd9Sstevel@tonic-gate 		for (p = tracemem + yypgo[i]; p < q; p += 2) {
1557c478bd9Sstevel@tonic-gate 			ggreed[i] += 2;
1567c478bd9Sstevel@tonic-gate 			if (*p > j)
1577c478bd9Sstevel@tonic-gate 				j = *p;
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		ggreed[i] = ggreed[i] + 2*j;
1607c478bd9Sstevel@tonic-gate 		if (j > maxoff)
1617c478bd9Sstevel@tonic-gate 			maxoff = j;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* now, prepare to put the shift actions into the amem array */
1657c478bd9Sstevel@tonic-gate 	for (i = 0; i < new_actsize; ++i)
1667c478bd9Sstevel@tonic-gate 		amem[i] = 0;
1677c478bd9Sstevel@tonic-gate 	maxa = amem;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i) {
1707c478bd9Sstevel@tonic-gate 		if (tystate[i] == 0 && adb > 1)
1717c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "State %d: null\n", i);
1727c478bd9Sstevel@tonic-gate 		indgo[i] = YYFLAG1;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	while ((i = nxti()) != NOMORE) {
1767c478bd9Sstevel@tonic-gate 		if (i >= 0)
1777c478bd9Sstevel@tonic-gate 			stin(i);
1787c478bd9Sstevel@tonic-gate 		else
1797c478bd9Sstevel@tonic-gate 			gin(-i);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (adb > 2) { /* print a array */
1837c478bd9Sstevel@tonic-gate 		for (p = amem; p <= maxa; p += 10) {
184e29394bdSmike_s 			(void) fprintf(ftable, "%4" PRIdPTR "  ", p-amem);
1857c478bd9Sstevel@tonic-gate 			for (i = 0; i < 10; ++i)
1867c478bd9Sstevel@tonic-gate 				(void) fprintf(ftable, "%4d  ", p[i]);
1877c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "\n");
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	/* write out the output appropriate to the language */
1917c478bd9Sstevel@tonic-gate 	aoutput();
1927c478bd9Sstevel@tonic-gate 	osummary();
1937c478bd9Sstevel@tonic-gate 	ZAPFILE(TEMPNAME);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static void
gin(int i)197e29394bdSmike_s gin(int i)
1987c478bd9Sstevel@tonic-gate {
199e29394bdSmike_s 	int *r, *s, *q1, *q2;
2007c478bd9Sstevel@tonic-gate 	int *p;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* enter gotos on nonterminal i into array amem */
2037c478bd9Sstevel@tonic-gate 	ggreed[i] = 0;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	q2 = tracemem + yypgo[i+1] - 1;
2067c478bd9Sstevel@tonic-gate 	q1 = tracemem + yypgo[i];
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* now, find a place for it */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* for( p=amem; p < &amem[new_actsize]; ++p ){ */
2117c478bd9Sstevel@tonic-gate 	p = amem;
2127c478bd9Sstevel@tonic-gate 	for (;;) {
2137c478bd9Sstevel@tonic-gate 		while (p >= &amem[new_actsize])
2147c478bd9Sstevel@tonic-gate 			exp_act(&p);
2157c478bd9Sstevel@tonic-gate 		if (*p)
2167c478bd9Sstevel@tonic-gate 			goto nextgp;
2177c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2187c478bd9Sstevel@tonic-gate 			s = p + *r + 1;
2197c478bd9Sstevel@tonic-gate 			/*
2207c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2217c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2227c478bd9Sstevel@tonic-gate 			 * expand it.
2237c478bd9Sstevel@tonic-gate 			 */
2247c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
2257c478bd9Sstevel@tonic-gate 				exp_act(&p);
2267c478bd9Sstevel@tonic-gate 				s = p + *r + 1;
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 			if (*s)
2297c478bd9Sstevel@tonic-gate 				goto nextgp;
2307c478bd9Sstevel@tonic-gate 			if (s > maxa) {
2317c478bd9Sstevel@tonic-gate 				while ((maxa = s) >= &amem[new_actsize])
2327c478bd9Sstevel@tonic-gate 					/* error( "amem array overflow" ); */
2337c478bd9Sstevel@tonic-gate 					exp_act(&p);
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 		/* we have found a spot */
2377c478bd9Sstevel@tonic-gate 		*p = *q2;
2387c478bd9Sstevel@tonic-gate 		if (p > maxa) {
2397c478bd9Sstevel@tonic-gate 			while ((maxa = p) >= &amem[new_actsize])
2407c478bd9Sstevel@tonic-gate 				/* error("amem array overflow"); */
2417c478bd9Sstevel@tonic-gate 				exp_act(&p);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2447c478bd9Sstevel@tonic-gate 			s = p + *r + 1;
2457c478bd9Sstevel@tonic-gate 			/*
2467c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2477c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2487c478bd9Sstevel@tonic-gate 			 * expand it.
2497c478bd9Sstevel@tonic-gate 			 */
2507c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
2517c478bd9Sstevel@tonic-gate 				exp_act(&p);
2527c478bd9Sstevel@tonic-gate 				s = p + *r + 1;
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			*s = r[1];
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		pgo[i] = p - amem;
2587c478bd9Sstevel@tonic-gate 		if (adb > 1)
2597c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable,
2601dd08564Sab 			    "Nonterminal %d, entry at %d\n", i, pgo[i]);
2617c478bd9Sstevel@tonic-gate 		goto nextgi;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		nextgp:
2647c478bd9Sstevel@tonic-gate 			++p;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	/* error( "cannot place goto %d\n", i ); */
2677c478bd9Sstevel@tonic-gate 	nextgi:;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static void
stin(int i)271e29394bdSmike_s stin(int i)
2727c478bd9Sstevel@tonic-gate {
273e29394bdSmike_s 	int *r, n, nn, flag, j, *q1, *q2;
2747c478bd9Sstevel@tonic-gate 	int *s;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	tystate[i] = 0;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* Enter state i into the amem array */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	q2 = tracemem + temp1[i + 1];
2817c478bd9Sstevel@tonic-gate 	q1 = tracemem + temp1[i];
2827c478bd9Sstevel@tonic-gate 	/* Find an acceptable place */
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	nn = -maxoff;
2857c478bd9Sstevel@tonic-gate 	more:
2867c478bd9Sstevel@tonic-gate 	for (n = nn; n < new_actsize; ++n) {
2877c478bd9Sstevel@tonic-gate 		flag = 0;
2887c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
2897c478bd9Sstevel@tonic-gate 			s = *r + n + amem;
290e29394bdSmike_s 			if (s < amem)
2917c478bd9Sstevel@tonic-gate 				goto nextn;
2927c478bd9Sstevel@tonic-gate 			/*
2937c478bd9Sstevel@tonic-gate 			 * Check if action table needs to
2947c478bd9Sstevel@tonic-gate 			 * be expanded or not. If so,
2957c478bd9Sstevel@tonic-gate 			 * expand it.
2967c478bd9Sstevel@tonic-gate 			 */
2977c478bd9Sstevel@tonic-gate 			while (s >= &amem[new_actsize]) {
2987c478bd9Sstevel@tonic-gate 				exp_act((int **)NULL);
2997c478bd9Sstevel@tonic-gate 				s = *r + n + amem;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			if (*s == 0)
3027c478bd9Sstevel@tonic-gate 				++flag;
3037c478bd9Sstevel@tonic-gate 			else if (*s != r[1])
3047c478bd9Sstevel@tonic-gate 				goto nextn;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * check that the position equals another
3097c478bd9Sstevel@tonic-gate 		 * only if the states are identical
3107c478bd9Sstevel@tonic-gate 		 */
3117c478bd9Sstevel@tonic-gate 		for (j = 0; j < nstate; ++j) {
3127c478bd9Sstevel@tonic-gate 			if (indgo[j] == n) {
3137c478bd9Sstevel@tonic-gate 				if (flag)
3147c478bd9Sstevel@tonic-gate 					/*
3157c478bd9Sstevel@tonic-gate 					 * we have some disagreement.
3167c478bd9Sstevel@tonic-gate 					 */
3177c478bd9Sstevel@tonic-gate 					goto nextn;
3187c478bd9Sstevel@tonic-gate 				if (temp1[j+1] + temp1[i] ==
3191dd08564Sab 				    temp1[j] + temp1[i+1]) {
3207c478bd9Sstevel@tonic-gate 					/* states are equal */
3217c478bd9Sstevel@tonic-gate 					indgo[i] = n;
3227c478bd9Sstevel@tonic-gate 					if (adb > 1)
3237c478bd9Sstevel@tonic-gate 						(void) fprintf(ftable,
3241dd08564Sab 						    "State %d: entry at"
3251dd08564Sab 						    " %d equals state %d\n",
3261dd08564Sab 						    i, n, j);
3277c478bd9Sstevel@tonic-gate 					return;
3287c478bd9Sstevel@tonic-gate 				}
3297c478bd9Sstevel@tonic-gate 				goto nextn;  /* we have some disagreement */
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		for (r = q1; r < q2; r += 2) {
3347c478bd9Sstevel@tonic-gate 			while ((s = *r + n + amem) >= &amem[new_actsize]) {
3357c478bd9Sstevel@tonic-gate 				/*
3367c478bd9Sstevel@tonic-gate 				 * error( "out of space");
3377c478bd9Sstevel@tonic-gate 				 */
3387c478bd9Sstevel@tonic-gate 				exp_act((int **)NULL);
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			if (s > maxa)
3417c478bd9Sstevel@tonic-gate 				maxa = s;
3427c478bd9Sstevel@tonic-gate 			if (*s != 0 && *s != r[1])
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
3457c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
3467c478bd9Sstevel@tonic-gate  *	Leave this untrasnlated. Yacc internal error.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate 				error(gettext(
3491dd08564Sab 				    "clobber of amem array, pos'n %d, by %d"),
3501dd08564Sab 				    s-amem, r[1]);
3517c478bd9Sstevel@tonic-gate 			*s = r[1];
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		indgo[i] = n;
3547c478bd9Sstevel@tonic-gate 		if (adb > 1)
3557c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable,
3561dd08564Sab 			    "State %d: entry at %d\n", i, indgo[i]);
3577c478bd9Sstevel@tonic-gate 		return;
3587c478bd9Sstevel@tonic-gate 		nextn:;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* error( "Error; failure to place state %d\n", i ); */
3627c478bd9Sstevel@tonic-gate 	exp_act((int **)NULL);
3637c478bd9Sstevel@tonic-gate 	nn = new_actsize - ACTSIZE;
3647c478bd9Sstevel@tonic-gate 	goto more;
3657c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
368e29394bdSmike_s static int
nxti(void)369a97db1b7SToomas Soome nxti(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	/* finds the next i */
372e29394bdSmike_s 	int i, max, maxi;
3737c478bd9Sstevel@tonic-gate 	max = 0;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	for (i = 1; i <= nnonter; ++i)
3767c478bd9Sstevel@tonic-gate 		if (ggreed[i] >= max) {
3777c478bd9Sstevel@tonic-gate 		max = ggreed[i];
3787c478bd9Sstevel@tonic-gate 		maxi = -i;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	for (i = 0; i < nstate; ++i)
3827c478bd9Sstevel@tonic-gate 		if (tystate[i] >= max) {
3837c478bd9Sstevel@tonic-gate 			max = tystate[i];
3847c478bd9Sstevel@tonic-gate 			maxi = i;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	if (nxdb)
3877c478bd9Sstevel@tonic-gate 		(void) fprintf(ftable, "nxti = %d, max = %d\n", maxi, max);
3887c478bd9Sstevel@tonic-gate 	if (max == 0)
3897c478bd9Sstevel@tonic-gate 		return (NOMORE);
3907c478bd9Sstevel@tonic-gate 	else
3917c478bd9Sstevel@tonic-gate 		return (maxi);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static void
osummary(void)395a97db1b7SToomas Soome osummary(void)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	/* write summary */
398e29394bdSmike_s 	int i, *p;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (foutput == NULL)
4017c478bd9Sstevel@tonic-gate 		return;
4027c478bd9Sstevel@tonic-gate 	i = 0;
4037c478bd9Sstevel@tonic-gate 	for (p = maxa; p >= amem; --p) {
4047c478bd9Sstevel@tonic-gate 		if (*p == 0)
4057c478bd9Sstevel@tonic-gate 			++i;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
4091dd08564Sab 	    "Optimizer space used: input %" PRIdPTR
4101dd08564Sab 	    "/%d, output %" PRIdPTR "/%d\n",
4111dd08564Sab 	    optimmem-tracemem + 1, new_memsize, maxa-amem + 1, new_actsize);
4127c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
4131dd08564Sab 	    "%" PRIdPTR " table entries, %d zero\n", (maxa-amem) + 1, i);
4147c478bd9Sstevel@tonic-gate 	(void) fprintf(foutput,
4151dd08564Sab 	    "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate static void
aoutput(void)420a97db1b7SToomas Soome aoutput(void)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	/* this version is for C */
4237c478bd9Sstevel@tonic-gate 	/* write out the optimized parser */
4247c478bd9Sstevel@tonic-gate 
425e29394bdSmike_s 	(void) fprintf(ftable, "# define YYLAST %" PRIdPTR "\n", maxa-amem + 1);
4267c478bd9Sstevel@tonic-gate 	arout(L"yyact", amem, (maxa - amem) + 1);
4277c478bd9Sstevel@tonic-gate 	arout(L"yypact", indgo, nstate);
4287c478bd9Sstevel@tonic-gate 	arout(L"yypgo", pgo, nnonter + 1);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static void
arout(wchar_t * s,int * v,int n)432a97db1b7SToomas Soome arout(wchar_t *s, int *v, int n)
4337c478bd9Sstevel@tonic-gate {
434e29394bdSmike_s 	int i;
4357c478bd9Sstevel@tonic-gate 
436*caeaa751SToomas Soome 	(void) fprintf(ftable, "static YYCONST yytabelem %ws[]={\n", s);
4377c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ) {
4387c478bd9Sstevel@tonic-gate 		if (i % 10 == 0)
4397c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, "\n");
4407c478bd9Sstevel@tonic-gate 		(void) fprintf(ftable, "%6d", v[i]);
4417c478bd9Sstevel@tonic-gate 		if (++i == n)
4427c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, " };\n");
4437c478bd9Sstevel@tonic-gate 		else
4447c478bd9Sstevel@tonic-gate 			(void) fprintf(ftable, ",");
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
448e29394bdSmike_s static int
gtnm(void)449a97db1b7SToomas Soome gtnm(void)
4507c478bd9Sstevel@tonic-gate {
451e29394bdSmike_s 	int s, val, c;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* read and convert an integer from the standard input */
4547c478bd9Sstevel@tonic-gate 	/* return the terminating character */
4557c478bd9Sstevel@tonic-gate 	/* blanks, tabs, and newlines are ignored */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	s = 1;
4587c478bd9Sstevel@tonic-gate 	val = 0;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	while ((c = getwc(finput)) != EOF) {
4617c478bd9Sstevel@tonic-gate 		if (iswdigit(c))
4627c478bd9Sstevel@tonic-gate 			val = val * 10 + c - L'0';
4637c478bd9Sstevel@tonic-gate 		else if (c == L'-')
4647c478bd9Sstevel@tonic-gate 			s = -1;
4657c478bd9Sstevel@tonic-gate 		else
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	*optimmem++ = s*val;
4697c478bd9Sstevel@tonic-gate 	if (optimmem >= &tracemem[new_memsize])
4707c478bd9Sstevel@tonic-gate 		exp_mem(0);
4717c478bd9Sstevel@tonic-gate 	return (c);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate void
exp_act(int ** ptr)475a97db1b7SToomas Soome exp_act(int **ptr)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	static int *actbase;
4787c478bd9Sstevel@tonic-gate 	int i;
4797c478bd9Sstevel@tonic-gate 	new_actsize += ACTSIZE;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	actbase = amem;
4827c478bd9Sstevel@tonic-gate 	amem = (int *) realloc((char *)amem, sizeof (int) * new_actsize);
4837c478bd9Sstevel@tonic-gate 	if (amem == NULL)
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE  -- This is a message from yacc.
4867c478bd9Sstevel@tonic-gate  *	This message is passed to error() function.
4877c478bd9Sstevel@tonic-gate  *
4887c478bd9Sstevel@tonic-gate  *	You may just translate this as:
4897c478bd9Sstevel@tonic-gate  *	'Could not allocate internally used memory.'
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate 		error(gettext(
4927c478bd9Sstevel@tonic-gate 		"couldn't expand action table"));
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	for (i = new_actsize-ACTSIZE; i < new_actsize; ++i)
4957c478bd9Sstevel@tonic-gate 		amem[i] = 0;
4967c478bd9Sstevel@tonic-gate 	if (ptr != NULL)
4977c478bd9Sstevel@tonic-gate 		*ptr = *ptr - actbase + amem;
4987c478bd9Sstevel@tonic-gate 	if (memp >= amem)
4997c478bd9Sstevel@tonic-gate 		memp = memp - actbase + amem;
5007c478bd9Sstevel@tonic-gate 	if (maxa >= amem)
5017c478bd9Sstevel@tonic-gate 		maxa = maxa - actbase + amem;
5027c478bd9Sstevel@tonic-gate }
503