xref: /openbsd/usr.bin/yacc/skeleton.c (revision 32fcbdd0)
1 /*	$OpenBSD: skeleton.c,v 1.40 2021/02/03 01:10:10 deraadt Exp $	*/
2 /*	$NetBSD: skeleton.c,v 1.10 1996/03/25 00:36:18 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Robert Paul Corbett.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "defs.h"
37 
38 /*  The definition of yysccsid in the banner should be replaced with	*/
39 /*  a #pragma ident directive if the target C compiler supports		*/
40 /*  #pragma ident directives.						*/
41 /*									*/
42 /*  If the skeleton is changed, the banner should be changed so that	*/
43 /*  the altered version can be easily distinguished from the original.	*/
44 /*									*/
45 /*  The #defines included with the banner are there because they are	*/
46 /*  useful in subsequent code.  The macros #defined in the header or	*/
47 /*  the body either are not useful outside of semantic actions or	*/
48 /*  are conditional.							*/
49 
50 char *banner[] =
51 {
52 	"#include <stdlib.h>",
53 	"#include <string.h>",
54 	"#define YYBYACC 1",
55 	"#define YYMAJOR 1",
56 	"#define YYMINOR 9",
57 	"#define YYLEX yylex()",
58 	"#define YYEMPTY -1",
59 	"#define yyclearin (yychar=(YYEMPTY))",
60 	"#define yyerrok (yyerrflag=0)",
61 	"#define YYRECOVERING() (yyerrflag!=0)",
62 	NULL
63 };
64 
65 
66 char *tables[] =
67 {
68 	"extern const short yylhs[];",
69 	"extern const short yylen[];",
70 	"extern const short yydefred[];",
71 	"extern const short yydgoto[];",
72 	"extern const short yysindex[];",
73 	"extern const short yyrindex[];",
74 	"extern const short yygindex[];",
75 	"extern const short yytable[];",
76 	"extern const short yycheck[];",
77 	"#if YYDEBUG",
78 	"extern const char *const yyname[];",
79 	"extern const char *const yyrule[];",
80 	"#endif",
81 	NULL
82 };
83 
84 
85 char *header[] =
86 {
87 	"#ifdef YYSTACKSIZE",
88 	"#undef YYMAXDEPTH",
89 	"#define YYMAXDEPTH YYSTACKSIZE",
90 	"#else",
91 	"#ifdef YYMAXDEPTH",
92 	"#define YYSTACKSIZE YYMAXDEPTH",
93 	"#else",
94 	"#define YYSTACKSIZE 10000",
95 	"#define YYMAXDEPTH 10000",
96 	"#endif",
97 	"#endif",
98 	"#define YYINITSTACKSIZE 200",
99 	"/* LINTUSED */",
100 	"int yydebug;",
101 	"int yynerrs;",
102 	"int yyerrflag;",
103 	"int yychar;",
104 	"short *yyssp;",
105 	"YYSTYPE *yyvsp;",
106 	"YYSTYPE yyval;",
107 	"YYSTYPE yylval;",
108 	"short *yyss;",
109 	"short *yysslim;",
110 	"YYSTYPE *yyvs;",
111 	"unsigned int yystacksize;",
112 	"int yyparse(void);",
113 	NULL
114 };
115 
116 
117 char *body[] =
118 {
119 	"/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
120 	"static int yygrowstack(void)",
121 	"{",
122 	"    unsigned int newsize;",
123 	"    long sslen;",
124 	"    short *newss;",
125 	"    YYSTYPE *newvs;",
126 	"",
127 	"    if ((newsize = yystacksize) == 0)",
128 	"        newsize = YYINITSTACKSIZE;",
129 	"    else if (newsize >= YYMAXDEPTH)",
130 	"        return -1;",
131 	"    else if ((newsize *= 2) > YYMAXDEPTH)",
132 	"        newsize = YYMAXDEPTH;",
133 	"    sslen = yyssp - yyss;",
134 	"#ifdef SIZE_MAX",
135 	"#define YY_SIZE_MAX SIZE_MAX",
136 	"#else",
137 	"#define YY_SIZE_MAX 0xffffffffU",
138 	"#endif",
139 	"    if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)",
140 	"        goto bail;",
141 	"    newss = (short *)realloc(yyss, newsize * sizeof *newss);",
142 	"    if (newss == NULL)",
143 	"        goto bail;",
144 	"    yyss = newss;",
145 	"    yyssp = newss + sslen;",
146 	"    if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)",
147 	"        goto bail;",
148 	"    newvs = (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs);",
149 	"    if (newvs == NULL)",
150 	"        goto bail;",
151 	"    yyvs = newvs;",
152 	"    yyvsp = newvs + sslen;",
153 	"    yystacksize = newsize;",
154 	"    yysslim = yyss + newsize - 1;",
155 	"    return 0;",
156 	"bail:",
157 	"    if (yyss)",
158 	"            free(yyss);",
159 	"    if (yyvs)",
160 	"            free(yyvs);",
161 	"    yyss = yyssp = NULL;",
162 	"    yyvs = yyvsp = NULL;",
163 	"    yystacksize = 0;",
164 	"    return -1;",
165 	"}",
166 	"",
167 	"#define YYABORT goto yyabort",
168 	"#define YYREJECT goto yyabort",
169 	"#define YYACCEPT goto yyaccept",
170 	"#define YYERROR goto yyerrlab",
171 	"int",
172 	"yyparse(void)",
173 	"{",
174 	"    int yym, yyn, yystate;",
175 	"#if YYDEBUG",
176 	"    const char *yys;",
177 	"",
178 	"    if ((yys = getenv(\"YYDEBUG\")))",
179 	"    {",
180 	"        yyn = *yys;",
181 	"        if (yyn >= '0' && yyn <= '9')",
182 	"            yydebug = yyn - '0';",
183 	"    }",
184 	"#endif /* YYDEBUG */",
185 	"",
186 	"    yynerrs = 0;",
187 	"    yyerrflag = 0;",
188 	"    yychar = (-1);",
189 	"",
190 	"    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
191 	"    yyssp = yyss;",
192 	"    yyvsp = yyvs;",
193 	"    *yyssp = yystate = 0;",
194 	"",
195 	"yyloop:",
196 	"    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
197 	"    if (yychar < 0)",
198 	"    {",
199 	"        if ((yychar = yylex()) < 0) yychar = 0;",
200 	"#if YYDEBUG",
201 	"        if (yydebug)",
202 	"        {",
203 	"            yys = 0;",
204 	"            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
205 	"            if (!yys) yys = \"illegal-symbol\";",
206 	"            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
207 	"                    YYPREFIX, yystate, yychar, yys);",
208 	"        }",
209 	"#endif",
210 	"    }",
211 	"    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
212 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
213 	"    {",
214 	"#if YYDEBUG",
215 	"        if (yydebug)",
216 	"            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
217 	"                    YYPREFIX, yystate, yytable[yyn]);",
218 	"#endif",
219 	"        if (yyssp >= yysslim && yygrowstack())",
220 	"        {",
221 	"            goto yyoverflow;",
222 	"        }",
223 	"        *++yyssp = yystate = yytable[yyn];",
224 	"        *++yyvsp = yylval;",
225 	"        yychar = (-1);",
226 	"        if (yyerrflag > 0)  --yyerrflag;",
227 	"        goto yyloop;",
228 	"    }",
229 	"    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
230 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
231 	"    {",
232 	"        yyn = yytable[yyn];",
233 	"        goto yyreduce;",
234 	"    }",
235 	"    if (yyerrflag) goto yyinrecovery;",
236 	"#if defined(__GNUC__)",
237 	"    goto yynewerror;",
238 	"#endif",
239 	"yynewerror:",
240 	"    yyerror(\"syntax error\");",
241 	"#if defined(__GNUC__)",
242 	"    goto yyerrlab;",
243 	"#endif",
244 	"yyerrlab:",
245 	"    ++yynerrs;",
246 	"yyinrecovery:",
247 	"    if (yyerrflag < 3)",
248 	"    {",
249 	"        yyerrflag = 3;",
250 	"        for (;;)",
251 	"        {",
252 	"            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
253 	"                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
254 	"            {",
255 	"#if YYDEBUG",
256 	"                if (yydebug)",
257 	"                    printf(\"%sdebug: state %d, error recovery shifting\\",
258 	" to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
259 	"#endif",
260 	"                if (yyssp >= yysslim && yygrowstack())",
261 	"                {",
262 	"                    goto yyoverflow;",
263 	"                }",
264 	"                *++yyssp = yystate = yytable[yyn];",
265 	"                *++yyvsp = yylval;",
266 	"                goto yyloop;",
267 	"            }",
268 	"            else",
269 	"            {",
270 	"#if YYDEBUG",
271 	"                if (yydebug)",
272 	"                    printf(\"%sdebug: error recovery discarding state %d\
273 \\n\",",
274 	"                            YYPREFIX, *yyssp);",
275 	"#endif",
276 	"                if (yyssp <= yyss) goto yyabort;",
277 	"                --yyssp;",
278 	"                --yyvsp;",
279 	"            }",
280 	"        }",
281 	"    }",
282 	"    else",
283 	"    {",
284 	"        if (yychar == 0) goto yyabort;",
285 	"#if YYDEBUG",
286 	"        if (yydebug)",
287 	"        {",
288 	"            yys = 0;",
289 	"            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
290 	"            if (!yys) yys = \"illegal-symbol\";",
291 	"            printf(\"%sdebug: state %d, error recovery discards token %d\
292  (%s)\\n\",",
293 	"                    YYPREFIX, yystate, yychar, yys);",
294 	"        }",
295 	"#endif",
296 	"        yychar = (-1);",
297 	"        goto yyloop;",
298 	"    }",
299 	"yyreduce:",
300 	"#if YYDEBUG",
301 	"    if (yydebug)",
302 	"        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
303 	"                YYPREFIX, yystate, yyn, yyrule[yyn]);",
304 	"#endif",
305 	"    yym = yylen[yyn];",
306 	"    if (yym)",
307 	"        yyval = yyvsp[1-yym];",
308 	"    else",
309 	"        memset(&yyval, 0, sizeof yyval);",
310 	"    switch (yyn)",
311 	"    {",
312 	NULL
313 };
314 
315 
316 char *trailer[] =
317 {
318 	"    }",
319 	"    yyssp -= yym;",
320 	"    yystate = *yyssp;",
321 	"    yyvsp -= yym;",
322 	"    yym = yylhs[yyn];",
323 	"    if (yystate == 0 && yym == 0)",
324 	"    {",
325 	"#if YYDEBUG",
326 	"        if (yydebug)",
327 	"            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
328 	" state %d\\n\", YYPREFIX, YYFINAL);",
329 	"#endif",
330 	"        yystate = YYFINAL;",
331 	"        *++yyssp = YYFINAL;",
332 	"        *++yyvsp = yyval;",
333 	"        if (yychar < 0)",
334 	"        {",
335 	"            if ((yychar = yylex()) < 0) yychar = 0;",
336 	"#if YYDEBUG",
337 	"            if (yydebug)",
338 	"            {",
339 	"                yys = 0;",
340 	"                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
341 	"                if (!yys) yys = \"illegal-symbol\";",
342 	"                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
343 	"                        YYPREFIX, YYFINAL, yychar, yys);",
344 	"            }",
345 	"#endif",
346 	"        }",
347 	"        if (yychar == 0) goto yyaccept;",
348 	"        goto yyloop;",
349 	"    }",
350 	"    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
351 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
352 	"        yystate = yytable[yyn];",
353 	"    else",
354 	"        yystate = yydgoto[yym];",
355 	"#if YYDEBUG",
356 	"    if (yydebug)",
357 	"        printf(\"%sdebug: after reduction, shifting from state %d \\",
358 	"to state %d\\n\", YYPREFIX, *yyssp, yystate);",
359 	"#endif",
360 	"    if (yyssp >= yysslim && yygrowstack())",
361 	"    {",
362 	"        goto yyoverflow;",
363 	"    }",
364 	"    *++yyssp = yystate;",
365 	"    *++yyvsp = yyval;",
366 	"    goto yyloop;",
367 	"yyoverflow:",
368 	"    yyerror(\"yacc stack overflow\");",
369 	"yyabort:",
370 	"    if (yyss)",
371 	"            free(yyss);",
372 	"    if (yyvs)",
373 	"            free(yyvs);",
374 	"    yyss = yyssp = NULL;",
375 	"    yyvs = yyvsp = NULL;",
376 	"    yystacksize = 0;",
377 	"    return (1);",
378 	"yyaccept:",
379 	"    if (yyss)",
380 	"            free(yyss);",
381 	"    if (yyvs)",
382 	"            free(yyvs);",
383 	"    yyss = yyssp = NULL;",
384 	"    yyvs = yyvsp = NULL;",
385 	"    yystacksize = 0;",
386 	"    return (0);",
387 	"}",
388 	NULL
389 };
390 
391 
392 void
write_section(char * section[])393 write_section(char *section[])
394 {
395 	int i;
396 	char *s;
397 
398 	for (i = 0; (s = section[i]); ++i) {
399 		++outline;
400 		fputs(s, code_file);
401 		putc('\n', code_file);
402 	}
403 }
404