xref: /titanic_51/usr/src/cmd/sgs/yacc/common/y2.c (revision 1100f00d5652de2808b73c61bcfdb3fc87ef1fc8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include "dextern.h"
32 #include "sgs.h"
33 #include <stdio.h>
34 
35 #define	IDENTIFIER 257
36 
37 #define	MARK 258
38 #define	TERM 259
39 #define	LEFT 260
40 #define	RIGHT 261
41 #define	BINARY 262
42 #define	PREC 263
43 #define	LCURLY 264
44 #define	C_IDENTIFIER 265  /* name followed by colon */
45 #define	NUMBER 266
46 #define	START 267
47 #define	TYPEDEF 268
48 #define	TYPENAME 269
49 #define	UNION 270
50 #define	ENDFILE 0
51 #define	LHS_TEXT_LEN		80	/* length of lhstext */
52 #define	RHS_TEXT_LEN		640	/* length of rhstext */
53 	/* communication variables between various I/O routines */
54 
55 #define	v_FLAG	0x01
56 #define	d_FLAG	0x02
57 #define	DEFAULT_PREFIX	"y"
58 
59 char *infile;				/* input file name 		*/
60 static int numbval;			/* value of an input number 	*/
61 static int toksize = NAMESIZE;
62 static wchar_t *tokname;	/* input token name		*/
63 char *parser = PARSER;		/* location of common parser 	*/
64 
65 static void finact(void);
66 static wchar_t *cstash(wchar_t *);
67 static void defout(void);
68 static void cpyunion(void);
69 static void cpycode(void);
70 static void cpyact(int);
71 static void lhsfill(wchar_t *);
72 static void rhsfill(wchar_t *);
73 static void lrprnt(void);
74 static void beg_debug(void);
75 static void end_toks(void);
76 static void end_debug(void);
77 static void exp_tokname(void);
78 static void exp_prod(void);
79 static void exp_ntok(void);
80 static void exp_nonterm(void);
81 static int defin(int, wchar_t *);
82 static int gettok(void);
83 static int chfind(int, wchar_t *);
84 static int skipcom(void);
85 static int findchtok(int);
86 static void put_prefix_define(char *);
87 
88 
89 /* storage of names */
90 
91 /*
92  * initial block to place token and
93  * nonterminal names are stored
94  * points to initial block - more space
95  * is allocated as needed.
96  */
97 static wchar_t cnamesblk0[CNAMSZ];
98 static wchar_t *cnames = cnamesblk0;
99 
100 /* place where next name is to be put in */
101 static wchar_t *cnamp = cnamesblk0;
102 
103 /* number of defined symbols output */
104 static int ndefout = 3;
105 
106 	/* storage of types */
107 static int defunion = 0;	/* union of types defined? */
108 static int ntypes = 0;		/* number of types defined */
109 static wchar_t *typeset[NTYPES]; /* pointers to type tags */
110 
111 	/* symbol tables for tokens and nonterminals */
112 
113 int ntokens = 0;
114 int ntoksz = NTERMS;
115 TOKSYMB *tokset;
116 int *toklev;
117 
118 int nnonter = -1;
119 NTSYMB *nontrst;
120 int nnontersz = NNONTERM;
121 
122 static int start;	/* start symbol */
123 
124 	/* assigned token type values */
125 static int extval = 0;
126 
127 	/* input and output file descriptors */
128 
129 FILE *finput;		/* yacc input file */
130 FILE *faction;		/* file for saving actions */
131 FILE *fdefine;		/* file for # defines */
132 FILE *ftable;		/* y.tab.c file */
133 FILE *ftemp;		/* tempfile to pass 2 */
134 FILE *fdebug;		/* where the strings for debugging are stored */
135 FILE *foutput;		/* y.output file */
136 
137 	/* output string */
138 
139 static wchar_t *lhstext;
140 static wchar_t *rhstext;
141 
142 	/* storage for grammar rules */
143 
144 int *mem0; /* production storage */
145 int *mem;
146 int *tracemem;
147 extern int *optimmem;
148 int new_memsize = MEMSIZE;
149 int nprod = 1;	/* number of productions */
150 int nprodsz = NPROD;
151 
152 int **prdptr;
153 int *levprd;
154 wchar_t *had_act;
155 
156 /* flag for generating the # line's default is yes */
157 int gen_lines = 1;
158 int act_lines = 0;
159 
160 /* flag for whether to include runtime debugging */
161 static int gen_testing = 0;
162 
163 /* flag for version stamping--default turned off */
164 static char *v_stmp = "n";
165 
166 int nmbchars = 0;	/* number of mb literals in mbchars */
167 MBCLIT *mbchars = (MBCLIT *) 0; /* array of mb literals */
168 int nmbcharsz = 0; /* allocated space for mbchars */
169 
170 void
171 setup(argc, argv)
172 int argc;
173 char *argv[];
174 {	int ii, i, j, lev, t, ty;
175 		/* ty is the sequencial number of token name in tokset */
176 	int c;
177 	int *p;
178 	char *cp;
179 	wchar_t actname[8];
180 	unsigned int options = 0;
181 	char *file_prefix = DEFAULT_PREFIX;
182 	char *sym_prefix = "";
183 #define	F_NAME_LENGTH	128
184 	char	fname[F_NAME_LENGTH+1];
185 
186 	foutput = NULL;
187 	fdefine = NULL;
188 	i = 1;
189 
190 	tokname = (wchar_t *)malloc(sizeof (wchar_t) * toksize);
191 	tokset = (TOKSYMB *)malloc(sizeof (TOKSYMB) * ntoksz);
192 	toklev = (int *)malloc(sizeof (int) * ntoksz);
193 	nontrst = (NTSYMB *)malloc(sizeof (NTSYMB) * nnontersz);
194 	mem0 = (int *)malloc(sizeof (int) * new_memsize);
195 	prdptr = (int **)malloc(sizeof (int *) * (nprodsz+2));
196 	levprd = (int *)malloc(sizeof (int) * (nprodsz+2));
197 	had_act = (wchar_t *)calloc((nprodsz + 2), sizeof (wchar_t));
198 	lhstext = (wchar_t *)malloc(sizeof (wchar_t) * LHS_TEXT_LEN);
199 	rhstext = (wchar_t *)malloc(sizeof (wchar_t) * RHS_TEXT_LEN);
200 	aryfil(toklev, ntoksz, 0);
201 	aryfil(levprd, nprodsz, 0);
202 	for (ii = 0; ii < ntoksz; ++ii)
203 		tokset[ii].value = 0;
204 	for (ii = 0; ii < nnontersz; ++ii)
205 		nontrst[ii].tvalue = 0;
206 	aryfil(mem0, new_memsize, 0);
207 	mem = mem0;
208 	tracemem = mem0;
209 
210 	while ((c = getopt(argc, argv, "vVdltp:Q:Y:P:b:")) != EOF)
211 		switch (c) {
212 		case 'v':
213 			options |= v_FLAG;
214 			break;
215 		case 'V':
216 			(void) fprintf(stderr, "yacc: %s %s\n",
217 			    (const char *)SGU_PKG,
218 			    (const char *)SGU_REL);
219 			break;
220 		case 'Q':
221 			v_stmp = optarg;
222 			if (*v_stmp != 'y' && *v_stmp != 'n')
223 /*
224  * TRANSLATION_NOTE  -- This is a message from yacc.
225  *	This message is passed to error() function.
226  *	Do not translate -Q and [y/n].
227  */
228 				error(gettext(
229 			"yacc: -Q should be followed by [y/n]"));
230 			break;
231 		case 'd':
232 			options |= d_FLAG;
233 			break;
234 		case 'l':
235 			gen_lines = 0;	/* don't gen #lines */
236 			break;
237 		case 't':
238 			gen_testing = 1;	/* set YYDEBUG on */
239 			break;
240 		case 'Y':
241 			cp = (char *)malloc(strlen(optarg)+
242 				sizeof ("/yaccpar") + 1);
243 			cp = strcpy(cp, optarg);
244 			parser = strcat(cp, "/yaccpar");
245 			break;
246 		case 'P':
247 			parser = optarg;
248 			break;
249 		case 'p':
250 			if (strcmp(optarg, "yy") != 0)
251 				sym_prefix = optarg;
252 			else
253 				sym_prefix = "";
254 			break;
255 		case 'b':
256 			file_prefix = optarg;
257 			break;
258 		case '?':
259 		default:
260 /*
261  * TRANSLATION_NOTE  -- This is a message from yacc.
262  *	This message is passed to error() function.
263  *	This is a usage message. The translate should be
264  *	consistent with man page translation.
265  */
266 			(void) fprintf(stderr, gettext(
267 "Usage: yacc [-vVdltY] [-Q(y/n)] [-b file_prefix] [-p sym_prefix]"
268 " [-P parser] file\n"));
269 			exit(1);
270 		}
271 	/*
272 	 * Open y.output if -v is specified
273 	 */
274 	if (options & v_FLAG) {
275 		strncpy(fname,
276 			file_prefix,
277 			F_NAME_LENGTH-strlen(".output"));
278 		strcat(fname, ".output");
279 		foutput = fopen(fname, "w");
280 		if (foutput == NULL)
281 			error(gettext(
282 			"cannot open y.output"));
283 	}
284 
285 	/*
286 	 * Open y.tab.h if -d is specified
287 	 */
288 	if (options & d_FLAG) {
289 		strncpy(fname,
290 			file_prefix,
291 			F_NAME_LENGTH-strlen(".tab.h"));
292 		strcat(fname, ".tab.h");
293 		fdefine = fopen(fname, "w");
294 		if (fdefine == NULL)
295 			error(gettext(
296 			"cannot open y.tab.h"));
297 	}
298 
299 	fdebug = fopen(DEBUGNAME, "w");
300 	if (fdebug == NULL)
301 /*
302  * TRANSLATION_NOTE  -- This is a message from yacc.
303  *	This message is passed to error() function.
304  *	Do not translate yacc.debug.
305  */
306 		error(gettext(
307 		"cannot open yacc.debug"));
308 	/*
309 	 * Open y.tab.c
310 	 */
311 	strncpy(fname,
312 		file_prefix,
313 		F_NAME_LENGTH-strlen(".tab.c"));
314 	strcat(fname, ".tab.c");
315 	ftable = fopen(fname, "w");
316 	if (ftable == NULL)
317 		error(gettext(
318 		"cannot open %s"), fname);
319 
320 	ftemp = fopen(TEMPNAME, "w");
321 	faction = fopen(ACTNAME, "w");
322 	if (ftemp == NULL || faction == NULL)
323 /*
324  * TRANSLATION_NOTE  -- This is a message from yacc.
325  *	This message is passed to error() function.
326  *	The message means: "Could not open a temporary file."
327  */
328 		error(gettext(
329 		"cannot open temp file"));
330 
331 	if ((finput = fopen(infile = argv[optind], "r")) == NULL)
332 /*
333  * TRANSLATION_NOTE  -- This is a message from yacc.
334  *	This message is passed to error() function.
335  */
336 		error(gettext(
337 		"cannot open input file"));
338 
339 	lineno = 1;
340 	cnamp = cnames;
341 	(void) defin(0, L"$end");
342 	extval = 0400;
343 	(void) defin(0, L"error");
344 	(void) defin(1, L"$accept");
345 	mem = mem0;
346 	lev = 0;
347 	ty = 0;
348 	i = 0;
349 	beg_debug();	/* initialize fdebug file */
350 
351 	/*
352 	 * sorry -- no yacc parser here.....
353 	 *	we must bootstrap somehow...
354 	 */
355 
356 	t = gettok();
357 	if (*v_stmp == 'y')
358 		(void) fprintf(ftable, "#ident\t\"yacc: %s %s\"\n",
359 		    (const char *)SGU_PKG, (const char *)SGU_REL);
360 	for (; t != MARK && t != ENDFILE; ) {
361 		int tok_in_line;
362 		switch (t) {
363 
364 		case L';':
365 			t = gettok();
366 			break;
367 
368 		case START:
369 			if ((t = gettok()) != IDENTIFIER) {
370 				error("bad %%start construction");
371 				}
372 			start = chfind(1, tokname);
373 			t = gettok();
374 			continue;
375 
376 		case TYPEDEF:
377 			tok_in_line = 0;
378 			if ((t = gettok()) != TYPENAME)
379 /*
380  * TRANSLATION_NOTE  -- This is a message from yacc.
381  *	This message is passed to error() function.
382  *	Do not translate %%type.
383  */
384 				error(gettext(
385 				"bad syntax in %%type"));
386 			ty = numbval;
387 			for (;;) {
388 				t = gettok();
389 				switch (t) {
390 
391 				case IDENTIFIER:
392 			/*
393 			 * The following lines are idented to left.
394 			 */
395 			tok_in_line = 1;
396 			if ((t = chfind(1, tokname)) < NTBASE) {
397 				j = TYPE(toklev[t]);
398 				if (j != 0 && j != ty) {
399 /*
400  * TRANSLATION_NOTE  -- This is a message from yacc.
401  *	This message is passed to error() function.
402  */
403 					error(gettext(
404 					"type redeclaration of token %ws"),
405 					tokset[t].name);
406 					}
407 				else
408 					SETTYPE(toklev[t], ty);
409 			} else {
410 				j = nontrst[t-NTBASE].tvalue;
411 				if (j != 0 && j != ty) {
412 /*
413  * TRANSLATION_NOTE  -- This is a message from yacc.
414  *	This message is passed to error() function.
415  *	Check how nonterminal is translated in translated
416  *	yacc man page or yacc user's document.
417  */
418 					error(gettext(
419 				"type redeclaration of nonterminal %ws"),
420 						nontrst[t-NTBASE].name);
421 					}
422 				else
423 					nontrst[t-NTBASE].tvalue = ty;
424 				}
425 			/* FALLTHRU */
426 			/*
427 			 * End Indentation
428 			 */
429 				case L',':
430 					continue;
431 
432 				case L';':
433 					t = gettok();
434 					break;
435 				default:
436 					break;
437 					}
438 				if (!tok_in_line)
439 /*
440  * TRANSLATION_NOTE  -- This is a message from yacc.
441  *	This message is passed to error() function.
442  */
443 					error(gettext(
444 					"missing tokens or illegal tokens"));
445 				break;
446 				}
447 			continue;
448 
449 		case UNION:
450 			/* copy the union declaration to the output */
451 			cpyunion();
452 			defunion = 1;
453 			t = gettok();
454 			continue;
455 
456 		case LEFT:
457 		case BINARY:
458 		case RIGHT:
459 			i++;
460 			/* FALLTHRU */
461 		case TERM:
462 			tok_in_line = 0;
463 
464 			/* nonzero means new prec. and assoc. */
465 			lev = (t-TERM) | 04;
466 			ty = 0;
467 
468 			/* get identifiers so defined */
469 
470 			t = gettok();
471 			if (t == TYPENAME) { /* there is a type defined */
472 				ty = numbval;
473 				t = gettok();
474 				}
475 
476 			for (;;) {
477 				switch (t) {
478 
479 				case L',':
480 					t = gettok();
481 					continue;
482 
483 				case L';':
484 					break;
485 
486 				case IDENTIFIER:
487 					tok_in_line = 1;
488 					j = chfind(0, tokname);
489 					if (j > NTBASE) {
490 /*
491  * TRANSLATION_NOTE  -- This is a message from yacc.
492  *	This message is passed to error() function.
493  */
494 						error(gettext(
495 				"%ws is not a token."),
496 						tokname);
497 					}
498 					if (lev & ~04) {
499 						if (ASSOC(toklev[j]) & ~04)
500 /*
501  * TRANSLATION_NOTE  -- This is a message from yacc.
502  *	This message is passed to error() function.
503  */
504 							error(gettext(
505 				"redeclaration of precedence of %ws"),
506 						tokname);
507 						SETASC(toklev[j], lev);
508 						SETPLEV(toklev[j], i);
509 					} else {
510 						if (ASSOC(toklev[j]))
511 						(void) warning(1, gettext(
512 				"redeclaration of precedence of %ws."),
513 							tokname);
514 						SETASC(toklev[j], lev);
515 						}
516 					if (ty) {
517 						if (TYPE(toklev[j]))
518 							error(gettext(
519 /*
520  * TRANSLATION_NOTE  -- This is a message from yacc.
521  *	This message is passed to error() function.
522  */
523 						"redeclaration of type of %ws"),
524 							tokname);
525 						SETTYPE(toklev[j], ty);
526 						}
527 					if ((t = gettok()) == NUMBER) {
528 						tokset[j].value = numbval;
529 						if (j < ndefout && j > 2) {
530 /*
531  * TRANSLATION_NOTE  -- This is a message from yacc.
532  *	This message is passed to error() function.
533  */
534 							error(gettext(
535 				"type number of %ws should be defined earlier"),
536 							tokset[j].name);
537 							}
538 						if (numbval >= -YYFLAG1) {
539 /*
540  * TRANSLATION_NOTE  -- This is a message from yacc.
541  *	This message is passed to error() function.
542  */
543 							error(gettext(
544 				"token numbers must be less than %d"),
545 							-YYFLAG1);
546 							}
547 						t = gettok();
548 						}
549 					continue;
550 
551 					}
552 				if (!tok_in_line)
553 /*
554  * TRANSLATION_NOTE  -- This is a message from yacc.
555  *	This message is passed to error() function.
556  */
557 					error(gettext(
558 					"missing tokens or illegal tokens"));
559 				break;
560 				}
561 			continue;
562 
563 		case LCURLY:
564 			defout();
565 			cpycode();
566 			t = gettok();
567 			continue;
568 
569 		default:
570 			error("syntax error");
571 
572 			}
573 
574 		}
575 
576 	if (t == ENDFILE) {
577 /*
578  * TRANSLATION_NOTE  -- This is a message from yacc.
579  *	This message is passed to error() function.
580  *	Do not translate %%%%.
581  */
582 		error("unexpected EOF before %%%%");
583 		}
584 
585 	/* t is MARK */
586 
587 	defout();
588 	end_toks();	/* all tokens dumped - get ready for reductions */
589 
590 	(void) fprintf(ftable, "\n#include <inttypes.h>\n");
591 	(void) fprintf(ftable, "\n#ifdef __STDC__\n");
592 	(void) fprintf(ftable, "#include <stdlib.h>\n");
593 	(void) fprintf(ftable, "#include <string.h>\n");
594 	(void) fprintf(ftable, "#define	YYCONST	const\n");
595 	(void) fprintf(ftable, "#else\n");
596 	(void) fprintf(ftable, "#include <malloc.h>\n");
597 	(void) fprintf(ftable, "#include <memory.h>\n");
598 	(void) fprintf(ftable, "#define	YYCONST\n");
599 	(void) fprintf(ftable, "#endif\n");
600 	(void) fprintf(ftable, "\n#include <values.h>\n");
601 
602 	if (sym_prefix[0] != '\0')
603 		put_prefix_define(sym_prefix);
604 
605 	(void) fprintf(ftable,
606 	"\n#if defined(__cplusplus) || defined(__STDC__)\n");
607 	(void) fprintf(ftable,
608 	"\n#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
609 	(void) fprintf(ftable, "extern \"C\" {\n");
610 	(void) fprintf(ftable, "#endif\n");
611 	(void) fprintf(ftable, "#ifndef yyerror\n");
612 	(void) fprintf(ftable, "#if defined(__cplusplus)\n");
613 	(void) fprintf(ftable, "	void yyerror(YYCONST char *);\n");
614 	(void) fprintf(ftable, "#endif\n");
615 	(void) fprintf(ftable, "#endif\n");
616 	(void) fprintf(ftable, "#ifndef yylex\n");
617 	(void) fprintf(ftable, "	int yylex(void);\n");
618 	(void) fprintf(ftable, "#endif\n");
619 	(void) fprintf(ftable, "	int yyparse(void);\n");
620 	(void) fprintf(ftable,
621 	"#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
622 	(void) fprintf(ftable, "}\n");
623 	(void) fprintf(ftable, "#endif\n");
624 	(void) fprintf(ftable, "\n#endif\n\n");
625 
626 	(void) fprintf(ftable, "#define yyclearin yychar = -1\n");
627 	(void) fprintf(ftable, "#define yyerrok yyerrflag = 0\n");
628 	(void) fprintf(ftable, "extern int yychar;\nextern int yyerrflag;\n");
629 	if (!(defunion || ntypes))
630 		(void) fprintf(ftable,
631 			"#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
632 	(void) fprintf(ftable, "YYSTYPE yylval;\n");
633 	(void) fprintf(ftable, "YYSTYPE yyval;\n");
634 	(void) fprintf(ftable, "typedef int yytabelem;\n");
635 	(void) fprintf(ftable,
636 		"#ifndef YYMAXDEPTH\n#define YYMAXDEPTH 150\n#endif\n");
637 	(void) fprintf(ftable, "#if YYMAXDEPTH > 0\n");
638 	(void) fprintf(ftable, "int yy_yys[YYMAXDEPTH], *yys = yy_yys;\n");
639 	(void) fprintf(ftable, "YYSTYPE yy_yyv[YYMAXDEPTH], *yyv = yy_yyv;\n");
640 	(void) fprintf(ftable, "#else	/* user does initial allocation */\n");
641 	(void) fprintf(ftable, "int *yys;\nYYSTYPE *yyv;\n#endif\n");
642 	(void) fprintf(ftable, "static int yymaxdepth = YYMAXDEPTH;\n");
643 
644 	prdptr[0] = mem;
645 	/* added production */
646 	*mem++ = NTBASE;
647 
648 	/* if start is 0, we will overwrite with the lhs of the first rule */
649 	*mem++ = start;
650 	*mem++ = 1;
651 	*mem++ = 0;
652 	prdptr[1] = mem;
653 
654 	while ((t = gettok()) == LCURLY)
655 		cpycode();
656 
657 	if (t != C_IDENTIFIER)
658 		error("bad syntax on first rule");
659 
660 	if (!start)
661 		prdptr[0][1] = chfind(1, tokname);
662 
663 	/* read rules */
664 
665 	while (t != MARK && t != ENDFILE) {
666 
667 		/* process a rule */
668 
669 		if (t == L'|') {
670 			rhsfill((wchar_t *)0); /* restart fill of rhs */
671 			*mem = *prdptr[nprod-1];
672 			if (++mem >= &tracemem[new_memsize])
673 				exp_mem(1);
674 		} else if (t == C_IDENTIFIER) {
675 			*mem = chfind(1, tokname);
676 			if (*mem < NTBASE)
677 /*
678  * TRANSLATION_NOTE  -- This is a message from yacc.
679  *	This message is passed to error() function.
680  *	Check how nonterminal is translated.
681  */
682 				error(gettext(
683 				"illegal nonterminal in grammar rule"));
684 			if (++mem >= &tracemem[new_memsize])
685 				exp_mem(1);
686 			lhsfill(tokname);	/* new rule: restart strings */
687 		} else
688 /*
689  * TRANSLATION_NOTE  -- This is a message from yacc.
690  *	This message is passed to error() function.
691  */
692 			error(gettext(
693 			"illegal rule: missing semicolon or | ?"));
694 
695 		/* read rule body */
696 
697 
698 		t = gettok();
699 	more_rule:
700 		while (t == IDENTIFIER) {
701 			*mem = chfind(1, tokname);
702 			if (*mem < NTBASE)
703 				levprd[nprod] = toklev[*mem]& ~04;
704 			if (++mem >= &tracemem[new_memsize])
705 				exp_mem(1);
706 			rhsfill(tokname);	/* add to rhs string */
707 			t = gettok();
708 			}
709 
710 		if (t == PREC) {
711 			if (gettok() != IDENTIFIER)
712 /*
713  * TRANSLATION_NOTE  -- This is a message from yacc.
714  *	This message is passed to error() function.
715  *	Do not translate %%prec.
716  */
717 				error(gettext(
718 				"illegal %%prec syntax"));
719 			j = chfind(2, tokname);
720 			if (j >= NTBASE)
721 /*
722  * TRANSLATION_NOTE  -- This is a message from yacc.
723  *	This message is passed to error() function.
724  *	Do not translate %%prec.
725  */
726 				error(gettext(
727 				"nonterminal %ws illegal after %%prec"),
728 				nontrst[j-NTBASE].name);
729 			levprd[nprod] = toklev[j] & ~04;
730 			t = gettok();
731 			}
732 
733 		if (t == L'=') {
734 			had_act[nprod] = 1;
735 			levprd[nprod] |= ACTFLAG;
736 			(void) fprintf(faction, "\ncase %d:", nprod);
737 			cpyact(mem-prdptr[nprod] - 1);
738 			(void) fprintf(faction, " break;");
739 			if ((t = gettok()) == IDENTIFIER) {
740 				/* action within rule... */
741 
742 				lrprnt();		/* dump lhs, rhs */
743 				(void) wsprintf(actname, "$$%d", nprod);
744 				/*
745 				 * make it nonterminal
746 				 */
747 				j = chfind(1, actname);
748 
749 				/*
750 				 * the current rule will become rule
751 				 * number nprod+1 move the contents down,
752 				 * and make room for the null
753 				 */
754 
755 				if (mem + 2 >= &tracemem[new_memsize])
756 					exp_mem(1);
757 				for (p = mem; p >= prdptr[nprod]; --p)
758 					p[2] = *p;
759 				mem += 2;
760 
761 				/* enter null production for action */
762 
763 				p = prdptr[nprod];
764 
765 				*p++ = j;
766 				*p++ = -nprod;
767 
768 				/* update the production information */
769 
770 				levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
771 				levprd[nprod] = ACTFLAG;
772 
773 				if (++nprod >= nprodsz)
774 					exp_prod();
775 				prdptr[nprod] = p;
776 
777 				/*
778 				 * make the action appear in
779 				 * the original rule
780 				 */
781 				*mem++ = j;
782 				if (mem >= &tracemem[new_memsize])
783 					exp_mem(1);
784 				/* get some more of the rule */
785 				goto more_rule;
786 			}
787 		}
788 		while (t == L';')
789 			t = gettok();
790 		*mem++ = -nprod;
791 		if (mem >= &tracemem[new_memsize])
792 			exp_mem(1);
793 
794 		/* check that default action is reasonable */
795 
796 		if (ntypes && !(levprd[nprod] & ACTFLAG) &&
797 				nontrst[*prdptr[nprod]-NTBASE].tvalue) {
798 			/* no explicit action, LHS has value */
799 			int tempty;
800 			tempty = prdptr[nprod][1];
801 			if (tempty < 0)
802 /*
803  * TRANSLATION_NOTE  -- This is a message from yacc.
804  *	This message is passed to error() function.
805  *	LHS means Left Hand Side. It does not need to be translated.
806  */
807 				error(gettext(
808 				"must return a value, since LHS has a type"));
809 			else if (tempty >= NTBASE)
810 				tempty = nontrst[tempty-NTBASE].tvalue;
811 			else
812 				tempty = TYPE(toklev[tempty]);
813 			if (tempty != nontrst[*prdptr[nprod]-NTBASE].tvalue) {
814 /*
815  * TRANSLATION_NOTE  -- This is a message from yacc.
816  *	This message is passed to error() function.
817  *	Check how action is transltated in yacc man page or documents.
818  */
819 				error(gettext(
820 				"default action causes potential type clash"));
821 			}
822 		}
823 
824 		if (++nprod >= nprodsz)
825 			exp_prod();
826 		prdptr[nprod] = mem;
827 		levprd[nprod] = 0;
828 		}
829 	/* end of all rules */
830 
831 	end_debug();		/* finish fdebug file's input */
832 	finact();
833 	if (t == MARK) {
834 		if (gen_lines)
835 			(void) fprintf(ftable, "\n# line %d \"%s\"\n",
836 				lineno, infile);
837 		while ((c = getwc(finput)) != EOF)
838 			(void) putwc(c, ftable);
839 		}
840 	(void) fclose(finput);
841 }
842 
843 static void
844 finact()
845 {
846 	/* finish action routine */
847 	(void) fclose(faction);
848 	(void) fprintf(ftable, "# define YYERRCODE %d\n", tokset[2].value);
849 }
850 
851 static wchar_t *
852 cstash(s)
853 register wchar_t *s;
854 {
855 	wchar_t *temp;
856 	static int used = 0;
857 	static int used_save = 0;
858 	static int exp_cname = CNAMSZ;
859 	int len = wslen(s);
860 
861 	/*
862 	 * 2/29/88 -
863 	 * Don't need to expand the table, just allocate new space.
864 	 */
865 	used_save = used;
866 	while (len >= (exp_cname - used_save)) {
867 		exp_cname += CNAMSZ;
868 		if (!used)
869 			free((char *)cnames);
870 		if ((cnames = (wchar_t *)
871 			malloc(sizeof (wchar_t)*exp_cname)) == NULL)
872 /*
873  * TRANSLATION_NOTE  -- This is a message from yacc.
874  *	This message is passed to error() function.
875  *
876  *	You may just translate this as:
877  *	'Could not allocate internally used memory.'
878  */
879 			error(gettext(
880 			"cannot expand string dump"));
881 		cnamp = cnames;
882 		used = 0;
883 	}
884 
885 	temp = cnamp;
886 	do {
887 		*cnamp++ = *s;
888 	} while (*s++);
889 	used += cnamp - temp;
890 	return (temp);
891 }
892 
893 static int
894 defin(int t, wchar_t *s)
895 {
896 	/* define s to be a terminal if t=0 or a nonterminal if t=1 */
897 
898 	int val;
899 
900 	if (t) {
901 		if (++nnonter >= nnontersz)
902 			exp_nonterm();
903 		nontrst[nnonter].name = cstash(s);
904 		return (NTBASE + nnonter);
905 		}
906 	/* must be a token */
907 	if (++ntokens >= ntoksz)
908 		exp_ntok();
909 	tokset[ntokens].name = cstash(s);
910 
911 	/* establish value for token */
912 
913 	if (s[0] == L' ' && s[2] == 0) { /* single character literal */
914 		val = findchtok(s[1]);
915 	} else if (s[0] == L' ' && s[1] == L'\\') { /* escape sequence */
916 		if (s[3] == 0) { /* single character escape sequence */
917 			switch (s[2]) {
918 				/* character which is escaped */
919 			case L'a':
920 				(void) warning(1, gettext(
921 /*
922  * TRANSLATION_NOTE  -- This is a message from yacc.
923  *	This message is passed to warning() function.
924  *	Do not trasnlate ANSI C, \\a.
925  */
926 		"\\a is ANSI C \"alert\" character"));
927 #if __STDC__ - 1 == 0
928 				val = L'\a';
929 				break;
930 #else
931 				val = L'\007';
932 				break;
933 #endif
934 			case L'v': val = L'\v'; break;
935 			case L'n': val = L'\n'; break;
936 			case L'r': val = L'\r'; break;
937 			case L'b': val = L'\b'; break;
938 			case L't': val = L'\t'; break;
939 			case L'f': val = L'\f'; break;
940 			case L'\'': val = L'\''; break;
941 			case L'"': val = L'"'; break;
942 			case L'?': val = L'?'; break;
943 			case L'\\': val = L'\\'; break;
944 /*
945  * TRANSLATION_NOTE  -- This is a message from yacc.
946  *	This message is passed to error() function.
947  */
948 			default: error(gettext(
949 				"invalid escape"));
950 			}
951 		} else if (s[2] <= L'7' && s[2] >= L'0') { /* \nnn sequence */
952 			int i = 3;
953 			val = s[2] - L'0';
954 			while (iswdigit(s[i]) && i <= 4) {
955 				if (s[i] >= L'0' && s[i] <= L'7')
956 					val = val * 8 + s[i] - L'0';
957 				else
958 /*
959  * TRANSLATION_NOTE  -- This is a message from yacc.
960  *	This message is passed to error() function.
961  */
962 					error(gettext(
963 					"illegal octal number"));
964 				i++;
965 			}
966 			if (s[i] != 0)
967 /*
968  * TRANSLATION_NOTE  -- This is a message from yacc.
969  *	This message is passed to error() function.
970  *	Do not translate \\nnn.
971  */
972 				error(gettext(
973 				"illegal \\nnn construction"));
974 			if (val > 255)
975 /*
976  * TRANSLATION_NOTE  -- This is a message from yacc.
977  *	This message is passed to error() function.
978  *	Do not translate
979  *		\\nnn, \\xnnnnnnnn.
980  */
981 				error(
982 "\\nnn exceed \\377; use \\xnnnnnnnn for wchar_t value of multibyte char");
983 			if (val == 0 && i >= 4)
984 /*
985  * TRANSLATION_NOTE  -- This is a message from yacc.
986  *	This message is passed to error() function.
987  *	Do not translate \\000.
988  */
989 				error(gettext(
990 				"'\\000' is illegal"));
991 		} else if (s[2] == L'x') { /* hexadecimal \xnnn sequence */
992 			int i = 3;
993 			val = 0;
994 /*
995  * TRANSLATION_NOTE  -- This is a message from yacc.
996  *	This message is passed to warning() function.
997  *	Do not translate \\x, ANSI C.
998  */
999 			(void) warning(1, gettext(
1000 			    "\\x is ANSI C hex escape"));
1001 			if (iswxdigit(s[i]))
1002 				while (iswxdigit(s[i])) {
1003 					int tmpval;
1004 					if (iswdigit(s[i]))
1005 						tmpval = s[i] - L'0';
1006 					else if (s[i] >= L'a')
1007 						tmpval = s[i] - L'a' + 10;
1008 					else
1009 						tmpval = s[i] - L'A' + 10;
1010 					val = 16 * val + tmpval;
1011 					i++;
1012 				}
1013 			else
1014 				error(gettext(
1015 				"illegal hexadecimal number"));
1016 			if (s[i] != 0)
1017 /*
1018  * TRANSLATION_NOTE  -- This is a message from yacc.
1019  *	This message is passed to error() function.
1020  *	Do not translate \\xnn.
1021  */
1022 				error(gettext(
1023 				"illegal \\xnn construction"));
1024 #define	LWCHAR_MAX	0x7fffffff
1025 			if ((unsigned)val > LWCHAR_MAX)
1026 /*
1027  * TRANSLATION_NOTE  -- This is a message from yacc.
1028  *	This message is passed to error() function.
1029  *	Do not translate \\xnnnnnnnn and %#x.
1030  */
1031 				error(gettext(
1032 				    " \\xnnnnnnnn exceed %#x"),
1033 				    LWCHAR_MAX);
1034 			if (val == 0)
1035 /*
1036  * TRANSLATION_NOTE  -- This is a message from yacc.
1037  *	This message is passed to error() function.
1038  *	Do not translate \\x00.
1039  */
1040 				error(gettext(
1041 				"'\\x00' is illegal"));
1042 			val = findchtok(val);
1043 		} else
1044 			error(gettext(
1045 			"invalid escape"));
1046 	} else {
1047 		val = extval++;
1048 	}
1049 	tokset[ntokens].value = val;
1050 	toklev[ntokens] = 0;
1051 	return (ntokens);
1052 }
1053 
1054 static void
1055 defout()
1056 {
1057 	/* write out the defines (at the end of the declaration section) */
1058 
1059 	register int i, c;
1060 	register wchar_t *cp;
1061 
1062 	for (i = ndefout; i <= ntokens; ++i) {
1063 
1064 		cp = tokset[i].name;
1065 		if (*cp == L' ')	/* literals */
1066 		{
1067 			(void) fprintf(fdebug, "\t\"%ws\",\t%d,\n",
1068 			    tokset[i].name + 1, tokset[i].value);
1069 			continue;	/* was cp++ */
1070 		}
1071 
1072 		for (; (c = *cp) != 0; ++cp) {
1073 			if (iswlower(c) || iswupper(c) ||
1074 			    iswdigit(c) || c == L'_')
1075 				/* EMPTY */;
1076 			else
1077 				goto nodef;
1078 		}
1079 
1080 		(void) fprintf(fdebug,
1081 		    "\t\"%ws\",\t%d,\n", tokset[i].name,
1082 		    tokset[i].value);
1083 		(void) fprintf(ftable,
1084 		    "# define %ws %d\n", tokset[i].name,
1085 		    tokset[i].value);
1086 		if (fdefine != NULL)
1087 			(void) fprintf(fdefine,
1088 			    "# define %ws %d\n",
1089 			    tokset[i].name,
1090 			    tokset[i].value);
1091 
1092 	nodef:;
1093 	}
1094 	ndefout = ntokens+1;
1095 }
1096 
1097 static int
1098 gettok()
1099 {
1100 	int i, base;
1101 	static int peekline; /* number of '\n' seen in lookahead */
1102 	int c, match, reserve;
1103 begin:
1104 	reserve = 0;
1105 	lineno += peekline;
1106 	peekline = 0;
1107 	c = getwc(finput);
1108 	/*
1109 	 * while (c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1110 	 */
1111 	while (iswspace(c)) {
1112 		if (c == L'\n')
1113 			++lineno;
1114 		c = getwc(finput);
1115 	}
1116 	if (c == L'/') { /* skip comment */
1117 		lineno += skipcom();
1118 		goto begin;
1119 	}
1120 
1121 	switch (c) {
1122 
1123 	case EOF:
1124 		return (ENDFILE);
1125 	case L'{':
1126 		(void) ungetwc(c, finput);
1127 		return (L'=');  /* action ... */
1128 	case L'<':  /* get, and look up, a type name (union member name) */
1129 		i = 0;
1130 		while ((c = getwc(finput)) != L'>' &&
1131 		    c != EOF && c != L'\n') {
1132 			tokname[i] = c;
1133 			if (++i >= toksize)
1134 				exp_tokname();
1135 			}
1136 		if (c != L'>')
1137 			error(gettext(
1138 			"unterminated < ... > clause"));
1139 		tokname[i] = 0;
1140 		if (i == 0)
1141 			error("missing type name in < ... > clause");
1142 		for (i = 1; i <= ntypes; ++i) {
1143 			if (!wscmp(typeset[i], tokname)) {
1144 				numbval = i;
1145 				return (TYPENAME);
1146 				}
1147 			}
1148 		typeset[numbval = ++ntypes] = cstash(tokname);
1149 		return (TYPENAME);
1150 
1151 	case L'"':
1152 	case L'\'':
1153 		match = c;
1154 		tokname[0] = L' ';
1155 		i = 1;
1156 		for (;;) {
1157 			c = getwc(finput);
1158 			if (c == L'\n' || c == EOF)
1159 				error(gettext(
1160 				"illegal or missing ' or \""));
1161 			if (c == L'\\') {
1162 				c = getwc(finput);
1163 				tokname[i] = L'\\';
1164 				if (++i >= toksize)
1165 					exp_tokname();
1166 			} else if (c == match) break;
1167 			tokname[i] = c;
1168 			if (++i >= toksize)
1169 				exp_tokname();
1170 			}
1171 		break;
1172 
1173 	case L'%':
1174 	case L'\\':
1175 
1176 		switch (c = getwc(finput)) {
1177 
1178 		case L'0':	return (TERM);
1179 		case L'<':	return (LEFT);
1180 		case L'2':	return (BINARY);
1181 		case L'>':	return (RIGHT);
1182 		case L'%':
1183 		case L'\\':	return (MARK);
1184 		case L'=':	return (PREC);
1185 		case L'{':	return (LCURLY);
1186 		default:	reserve = 1;
1187 			}
1188 
1189 	default:
1190 
1191 		if (iswdigit(c)) { /* number */
1192 			numbval = c - L'0';
1193 			base = (c == L'0') ? 8 : 10;
1194 			for (c = getwc(finput);
1195 			    iswdigit(c);
1196 			    c = getwc(finput)) {
1197 				numbval = numbval*base + c - L'0';
1198 				}
1199 			(void) ungetwc(c, finput);
1200 			return (NUMBER);
1201 		} else if (iswlower(c) || iswupper(c) ||
1202 		    c == L'_' || c == L'.' ||
1203 		    c == L'$') {
1204 			i = 0;
1205 			while (iswlower(c) || iswupper(c) ||
1206 			    iswdigit(c) || c == L'_' ||
1207 			    c == L'.' || c == L'$') {
1208 				tokname[i] = c;
1209 				if (reserve && iswupper(c))
1210 					tokname[i] = towlower(c);
1211 				if (++i >= toksize)
1212 					exp_tokname();
1213 				c = getwc(finput);
1214 				}
1215 			}
1216 		else
1217 			return (c);
1218 
1219 		(void) ungetwc(c, finput);
1220 		}
1221 
1222 	tokname[i] = 0;
1223 
1224 	if (reserve) { /* find a reserved word */
1225 		if (!wscmp(tokname, L"term"))
1226 			return (TERM);
1227 		if (!wscmp(tokname, L"token"))
1228 			return (TERM);
1229 		if (!wscmp(tokname, L"left"))
1230 			return (LEFT);
1231 		if (!wscmp(tokname, L"nonassoc"))
1232 			return (BINARY);
1233 		if (!wscmp(tokname, L"binary"))
1234 			return (BINARY);
1235 		if (!wscmp(tokname, L"right"))
1236 			return (RIGHT);
1237 		if (!wscmp(tokname, L"prec"))
1238 			return (PREC);
1239 		if (!wscmp(tokname, L"start"))
1240 			return (START);
1241 		if (!wscmp(tokname, L"type"))
1242 			return (TYPEDEF);
1243 		if (!wscmp(tokname, L"union"))
1244 			return (UNION);
1245 		error(gettext(
1246 		    "invalid escape, or illegal reserved word: %ws"),
1247 		    tokname);
1248 		}
1249 
1250 	/* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */
1251 
1252 	c = getwc(finput);
1253 	/*
1254 	 * while (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '/')
1255 	 * {
1256 	 */
1257 	while (iswspace(c) || c == L'/') {
1258 		if (c == L'\n') {
1259 			++peekline;
1260 		} else if (c == L'/') { /* look for comments */
1261 			peekline += skipcom();
1262 			}
1263 		c = getwc(finput);
1264 		}
1265 	if (c == L':')
1266 		return (C_IDENTIFIER);
1267 	(void) ungetwc(c, finput);
1268 	return (IDENTIFIER);
1269 }
1270 
1271 static int
1272 fdtype(int t)
1273 {
1274 	/* determine the type of a symbol */
1275 	int v;
1276 	if (t >= NTBASE)
1277 		v = nontrst[t-NTBASE].tvalue;
1278 	else
1279 		v = TYPE(toklev[t]);
1280 	if (v <= 0)
1281 		error(gettext(
1282 		    "must specify type for %ws"),
1283 		    (t >= NTBASE) ? nontrst[t-NTBASE].name:
1284 		    tokset[t].name);
1285 	return (v);
1286 }
1287 
1288 static int
1289 chfind(int t, wchar_t *s)
1290 {
1291 	int i;
1292 
1293 	if (s[0] == ' ')
1294 		t = 0;
1295 	TLOOP(i) {
1296 		if (!wscmp(s, tokset[i].name)) {
1297 			return (i);
1298 		}
1299 	}
1300 	NTLOOP(i) {
1301 		if (!wscmp(s, nontrst[i].name)) {
1302 			return (i + NTBASE);
1303 		}
1304 	}
1305 	/* cannot find name */
1306 	if (t > 1)
1307 		error(gettext(
1308 		"%ws should have been defined earlier"), s);
1309 	return (defin(t, s));
1310 }
1311 
1312 static void
1313 cpyunion()
1314 {
1315 	/*
1316 	 * copy the union declaration to the output,
1317 	 * and the define file if present
1318 	 */
1319 	int level, c;
1320 	if (gen_lines)
1321 		(void) fprintf(ftable, "\n# line %d \"%s\"\n", lineno, infile);
1322 	(void) fprintf(ftable, "typedef union\n");
1323 	if (fdefine)
1324 		(void) fprintf(fdefine, "\ntypedef union\n");
1325 	(void) fprintf(ftable, "#ifdef __cplusplus\n\tYYSTYPE\n#endif\n");
1326 	if (fdefine)
1327 		(void) fprintf(fdefine,
1328 		    "#ifdef __cplusplus\n\tYYSTYPE\n#endif\n");
1329 
1330 	level = 0;
1331 	for (;;) {
1332 		if ((c = getwc(finput)) == EOF)
1333 /*
1334  * TRANSLATION_NOTE  -- This is a message from yacc.
1335  *	This message is passed to error() function.
1336  *	EOF - End Of File.
1337  *	Do not translate %%union.
1338  */
1339 			error(gettext(
1340 			"EOF encountered while processing %%union"));
1341 		(void) putwc(c, ftable);
1342 		if (fdefine)
1343 			(void) putwc(c, fdefine);
1344 
1345 		switch (c) {
1346 
1347 		case L'\n':
1348 			++lineno;
1349 			break;
1350 
1351 		case L'{':
1352 			++level;
1353 			break;
1354 
1355 		case L'}':
1356 			--level;
1357 			if (level == 0) { /* we are finished copying */
1358 				(void) fprintf(ftable, " YYSTYPE;\n");
1359 				if (fdefine)
1360 					(void) fprintf(fdefine,
1361 					" YYSTYPE;\nextern YYSTYPE yylval;\n");
1362 				return;
1363 				}
1364 			}
1365 		}
1366 }
1367 
1368 static void
1369 cpycode()
1370 {
1371 	/* copies code between \{ and \} */
1372 
1373 	int c;
1374 	c = getwc(finput);
1375 	if (c == L'\n') {
1376 		c = getwc(finput);
1377 		lineno++;
1378 		}
1379 	if (gen_lines)
1380 		(void) fprintf(ftable, "\n# line %d \"%s\"\n", lineno, infile);
1381 	while (c != EOF) {
1382 		if (c == L'\\') {
1383 			if ((c = getwc(finput)) == L'}')
1384 				return;
1385 			else
1386 				(void) putwc(L'\\', ftable);
1387 		} else if (c == L'%') {
1388 			if ((c = getwc(finput)) == L'}')
1389 				return;
1390 			else
1391 				(void) putwc(L'%', ftable);
1392 		}
1393 		(void) putwc(c, ftable);
1394 		if (c == L'\n')
1395 			++lineno;
1396 		c = getwc(finput);
1397 		}
1398 /*
1399  * TRANSLATION_NOTE  -- This is a message from yacc.
1400  *	This message is passed to error() function.
1401  *	Do not translate %%}.
1402  */
1403 	error(gettext(
1404 	"eof before %%}"));
1405 }
1406 
1407 static int
1408 skipcom()
1409 {
1410 	/* skip over comments */
1411 	int c, i = 0;  /* i is the number of lines skipped */
1412 
1413 	/* skipcom is called after reading a / */
1414 
1415 	if (getwc(finput) != L'*')
1416 		error(gettext(
1417 		"illegal comment"));
1418 	c = getwc(finput);
1419 	while (c != EOF) {
1420 		while (c == L'*') {
1421 			if ((c = getwc(finput)) == L'/')
1422 				return (i);
1423 			}
1424 		if (c == L'\n')
1425 			++i;
1426 		c = getwc(finput);
1427 		}
1428 /*
1429  * TRANSLATION_NOTE  -- This is a message from yacc.
1430  *	This message is passed to error() function.
1431  *	EOF -- End Of File.
1432  */
1433 	error(gettext(
1434 	"EOF inside comment"));
1435 	/* NOTREACHED */
1436 	return (0);
1437 }
1438 
1439 static void
1440 cpyact(int offset)
1441 {
1442 	/* copy C action to the next ; or closing } */
1443 	int brac, c, match, i, t, j, s, tok, argument, m;
1444 	wchar_t id_name[NAMESIZE+1];
1445 	int id_idx = 0;
1446 
1447 	if (gen_lines) {
1448 		(void) fprintf(faction, "\n# line %d \"%s\"\n", lineno, infile);
1449 		act_lines++;
1450 	}
1451 	brac = 0;
1452 	id_name[0] = 0;
1453 loop:
1454 	c = getwc(finput);
1455 swt:
1456 	switch (c) {
1457 	case L';':
1458 		if (brac == 0) {
1459 			(void) putwc(c, faction);
1460 			return;
1461 		}
1462 		goto lcopy;
1463 	case L'{':
1464 		brac++;
1465 		goto lcopy;
1466 	case L'$':
1467 		s = 1;
1468 		tok = -1;
1469 		argument = 1;
1470 		while ((c = getwc(finput)) == L' ' || c == L'\t')
1471 			/* NULL */;
1472 		if (c == L'<') { /* type description */
1473 			(void) ungetwc(c, finput);
1474 			if (gettok() != TYPENAME)
1475 /*
1476  * TRANSLATION_NOTE  -- This is a message from yacc.
1477  *	This message is passed to error() function.
1478  *	Do not translate $<ident>
1479  */
1480 				error(gettext(
1481 				"bad syntax on $<ident> clause"));
1482 			tok = numbval;
1483 			c = getwc(finput);
1484 		}
1485 		if (c == L'$') {
1486 			(void) fprintf(faction, "yyval");
1487 			if (ntypes) { /* put out the proper tag... */
1488 				if (tok < 0)
1489 					tok = fdtype(*prdptr[nprod]);
1490 				(void) fprintf(faction,
1491 				    ".%ws", typeset[tok]);
1492 			}
1493 			goto loop;
1494 		}
1495 		if (iswalpha(c)) {
1496 			int same = 0;
1497 			int id_sw = 0;
1498 			(void) ungetwc(c, finput);
1499 			if (gettok() != IDENTIFIER)
1500 /*
1501  * TRANSLATION_NOTE  -- This is a message from yacc.
1502  *	This message is passed to error() function.
1503  *	Check how action is translated in yacc man page/document.
1504  */
1505 				error(gettext(
1506 				"bad action format"));
1507 			/*
1508 			 * Save the number of non-terminal
1509 			 */
1510 			id_sw = nnonter;
1511 			t = chfind(1, tokname);
1512 			/*
1513 			 * Check if the identifier is added as a non-terminal
1514 			 */
1515 			if (id_sw != nnonter)
1516 				id_sw = 1;
1517 			else
1518 				id_sw = 0;
1519 			while ((c = getwc(finput)) == L' ' ||
1520 			    c == L'\t')
1521 				/* NULL */;
1522 			if (c == L'#') {
1523 				while ((c = getwc(finput)) == L' ' ||
1524 				    c == L'\t')
1525 					/* NULL */;
1526 				if (iswdigit(c)) {
1527 					m = 0;
1528 					while (iswdigit(c)) {
1529 						m = m*10+c-L'0';
1530 						c = getwc(finput);
1531 					}
1532 					argument = m;
1533 				} else
1534 					error(gettext(
1535 					"illegal character \"#\""));
1536 			}
1537 			if (argument < 1)
1538 /*
1539  * TRANSLATION_NOTE  -- This is a message from yacc.
1540  *	This message is passed to error() function.
1541  *	Check how action is translated in yacc man page/document.
1542  */
1543 				error(gettext(
1544 				"illegal action argument no."));
1545 			for (i = 1; i <= offset; ++i)
1546 				if (prdptr[nprod][i] == t)
1547 					if (++same == argument) {
1548 						(void) fprintf(faction,
1549 						    "yypvt[-%d]", offset-i);
1550 						if (ntypes) {
1551 							if (tok < 0)
1552 								tok =
1553 								/* CSTYLED */
1554 								fdtype(prdptr[nprod][i]);
1555 							(void) fprintf(faction,
1556 							".%ws", typeset[tok]);
1557 						}
1558 						goto swt;
1559 					}
1560 			/*
1561 			 * This used to be handled as error.
1562 			 * Treat this as a valid C statement.
1563 			 * (Likely id with $ in.)
1564 			 * If non-terminal is added, remove it from the list.
1565 			 */
1566 			fprintf(faction, "$%ws", tokname);
1567 /*
1568  * TRANSLATION_NOTE  -- This is a message from yacc.
1569  *	This message is passed to warning() function.
1570  *	Do not translate Ansi C.
1571  */
1572 			warning(1, gettext(
1573 	"Illegal character '$' in Ansi C symbol: %ws$%ws."),
1574 			    id_name, tokname);
1575 
1576 			if (id_sw == 1)
1577 				--nnonter;
1578 			goto swt;
1579 		}
1580 		if (c == '-') {
1581 			s = -s;
1582 			c = getwc(finput);
1583 		}
1584 		if (iswdigit(c)) {
1585 			j = 0;
1586 			while (iswdigit(c)) {
1587 				j = j*10 + c - L'0';
1588 				c = getwc(finput);
1589 			}
1590 			j = j*s - offset;
1591 			if (j > 0) {
1592 /*
1593  * TRANSLATION_NOTE  -- This is a message from yacc.
1594  *	This message is passed to error() function.
1595  *	Do not translate $%d.
1596  */
1597 				error(gettext(
1598 				    "Illegal use of $%d"),
1599 				    j + offset);
1600 			}
1601 			(void) fprintf(faction, "yypvt[-%d]", -j);
1602 			if (ntypes) { /* put out the proper tag */
1603 				if (j + offset <= 0 && tok < 0)
1604 /*
1605  * TRANSLATION_NOTE  -- This is a message from yacc.
1606  *	This message is passed to error() function.
1607  *	Do not translate $%d.
1608  */
1609 					error(gettext(
1610 					    "must specify type of $%d"),
1611 					    j + offset);
1612 				if (tok < 0)
1613 					tok = fdtype(prdptr[nprod][j+offset]);
1614 				(void) fprintf(faction,
1615 				    ".%ws", typeset[tok]);
1616 			}
1617 			goto swt;
1618 		}
1619 		(void) putwc(L'$', faction);
1620 		if (s < 0)
1621 			(void) putwc(L'-', faction);
1622 		goto swt;
1623 	case L'}':
1624 		if (--brac)
1625 			goto lcopy;
1626 		(void) putwc(c, faction);
1627 		return;
1628 	case L'/':	/* look for comments */
1629 		(void) putwc(c, faction);
1630 		c = getwc(finput);
1631 		if (c != L'*')
1632 			goto swt;
1633 		/* it really is a comment */
1634 		(void) putwc(c, faction);
1635 		c = getwc(finput);
1636 		while (c != EOF) {
1637 			while (c == L'*') {
1638 				(void) putwc(c, faction);
1639 				if ((c = getwc(finput)) == L'/')
1640 					goto lcopy;
1641 			}
1642 			(void) putwc(c, faction);
1643 			if (c == L'\n')
1644 				++lineno;
1645 			c = getwc(finput);
1646 		}
1647 		error("EOF inside comment");
1648 		/* FALLTHRU */
1649 	case L'\'':	/* character constant */
1650 	case L'"':	/* character string */
1651 		match = c;
1652 		(void) putwc(c, faction);
1653 		while ((c = getwc(finput)) != EOF) {
1654 			if (c == L'\\') {
1655 				(void) putwc(c, faction);
1656 				c = getwc(finput);
1657 				if (c == L'\n')
1658 					++lineno;
1659 			} else if (c == match)
1660 				goto lcopy;
1661 			else if (c == L'\n')
1662 /*
1663  * TRANSLATION_NOTE  -- This is a message from yacc.
1664  *	This message is passed to error() function.
1665  *	This error message is issued when
1666  *	quoted string has multiple lines.
1667  */
1668 				error(gettext(
1669 				"newline in string or char. const."));
1670 			(void) putwc(c, faction);
1671 		}
1672 		error(gettext(
1673 		"EOF in string or character constant"));
1674 		/* FALLTHRU */
1675 	case EOF:
1676 /*
1677  * TRANSLATION_NOTE  -- This is a message from yacc.
1678  *	This message is passed to error() function.
1679  *	Check how 'action' is translated in yacc mapage/document.
1680  */
1681 		error(gettext(
1682 		"action does not terminate"));
1683 		/* FALLTHRU */
1684 	case L'\n':
1685 		++lineno;
1686 		goto lcopy;
1687 	}
1688 lcopy:
1689 	(void) putwc(c, faction);
1690 	/*
1691 	 * Save the possible identifier name.
1692 	 * Used to print out a warning message.
1693 	 */
1694 	if (id_idx >= NAMESIZE) {
1695 		/*
1696 		 * Error. Silently ignore.
1697 		 */
1698 		/* EMPTY */;
1699 	}
1700 	/*
1701 	 * If c has a possibility to be a
1702 	 * part of identifier, save it.
1703 	 */
1704 	else if (iswalnum(c) || c == L'_') {
1705 		id_name[id_idx++] = c;
1706 		id_name[id_idx] = 0;
1707 	} else {
1708 		id_idx = 0;
1709 		id_name[id_idx] = 0;
1710 	}
1711 	goto loop;
1712 }
1713 
1714 static void
1715 lhsfill(s)	/* new rule, dump old (if exists), restart strings */
1716 wchar_t *s;
1717 {
1718 	static int lhs_len = LHS_TEXT_LEN;
1719 	int s_lhs = wslen(s);
1720 	if (s_lhs >= lhs_len) {
1721 		lhs_len = s_lhs + 2;
1722 		lhstext = (wchar_t *)
1723 			realloc((char *)lhstext, sizeof (wchar_t)*lhs_len);
1724 		if (lhstext == NULL)
1725 /*
1726  * TRANSLATION_NOTE  -- This is a message from yacc.
1727  *	This message is passed to error() function.
1728  *	LHS -- Left Hand Side.
1729  */
1730 			error(gettext(
1731 			"couldn't expanded LHS length"));
1732 	}
1733 	rhsfill((wchar_t *)0);
1734 	(void) wscpy(lhstext, s); /* don't worry about too long of a name */
1735 }
1736 
1737 static void
1738 rhsfill(s)
1739 wchar_t *s;	/* either name or 0 */
1740 {
1741 	static wchar_t *loc;	/* next free location in rhstext */
1742 	static int rhs_len = RHS_TEXT_LEN;
1743 	static int used = 0;
1744 	int s_rhs = (s == NULL ? 0 : wslen(s));
1745 	register wchar_t *p;
1746 
1747 	if (!s)	/* print out and erase old text */
1748 	{
1749 		if (*lhstext)		/* there was an old rule - dump it */
1750 			lrprnt();
1751 		(loc = rhstext)[0] = 0;
1752 		return;
1753 	}
1754 	/* add to stuff in rhstext */
1755 	p = s;
1756 
1757 	used = loc - rhstext;
1758 	if ((s_rhs + 3) >= (rhs_len - used)) {
1759 		static wchar_t *textbase;
1760 		textbase = rhstext;
1761 		rhs_len += s_rhs + RHS_TEXT_LEN;
1762 		rhstext = (wchar_t *)
1763 			realloc((char *)rhstext, sizeof (wchar_t)*rhs_len);
1764 		if (rhstext == NULL)
1765 /*
1766  * TRANSLATION_NOTE  -- This is a message from yacc.
1767  *	This message is passed to error() function.
1768  *	RHS -- Right Hand Side.
1769  */
1770 			error(gettext(
1771 			"couldn't expanded RHS length"));
1772 		loc = loc - textbase + rhstext;
1773 	}
1774 
1775 	*loc++ = L' ';
1776 	if (*s == L' ') /* special quoted symbol */
1777 	{
1778 		*loc++ = L'\'';	/* add first quote */
1779 		p++;
1780 	}
1781 	while (*loc = *p++)
1782 		if (loc++ > &rhstext[ RHS_TEXT_LEN ] - 3)
1783 			break;
1784 
1785 	if (*s == L' ')
1786 		*loc++ = L'\'';
1787 	*loc = 0;		/* terminate the string */
1788 }
1789 
1790 static void
1791 lrprnt()	/* print out the left and right hand sides */
1792 {
1793 	wchar_t *rhs;
1794 	wchar_t *m_rhs = NULL;
1795 
1796 	if (!*rhstext)		/* empty rhs - print usual comment */
1797 		rhs = L" /* empty */";
1798 	else {
1799 		int idx1; /* tmp idx used to find if there are d_quotes */
1800 		int idx2; /* tmp idx used to generate escaped string */
1801 		wchar_t *p;
1802 		/*
1803 		 * Check if there are any double quote in RHS.
1804 		 */
1805 		for (idx1 = 0; rhstext[idx1] != 0; idx1++) {
1806 			if (rhstext[idx1] == L'"') {
1807 				/*
1808 				 * A double quote is found.
1809 				 */
1810 				idx2 = wslen(rhstext)*2;
1811 				p = m_rhs = (wchar_t *)
1812 				    malloc((idx2 + 1)*sizeof (wchar_t));
1813 				if (m_rhs == NULL)
1814 /*
1815  * TRANSLATION_NOTE  -- This is a message from yacc.
1816  *	This message is passed to error() function.
1817  *	RHS - Right Hand Side.
1818  *
1819  *	You may just translate this as:
1820  *	'Could not allocate internally used memory.'
1821  */
1822 					error(gettext(
1823 					"Couldn't allocate memory for RHS."));
1824 				/*
1825 				 * Copy string
1826 				 */
1827 				for (idx2 = 0; rhstext[idx2] != 0; idx2++) {
1828 					/*
1829 					 * Check if this quote is escaped or not
1830 					 */
1831 					if (rhstext[idx2] == L'"') {
1832 						int tmp_l = idx2-1;
1833 						int cnt = 0;
1834 						while (tmp_l >= 0 &&
1835 						    rhstext[tmp_l] == '\\') {
1836 							cnt++;
1837 							tmp_l--;
1838 						}
1839 						/*
1840 						 * If quote is not escaped,
1841 						 * then escape it.
1842 						 */
1843 						if (cnt%2 == 0)
1844 							*p++ = L'\\';
1845 					}
1846 					*p++ = rhstext[idx2];
1847 				}
1848 				*p = 0;
1849 				/*
1850 				 * Break from the loop
1851 				 */
1852 				break;
1853 			}
1854 		}
1855 		if (m_rhs == NULL)
1856 			rhs = rhstext;
1857 		else
1858 			rhs = m_rhs;
1859 	}
1860 	(void) fprintf(fdebug, "\t\"%ws :%ws\",\n", lhstext, rhs);
1861 	if (m_rhs)
1862 		free(m_rhs);
1863 }
1864 
1865 
1866 static void
1867 beg_debug()	/* dump initial sequence for fdebug file */
1868 {
1869 	(void) fprintf(fdebug,
1870 	    "typedef struct\n");
1871 	(void) fprintf(fdebug,
1872 	    "#ifdef __cplusplus\n\tyytoktype\n");
1873 	(void) fprintf(fdebug, "#endif\n{\n");
1874 	(void) fprintf(fdebug,
1875 	    "#ifdef __cplusplus\nconst\n#endif\n");
1876 	(void) fprintf(fdebug, "char *t_name; int t_val; } yytoktype;\n");
1877 	(void) fprintf(fdebug,
1878 	    "#ifndef YYDEBUG\n#\tdefine YYDEBUG\t%d", gen_testing);
1879 	(void) fprintf(fdebug, "\t/*%sallow debugging */\n#endif\n\n",
1880 	    gen_testing ? " " : " don't ");
1881 	(void) fprintf(fdebug, "#if YYDEBUG\n\nyytoktype yytoks[] =\n{\n");
1882 }
1883 
1884 
1885 static void
1886 end_toks()	/* finish yytoks array, get ready for yyred's strings */
1887 {
1888 	(void) fprintf(fdebug, "\t\"-unknown-\",\t-1\t/* ends search */\n");
1889 	(void) fprintf(fdebug, "};\n\n");
1890 	(void) fprintf(fdebug,
1891 	    "#ifdef __cplusplus\nconst\n#endif\n");
1892 	(void) fprintf(fdebug, "char * yyreds[] =\n{\n");
1893 	(void) fprintf(fdebug, "\t\"-no such reduction-\",\n");
1894 }
1895 
1896 
1897 static void
1898 end_debug()	/* finish yyred array, close file */
1899 {
1900 	lrprnt();		/* dump last lhs, rhs */
1901 	(void) fprintf(fdebug, "};\n#endif /* YYDEBUG */\n");
1902 	(void) fclose(fdebug);
1903 }
1904 
1905 
1906 /*
1907  * 2/29/88 -
1908  * The normal length for token sizes is NAMESIZE - If a token is
1909  * seen that has a longer length, expand "tokname" by NAMESIZE.
1910  */
1911 static void
1912 exp_tokname()
1913 {
1914 	toksize += NAMESIZE;
1915 	tokname = (wchar_t *)
1916 	    realloc((char *)tokname, sizeof (wchar_t) * toksize);
1917 }
1918 
1919 
1920 /*
1921  * 2/29/88 -
1922  *
1923  */
1924 static void
1925 exp_prod()
1926 {
1927 	int i;
1928 	nprodsz += NPROD;
1929 
1930 	prdptr = (int **) realloc((char *)prdptr, sizeof (int *) * (nprodsz+2));
1931 	levprd  = (int *)  realloc((char *)levprd, sizeof (int) * (nprodsz+2));
1932 	had_act = (wchar_t *)
1933 	    realloc((char *)had_act, sizeof (wchar_t) * (nprodsz+2));
1934 	for (i = nprodsz-NPROD; i < nprodsz+2; ++i)
1935 		had_act[i] = 0;
1936 
1937 	if ((*prdptr == NULL) || (levprd == NULL) || (had_act == NULL))
1938 /*
1939  * TRANSLATION_NOTE  -- This is a message from yacc.
1940  *	This message is passed to error() function.
1941  *
1942  *	You may just translate this as:
1943  *	'Could not allocate internally used memory.'
1944  */
1945 		error(gettext(
1946 		"couldn't expand productions"));
1947 }
1948 
1949 /*
1950  * 2/29/88 -
1951  * Expand the number of terminals.  Initially there are NTERMS;
1952  * each time space runs out, the size is increased by NTERMS.
1953  * The total size, however, cannot exceed MAXTERMS because of
1954  * the way LOOKSETS(struct looksets) is set up.
1955  * Tables affected:
1956  *	tokset, toklev : increased to ntoksz
1957  *
1958  *	tables with initial dimensions of TEMPSIZE must be changed if
1959  *	(ntoksz + NNONTERM) >= TEMPSIZE : temp1[]
1960  */
1961 static void
1962 exp_ntok()
1963 {
1964 	ntoksz += NTERMS;
1965 
1966 	tokset = (TOKSYMB *) realloc((char *)tokset, sizeof (TOKSYMB) * ntoksz);
1967 	toklev = (int *) realloc((char *)toklev, sizeof (int) * ntoksz);
1968 
1969 	if ((tokset == NULL) || (toklev == NULL))
1970 /*
1971  * TRANSLATION_NOTE  -- This is a message from yacc.
1972  *	This message is passed to error() function.
1973  *	Do not translate NTERMS.
1974  *
1975  *	You may just translate this as:
1976  *	'Could not allocate internally used memory.'
1977  */
1978 		error(gettext(
1979 		"couldn't expand NTERMS"));
1980 }
1981 
1982 
1983 static void
1984 exp_nonterm()
1985 {
1986 	nnontersz += NNONTERM;
1987 
1988 	nontrst = (NTSYMB *)
1989 	    realloc((char *)nontrst, sizeof (TOKSYMB) * nnontersz);
1990 	if (nontrst == NULL)
1991 /*
1992  * TRANSLATION_NOTE  -- This is a message from yacc.
1993  *	This message is passed to error() function.
1994  *	Do not translate NTERMS.
1995  *
1996  *	You may just translate this as:
1997  *	'Could not allocate internally used memory.'
1998  */
1999 		error(gettext(
2000 		"couldn't expand NNONTERM"));
2001 }
2002 
2003 void
2004 exp_mem(flag)
2005 int flag;
2006 {
2007 	int i;
2008 	static int *membase;
2009 	new_memsize += MEMSIZE;
2010 
2011 	membase = tracemem;
2012 	tracemem = (int *)
2013 		realloc((char *)tracemem, sizeof (int) * new_memsize);
2014 	if (tracemem == NULL)
2015 /*
2016  * TRANSLATION_NOTE  -- This is a message from yacc.
2017  *	This message is passed to error() function.
2018  *
2019  *	You may just translate this as:
2020  *	'Could not allocate internally used memory.'
2021  */
2022 		error(gettext(
2023 		"couldn't expand mem table"));
2024 	if (flag) {
2025 		for (i = 0; i <= nprod; ++i)
2026 			prdptr[i] = prdptr[i] - membase + tracemem;
2027 		mem = mem - membase + tracemem;
2028 	} else {
2029 		size += MEMSIZE;
2030 		temp1 = (int *)realloc((char *)temp1, sizeof (int)*size);
2031 		optimmem = optimmem - membase + tracemem;
2032 	}
2033 }
2034 
2035 static int
2036 findchtok(chlit)
2037 int chlit;
2038 /*
2039  * findchtok(chlit) returns the token number for a character literal
2040  * chlit that is "bigger" than 255 -- the max char value that the
2041  * original yacc was build for.  This yacc treate them as though
2042  * an ordinary token.
2043  */
2044 {
2045 	int	i;
2046 
2047 	if (chlit < 0xff)
2048 		return (chlit); /* single-byte char */
2049 	for (i = 0; i < nmbchars; ++i) {
2050 		if (mbchars->character == chlit)
2051 			return (mbchars->tvalue);
2052 	}
2053 
2054 	/* Not found.  Register it! */
2055 	if (++nmbchars > nmbcharsz) { /* Make sure there's enough space */
2056 		nmbcharsz += NMBCHARSZ;
2057 		mbchars = (MBCLIT *)
2058 		    realloc((char *)mbchars, sizeof (MBCLIT)*nmbcharsz);
2059 		if (mbchars == NULL)
2060 			error(gettext(
2061 			"too many character literals"));
2062 	}
2063 	mbchars[nmbchars-1].character = chlit;
2064 	return (mbchars[nmbchars-1].tvalue = extval++);
2065 	/* Return the newly assigned token. */
2066 }
2067 
2068 /*
2069  * When -p is specified, symbol prefix for
2070  *	yy{parse, lex, error}(),
2071  *	yy{lval, val, char, debug, errflag, nerrs}
2072  * are defined to the specified name.
2073  */
2074 static void
2075 put_prefix_define(char *pre)
2076 {
2077 	char *syms[] = {
2078 		/* Functions */
2079 		"parse",
2080 		"lex",
2081 		"error",
2082 		/* Variables */
2083 		"lval",
2084 		"val",
2085 		"char",
2086 		"debug",
2087 		"errflag",
2088 		"nerrs",
2089 		NULL};
2090 	int i;
2091 
2092 	for (i = 0; syms[i]; i++)
2093 		fprintf(ftable, "#define\tyy%s\t%s%s\n",
2094 		    syms[i], pre, syms[i]);
2095 }
2096