xref: /titanic_50/usr/src/cmd/sgs/lex/common/header.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.
24  * All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	All Rights Reserved	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "ldefs.c"
34 
35 static void rhd1(void);
36 static void chd1(void);
37 static void chd2(void);
38 static void ctail(void);
39 static void rtail(void);
40 
41 void
42 phead1(void)
43 {
44 	ratfor ? rhd1() : chd1();
45 }
46 
47 static void
48 chd1(void)
49 {
50 	if (*v_stmp == 'y')
51 		(void)  fprintf(fout, "#ident\t\"lex: %s %s\"\n",
52 		    (const char *)SGU_PKG, (const char *)SGU_REL);
53 	if (handleeuc) {
54 		fprintf(fout, "#ifndef EUC\n");
55 		fprintf(fout, "#define EUC\n");
56 		fprintf(fout, "#endif\n");
57 		fprintf(fout, "#include <stdio.h>\n");
58 		fprintf(fout, "#include <stdlib.h>\n");
59 		fprintf(fout, "#include <inttypes.h>\n");
60 		fprintf(fout, "#include <widec.h>\n");
61 		if (widecio) { /* -w option */
62 			fprintf(fout, "#define YYTEXT yytext\n");
63 			fprintf(fout, "#define YYLENG yyleng\n");
64 			fprintf(fout, "#ifndef __cplusplus\n");
65 			fprintf(fout, "#define YYINPUT input\n");
66 			fprintf(fout, "#define YYOUTPUT output\n");
67 			fprintf(fout, "#else\n");
68 			fprintf(fout, "#define YYINPUT lex_input\n");
69 			fprintf(fout, "#define YYOUTPUT lex_output\n");
70 			fprintf(fout, "#endif\n");
71 			fprintf(fout, "#define YYUNPUT unput\n");
72 		} else { /* -e option */
73 			fprintf(fout, "#include <limits.h>\n");
74 			fprintf(fout, "#include <sys/euc.h>\n");
75 			fprintf(fout, "#define YYLEX_E 1\n");
76 			fprintf(fout, "#define YYTEXT yywtext\n");
77 			fprintf(fout, "#define YYLENG yywleng\n");
78 			fprintf(fout, "#define YYINPUT yywinput\n");
79 			fprintf(fout, "#define YYOUTPUT yywoutput\n");
80 			fprintf(fout, "#define YYUNPUT yywunput\n");
81 		}
82 	} else { /* ASCII compatibility mode. */
83 		fprintf(fout, "#include <stdio.h>\n");
84 		fprintf(fout, "#include <stdlib.h>\n");
85 		fprintf(fout, "#include <inttypes.h>\n");
86 	}
87 	if (ZCH > NCH)
88 		(void) fprintf(fout, "# define U(x) ((x)&0377)\n");
89 	else
90 	(void) fprintf(fout, "# define U(x) x\n");
91 	(void) fprintf(fout, "# define NLSTATE yyprevious=YYNEWLINE\n");
92 	(void) fprintf(fout, "# define BEGIN yybgin = yysvec + 1 +\n");
93 	(void) fprintf(fout, "# define INITIAL 0\n");
94 	(void) fprintf(fout, "# define YYLERR yysvec\n");
95 	(void) fprintf(fout, "# define YYSTATE (yyestate-yysvec-1)\n");
96 	if (optim)
97 		(void) fprintf(fout, "# define YYOPTIM 1\n");
98 #ifdef DEBUG
99 	(void) fprintf(fout, "# define LEXDEBUG 1\n");
100 #endif
101 	(void) fprintf(fout, "# ifndef YYLMAX \n");
102 	(void) fprintf(fout, "# define YYLMAX BUFSIZ\n");
103 	(void) fprintf(fout, "# endif \n");
104 	(void) fprintf(fout, "#ifndef __cplusplus\n");
105 	if (widecio)
106 		(void) fprintf(fout,
107 		"# define output(c) (void)putwc(c,yyout)\n");
108 	else
109 		(void) fprintf(fout,
110 		"# define output(c) (void)putc(c,yyout)\n");
111 	(void) fprintf(fout, "#else\n");
112 	if (widecio)
113 		(void) fprintf(fout,
114 		"# define lex_output(c) (void)putwc(c,yyout)\n");
115 	else
116 		(void) fprintf(fout,
117 		"# define lex_output(c) (void)putc(c,yyout)\n");
118 	(void) fprintf(fout, "#endif\n");
119 	(void) fprintf(fout,
120 	"\n#if defined(__cplusplus) || defined(__STDC__)\n");
121 	(void) fprintf(fout,
122 	"\n#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
123 	(void) fprintf(fout, "extern \"C\" {\n");
124 	(void) fprintf(fout, "#endif\n");
125 	(void) fprintf(fout, "\tint yyback(int *, int);\n"); /* ? */
126 	(void) fprintf(fout, "\tint yyinput(void);\n"); /* ? */
127 	(void) fprintf(fout, "\tint yylook(void);\n"); /* ? */
128 	(void) fprintf(fout, "\tvoid yyoutput(int);\n"); /* ? */
129 	(void) fprintf(fout, "\tint yyracc(int);\n"); /* ? */
130 	(void) fprintf(fout, "\tint yyreject(void);\n"); /* ? */
131 	(void) fprintf(fout, "\tvoid yyunput(int);\n"); /* ? */
132 	(void) fprintf(fout, "\tint yylex(void);\n");
133 	(void) fprintf(fout, "#ifdef YYLEX_E\n");
134 	(void) fprintf(fout, "\tvoid yywoutput(wchar_t);\n");
135 	(void) fprintf(fout, "\twchar_t yywinput(void);\n");
136 	(void) fprintf(fout, "\tvoid yywunput(wchar_t);\n");
137 	(void) fprintf(fout, "#endif\n");
138 
139 	/* XCU4: type of yyless is int */
140 	(void) fprintf(fout, "#ifndef yyless\n");
141 	(void) fprintf(fout, "\tint yyless(int);\n");
142 	(void) fprintf(fout, "#endif\n");
143 	(void) fprintf(fout, "#ifndef yywrap\n");
144 	(void) fprintf(fout, "\tint yywrap(void);\n");
145 	(void) fprintf(fout, "#endif\n");
146 	(void) fprintf(fout, "#ifdef LEXDEBUG\n");
147 	(void) fprintf(fout, "\tvoid allprint(char);\n");
148 	(void) fprintf(fout, "\tvoid sprint(char *);\n");
149 	(void) fprintf(fout, "#endif\n");
150 	(void) fprintf(fout,
151 	"#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
152 	(void) fprintf(fout, "}\n");
153 	(void) fprintf(fout, "#endif\n\n");
154 	(void) fprintf(fout, "#ifdef __cplusplus\n");
155 	(void) fprintf(fout, "extern \"C\" {\n");
156 	(void) fprintf(fout, "#endif\n");
157 	(void) fprintf(fout, "\tvoid exit(int);\n");
158 	(void) fprintf(fout, "#ifdef __cplusplus\n");
159 	(void) fprintf(fout, "}\n");
160 	(void) fprintf(fout, "#endif\n\n");
161 	(void) fprintf(fout, "#endif\n");
162 	(void) fprintf(fout,
163 	"# define unput(c)"
164 	" {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
165 	(void) fprintf(fout, "# define yymore() (yymorfg=1)\n");
166 	if (widecio) {
167 		(void) fprintf(fout, "#ifndef __cplusplus\n");
168 		(void) fprintf(fout, "%s%d%s\n",
169 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
170 		ctable['\n'],
171 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
172 		(void) fprintf(fout, "#else\n");
173 		(void) fprintf(fout, "%s%d%s\n",
174 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
175 		ctable['\n'],
176 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
177 		(void) fprintf(fout, "#endif\n");
178 		(void) fprintf(fout,
179 		"# define ECHO (void)fprintf(yyout, \"%%ws\",yytext)\n");
180 		(void) fprintf(fout,
181 		"# define REJECT { nstr = yyreject_w(); goto yyfussy;}\n");
182 		(void) fprintf(fout, "#define yyless yyless_w\n");
183 		(void) fprintf(fout, "int yyleng;\n");
184 
185 		/*
186 		 * XCU4:
187 		 * If %array, yytext[] contains the token.
188 		 * If %pointer, yytext is a pointer to yy_tbuf[].
189 		 */
190 
191 		if (isArray) {
192 			(void) fprintf(fout, "#define YYISARRAY\n");
193 			(void) fprintf(fout, "wchar_t yytext[YYLMAX];\n");
194 		} else {
195 			(void) fprintf(fout, "wchar_t yy_tbuf[YYLMAX];\n");
196 			(void) fprintf(fout, "wchar_t * yytext = yy_tbuf;\n");
197 			(void) fprintf(fout, "int yytextsz = YYLMAX;\n");
198 			(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
199 			(void) fprintf(fout, "#define YYTEXTSZINC 100\n");
200 			(void) fprintf(fout, "#endif\n");
201 		}
202 	} else {
203 		(void) fprintf(fout, "#ifndef __cplusplus\n");
204 		(void) fprintf(fout, "%s%d%s\n",
205 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
206 		ctable['\n'],
207 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
208 		(void) fprintf(fout, "#else\n");
209 		(void) fprintf(fout, "%s%d%s\n",
210 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
211 		ctable['\n'],
212 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
213 		(void) fprintf(fout, "#endif\n");
214 		fprintf(fout, "#define ECHO fprintf(yyout, \"%%s\",yytext)\n");
215 		if (handleeuc) {
216 			(void) fprintf(fout,
217 "# define REJECT { nstr = yyreject_e(); goto yyfussy;}\n");
218 			(void) fprintf(fout, "int yyleng;\n");
219 			(void) fprintf(fout, "size_t yywleng;\n");
220 			/*
221 			 * XCU4:
222 			 * If %array, yytext[] contains the token.
223 			 * If %pointer, yytext is a pointer to yy_tbuf[].
224 			 */
225 			if (isArray) {
226 				(void) fprintf(fout, "#define YYISARRAY\n");
227 				(void) fprintf(fout,
228 				"unsigned char yytext[YYLMAX*MB_LEN_MAX];\n");
229 				(void) fprintf(fout,
230 				"wchar_t yywtext[YYLMAX];\n");
231 			} else {
232 				(void) fprintf(fout,
233 				"wchar_t yy_twbuf[YYLMAX];\n");
234 				(void) fprintf(fout,
235 				"wchar_t yy_tbuf[YYLMAX*MB_LEN_MAX];\n");
236 				(void) fprintf(fout,
237 				"unsigned char * yytext ="
238 				"(unsigned char *)yy_tbuf;\n");
239 				(void) fprintf(fout,
240 				"wchar_t * yywtext = yy_twbuf;\n");
241 				(void) fprintf(fout,
242 						"int yytextsz = YYLMAX;\n");
243 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
244 				(void) fprintf(fout,
245 					"#define YYTEXTSZINC 100\n");
246 				(void) fprintf(fout, "#endif\n");
247 			}
248 		} else {
249 			(void) fprintf(fout,
250 "# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
251 			(void) fprintf(fout, "int yyleng;\n");
252 
253 			/*
254 			 * XCU4:
255 			 * If %array, yytext[] contains the token.
256 			 * If %pointer, yytext is a pointer to yy_tbuf[].
257 			 */
258 			if (isArray) {
259 				(void) fprintf(fout, "#define YYISARRAY\n");
260 				(void) fprintf(fout, "char yytext[YYLMAX];\n");
261 			} else {
262 				(void) fprintf(fout, "char yy_tbuf[YYLMAX];\n");
263 				(void) fprintf(fout,
264 				"char * yytext = yy_tbuf;\n");
265 				(void) fprintf(fout,
266 					"int yytextsz = YYLMAX;\n");
267 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
268 				(void) fprintf(fout,
269 					"#define YYTEXTSZINC 100\n");
270 				(void) fprintf(fout, "#endif\n");
271 			}
272 		}
273 	}
274 	(void) fprintf(fout, "int yymorfg;\n");
275 	if (handleeuc)
276 		(void) fprintf(fout, "extern wchar_t *yysptr, yysbuf[];\n");
277 	else
278 		(void) fprintf(fout, "extern char *yysptr, yysbuf[];\n");
279 	(void) fprintf(fout, "int yytchar;\n");
280 	(void) fprintf(fout, "FILE *yyin = {stdin}, *yyout = {stdout};\n");
281 	(void) fprintf(fout, "extern int yylineno;\n");
282 	(void) fprintf(fout, "struct yysvf { \n");
283 	(void) fprintf(fout, "\tstruct yywork *yystoff;\n");
284 	(void) fprintf(fout, "\tstruct yysvf *yyother;\n");
285 	(void) fprintf(fout, "\tint *yystops;};\n");
286 	(void) fprintf(fout, "struct yysvf *yyestate;\n");
287 	(void) fprintf(fout, "extern struct yysvf yysvec[], *yybgin;\n");
288 }
289 
290 static void
291 rhd1(void)
292 {
293 	(void) fprintf(fout, "integer function yylex(dummy)\n");
294 	(void) fprintf(fout, "define YYLMAX 200\n");
295 	(void) fprintf(fout, "define ECHO call yyecho(yytext,yyleng)\n");
296 	(void) fprintf(fout,
297 	"define REJECT nstr = yyrjct(yytext,yyleng);goto 30998\n");
298 	(void) fprintf(fout, "integer nstr,yylook,yywrap\n");
299 	(void) fprintf(fout, "integer yyleng, yytext(YYLMAX)\n");
300 	(void) fprintf(fout, "common /yyxel/ yyleng, yytext\n");
301 	(void) fprintf(fout,
302 	"common /yyldat/ yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta\n");
303 	(void) fprintf(fout,
304 	"integer yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta(YYLMAX)\n");
305 	(void) fprintf(fout, "for(;;){\n");
306 	(void) fprintf(fout, "\t30999 nstr = yylook(dummy)\n");
307 	(void) fprintf(fout, "\tgoto 30998\n");
308 	(void) fprintf(fout, "\t30000 k = yywrap(dummy)\n");
309 	(void) fprintf(fout, "\tif(k .ne. 0){\n");
310 	(void) fprintf(fout, "\tyylex=0; return; }\n");
311 	(void) fprintf(fout, "\t\telse goto 30998\n");
312 }
313 
314 void
315 phead2(void)
316 {
317 	if (!ratfor)
318 		chd2();
319 }
320 
321 static void
322 chd2(void)
323 {
324 	(void) fprintf(fout, "#ifdef __cplusplus\n");
325 	(void) fprintf(fout,
326 	"/* to avoid CC and lint complaining yyfussy not being used ...*/\n");
327 	(void) fprintf(fout, "static int __lex_hack = 0;\n");
328 	(void) fprintf(fout, "if (__lex_hack) goto yyfussy;\n");
329 	(void) fprintf(fout, "#endif\n");
330 	(void) fprintf(fout, "while((nstr = yylook()) >= 0)\n");
331 	(void) fprintf(fout, "yyfussy: switch(nstr){\n");
332 	(void) fprintf(fout, "case 0:\n");
333 	(void) fprintf(fout, "if(yywrap()) return(0); break;\n");
334 }
335 
336 void
337 ptail(void)
338 {
339 	if (!pflag)
340 		ratfor ? rtail() : ctail();
341 	pflag = 1;
342 }
343 
344 static void
345 ctail(void)
346 {
347 	(void) fprintf(fout, "case -1:\nbreak;\n");		/* for reject */
348 	(void) fprintf(fout, "default:\n");
349 	(void) fprintf(fout,
350 	"(void)fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
351 	(void) fprintf(fout, "} return(0); }\n");
352 	(void) fprintf(fout, "/* end of yylex */\n");
353 }
354 
355 static void
356 rtail(void)
357 {
358 	int i;
359 	(void) fprintf(fout,
360 	"\n30998 if(nstr .lt. 0 .or. nstr .gt. %d)goto 30999\n", casecount);
361 	(void) fprintf(fout, "nstr = nstr + 1\n");
362 	(void) fprintf(fout, "goto(\n");
363 	for (i = 0; i < casecount; i++)
364 		(void) fprintf(fout, "%d,\n", 30000+i);
365 	(void) fprintf(fout, "30999),nstr\n");
366 	(void) fprintf(fout, "30997 continue\n");
367 	(void) fprintf(fout, "}\nend\n");
368 }
369 
370 void
371 statistics(void)
372 {
373 	(void) fprintf(errorf,
374 "%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions,\n",
375 	tptr, treesize, nxtpos-positions, maxpos, stnum + 1, nstates, rcount);
376 	(void) fprintf(errorf,
377 	"%d/%d packed char classes(%%k), ", pcptr-pchar, pchlen);
378 	if (optim)
379 		(void) fprintf(errorf,
380 		" %d/%d packed transitions(%%a), ", nptr, ntrans);
381 	(void) fprintf(errorf, " %d/%d output slots(%%o)", yytop, outsize);
382 	(void) putc('\n', errorf);
383 }
384